Variables
in MEL
General Rules -- Defining variables
-- Strings -- Vectors --
Arrays -- Matrices -- Scope
-- Global/Local
|
General
Rules
- always preceded
with a "$" symbol ( "$myVar" )
- no white spaces
allowed
- can not start with
a number ( "$2" )
- a variable's type
cannot be redefined once it is created, until the maya session is ended.
|
Defining
variables
automatic assignment
You can declare a variable using the above rules and simply give
it a value, as in the example below. Maya will look at the value and
automatically determine the data type of the variable.
$myVar = 3.14;
// this variable will be a float
from now on.
$myVar2 = 3; // this
will be an integer
Once a variable
is assigned, you need to treat it accordingly. Otherwise you may get
undesired results:
$myVar2 = 3 +
3.14 // the result will be "6" since
the variable is an integer as it was defined above.
In some cases you
may be getting the value as a return result from another mel command.
You will probably then want to do some operation to that returned result,
so it's a good idea to already know the datatype in order to know what
you can do with it.
IT IS BEST TO
ALWAYS EXPLICITY DEFINE A VARIABLE
Use the data type keyword to declare the variable:
int $myInteger;
float $myFloat;
string $myString;
vector $myVector;
If you do as above,
each variable will inherit a default "zero" value.
$myInteger = 0
$myFloat = 0.0
$myString = ""
$myVector = <<0,0,0>>
You may want to
set the inititial value at the time you declare the variable:
int $myInteger
= 3;
float $myFloat = 3.14;
string $myString = "hello";
vector $myVector = <<0,0,0>>;
|
STRINGS:
string
$myString = "hello world" ;
concatenate using
"+"
string $myString
= "hello";
$myString = ( $myString + " and goodbye" ) ;
// $myString
now equals "hello and goodbye"
use the "\"
to denote special characters:
\t = tab
\n = new line
\r = carriage
return
\\ = backslash
\" = a quote character you want in the actual string
A common special
characer is the new line, often concatenated to a variable for printing
out messages from your script:
print ($myString
+ "\n") ;
|
VECTORS:
vector
$myVector = << 12, 3, 5 >> ;
Always a set of three floats.
Use "<<"
and ">>" when defining
vector $myVector
= <<12, 3, 5>> ;
You can get the
value of any one component of the vector:
vector $myVector
= <<12, 3, 5>> ;
float $myX = $newVector.x ;
// $myX now equals
"12"
You cannot set the
value of one component in the same way, however:
vector $myVector
= <<12, 3, 5>> ;
$myVector.x = 52
; // this will not work and Maya will give you an
error message
Set the value of
one or more components of a vector like this:
vector $myVector
= <<12, 3, 5>> ;
$myVector = << 52, $myVector.y, $myVector.z >>;
// $myVector now
equals "<< 52, 3, 5 >>"
|
ARRAYS:
float $floatArray[]
= {3.14, 5.0, 32.3, 56.43} ;
Arrays are a list
of variables, all of the same type, so you define an array with a data
type keyword:
float $floatArray[];
string $stringArray[];
OR
float $floatArray[]
= {3.14, 5.0, 32.3, 56.43};
string $stringArray[] = {"sphere", "cube", "torus"};
Assign values to
single elements of an array (only after the array has been defined):
float $floatArray[]
= {3.14, 5.0, 32.3, 56.43};
$floatArray[0] = 1.55 ;
// this changes
the first element of the array, "3.14", to "1.55"
A common usage of
arrays in mel is when you need to determine what is selected:
$selectionList[]
= `ls -sl`;
// note the type
of array is not defined here, but it could be, in either case the
result will be strings
Or to get the transform
node name of a newly created object:
$newObj[] = `sphere`;
select $newObj[0];
// this retrieves
the first element of the array (which is the transform node, while
the second element is the shape node)
Append to an array
of a unknown size using the "size" command:
$array[ size[$array]
] = 1.55 ;
Clear the contents
of an array:
clear($array);
|
MATRIX:
matrix $myMatrix[2]
[4] ;
A matrix is a two-dimensional
array, with any number of rows and columns:
matrix $myMatrix[2]
[4] ;
// this defines a matrix with two rows and four columns
Initialize the values
in a matrix:
matrix $myMatrix[2]
[4] = << 3,4,6,7 ; 3,9,0,1 >>;
Visualize it like
a spreadsheet layout:
|
SCOPE
of a variable:
A local variable
only exists within a procedure or a statement block within braces.
int $myVar =
5;
for ($i = 1; $i <= 10; $i++) {
$myVar = $i;
}
{ float $myVar = 3.14; }
print ( $myVar + "\n" );
// the result
is "10" from the "for" loop. Since the "float
$myVar = 3.14" statement was
// enclosed
in braces, the value of 3.14 is not printed, as well as being able
to declare
// $myVar as a float!
|
GLOBAL
vs LOCAL:
Once a variable
is declared as global, it can exist and be addressed between different
scripts and procedures.
global int $myVar
= 5;
You can remind yourself
a variable is global with consistent naming conventions. A common practice
is to use a leading lower case 'g'.
global int $gMyVar
= 5;
To refer to the
variable elsewhere, you need to include the "global" keyword:
global int $gMyVar;
// if the variable
was initialized previously, it will not be initialized again
then you can simply
refer to "$gMyVar" in the rest of the procedure, block, or
script.
|