The following topics describe solutions to common mistakes in expression flow control statements.
If you use a while, do, or for loop in an expression, remember to change the variable or attribute being tested in the test condition of the loop. Failing to do so can halt Maya operation.
Suppose you create an object named Balloon and decide to use a while loop to increase its Y scaling after three seconds of animation play.
while (time > 3) Balloon.scaleY = time;
Though you might think this expression sets Balloon’s scaleY attribute to the increasing value of time after the animation time exceeds 3 seconds, it actually halts Maya operation as soon as time exceeds 3. At that moment, the while condition is true, so the while loop statement Balloon.scaleY = time executes repeatedly and endlessly.
Even though a statement sets an attribute within an expression, Maya updates the attribute only after the expression finishes executing. Because the expression never finishes executing, Maya halts.
Unless you change Balloon.scaleY within the while loop to a value less than or equal to 3, the statement executes infinitely.
To get the desired result without halting Maya, use this expression:
if (time > 3) Balloon.scaleY = time;
Suppose you create objects named Cone and Ball, then use a while statement to link the Ball’s translateY attribute to the Cone’s translateY attribute:
while (Cone.translateY > 0) Ball.translateY = Cone.translateY;
At first glance, the expression seems to set Ball’s translateY position to the value of the Cone’s translateY position whenever Cone’s translateY is greater than 0.
In fact, the expression halts Maya as soon as you translate the Cone to a Y position greater than 0. At that moment, the while condition is true, so the while loop statement Ball.translateY = Cone.translateY executes endlessly.
Nothing you do in the user interface can change the Cone’s translateY position. It stays at translateY value of 0.
Unless you change Cone.translateY within the while loop to a value less than or equal to 0, the statement executes infinitely.
To get the desired result without halting Maya, use this expression:
if (Cone.translateY > 0) Ball.translateY = Cone.translateY;
If you use the == operator to compare a floating point variable or attribute to 0, your expression might not work correctly. This typically occurs when you assume the value returned by a built-in function such as cosd will be exactly 0.
float $x = cosd(90); if ($x == 0) print("This equals 0.\n"); else print("This doesn't equal 0.\n");
The expression displays the following text:
This doesn't equal 0.
Though the cosine of 90 degrees is mathematically 0, the cosd(90) function returns the value 6.123e-17, which is extremely close to 0 but not exactly equal. Though the number for practical purposes is the same as 0, it’s stored in the computer as a fractional quantity above 0 because of the way computers handle floating point numbers.
To fix the problem, compare the values as in this expression:
float $x = cosd(90); if (($x > -0.0001) && ($x < 0.0001)) print("This equals 0.\n"); else print("This doesn't equal 0.\n");
The expression displays the following text:
This equals 0.
By checking that $x is between -0.0001 and 0.0001, the appropriate print statement executes. The value returned by cosd(90) is so close to 0 that it’s within the small range specified in the if statement’s numerical comparison.
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License