A statement is how we set or compare data within MEL. The simplest of all statements, the assignment statement, we have already seen used during the declaration of variables . There are three types of statements:

  • Assignment

  • Arithmetic

  • Conditional

The Assignment Statement

An assignment statement s most important element is the singular equal sign = . After evaluation of an assignment statement, the variable on the left side of the equal sign is set to the value on the right side. Assignment is not limited to being used during declaration. In fact, we are able to set the value of a variable at anytime after declaration and can do so repeatedly. Whenever possible, reuse variables to save on memory usage, seen in Example 4.10.

Example 4.10: Re-declaring a variable value.

 float $tempFloat = 3.48;     $tempFloat = 3.21212; 

Note that when we reassign a new value to the variable $tempFloat, we do not re-declare its type as a float.

The humble assignment statement is the basis for all data gathering within MEL. In Chapter 3, MEL Basics, we learned that most commands contain an aspect called a return value . By putting a MEL command on the right side of the = in an assignment statement, we can capture that return value and assign it to a variable. In order to capture the return value, we enclose the command within single left-hand quotes (`). In Example 4.11, we capture the value of the translate X value of an object called ball, and store that value in a float variable called $translateX.

Example 4.11: Capturing the value returned by the getAttr statement.

 float $translateX = `getAttr ball.translateX` ; 

When we use an assignment statement to capture the return value of a command, it is important to assign it to a variable that can actually hold that data. For many beginning scripters, the most frustrating aspect of this comes with array variables when they want to catch only one item. A perfect example is building a selection list when we have only one object selected, seen in Example 4.12.

Example 4.12: Attempting to assign captured data to the wrong type of variable.

 string $object = `ls selected` ;  //ERROR : Cannot cast data of type string[] to string.  

The command ls always returns a string array, even if that array is empty or has just one object in it. Data return types for each command can be found within the documentation for that command.

The Arithmetic Statement

An arithmetic statement is simply a statement containing a mathematical equation. We don t necessarily need to capture the result of any mathematical statement to a variable; we can often put a mathematical statement in any command or statement directly. This can save both a line of programming, as well putting one less variable in memory. In Example 4.13, we demonstrate using both methods .

Example 4.13: The arithmetic statement in action.

 float $value = `getAttr ball.translateX`;     setAttr ball.translateX ($value + 5) ;     setAttr ball.translateX (`getAttr ball.translateX` + 5); 

Within MEL, only certain mathematical functions are allowed with certain number types.

Refer to Table 4.3 to see how each operator functions on each data type.

Table 4.3: The Operators and Variables



Valid Data Types














Division Remainder



Cross Product



F = Floating Point
I = Integer
M = Matrix
S = String
V= Vector

One concept that often seems strange is using strings within a mathematical function. The only operator that works is addition, which takes multiple items and compiles them into one string, seen in Example 4.14.

Example 4.14: String addition.

 string $myString = ("red " +"ball");  //Result: red ball  

Adding strings together can be most useful when adding a variable into a command. In Example 4.15, we use the first item in the string array $selectionList and create a new string that has .translateX appended to the selected object; in this example, myBall .

Example 4.15: Accessing an array element.

 string $selectionList[] = `ls selection` ;     string $channel = ($selectionList[0] + ".translateX");  // Result: myBall.translateX  

Math Shortcuts

Arithmetical statements are used so often within all computer programs, including scripts, that the creators of these languages have developed a shorthand notation for writing two common operations carried out on variables.

We often find ourselves taking a variable, and adding or subtracting a number to it, as in Example 4.16.

Example 4.16: A common action done on variables.

 float $num = `getAttr myBall.translateX`;     $num = ($num + 5) ; 

MEL, like many other languages, provides a typing shortcut for carrying out this operation. It works with all operators for integers, floats, and vectors. This shorthand speeds up typing, not the actual execution of the script. In Example 4.17, we take the value of myBall s translate X channel, assign it to the variable $num, and then reassign $num that value multiplied by 3.

Example 4.17: An arithmetic shortcut.

 float $num = `getAttr myBall.translateX`;     $num *= 3 ; 

While all the operators work on int, float, and vector variables, only the += shortcut will work with string variables.

The other operation we find ourselves doing quite often is increasing or decreasing a variable, often an integer, by 1. To do this, we use a double operator, as seen in Example 4.18. Used mainly for iterating through an array, the important syntax is whether the operator comes before or after the variable. When it comes after the variable, MEL looks at the value of the variable and uses it in the current statement before increasing it by 1. When the operator comes before the variable, MEL calculates the new value before using it in the current statement. Both of these usages are demonstrated in Example 4.18.

Example 4.18: Increasing and decreasing a variable by 1.

 int $x = 0 ;     int $i = $x++;     print $i ;     0     print $x ;     1     int $y = 10 ;     int $n = --$x ;     print $n ;     9     print $y ;     9 

The Conditional Statement

A conditional statement executes a command based on whether a given value satisfies a test condition. The condition is always reduced to a simple Boolean question; it s either true or false.

The if Statement

The basic conditional action is the if statement. The syntax of an if statement is seen in Example 4.19, with the test condition enclosed in parentheses.

Example 4.19: The if statement syntax.

 if (condition)         statement; 

Within the parentheses, you can place a simple argument, such as a variable, or a command you want to capture the return value from. It is remarkably similar to vernacular language, such as If it s raining outside, open your umbrella. In Example 4.20, we test to see if an object called ball exists within the scene, and if so, we delete it. We don t just execute the command delete, because if an object called ball does not exist, the command would issue an error.

Example 4.20: The if statement in action.

 if (`objExists ball`)         delete ball ; 

If statements are wonderful for controlling the flow of actions within a script, and checking for errors, seen in Example 4.21. In Example 4.21, we check the size of an array holding our selection list, and issue an error, using the command error if the user has selected no objects, or if they have selected more than one object.

Example 4.21: Using an if statement as an error check.

 if (`size $selListing`)         error "Please select just one object." ;  else  

The if statement has two possible sub-statements that greatly increase the power of the if statement. The first is the argument else . By using the else statement, we can institute a course of action if your test condition of the if statement fails. The syntax of the if-else is seen in Example 4.22.

Example 4.22: The if-else statement syntax.

 if (condition)         statement;     else         statement; 

If-else statements are excellent for building flexible scripts. While if statements execute a command only when the condition is true, if-else statements provide a sort of backup plan. Even if a test condition fails, the script still execute a command.

The second possible addendum to the if statement is the else if argument. else if allows us to layer if statements together. The syntax of the if-else if is seen in Example 4.23.

Example 4.23: The if-else if statement syntax.

 if (condition)         statement;     else if (condition)         statement; 

We can layer as many else if as we want, which can be useful for producing modal scripts. We could, for example, execute different actions based on what type of geometry is selected.

We can also use the else statement with the else-if statement, which allows you full control no matter what your return value might be. In Example 4.24, the syntax of this statement, the if-else if-else statement, is shown.

Example 4.24: The syntax for the most complex if statement variation, the if-else if-else statement.

 if (condition)         statement;     else if (condition)         statement;     else         statement; 

The switch Statement

The other conditional action is the switch statement. Using switch we can construct complicated rules based on the value of a return statement. Given the value of a conditional argument, the switch statement executes one of several possible commands. The syntax for the switch command, seen in Example 4.25, is rather daunting for people new to scripting. The command switch also introduces us to the flow control word break . When MEL encounters the command break , it exits the current argument or command and continues on to the next part of the script.

Example 4.25: The switch statement syntax.

 switch (condition)         {             case  possibility  :                 statement;                 break;             case  possibility  :                 statement;                 break;             case  possibility  :                 statement;                 break;             case  possibility  :                 statement;                 break;             default:                 statement;                 break;         } 

While the break statement is not necessary, without it each statement will execute until it encounters a break or the switch statement ends. We also used the default argument, which acts in a manner similar to the else argument of an if-else statement. If none of the values provided by the case statements are satisfied, the default statement is evaluated.

Conditional Operators

Much like mathematical statements, conditional statements have operators. These operators can be split into two basic groups, logical and comparison . Both use two arguments and return either a 1 if true or a 0 if false.

There are three logical operators, as shown in Table 4.4.

Table 4.4: The Logical Operators



Returns true if either of the two arguments is true.


Returns true if both arguments are true.


Returns true if the argument is false.

If we now use these operators in a conditional statement, we can compare two pieces of data. The syntax usage of all three logical operators are seen in Example 4.26, which uses simple if statements for demonstration purposes, although the operators work in all variations of the if statement and the switch statement.

Example 4.26: Using conditional operators in conditional statements.

 if (argument  argument)         statement;     if (argument && argument)         statement;     if (! argument)         statement; 

The other operator type used in a conditional statement is the comparison, or relational operator. The relational operators are used, to no one s surprise, to compare two pieces of data in relation to each other. The available operators are shown in Table 4.5.

Table 4.5: The Relational Operators




Returns true if the value on the left side of the operator is greater than the value on the right side.


Returns true if the value on the left side is less than the value on the right side.


Returns true if the values are exactly equal.


Returns true if the values are not exactly equal.


Returns true if the value on the left side is greater than or equal to the value on the right side.


Returns true if the value on the left side is less than or equal to the value on the right side.

All relational operators can be used with integers, floating points, and vectors. The is equal to operator (==) and the does not equal operator (!=) also work with string variables. It is important to remember that when we compare strings, the comparison is case sensitive. When a comparison is made between vectors using the is equal to or does not equal operators, a comparison is made between the corresponding elements of each vector. When using the other relational operators, a comparison is made using the magnitude of each vector.

While statements using logical and relational operators return an integer value of either 1 or 0 and can be used to assign a value to a variable, they are most often directly used in conditional statements, as in Example 4.27.

Example 4.27: Using an inline captured value in a conditional statement.

 if (`getAttr myBall.translateX` > 0)         setAttr myBall.translateX 0; 

Operators can be grouped together following algebraic rules to control the evaluation of conditional statements. In Example 4.28, we execute the command setAttr if, and only if, both the translate X value of the object ball is greater than 0 and the current time is less than frame 100.

Example 4.28: Using multiple conditional statements.

 if ((`getAttr ball.translateX` > 0)         &&         (`currentTime query` <= 100))             setAttr ball.translateY 0; 

The MEL Companion
The MEL Companion: Maya Scripting for 3D Artists (Charles River Media Graphics)
ISBN: 1584502754
EAN: 2147483647
Year: 2003
Pages: 101

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: