PDA

View Full Version : Rewrite this as a Python Class?


EightBit
07-26-2011, 04:39 AM
Hi all:
I've been studying up on Python classes and trying to figure out how to use them in my Maya Coding.
If someone has a few minutes , maybe you could show how this def could be written as a class?
Thanks much.

def eye_rig(eyeObj,eyeTarget,controllers):
"Create EyeRig weighted between target (via aimContraint) and animation override"

# Create control hierarchy of groups or locators:
#controllers = ['anim','aim','out']
controlsList = []
testing = 0

# For each controller, create a locator and add it to the list of controllers
for control in controllers:
objTmp = eyeObj+'_'+control
cmds.spaceLocator(n=objTmp)
controlsList.append(objTmp)

#Final control (last item in list) will receive all the rotations and pass them to the eye via parentConstraint.
# It will have UD attrs to weight inputs of other controls
finalControl = controlsList[-1]

# For each controller, we'll create a multDiv. We pipe the control's rotation into the md, weight it w/UD attr on 'out' object.
# Rotations coming out of multDivs are added w/pma node and passed to 'out' object

# Create pma node first, so md can be connected when created:
sumNodeTmp = cmds.createNode('plusMinusAverage',n=eyeObj+'eyeRot_sum')
print 'sumNodeTmp:',sumNodeTmp

# Summed rotations piped into 'out.rotate' (This is the last item in the 'controlsList' List):
cmds.connectAttr(sumNodeTmp+'.output3D',finalControl+'.rotate',force=1)

# Create _md node for each controller rotations, pipe into rotation sum
count = 0
while count < (len(controlsList)-1):
nodeTmp = controlsList[count]
attrTmp = controllers[count]
print 'nodeTmp:',nodeTmp
# For each controller, create a MultDiv to weight the rotation.
# They will be weighted by a custom attr on the 'out' object:
mdTmp = cmds.createNode('multiplyDivide',n=nodeTmp+'_md')
# Connect rotations to multDiv.input1 (more rotation influences can be added)
cmds.connectAttr(nodeTmp+'.rotate',mdTmp+'.input1',force=1)
# Pipe the md output to the rotation sum:
connectAttr(mdTmp+'.output',sumNodeTmp+'.input3D['+str(count)+']',force=1)

# Create UD attrs on 'out' (last item in list)to weight the rotations of each controller.
# Pipe UD into the corresponding md nodes.

# Create UD Attrs on 'out'
cmds.addAttr(finalControl,ln=attrTmp,min=0,max=1,dv=1,at='float')
cmds.setAttr (finalControl+'.'+attrTmp, e=1, keyable=1)# If not keyable, won't show in channel box
# Connect Anim, Aim to inputs of respective multDiv:
connectAttrsTmp =['2X','2Y','2Z']
for tmpConnectAttr in connectAttrsTmp:
cmds.connectAttr(finalControl+'.'+attrTmp,mdTmp+'.input'+tmpConnectAttr)

# If test, offset the controllers for visual testing:
if testing:
cmds.setAttr(nodeTmp+'.translateX',count+0.5)
count += 1

# Group the controllers
eyeRigGroupTmp = cmds.group(controlsList, n = eyeObj+'_eyeDir_grp')

# Position the eyeRig at the eye
parentUnparent (eyeObj,eyeRigGroupTmp)

# Setup aimConstraint for eye
cmds.aimConstraint (eyeTarget,eyeObj+'_aim',offset = [0, 0, 0], w = 1, aimVector = [1,0,0], upVector = [0,1,0], worldUpType = 'vector', worldUpVector = [0,1,0])

# Constrain Eye to "out" control
cmds.parentConstraint (eyeObj,finalControl, offset = (0, 0, 0 ), w = 1)

def parentUnparent(parent,children):
# Use temporary point-, scale- constraints to snap parent to future child
delete( cmds.pointConstraint (parent, children, offset = (0, 0, 0 ), w = 1) )
delete( cmds.orientConstraint (parent, children, offset = (0, 0, 0 ), w = 1) )
delete( cmds.scaleConstraint (parent, children, offset = (1, 1, 1 ), w = 1) )

eye_rig('testX_eyeL','testX_eyeL_tgt',['anim','aim','out'])

Narann
07-26-2011, 09:52 AM
Use Class have sense when you have many functions which used the same variables.

Each class store some variables specifics to his object and use them in his method.

You should read a little tutorial on oriented object programming.

:wavey:

CGTalk Moderation
07-26-2011, 09:52 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.