So after years of admittedly putting it off, I have finally taken the plunge into learning some python for Maya. Sure I have looked at it, and even tried to use it before. However I had gotten so experienced with MEL that I didn’t want to waste time learning a new language when I wanted to write a script “right now”. So I put it of and put it off till I ran across this book at Barnes and Noble. Maya Python for Games and Film, a recent published book covering all the ins and outs of Mayas Python API. It inspired me to finally give python a good hard look, and see what I could do with it.
I decided to start out by rewriting an old script of mine, one of my first “big” scripts from years ago. Now being I had previous experience with C++ before MEL I knew, and admittedly forgot some of, the advantages of a class based Object Oriented Language. Not to mention how to properly use it, so none of that will be in the next example. However it will be soon.
'''----------------------------------------------------------------------
maya python script
Curve to Joints
pyV:1.0
python rewrite of Curve to Joints Mel script
Feb 11 2012 By Jeremy Hagan : JHagan@xupsetup.com
----------------------------------------------------------------------'''
import maya.cmds as cmds
# user gui
def jhJointsOnCurve(jocWindow = 'jocWindow', sizeW=220, sizeH=285):
# Check for and delete existing windows of same name
if (cmds.window(str(jocWindow), exists=True)):
cmds.deleteUI(str(jocWindow))
# Window Create
shWindow = cmds.window(str(jocWindow), title='Joints on Curve', \
resizeToFitChildren=False, \
widthHeight=(sizeW, sizeH))
# Layout
cmds.columnLayout( adjustableColumn=True, rowSpacing=5, columnAlign = 'center', \
columnAttach = ('both', 5))
# Controls
cmds.separator ( height = 5, style ='none')
cmds.text('jocNameCurve', label = 'Curve Name')
curveText = cmds.textField ('jocCurveInput', text='curve name here')
cmds.text('jocNameJoints', label='Name of Joint Chain')
#useCurveCheck = cmds.checkBox('jocUseCurve', label='Use Curve Name') # to emp later
nameText = cmds.textField ( 'jocSetName' , text='CurveNameHere')
cmds.separator ( height = 10, style = 'single' )
numberSlide = cmds.intSliderGrp('jocNumJoints', field = True, label='Number of Joints', value = 5)
cmds.separator ( height = 20, style = 'in' )
# Execute button
createButton = cmds.button( label='Create Chain', \
command=lambda x:jhConnectJointsOnCurve( \
cmds.textField(curveText, query = True, text = True),\
cmds.textField(nameText, query = True, text = True) , \
cmds.intSliderGrp(numberSlide, query = True, value = True) ) )
cmds.setParent( '..' )
cmds.showWindow(shWindow)
# def for creating joints along curve
def jhConnectJointsOnCurve (curve, name, number, spline = False, strech = False):
cmds.undoInfo(openChunk=True)
# grab some data
degree = cmds.getAttr(str(curve) + ".degree")
cvspans = cmds.getAttr(str(curve) + ".spans")
post = 0
spans = 1.0/number
# Set some var for keeping track
jointsInChain = []
jLocate = []
sLocate = cmds.group(empty = True, name = 'temLoc')
newSpans = number
tmpPath = cmds.pathAnimation(sLocate, curve = str(curve), fractionMode = True, name = 'PathTemp')
# move tmpPath along curve to locations for joints and place temp groups
for grp in range(0, number + 1):
if(grp > 0):
post = spans * grp
cmds.setAttr((str(tmpPath) + ".uValue"), post)
jLocate.append(cmds.group(empty = True, name = ("Loc" + str(grp))))
cmds.xform(jLocate[grp], translation = cmds.xform(sLocate, query = True, translation = True))
# make new joints at postions of temp grp locators.
cmds.select(deselect = True)
for joints in range(0, newSpans + 1):
jointsInChain.append( cmds.joint( name = (str(name) + '_' + str(joints)), \
position = cmds.xform(jLocate[joints], query = True, translation = True)) )
# Quick orient
cmds.joint(jointsInChain, edit = True , \
zeroScaleOrient = True, orientJoint = 'xyz', secondaryAxisOrient = 'yup')
cmds.joint(jointsInChain[number], edit = True, orientation = (0, 0, 0))
# cleanup
cmds.delete(str(sLocate))
for trash in jLocate:
cmds.delete(str(trash))
cmds.undoInfo(closeChunk=True)
# Pass on list of joints created
return jointsInChain
So nothing to wild just yet, and honestly not much different then the MEL counter part other then being a tad bit more efficient. But of course the next step is to play around with some class structures and see where it takes me. Feel free to point out any tips and advice as to what to do differently with this. Just remember I am still thinking “MEL” and its way of doing things, so it will take a bit for me to relearn my coding structures.