#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    4
    Rep Power
    0

    For loop, using variable in object name, tcl $ ?


    Hello all.
    I need to use a for loop to cycle through a list and perform a number of procedures on those variables:

    Code:
    MDCaesarlist=['MD_CAESAR_Female_A','MD_CAESAR_Female_B']
    for x in MDCaesarlist:
    	print x
    	scene.x.SetControl(part="head", ctype="fixate", ref=target)
    I want to substitute in the variables for the x in the scene command. In other words, I want to be manipulating the scene.MD_CAESAR_Female_A object.
    However, it's giving me:

    Code:
    MD_CAESAR_Female_A
      File "<console>", line 3, in ?
      File "jsPeabody.py", line 2175, in __getattr__
      File "jsObject.py", line 363, in __getattr__
    AttributeError: x not recognized on Scene
    I know that in tcl you can use the $ to specify when you want to reference the value of a variable as opposed to just the variable name. That's what I need to do here.

    Any help? Thanks in advance. And sorry if this has been asked before. I couldn't find it anywhere.

    PS -- Running Python 2.1.1 on win32, interfacing with Jack 4.1 biomechanical modeling software.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You need the getattr builtin function.

    From the docs:
    getattr( object, name[, default])
    Return the value of the named attributed of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
    so your code would be:
    Code:
    MDCaesarlist=['MD_CAESAR_Female_A','MD_CAESAR_Female_B']
    for x in MDCaesarlist:
    	print x
    	getattr(scene, x).SetControl(part="head", ctype="fixate", ref=target)
    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    4
    Rep Power
    0
    Oh, awesome!

    Thank you so much. That was just what I needed.
    I had been doing so much manual clicking and entering data into Excel. This is going to save me a bunch of time.

    Thanks Dave!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    I had been doing so much manual clicking and entering data into Excel.
    Then I must point you to...

    http://www.markcarter.me.uk/computing/python/excel.html

    and

    http://msdn.microsoft.com/library/de...bjectmodel.asp


IMN logo majestic logo threadwatch logo seochat tools logo