float $counter
Variables that are local to a procedure are separate from global variables and separate from variables in other procedures. A local variable will override a global variable with the same name within the procedure.
All this allows you to write procedures without worrying whether the variable names you choose will conflict with Maya or other procedures.
If you want to create and maintain a variable in one procedure and also use it outside of that procedure, you can declare it as a global variable. For example:
global float $counter;
The $counter variable can be read or changed by any MEL code at the top level, and in other procedures that also declare $counter to be global. Also, if a global $counter variable already exists, this procedure will use it instead of creating a new variable.
Examples of Global Variable Use
Below are some code examples that use global variables.
The following code uses the $globalText global variable in the myProcedure procedure; the globalText global variable was defined outside of procedures:
global string $globalText; global proc myProcedure() { global string $globalText; print($globalText); }
In this example, MEL makes a window that can be used to create more child windows. The $chosenOne global variable tracks which child window the user has selected. Within each newly created child window, there is a chooseMe button that the you can click to set that specific window as the $chosenOne. When you click Delete Chosen Window in the main, parent window, the $chosenOne gets deleted.
In the image below, window4 is the $chosenOne, and will be deleted when Delete Chosen Window is clicked.
global proc int pressMe(string $thisOne) { global string $chosenOne; print ("was " + $chosenOne + "\n"); $chosenOne = $thisOne; print ("nowIs " + $chosenOne + "\n"); return 1; } ; global proc int satWindow(){ string $temp = `window -width 150`; button -label "Choose Me" -command ("pressMe " + $temp); showWindow; return 1; } global proc int baseWindow() { global string $chosenOne; window -width 150; columnLayout -adjustableColumn true; button -label "Make another Window" -command "satWindow"; button -label "Delete Chosen Window" -command "deleteUI -window $chosenOne"; showWindow; return 1; } baseWindow()