Search code examples
pythonmayamelpymel

Set attribute without string concatination


I'm wondering if there is a way to perform the following, I'm aware that to set say the translation of an object, in pymel it's as simple as this:

object.translateX.set(1.5)

and you can also set this using the 'setAttr' command:

setAttr(object + '.translateX', 1.5) or setAttr('%s.translateX' % object, 1.5)

However, what If I only wanted to use the first example with something a little more advanced where the attribute name would change?

Example:

object.translateX.set(1.5)
object.translateY.set(1.5)
object.translateZ.set(1.5)
object.rotateX.set(1.5)
object.rotateY.set(1.5)
object.rotateZ.set(1.5)

I'd much rather write something like this:

for i in range(0,5,1):
    t = ['translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ']
    object.t[i].set(1.5)

However this obviously doesn't work, but can someone tell me if there's a way to do this?


Solution

  • Now, I do not know pymel or anything related to Maya, but if object.translateX.set(1.5) works, then I think the access works like normal object attribute access, so you can get an attribute by name using the getattr(object, attrname[, default_value]).

    BTW range(0, 5, 1) is the same as range(5); and means a list [ 0, 1, 2, 3, 4 ]; your list has 6 elements so you'd need range(6) to iterate over it - but as for loop can iterate over any iterable, you should just iterate over your attribute names. Thus:

    attributes = ['translateX', 'translateY', 'translateZ', 
                  'rotateX', 'rotateY', 'rotateZ']
    for a in attributes:
        getattr(object, a).set(1.5)
    

    should do what you wanted.

    Update: pymel seems to also support .attr() for objects, thus

    for a in attributes:
        object.attr(a).set(1.5)