PDA

View Full Version : Learning Python- Selecting and Parenting?


cwisbg
12-09-2011, 10:37 PM
Im in the prosses of learning python and its definetly a chalange.

this is just a script for creating a line of cube

import maya.cmds as mc def cuber():
i = 1
b = 1
while i <= 10:
mc.polyCube(n = "cube_1", w = 1, h = 1, d = .9)
sel = mc.ls(sl=True)
mc.move(0,0,b, r = 1)
i = i + 1
b = b + 1
print "Cube", sel, "is selected"
cuber()


anyways the goal here is to take the created cubes and parent the second to the first, third to the second and so on down the line.

im pretty lost as to how to do this and am caught up in storing what i want to be parented, and figuring out how to just parent though the list.
any suggestions or tips would be much appreciated!

i dont know if this is the correct way of getting this info but i did it this way so it ran 10 times creating 10 cubes that move over every new cube. If there is a better way please let me know

kevinsallee
12-10-2011, 12:09 AM
first you should specifically name your cubes from 1 to 10 in your script (it's better, even if maya does it for you you're sure there won't be any issues later on):

mc.polyCube(n = "cube_"+i, w = 1, h = 1, d = .9)

then, just for syntax, i = i + 1 is i+=1

for your cubes parenting do something like

for i in range(0,10):
mc.parent("cube_"+(i +1), "cube_"+i)

this parents cube_1 to cube_0, cube_2 to cube_1, etc

kevinsallee
12-10-2011, 12:13 AM
by the way, mc.move is good but for attributes i prefer doing:

mc.setAttr("cube_"+i+".tx, factor)

you can do it for tx, ty, tz, or translate, or whatever you want, and since you're in your while ( i would have used a for i in range(1,11)), you can increment your factor every time of the translation factor you want.
for example if you want your cubes every 5 units, you put factor = 5 at the beginning (or 0 if you don't want the first cube to move) and then factor +=5 at the end of the while or for

cwisbg
12-10-2011, 05:09 PM
"mc.polyCube(n = "cube_"+i, w = 1, h = 1, d = .9)"

when i add this to it i get the following error
# Error: line 1: TypeError: file <maya console> line 6: cannot concatenate 'str' and 'int' objects #

cdmcg
12-10-2011, 07:05 PM
the best way would be to run

import maya.cmds as mc
def cuber():
i = 1
while i <= 10:
cube=mc.polyCube(n = "cube_%i"% i, w = 1, h = 1, d = .9)[0]
mc.move(0,0,i, r = 1)
i += 1
print "Cube %s is selected"% cube
cuber()

Now, if you want to parent them, the way I usually handle this is through a dictionary.

import maya.cmds as mc
def cuber():
i = 1
cubeParent=''
cubeDic={}
while i <= 10:
cube=mc.polyCube(n = "cube_%i"% i, w = 1, h = 1, d = .9)[0]
mc.move(0,0,i, r = 1)
i += 1
print "Cube %s is selected"% cube
if cubeParent!='':
cubeDic[cubeParent]=cube
cubeParent=cube
else:
cubeParent=cube
for key in cubeDic.keys():
cmds.parent(cubeDic.get(key),key)
cuber()

This is at least how I would handle the problem. For some strange reason it is giving me really weird indents inside the code wrapper so if you copy it over, make sure to clean up the spacing.

pepperFish
12-11-2011, 04:16 AM
Here is another way of doing it using a list. This way you don't have to name the objects.


def cuber():
#populate first value in the list, so the first cube in the loop has something to parent to
cubeList = [mc.polyCube(w = 1, h = 1, d = .9)[0]]

# for some reason I don't like while loops, I prefer for ... perhaps less potential for infite loops?
# range is a builtin python function which generates a list of number from x to y
for i in range(1,10):
cube=mc.polyCube(w = 1, h = 1, d = .9)[0]
mc.move(0,0,i, r = 1)
mc.parent(cube,cubeList[i-1]) # this could also be cubeList[-1] for last value in the list
cubeList.append(cube)
cuber()

CGTalk Moderation
12-11-2011, 04:16 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.