Datasheet

10
Chapter 1 Customization and MEL Scripting
scriptJob “triggers” a command when a particular event occurs. If the event does not
occur, no action is taken. The easiest way to apply
scriptJob is with the following line:
scriptJob -event “event_name” “command”;
The -event flag offers a long list of events to choose from. Perhaps the most useful is
“SelectionChanged”, which triggers the command any time any object in Maya is selected
or deselected. The following list includes other events:
“Se lectTypeChanged” Triggered when the selection type is changed (for example, Select
Surface Objects to Select Point Components).
“Too lChanged” Triggered when a new tool is selected (for example, switching from the Move
tool to the Scale tool).
“timeChanged Triggered when the Timeline moves to a new frame.
When
scriptJob triggers the command, it outputs a job number. You can use the job
number to kill the triggered
scriptJob. This is often necessary to avoid multiple iterations
of
scriptJob running simultaneously. In order to capture the job number, you can use the
following line:
$jobNumber = `scriptJob -event “SelectionChanged” “SaveScene”`;
In this example, Maya saves the scene file each time there is a selection change. To kill
the job, you can use the following line at a different point in the script:
scriptJob -kill $jobNumber;
One way to avoid killing the job is to add the -runOnce flag to the scriptJob command, which
allows the job to be triggered only one time.
Timing with timerX and Killing with scriptJob
Obviously, saving with every selection change is overkill. A second important element of an
autosave script is thus an ability to track time.
timerX provides that ability by serving as an
internal stopwatch. You can note the current time, then derive elapsed time, with the follow-
ing lines:
int $startTime = `timerX`;
int $totalTime = `timerX -startTime $startTime`;
If these two lines of code are placed within two different areas or procedures of a script,
$totalTime becomes a value that represents the number of seconds Maya has been running.
In actuality,
timerX measures time in 10ths of a second; using an int variable, however,
ensures that the value is rounded off to a whole second.
07405c01.indd 1007405c01.indd 10 1/17/07 8:30:50 PM1/17/07 8:30:50 PM