Python Commands
Maya Python: Fundamentals
#COMMON RIGGING PYTHON COMMANDS
#--------------
#IMPORTS
#--------------
import maya.cmds as cmds
from zoo.zmaya import align_utils
import SHAPES
#--------------
#PYTHON MAIN STUFF
#--------------
## for loop enumerate (given selJnts is a list, jnt becomes easier to read)
for i, jnt in enumerate(selJnts):
print jnt
print i
#for loop from a list
for i in range (0, len(listItems)):
print[i]
print listItems[i]
for obj in filteredObjs:
wildcardName = ('_' + obj.split('_')[-1])
controlObject = obj.replace (wildcardName, '')
trigger = triggered.Trigger.Create(obj, (controlObject,), cmdCls=triggered.SelectConnectsCommand)
##Less typing which means less chance for error! Total win.
##As a general rule, if you're using range, you could probably be doing something better. There is a time for range, but not often.
##And if you do use range, use it in conjunction with zip like so:
for i, obj in zip(range(len(filteredObjs)), filteredObjs):
print 'the index is', i, 'and the object is', obj
##zip is super useful when you have two lists of things that you need to "zip" together (zip as in zipper, think each side of the zipper)
##Also useful is the enumerate iterator. Like so:
for idx, obj in enumerate(filteredObjs):
print 'the object', obj, 'appears at index', idx
#lists
x = [1, 2, 3]
#or empty list
x = []
#append
x = [1, 2, 3]
x.append([4, 5])
# if else and else if and or. == is equals
if firstLetter == 'L' or lastLetter == 'L':
print 'left'
elif firstLetter == 'R' or lastLetter == 'R':
print 'right'
else:
print 'center'
#dictionary (like a list but more like a form
joints = {'L_wrist_half': [0, 'L_wrist', 'L_wrist'],
'L_elbow_half': [0, 'L_bicep_twist6', 'L_elbow' ],
'L_bicep_half': [0, 'L_bicep', 'L_bicep']}
#return values from dictionary
for k, v in joints.items():
joint = k
half = v[0]
parentJoint = v[1]
matchJoint = v[2]
#or
d = {'a': [1, 2, 3], 'b': [4]}
d['a']
#functions
def matchJntFunction(matchJnt, sourceJnt):
print matchJnt
print sourceJnt
#call function
matchJntFunction(matchJnt, sourceJnt)
#--------------
#VARIABLES
#--------------
#set variable
variable = 1
#adding to strings
matchJnt = (bodyPart + '_noRot')
#--------------
# MAYA OBJECT HANDLING
#--------------
# returns a string of the selected objects
selOjects = cmds.ls(selection = True)
#the first selected object object
firstObject = cmds.ls(selection = True)[0]
#select an object specified by name
cmds.select('objectName', r = True)
#select hirachy from given object
cmds.select('root', r = True, hi = 1)
#dulpicate and rename (ood for avoiding name clashes?)
alembicDuplicate = cmds.duplicate( sourceObject, rr=True, n=alembicDuplicate )[0]
#pickwalking
cmds.pickWalk( ('object', direction='up' )
#filter object of a certain type from a selection in this case joints
skeleObjs = cmds.ls(selection = True)
for jnt in skeleObjs:
if cmds.nodeType(jnt) == 'joint':
print jnt
#or to select objects of a certain type, in thiscase jnts
jntObjs = []
cmds.select('root', r = True, hi = 1)
skeleObjs = cmds.ls(selection = True)
for i in skeleObjs:
if cmds.nodeType(i) == 'joint':
jntObjs.append(i)
cmds.select(d = True)
for i in range (0, len(jntObjs)):
cmds.select(jntObjs[i], add = 1)
#freeze transforms on curve
cmds.makeIdentity( curveObject, apply=True, scale=True, rotate=True, translate=True )
#--------------
#MATCHING AND CONSTRAINTS AND PARENTING
#--------------
#align rot and trans (zooTools)
align_utils.alignSimple(sourceJnt, matchJoint)
#Parenting
cmds.parent( ('sourceObj', 'parent' )
#unparent
cmds.parent('objName', w=1 )
# pointConstraint
cmds.pointConstraint('targetObject', 'object1', 'object2')
#--------------
# BUILD DELETE MAYA OBJECTS
#--------------
#ik
cmds.ikHandle( n='name_ik', sj='startJoint', ee='endJnt', solver='ikSCsolver')
#jnt
cmds.joint( n='joint1', r=1, p=(0, 0, 0))
#locator
cmds.spaceLocator(n=('locator1'))
#delete
cmds.delete('obj')
#--------------
# MAYA ATTRIBUTE HANDLING
#--------------
#set attribute values
#prenamed enum
cmds.setAttr( (jnt + '.side'), sideV )
#integer/float
cmds.setAttr( (jnt + '.type'), 18 )
#actually put in a random string
cmds.setAttr( (jnt + '.otherType'), jntName, type="string" )
#setting vector like trans or rot or rgb
cmds.setAttr( (joint + '.rotate'), 0 ,0 ,0 )
#Unlocking attributes
cmds.setAttr('Thumb_1_L.translateY', lock = 0)
#zero joint rot axis
cmds.joint(cmds.listRelatives('root', ad = 1, c = 1), e = 1, zso = 1)
#--------------
#NAME STRING HANDLING
#--------------
#replace
jntName = jnt.replace ('L_', 'R_')
#split into letters
firstLetter = joint.split('_')[0]
#strip for spaces into list
jointList = 'root spine1 spine2 spine3 spine4 spine5'
jointCheck = [x.strip() for x in jointList.split(' ')]
#strip from a txt file on dsk
#Change the path to suit
path = '/Users/andrewsilke/Library/Preferences/Autodesk/maya/2015-x64/scripts/silky/natSkeleBuilderControlSwap.txt'
from silky import curveControl_functionalised
reload(curveControl_functionalised)
with open(path) as f:
txtFileLines = [word.strip() for word in f]
for i in range (0, len(txtFileLines)):
objects = txtFileLines[i].rstrip('\r\n')
#remove white spaces
objects = txtFileLines[i].replace(' ','').split(',')
sourceObject = objects[0]
masterObject = objects[1]
print ('source object' + str(i) + ' = ' + sourceObject)
print ('master object' + str(i) + ' = ' + masterObject)
deleteObjects = 1
#main function
curveControl_functionalised.joinCurves( sourceObject, masterObject, deleteObjects)
#Append to lists
x = [1, 2, 3]
x.append([4, 5])
#test to see if letters are in a string
stringA = 'dsaf_R_adsf'
if ('_R' in stringA) or ('_R' in stringA):
print 'yaya'
##regular expressions good to know!!
##https://docs.python.org/2/library/re.html
import re
s='abc4def1'
##$ is at end ? is
re.findall('def[0-9]?$', s)
#--------------
#CREATE EXPRESSIONS
#--------------
#create expression with line breaks
expressionTwist = ('// Elbow Halfway' '\n'
'L_elbow_half.rotateZ = L_elbow.rotateZ * 0.5;' '\n')
cmds.expression( s=expressionTwist )
#delete expressions on an object
cmds.select ('object', replace=1)
cmds.delete (expressions=1)
Instructor
Andrew Silke
Comments are closed.