Choosing between Local and Universal Variables

In Maya, tradition variables are moreover restricted or international. If they’re restricted, they are able to be “seen” just by the appearance, writing, or process in which they are affirmed. (Every one the variables affirmed in the preceding part are local.) In difference, a correctly practical universal variable can be seen all over the place. To say publicly a worldwide changeable, the word global should be additional: global int $test; If global, the changeable is potentially easy to get to by every one expressions, scripts, and events therein. Even although the changeable is affirmed as worldwide, though, it should be re-declared in each process that wants to use it:

proc routineA () {
global int $test = 10;
routineB;
}
proc routineB () {
global int $test;
int $test2 = $test;
print $test2;
}

Choosing-between-Local -Universal-Variables

In this instance, two actions survive. $test is affirmed inside the corpse of process routineA. For process routineB to productively make use of $test, $test should be re-declared inside the corpse of routineB. Universal variables, though simple to comprise, are potentially unsafe. A universal variable, once confirmed, outlives the appearance or writing in which it’s affirmed. If additional than one appearance or writing uses the similar universal changeable name, consequences can turn out to be puzzling or unwanted. (To print a list of all worldwide variables confirmed by MEL scripts, carry out env in the Script Editor; tradition variables are at the end of the list.) As a substitute to worldwide variables, you can pass local variables from one process to the after that:

int $varA;
stepA($varA);
proc stepA(int $varA) {
$varA = 10;
stepB($varA);
}
proc stepB(int $varA) {
int $varB = $varA;
print $varB; }

In this instance, $varA is confirmed on the opening line as a restricted changeable. On the next line, process stepA is called by means of $varA scheduled in the parentheses. $varA is in that way approved to stepA. In arrange for $varA to be utilized by stepA, the process parentheses should comprise int $varA. The next process, stepB, should as well comprise int $varA in its process parentheses if it is to use the changeable. in addition, you can pass additional than one changeable to a process. An inexpensive technique with which to attain this involves the use of an array:

float $testArray[5] = {5.4, 7, 12, 2.1, 4};
stepA($testArray);
proc stepA(float $testArray[]) {
$testArray[2] = $testArray[2] + 3;
stepB($testArray);
}
proc stepB(float $testArray[]) {
print ($testArray[2]); }

Choosing-between-Local

Actions have the aptitude to turn out to be worldwide and thus turn out to be executable from the work area of the Script Editor and easy to get to to language, scripts, and actions therein, as with this instance: global proc testProcedure () { } For a account of array variables, observe the preceding section. For additional examples of superior changeable building, browse from side to side your Maya scripts folder (for instance, C:\Program Files\Alias\Maya8.0\scripts\). Dozens of MEL scripts are provided with the agenda. Most recent, when difficult any of the examples in this part, I advocate that you alternate your own changeable and process names. If, by misfortune, you effort to say publicly a changeable whose name has previously been utilized, you will take delivery of an “unacceptable redeclaration of changeable” error.