This one comes up a lot: how do you get Maya to do something when you change an attribute?
Usually this question is from a user who has a list (enum) attribute, and wants to change elements in the scene based on the attribute's value.
For example you have an attribute with two values: IK and FK, and selecting one or the other switches the character rig into IK or FK mode. That sort of thing...
It's straightforward enough, but a lot of people make the mistake of opting for an expression (you have problems triggering the expression.)
Instead, the scriptJob was designed to do the very thing you're after: execute a MEL script when an event or condition occurs - and that includes when you change the value of an attribute.
For this example, I'm added an enum attribute with several values to a polyCube.
1) What do you want to happen?
Decide what you want to happen when you trigger the script in advance...(does that go without saying?)
To keep the example simple, I'll just print out the current value of the attribute. As a rule, I encapsulate my 'do this' script in a procedure.
global proc doThis()
{
print `getAttr -asString pCube1.fruity`;
print "\n";
}
Told you it was a simple example...
2) The scriptJob:
int $index = ` scriptJob -attributeChange "pCube1.fruity"
"doThis()" ` ;
The scriptJob flag -attributeChange takes two values: the first is the trigger attribute, the second is the script that you want to execute when the trigger occurs.
Only thing to note is that I always store the return value of the scriptJob as an integer, so I can delete the scriptJob if necessary later with :
scriptJob -kill $index;
Otherwise, once the scriptJob is in place, I simply refine the script in my doThis proc, refining and redeclaring it until it does what I want....
Enjoy!
Owen



Subscribe
Comments