The if conditional statement has the following format:
if (test condition)
statement;
if (test condition) {
statement;
statement;
...
}
If test condition is true, statement executes. In MEL, everything is true, what is not 0.
Examples
if (9836 % 27 > 13)
print("Wow, that's interesting!\n");
if (3743 % 17 < 8)
{
print("These are...");
print(" useful results.\n");
}
The else statement works only with the if conditional statement. It has the following format:
if (test condition)
statement1;
else
statement2;
if (test condition) {
statement3;
statement4;
} else {
statement5;
statement6;
}
If test condition is true, statement1 executes, otherwise statement2 executes. The statement1 and statement2 can be any valid statements.
Examples
if (rand(10) < 5)
print("That was random.\n");
else
print("No it wasn't.\n");
The else if statement works only with the if else conditional statement. It has the following format:
If test condition1 is true, statement1 executes. Otherwise, if test condition2 is true, statement2 executes. If neither test condition is true, neither statement will be executed. The statement1 and statement2 can be any valid statement.
if (test condition1)
statement1;
else if (test condition2)
statement2;
Example
In the above example:
float $random = rand(2);
if ($random < 1)
print("That was random.\n");
else if ($random < 2)
print("Sure was.\n");
else
print("That's impossible...\n");
The rand(2) call would rarely return a value that was exactly 2.
A switch statement executes one of several pieces of statements based on the value of the control value. This control value can be an int, float, string, or vector data type. The body of the switch is enclosed in braces and is comprised of one or more blocks of statements called cases. Cases are labeled by the word case followed by a value of the same type as the control value, then a colon.
The control value determines which case statement gets executed. If a case statement value is equal to the control value statement, that case statement is executed.
The switch statement has the following general form:
After a case statement, you may have any statement including flow control. To prevent the MEL execution from continuing on to the next case, you can use a break statement. Within a switch, a break statement causes the rest of the switch statement to be ignored.
switch (control value) {
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
...
default:
statement4;
break;
}
Example: break statement
int $sway = rand(3);
switch ($sway) {
case 0:
print("Case 0\n"); //
Executed only if $sway = 0
break;
case 1:
print("Case 1\n"); //
Executed only if $sway = 1
break;
case 2:
while (rand(10) < 7) { // These
statements
print("I
say!\n"); // are executed only
print("Case 2\n"); // if $sway = 2
}
break;
}
LOOPS:
Conditional statements execute statements when a certain test condition is true. Loop statements repeatedly execute statements while a certain test condition is true.
Although looping allows you great control and power in writing scripts, it can also be a source of problems. Loops that endlessly repeat execution of statements will do just that-never end. For example, executing "while (1);" will loop nothing forever. Maya will never stop executing this line; don't try it unless you are prepared to quit Maya.
This section describes the three main loop statements, while, do-while, and for. The loop interruption statements, continue and break, are also described.
A while loop continues to execute some designated statements, as long as a certain condition is true. It has the following format:
while (test condition)
statement;
If test condition is true, Maya executes the statement. Maya
then evaluates the test condition again. If the test condition is true, it
executes the statement again. This process continues until the test condition is
false. At that time the loop exits.
Example
int $files = 5;
while ($files > 2)
print("There are " + $files-- + " files left.\n");
The above example produces the following output:
There are 5 files left.
There are 4 files left.
There are 3 files left.
When the condition is first evaluated in the above example, files has the
value of 5 which creates the true condition that 5 is greater than 2. Therefore,
the print statement is executed, which also decrements files to a value of 4.At this point, the test condition is evaluated again with a value of 4 for files. The test condition is again true so the print statement is executed, decrementing files to a value of 3. Again the test condition is evaluated to be true, and so the print statement is executed. Finally, with a value of 2 for files, the test condition is false, since 2 is not greater than 2. The while statement exits.
A do-while loop is very similar to a while loop because both execute some specified statement as long as a certain test condition is true. However, the do-while loop first executes the statement and then checks whether or not to repeat the loop. This ensures that the statement is executed at least once.
A do-while loop has the following format.
do
statement;
while (test condition);
Note that the do-while loop must terminate with a semicolon (;).Example
do {
print("Here comes the rain again.\n");
print("Raining on my head like a memory.\n");
} while (rand(2));
Often you may want a loop control that has initialization, test, and an increment statement. For this you can use the for loop. The for loop has the following format:
for (initializer; test condition; incrementor)
statement;
The initializer is executed only once, before the rest of the for loop is
executed. The initializer is a statement that is generally used to assign a
value to a variable in the test condition. You can use the initializer to
execute any statement or you can omit the initializer entirely. However, it
cannot be used to declare a variable.
The for loop evaluates test condition before executing statement. This
part of the for loop is exactly like the while loop. In fact, a for loop is just
a while loop with an initializer and incrementor. However, if the test condition
is missing, the test condition is assumed to always be true, which would create
an error in a while loop. While the test condition is true, the statement is
executed; as soon as it is evaluated to be false, the loop is exited. The
incrementor is executed at the end of each iteration of the loop, as if it were
added to the end of the loop. Typically, the incrementor is used to increment
some loop-control variable, although you can use it for any statement or omit it
entirely.
Example
for ($xxx = 0; $xxx < 3; $xxx++)
print("Hit me three times.\n");
The for loop also allows you to use multiple initializers or multiple incrementors. To do this, separate each initializer and incrementor with a comma (,).
The continue statement works inside all loops. It forces the next iteration of the loop to occur, skipping any remaining statements in the loop.
Example
float $fox = 5;
for ($fox = 0; $fox < 5; $fox++)
{
print("$fox equals: " + $fox + "\n");
if ($fox > 2)
continue;
print(" Got here\n");
}
The output is:
$fox equals: 0
Got here
$fox equals: 1
Got here
$fox equals: 2
Got here
$fox equals: 3
$fox equals: 4
Without the if statement, the loop would also output Got here after the lines $fox equals: 3 and $fox equals: 4. Maya ignores the continue statement until $fox increases to a value greater than 2. When $fox becomes 3 or greater, the continue statement executes and skips the remaining statement in the loop, the statement that prints got here.
Example
float $free = 0;
while ($free < 10)
{
print("$free equals: " + $free + "\n");
if ($free++ == 3)
break;
}
print("I'm free!"
);
The output is:$free equals: 0
$free equals: 1
$free equals: 2
$free equals: 3
I'm free!
Without the if statement, the loop would execute 10 times and display the numbers 0 through 9. The break statement terminates the loop after $free is equal to 3, so the print statement in the loop is only executed four times.
This website has been archived and is no longer maintained.