I'm trying to make a while loop
in Python to find the next item in a chain in Autodesk Maya. It cycles up through a hierarchy of objects until it finds the object with a certain attribute. Currently it first checks if the current object doesn't have a parent, then checks if it has the attribute parent
on it, if it does it won't go into the while loop
, and will print a statement.
If the object does have a parent, it will run a while loop
for as long as an object has a parent. The below code lists the parent of that selected object:
while pm.listRelatives( pm.ls( sl = True ), p = True ):
Then it will check if the current object has the attribute, if it doesn't it will select the next object up the hierarchy until it does, if it reaches the next to end, it will break out of the loop. What I'm wondering, is there a more efficient way to do this? Preferably a way to have one while loop
with a condition that will work even if it can`t find the object next in the chain.
import pymel.core as pm
if not pm.listRelatives( pm.ls( sl = True )[ 0 ], p = True ):
if pm.attributeQuery( 'parent', n = pm.ls( sl = True, tl = True )[ 0 ], ex = True ) == 1:
print 'found parent on no parent ' + pm.ls( sl = True, tl = True )[ 0 ]
else:
while pm.listRelatives( pm.ls( sl = True ), p = True ):
if pm.attributeQuery( 'parent', n = pm.ls( sl = True, tl = True )[ 0 ], ex = True ) == 1:
print 'found parent on selected ' + pm.ls( sl = True, tl = True )[ 0 ]
break
else:
print 'parent not found'
pm.select( pm.listRelatives( pm.ls( sl = True, tl = True ), p = True ) )
For looping up the chain:
def loop_up(item):
current = [item]
while current:
yield current[0]
current = cmds.listRelatives(current[0], p=True)
this will return all the items in the chain, beginning with the first item you passed in. Since it's a generator (thanks to the yield
) you can break out at any time:
for each_bone in loop_up(startbone):
if is_what_youre_looking_for(each_bone):
# do something
break
# if you get here you didn't find what you're looking for
print "parent attribute not found"
The only issue here is that it won't support multple parents (ie, instanced shapes). That's trickier because you would have to iterate up multiple chains (which may overlap) in parallel. However its not a common problem