Pages

Thursday 2 September 2010

maya python tips

I thought I would share 2 Maya python tips here for more elegant code:

(this code assumes you have namespace of maya.cmds as mc)

# filter objects with some attribute from a selection
# as this doesn`t work...
rigs = mc.ls( '*.rigNode', sl=1, o=1 )

# ...list comprehension can be used
rigs = [ o for o in mc.ls(sl=1) if mc.objExists( o + '.rigNode' ) ]


# example on handling returned NoneType
# test if joint has no child joints, if not is a good test for NoneType return value
if not mc.listRelatives( someJoint, c=1, typ= 'joint' ): print 'joint has no child joints'

# and opposite if statement is good test that return value was not NoneType
childJoints = mc.listRelatives( someJoint, c=1, typ= 'joint' )
if childJoints: print 'child joints:', childJoints

No comments: