Popular Posts

Wednesday, February 23, 2011

Mel Scripting: What is a scriptJob?

When I started mel scripting, I read a ton of forums that talked about how scriptjobs helped... but they never really explained how they worked. When i first tried to use a scriptjob... I crashed Maya. After some experimenting and reading up on details, i finally figured out how they work.



A scriptjob is a way to execute a procedure by doing another action, such as selecting an object or just if maya is sitting idle.

Example:

Lets say you want to write a script print the objects name and it's transform information by just selecting the object. First, lets write the procedure.


proc print_info(){
     //Create an array and store whatever you have selected into it.
     string $sel[] = `ls -sl`;


     //For each object in your selection
     for ($each in $sel){
         //Get the name.
         string $name = $each;
         //Get the X,Y and Z transform value
         string $x = `getAttr ($each + ".translateX")`;
         string $y = `getAttr ($each + ".translateY")`;
         string $z = `getAttr ($each + ".translateZ")`;
         
         //print the name and transform data for the selected object
         print ("You have selected " + $name + ".");
         print ("It's transform information is " + $x + ", " + $y + ", " + $z + ".");
     }


}

Now we need to setup a scriptjob to run this whenever you select an object. Add this line under the procedure. We will set an event to happen whenever a selection is changed.

int $jobID = `scriptJob -e "SelectionChanged" print_info`;

Now, execute the script. You will notice it will print:
"You have selected (object name here).It's transform information is (x), (y), (z)."


That is how a scriptjob works! Now you need to know how to turn it off, otherwise it could get annoying. If you noticed, i created an int variable for the scriptjob. This variable is the JobID. When you want to turn off the scriptjob, you need to find it's job ID. Then type a script like this:

scriptJob -k $jobID;

This will kill the scriptjob of that specific jobID number. If you want to turn off ALL scriptjobs, enter this:

scriptJob -ka;

This is very important because depending on how complicated the procedure, it will always be running. Whatif you accidently select 1000 objects? what if the procedure takes a couple of seconds per object?  A scriptjob is a very powerful fucntion, but can cause maya to lag or even crash if you are not careful.

For more information on scriptJobs amd different events that you can use in a scriptJob, Click Here.

No comments:

Post a Comment