maya 中列表的 setAttr python

setAttr of a list in maya python

我仍在弄清楚 Python 和 Maya 是如何协同工作的,请原谅我的无知。因此,我尝试使用如下循环更改 Maya 中关节列表的属性:

for p in jointList: cmd.getAttr(p, 'radius', .5)

我得到这个错误:

Invalid argument 1, '[u'joint1']'. Expected arguments of type ( list, )

我不知道我做错了什么。

您需要将节点和通道都指定为第一个参数,例如'joint1.radius'。

要将所有关节的半径设置为 .5,代码为:

for p in jointList:
    cmd.setAttr(p + '.radius', .5)

除非您使用 pyMel,否则您需要指定要获取或设置的属性名称和节点。

对于 getAttr :

for p in jointList:
    val = cmd.getAttr('%s.radius' % (p))

对于 setAttr :

for p in jointList:
    cmd.setAttr('%s.radius' % (p), .5)
# lets have a look on the valid/available attributes 
# and change some attributes

# create list based on your selection
item_list = cmds.ls(selection=True)
for item in item_list:
    # iterate all keyable and unlocked attributes
    for key in cmds.listAttr(item, keyable = True, unlocked=True):
        # get attr
        value = cmds.getAttr("{0}.{1}".format(item, key))
        print "{0}:{1}".format(key, value)

# lets set some attributes
attr_id = "radius"
attr_value = 5
for item in item_list:
    # check object exists
    if cmds.objExists(item):
       # check object type
       if cmds.objectType(item, isType="transform"):
          # check objects attr exists
          if cmds.attributeQuery(attr_id, node = item, exists=True):
             print "set Attr"
             cmds.setAttr("{0}.{1}".format(item,attr_id), attr_value)

根据文档中的示例:

http://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/CommandsPython/getAttr.html

http://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/CommandsPython/setAttr.html

将对象名称和属性传递给 getAttr() 函数时,您需要将其指定为字符串。

例如

translate = cmds.getAttr('pSphere1.translate')

将return pSphere1 上翻译的属性值

jointList = cmds.ls(type='joint')    
for joint in jointList:
        jointRadius = cmds.getAttr('{}.radius'.format(joint))
        #Do something with the jointRadius below

如果你想设置它

newJointRadius = 20

jointList = cmds.ls(type='joint') 
for joint in jointList:
    cmds.setAttr('{}.radius'.format(joint), newJointRadius)