This is related to http://www.daniweb.com/software-development/python/threads/111526
However, in this case, I don't necessarily know the value of the var from which I am getting the name using vars(). Is there a way to get it?

prefix = 'char'
lidControls = []
lidControls2Make = ['uprLid', 'lwrLid','lwrLidTrack', 'uprLidTrack','uprLidRef','lidRig']
for control in lidControls2Make:
    objTmp = prefix + '_'+control
    # Assign the name to the control so we can access it by name later:
    vars()[control] = objTmp
    # So far, so good. The task is accomplished
    # But how do I get the name of the variable created by vars()[control]?
    # I want do this on each iteration of the loop:
    varNameVal = dict(vars()[control]=objTmp) # This fails: "keyword can't be an expression"
    print 'The value of ',varNameVal.keys()[0],'is now:',varNameVal.values()[0]\
    #Result: The value of uprLid is now char_uprLid

Thanks much.

Edited by pixeldroid: n/a

6 Years
Discussion Span
Last Post by pixeldroid

Looks that you are trying to do something super complicated to confuse yourself and maybe others that read your code. What is ultimate purpose of this metaprogramming? I don't think it serves any proper purpose.

Generally if somebody wants to do this kind of trickery, they should be using sequence types or dictionaries of normal kind. I think your difficulty is sign that you are doing the wrong thing (even that could be overcome if your purpose would be clear)

from pprint import pprint
prefix = 'char'
lidControls = dict()
lidControls2Make = ['uprLid', 'lwrLid','lwrLidTrack', 'uprLidTrack','uprLidRef','lidRig']
for control in lidControls2Make:
    lidControls[control] = '%s_%s' % (prefix, control)
    print 'The value of  %s is now: %s' %(control, lidControls[control])
#Result: The value of uprLid is now char_uprLid
print('lidControls = ')

Your code does exactly what I want - thanks very much.
What I am doing with the code is creating an eyelid rig for a 3d character rig in Maya. The rig causes the eyelids to track the movement of the character's eyeball so they require less attention while being animated. I think that is a proper purpose, but who knows?
You are correct in that I was taking the wrong approach to the problem. I'm not a professional coder, but I've gotten far enough to need this function, thanks for moving me along...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.