Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Melscript Attr toggle help

Melscript Attr toggle help

Anonymous
Not applicable
2,803 Views
11 Replies
Message 1 of 12

Melscript Attr toggle help

Anonymous
Not applicable

Hello! Im a bit new to melscript. I'm trying to create a script that will toggle the IK/FK attribute on a controller. I have it working before the "else" command but I cant seem to figure out how to get it to toggle 1 to zero in the second half. 

select -r samus_RIG:C_IkSpinehipCtrl ;

if ( "samus_RIG:C_IkSpinehipCtrl.toFkSpine" == 0 );

{

setAttr "samus_RIG:C_IkSpinehipCtrl.toFkSpine" 1;

}

else

{

setAttr "samus_RIG:C_IkSpinehipCtrl.toFkSpine" 0;

}

 

select -cl ;

0 Likes
Accepted solutions (3)
2,804 Views
11 Replies
Replies (11)
Message 2 of 12

lee.dunham
Collaborator
Collaborator
Accepted solution

You'll want to look at the usage of ! in mel scripts.

 

setAttr "pCube1.visibility" (!`getAttr "pCube1.visibility"`);

 

Message 3 of 12

mcw0
Advisor
Advisor
Accepted solution

If it's a true toggle you want, you only need the following...

 

1 - `getAttr samus_RIG:C_IkSpinehipCtrl.toFkSpine`;

Message 4 of 12

lee.dunham
Collaborator
Collaborator

Possible to clarify what you mean by a "true toggle"?

Message 5 of 12

mspeer
Consultant
Consultant
Accepted solution

Hi!

 

@Anonymous in case you are confused by these 2 complete different examples, that's because you can set boolean values by numbers (0 or 1) or by using a state (false or true).

So all of this is valid (example for cube visibility):

 

setAttr pCube1.visibility 0;
setAttr pCube1.visibility 1;
setAttr pCube1.visibility false;
setAttr pCube1.visibility true;

 

 

The example from @lee.dunham works by negating the true or false state (NOT ...).

Try both:

 

print (! true);
print (! false);

 

 

The example from @mcw0 works by using a simple mathematical calculation (1 - ...).

Try both:

 

print (1 - 1);
print (1 - 0);

 

 

Message 6 of 12

lee.dunham
Collaborator
Collaborator

Worth pointing out that using as the not operator returns an integer (0/1), it can be used for most numerical attributes (int, float etc).

 

setAttr "samus_RIG:C_IkSpinehipCtrl.toFkSpine" (!`getAttr "samus_RIG:C_IkSpinehipCtrl.toFkSpine"`); 

 

Due to its behaviour, its usually easier to read from context that it will be 0/1, on/off, true/false.

 

Message 7 of 12

mcw0
Advisor
Advisor

Lee,

 

I didn't see your response when I typed mine.  I only saw the post.  And in the original post, it was doing a setAttr of a value.  By "true toggle", I meant like a switch.  If it's 1, then flip it to 0.  And vice versa.  The conditional does that but with more lines of code.

 

I was unfamiliar with your usage of "!" so I've learned something new.  Thank you.

Message 8 of 12

hannah.novotny
Community Visitor
Community Visitor

Thank you guys so much! I'm sorry for the delayed response, I'm mostly on my work computer during the week, but I'll try to test these out soon! 

0 Likes
Message 9 of 12

Dautryl
Contributor
Contributor

Hi!
First of all, sorry for the digging and thank you for that solution, I have been using it for a while now.

 

I am trying to transition from MEL to Python, I can't find a way to make that one work... Anyone knows how to do it?

 

My progression so far:

 

import maya.cmds as cmds

attrList = [
    "myObject.holdOut",
    "myOtherObject.holdOut"
    ]

for i in attrList:
    cmds.setAttr(attrList, cmds.getAttr(not attrList))

Which result in `error line 9: No object matches name: False`

So it really is getting the boolean toggle mixed with cmds syntax that puts me int.

0 Likes
Message 10 of 12

lee.dunham
Collaborator
Collaborator
for attr in attrList:
    cmds.setAttr(attr, not cmds.getAttr(attr))

 

You needed to use the iterated attr and invert the getAttr result 😉

Message 11 of 12

Dautryl
Contributor
Contributor
Awesome! Thanks!!! 😊
Message 12 of 12

triffski
Advocate
Advocate

Lovely stuff! 🙂 

0 Likes