I am an animator who is a beginner at coding...I appreciate your help!
I am writing a Python script to automatically duplicate controller objects in Maya. It creates controllers for each bone in the character's hand.
It worked fine as a single long function, but I want to have a Main Function, "handCtrls()", call functions within it for the various tasks. I also need to pass variables to the sub-functions, because I am iterating on the variable "i".
I get an error on the line which calls the function "nullGrouper(side, boney, i, ctrl)", and the error says that "global variable 'ctrl' is undefined". But it is defined in the "ctrlDup" function, and I used "return ctrl", which I though would send that variable back to 'handCtrls()'.
here is the code:
import maya.cmds as c
def ctrlDup(side, boney, i, ctrlOrig):
#Select and duplicate the original Control, and place it at the appropiate boney
c.select(ctrlOrig)
ctrl = c.duplicate(ctrlOrig, n=side + boney + str(i) + '_CTRL')
print(ctrl)
print('ctrlDup passed')
return ctrl
def nullGrouper(side, boney, i, ctrl):
#Create null group at same position as Control
print(ctrl)
print(ctrl[0])
nullGrp = c.group(n=side + boney + str(i) + '_GRP', empty=1, world=1)
print(nullGrp)
c.parentConstraint(ctrl, nullGrp, n='nullGrp_parentConstr_temp')
c.delete('nullGrp_parentConstr_temp')
print('nullGrouper passed')
return ctrl, nullGrp
def handCtrls():
#First select the Controller to be duplicated
sel = c.ls(sl=1)
ctrlOrig = sel[0]
#List sides
sideList = ['L_', 'R_']
#List bones of part
boneyList = ['thumb', 'index', 'middle', 'ring', 'pinky']
#Now, iterate across finger joints, up to 3, group controls, and parent them
for side in sideList:
for boney in boneyList:
for i in range(1, 4, 1):
#Check if boney is thumb, and joint number is 3
if (boney!='thumb') or (i!=3):
ctrlDup(side, boney, i, ctrlOrig)
nullGrouper(side, boney, i, ctrl)
else:
pass
print("It's done.")
handCtrls()
There are several 'print' commands just to check if the variable is getting passed. Thank you!
It is because you are not storing the return
...
ctrl = ctrlDup(side, boney, i, ctrlOrig)
nullGrouper(side, boney, i, ctrl)