Assigning values to variables and attributes
 
 
 

Once you have declared a variable you can assign values to it using the = operator. The variable on the left side of the = operator is assigned the value of the expression on the right side of the operator. For example:

$x = $y * 10;

If you assign a value that does not match the type of the variable, the MEL interpreter will try to convert the value into the variables type.

For example:

// Declare the variables...
float $foo;
float $bar;
int $baz;
// Now assign values...
$test = 5.4;
// value of $test is 5.4
$bar = $test + 2;
// value of $bar is 7.4
$baz = $bar;
// the value of $baz is 7
// MEL converted the value to an integer.

An assignment evaluates as the assigned value.

NoteMEL does not support assigning string attributes to strings in statements like:

string $str = foo.str

Use this instead:

string $str = `getAttr foo.str`;

Combining declaration and assignment

For convenience you can assign a value to a variable when you create it. For example:

float $param = 1.5;
int $counter = 10;
string $name = "Alice";
vector $position = <<1.5, 2.5, 3.5>>;

If the type of a variable is obvious from the value you assign to it, you can leave out the type keyword. For example:

$name = "Alice";
$position = <<1.5, 2.5, 3.5>>;

This is called an implicit declaration. However it’s good practice to use the always use the type keyword to make your scripts more readable.

Chaining assignments

When you are assigning the same value to several variables, you can chain the assignments together:

$x = $y = $z = 0;

Convenience assignment operators

The following type of expression occurs often when you are programming:

$x = $x + 1;

This is so common that MEL provides several convenience operators to accomplish this with less typing. For example:

$x++ // adds 1 to the value of $x
$x-- // subtracts 1 from the value of $x
$x += 5 // adds 5 to the value of $x
$x -= 3 // subtracts 3 from the value of $x