2009

Table Of Contents
is always prefixed by the $ symbol, indicating to the scripting language that
the following characters name a variable.
The scope of the variable determines where the variable can be accessed from.
If a variable is declared within a block of code, it cannot be accessed from
outside the block of code.
Name and type cannot change after creation. The value of a variable can
change, but the value must be of the earlier defined type.
NOTE Variables in MEL should always have their type explicitly defined. If not
explicitly stated, the scripting language attempts to imply a data type for the
variable from the context of the first appearance of the variable. It is good practice
to explicitly declare variables as implicitly defining variables can lead to scripting
errors and slows down the execution of the script.
To store scene information as a variable
1 In the Script Editor, type the following and press enter:
float $diameter_barrel;
This command declares a variable of data type float, with the name
diameter_barrel. A floating point value is a number containing a decimal.
2 To assign a value to the variable, type the following.
$diameter_barrel = 4.2;
The diameter of the barrel is 4.2 units. This value from the scene is
assigned to the variable. A value is assigned to a variable with the
assignment operator (=).
Storing other types of data
MEL also supports other data types that are common to most other
programming languages.
To declare a string variable
1 Type the following in the Script Editor
string $testString = "this is a test";
print $testString;
The following is output to the Script Editor:
this is a test
616 | Chapter 13 Scripting in Maya