-1

please bear with me - I'm new to all this. I tried the searches and have only found bits and pieces to what I'm looking for, but not what I need to connect them.

Basically, I'm trying to create a Python script that allows the user to simply "0" out multiple selected attributes on Maya's Channel box.

So far I have:

import maya.cmds as cmds

selObjs = cmds.ls(sl=1)
selAttrs = cmds.channelBox("mainChannelBox", q=1, sma=1)

print selObjs # returns [u'pCube1']
print selAttrs # returns [u'ty']

If I would like to set the attributes:

cmds.setAttr(selObjs  + "." + selAttrs, '0')

of course this is wrong, so how do I properly execute the setAttr command in this sceneario? (The intention includes having to set them if I have multiple selected attributes in the channel box).

I found that in MEL, it works like this. So really I just need help figuring out how to create the python counterpart of this:

string $object[] = `ls -sl`;
string $attribute[] = `channelBox -q -sma mainChannelBox`;
for ($item in $object)
for($attr in $attribute)
setAttr ($item + "." + $attr) 0;

Moving after that, I need an if loop where, if the attribute selected is a scale attribute, the value should be 1 - but this is something I'll look into later, but wouldn't mind being advised on.

Thanks!

Gogo
  • 25
  • 1
  • 7
  • What happens if you use the code you say "is wrong"? Do you get an exception? The obvious thing I see is that you're trying to treat your one-item lists as if they were strings. You might want to try `cmds.setAttr(selObjs[0] + "." + selAttrs[0], '0')`, or just write `for` loops like you have in your MEL code. – Blckknght Mar 04 '16 at 08:33
  • yes learned the hard way that what i was trying to do up there was to concatenate the lists (and not their contents) XD - learning pains – Gogo Mar 05 '16 at 12:43

2 Answers2

1

So here's what I finally came up with:

import maya.cmds as cmds

selObjs = cmds.ls(sl=1)
selAttrs = cmds.channelBox("mainChannelBox", q=1, sma=1)
scales = ['sy','sx','sz','v']


if not selObjs:
    print "no object and attribute is selected!"
elif not selAttrs:
    print "no attribute is selected!"
else:
    for eachObj in selObjs:
        for eachAttr in selAttrs:
            if any(scaleVizItem in eachAttr for scaleVizItem in scales):
                cmds.setAttr (eachObj+"."+eachAttr, 1)
            else:
                cmds.setAttr (eachObj+"."+eachAttr, 0)

This will reset the basic transformations to their defaults. Including an if for the scale and visibility values.

Gogo
  • 25
  • 1
  • 7
0

I managed to come up with this:

import maya.cmds as cmds

selObjs = cmds.ls(sl=1)
selAttrs = cmds.channelBox("mainChannelBox", q=1, sma=1)

for each in selObjs:
       for eachAttr in selAttrs:
           cmds.setAttr (each+"."+eachAttr, 0)

And It's working to zero out selected attributes perfectly. Now im at the stage of figuring out how to get the script to recognize if it contains scale attributes - to change that value to 1 instead of 0. (stuck at how to extract values from a list at the moment)

Gogo
  • 25
  • 1
  • 7