Using Variables within Expressions or MEL Scripts

Variables are virtual buckets that hold information that can be read at any time and updated repeatedly. Two types of variables are available to expressions and MEL scripts within Maya: custom and predefined. For custom variables, six subtypes exist: integer, float, string, vector, array, and matrix.

Custom variables are any variable that you define. You can define a variable by declaring it. For example, declaring a variable named test in an expression or script looks like this:

int $test;

In this example, the variable $test is defined as an integer variable with the inclusion of int. An integer variable stores any whole number (one without decimal places). Once a variable is declared as a specific type, such as int, you cannot re-declare it as a different type (unless you restart Maya). To make a new variable a float, which can store decimal places, you can use this:

float $test2;

When a variable is declared in such a fashion, its default value is 0. To give the variable a definitive value, you can use a line similar to this at a different point in the expression or script:

If $test2 was declared as an integer, 10.5 would be stored as 10. The third type of variable, string, holds letters, words or phrases. Here's an example:

string $testString = "Howdy Pardner";

The fourth type of variable, vector, holds three floating-point numbers. To declare a vector variable and fill it with three numbers, use this line:

To access the declared vector variable's individual components, add .x, .y, or .z to the variable name. For example, to read and print the first component of $testVector, try this:

string $temp = $testVector.x; print $temp;

An array is an ordered list of values. The array variable can store a long list of values under a single variable name. For example, you can declare an array like so:

string $testCo1ors[3] = {"red", "green", "blue"};

Using Variables within Expressions or MEL Scripts 53

The [3] defines the number of value positions available to the array. You can retrieve a single value by designating its position within the array. The first position is 0, the second is 1, and so on. For example, to print the word green, you can use this line:

A matrix is an ordered list of values arranged into rows and columns. You can declare a matrix variable with the following line:

In this example, the matrix has three rows and two columns. You can declare a matrix and fill it with values using a single line. The values are written across rows from left to right, starting with the top row:

matrix $test[3][2] = <<5, 10; 15, 6; 12.2, 4.7>>;

Note that an extra semicolon must be inserted at the end of each row. You can also write values to specific locations or retrieve values from specific locations in a previously declared matrix:

As with arrays, the first row or first column of a matrix is numbered 0. You can use variables at almost any point in an expression or script. The value that the variable holds is swapped for the variable name. For example, you can use variables as part of an If statement or a button command:

int $testNumber = 5;

string $testCommand = "SaveScene";

button -label "Save" -command $testCommand;

You can update variables by using math operators and specific values or other variables:

Common operators include * (multiply), / (divide), + (add), and - (subtract). A long list of math functions are also available, such as rand() (random) and sqrt() (square root). The list of functions is accessible by browsing through the Insert Functions menu in the Expression Editor (choose Window ^ Animation Editors ^ Expression Editor). You can find descriptions of the functions by choosing Help ^ MEL Command Reference in the Script Editor.

You can give a variable any name so long as that name doesn't exist as a predefined variable. (Keep in mind that Maya is case sensitive to variable names.) A predefined variable is one that is supplied by Maya and is globally available to all expressions, scripts, and procedures therein. Predefined variables include time and frame. (Procedures may be thought of as isolated subroutines that are activated only when the procedure name is called.)

+2 0

Average user rating: 5 stars out of 2 votes

Post a comment

  • Receive news updates via email from this site