0

I'm still figuring out how Python and Maya work together so forgive ignorance on my part. So I'm trying to change the attributes of a list of joints in maya using a loop like so:

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

and I get this error:

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

I have no idea what I'm doing wrong.

Fred Barclay
  • 834
  • 1
  • 13
  • 24
Tomethy
  • 3
  • 1
  • 3
  • I'm not familiar with Maya but it appears to me that at least one of your arguments is not a Python list. https://stackoverflow.com/documentation/python/209/list#t=201608050111401102648 It looks like you might be passing "radius" as a string. – Fred Barclay Aug 05 '16 at 01:12

4 Answers4

3

Unless you work with pyMel you need to specify attr name and node to get or set.

for getAttr :

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

for setAttr :

for p in jointList:
    cmd.setAttr('%s.radius' % (p), .5)
Achayan
  • 5,720
  • 2
  • 39
  • 61
1

You need to specify both the node and the channel as your first argument, like 'joint1.radius'.

to set the radius to .5 on all your joints, your code would be:

for p in jointList:
    cmd.setAttr(p + '.radius', .5)
MikkelBF
  • 11
  • 2
1

Going from examples in the documentation:

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

You need to specify the object name and attribute, as a string, when you pass it into the getAttr() function.

e.g.

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

will return the attribute value for the translate on pSphere1

or

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

And if you want to set it

newJointRadius = 20

jointList = cmds.ls(type='joint') 
for joint in jointList:
    cmds.setAttr('{}.radius'.format(joint), newJointRadius)
-1
# 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)
Ari Gold
  • 1,528
  • 11
  • 18
  • Welcome to Stack Overflow! While this code snippet may solve the question, including an [explanation](http://meta.stackexchange.com/q/114762/305455) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – jmattheis Aug 22 '16 at 18:21
  • There's no need for a list comprehension and no explanation. – Green Cell Oct 20 '16 at 01:55