Search code examples
pythonmayasetattributemel

Proper way to use setAttr with channel box selection


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!


Solution

  • 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.