Since you stated you are trying to write the most efficient code using the best 
coding practices, 
you absolutely should not use vars() or globals() for this purpose. I really 
think its poor habit to
pass in globals unless they are some kind of constant maybe, or environment 
setting. Like say,
a DEBUG flag. Even then I still think globals are kinda evil. Everything should 
always get set up
either through your __init__ in the class, or by setting class level attributes 
once when the script
sets up.
Excerpt on vars(): http://docs.python.org/library/functions.html
Note The returned dictionary should not be modified: the effects on the 
corresponding symbol table are undefined. [3]

vars() and globals() are kind of there for more advanced uses, say where you 
are doing dynamic building of classes or whatnot. Its really unnecessary in 
this case. One reason globals are really evil if misused is because you can't 
always see where objects came from. They were created somewhere else outside 
the current body of code. All the data that is specifically relevant to that 
class should live in that class, and you can wrap it up nicely however you want.

Since your create() method doesnt return anything, you will need to use 
self.lidControls to allow the data to live after create() is done.
The proper way to do this is to have all your important containers that need to 
be built up and remembered, inside your __init__, as you have it
in this example. Is lidControls something the user will access again after 
running create(). You can wrap it up nicer if you want with a getter.

def getControl(self, name):
        return self.lidControls.get(name, "")

Are these controls always constant?
> ['uprLid', 'lwrLid','lwrLidTrack',
> 'lidControls['uprLidTrack']','uprLidRef','lidRig']

If so, I can see why you were trying to make them attributes. Then you are 
right that you could do setattr() if you want, in order to access them as:  
lidRigObj.uprLid

-- justin



On Aug 21, 2011, at 7:55 AM, PixelMuncher wrote:

> @ Justin
> The script is for creating an eyelid rig which tracks the movement of
> an eyeball (based on Stop Staring 3rd Ed.).
> I'm not a professional coder but I like to write tools for my own use.
> I've been using Python for about a year (its my favorite language to
> date). This is the first time I'm using classes in my coding, so its a
> learning experience.  I try to write the most efficient code and use
> best coding practices.
> The problem I encountered was how use a list to create nodes and
> assign them to vars for further processing.  I asked around on a
> couple of forums for how to accomplish this.  The reason I thought of
> using vars is they are easy to read in the code.
> I've already learned that 'vars()/globals()' is not the way to go for
> a few reasons.  Others have also suggested using a dictionary, which
> is what I ended up doing.
> Here are the solutions that have worked:
> ----------------------- CODE --------------------------------------
> EyeRig.py
> class lidRig(object):
> 
>    def __init__(self):
>        print 'lidRig - For instructions, run
> EyeRig.ildRig().useage()'
>        self.lidControls = dict()
> 
>    def useage(self):
>        print lidRigUseage,'\n',UDAttrRemove
> 
>    def create (self, lidRigPrefix, uprLidJoint, lwrLidJoint,
> eyeParent, faceControl, lidControls2Make):
>        # lidControls2Make vars are ['uprLid', 'lwrLid','lwrLidTrack',
> 'lidControls['uprLidTrack']','uprLidRef','lidRig']
>        controlNode = 'loc'
>        # For each item in the 'lidControls2Make' list, create a group
> or locator and add it to the list of controllers
>        for control in lidControls2Make:
> 
>            # Create group/locator.  Use dictionary to store reference
> based on name of control:
>            if controlNode == 'group':
>                self.lidControls[control] = cmds.group(em = 1, n= '%s_
> %s' % (lidRigPrefix, control))
>            else:
>                self.lidControls[control] = cmds.spaceLocator(n= '%s_
> %s' % (lidRigPrefix, control))[0]
>            print 'Control:  %s. Object: %s' %(control,
> self.lidControls[control])
> 
>        # uprLid controller will be parented under lwrLid controller w/
> max rotation set to prevent passing thru lower lid
>        # Set the max XRot of the uprLid to 0:
>        cmds.transformLimits(self.lidControls['uprLid'],rx=(-45,
> 0),erx=(0, 1))# Need to set both limits, but erx unsets the min (first
> value)
> 
>        # Setup Hierarchy of controllers
>        cmds.parent
> (self.lidControls['uprLid'],self.lidControls['lwrLid'])
>        cmds.parent
> (self.lidControls['lwrLid'],self.lidControls['lwrLidTrack'])
>        ...
> ------------------------ END
> -------------------------------------------
> 
> What I like about Chris' approach is that referencing the var is a bit
> easier to write than the dictionary:
> ----------------------- CODE -------
> for control in lidControls2Make:
>        self.lidControls[control] = cmds.group(em = 1, n= '%s_%s' %
> (lidRigPrefix, control))
> 
> # Setup Hierarchy of controllers
> cmds.parent (lidRig.uprLid,lidRig.lwrLid)
> cmds.parent (lidRig.lwrLid,lidRig.lwrLidTrack)
> ...
> ----------------------- END -------
> 
> I see in your suggestion that the dictionary can be created inside
> 'create()' and it won't require using 'self.' each time it's
> referenced.
> For readability, I found that I could assign the vars from the
> dictionary and the vars would then be local to 'create()', (I realize
> that is not totally necessary):
> ----------------------- CODE -------
> ...
> lidControls = {}
> for control in lidControls2Make:
>        lidControls[control] = cmds.group(em = 1, n= '%s_%s' %
> (lidRigPrefix, control))
> 
> # Create vars to make code easier to read:
> uprLid = lidControls['uprLid']
> lwrLid = lidControls['lwrLid']
> 
> # Setup Hierarchy of controllers
> cmds.parent (uprLid,lwrLid)
> cmds.parent (lwrLid,lwrLidTrack)
> ...
> ----------------------- END -------
> Any further suggestions or insights are welcome.
> Thanks.
> 
> -- 
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: 
> http://groups.google.com/group/python_inside_maya/subscribe

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to