I have some code which uses vars() to assign a variable name/value
inside of a loop.
I works correctly in the script editor, but fails from within a Class.
In the SE:
------ CODE -----------
prefix = 'char'
lidControls2Make = ['uprLid', 'lwrLid','lwrLidTrack',
'uprLidTrack','uprLidRef','lidRig']
for control in lidControls2Make:
# Use vars() to create new var
vars()[control] = prefix + '_'+control
cmds.spaceLocator(n=vars()[control])
print 'The value of %s is now %s' %(control,vars()[control])
cmds.transformLimits(uprLid,rx=(-45, 0),erx=(0, 1))
cmds.parent (uprLid,lwrLid)
cmds.parent (lwrLid,lwrLidTrack)
cmds.parent (uprLidRef,uprLidTrack)
cmds.parent (lwrLidTrack,lidRig)
cmds.parent (uprLidTrack,lidRig)[/CODE]
----------------------------
However, when I put the code in a class, the variable assignment is
lost once I'm outside the loop:
------ CODE -----------
class lidRig(object):
def __init__(self):
...
def create (self, lidRigPrefix, uprLidJoint, lwrLidJoint,
eyeParent, faceControl, lidControls2Make):
# lidControls2Make vars are ['uprLid', 'lwrLid','lwrLidTrack',
'uprLidTrack','uprLidRef','lidRig']
# For each item in the list, create a locator
for control in lidControls2Make:
vars()[control] = lidRigPrefix + '_'+control
cmds.spaceLocator(n=vars()[control])
print 'The value of %s is now %s' %(control,vars()
[control])
print 'uprLid: %s'%(uprLid)
...
# The value of uprLid is now charName_L_uprLid
# The value of lwrLid is now charName_L_lwrLid
# ...
# But an error occurs on the 'print' command:
# NameError: global name 'uprLid' is not defined #
#So the var assignment is not being retained outside the for loop.
# if I create an uprLid var outside of the Class, and then use global,
the print statement shows the var to be empty:
uprLid:
----------------------------
Anyone know the fix?
Thanks.
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe