如何从maya python中的列表中获取或设置对象的属性?

时间:2013-07-06 06:25:36

标签: python list maya getattr

list = cmds.ls(sl = True)

如何为translateY中的对象获取或设置Attr,例如list

2 个答案:

答案 0 :(得分:1)

除非有一个我不知道的Maya特定问题,否则在Python中有几种方法可以做到这一点:

for myObject in myList:

    # directly getting and setting attribute
    myObject.translateY = 30.0     # set
    a = myObject.translateY        # get

    # alternatively, via setattr and getattr built-in functions.
    setattr(myObject, "translateY", 40.0)
    # getter which Raises exception if myObject has no "translateY" attr:
    a = getattr(myObject, "translateY")
    # getter which supplies defaultVal if myObject has no "translateY" attr
    a = getattr(myObject, "translateY", defaultVal)  

顺便说一下,调用变量“list”是不好的形式,因为这个名称将影响Python的内置列表功能。最好使用像“myList”这样的东西。

答案 1 :(得分:0)

如果你使用pymel它会更简单......

sel = selected()

for i in sel:
    print i.ty.get()
    i.ty.set(i.ty.get() + 1)