Creating a conditional expression

In the following steps, you create a default sphere and write an expression to increase its Scale Y attribute based on the animation playback time. In the first two seconds of the animation time, Scale Y increases with the value of the time. At two seconds and thereafter, Scale Y no longer increases.

To create an expression using conditional statements

  1. Make sure you’ve done the steps in Preparing for the lessons.
  2. Create a NURBS or polygonal sphere at the origin. In the Attribute Editor, click on the transform node (nurbsSphere1 or pSphere1).
  3. Ensure that the X scale, Y scale, and Z scale values are equal to 1. Rename the sphere Balloon.
    TipYou can disable the Interactive Completion option under Create > Polygon Primitives and Create > NURBS Primitives and you will, by default, create a sphere at the origin with an X, Y, Z scale of 1.
  4. Go to the start time.
  5. With Balloon selected, choose Window > Animation Editors > Expression Editor.
  6. Enter this expression:
    if (time < 2)
    	Balloon.scaleY = time;

    This expression is an if statement. The if keyword causes the expression to make a decision based on a comparison of two or more items. In this case, the expression compares the value of time to the value 2.

    The expression checks whether the value of time is less than two seconds. If so, it does the assignment Balloon.scaleY = time. If time is not less than two seconds, the assignment doesn’t occur.

    NoteWhen you compare the value of time to a number in an expression, Maya interprets time as seconds rather than milliseconds, minutes, or any other unit of time. In the example, Maya interprets 2 as two seconds.

    Notice how the assignment Balloon.scaleY = time has been formatted so it appears indented under if (time < 2). Maya ignores all indentation, extra spaces, and blank lines between statements. We used the indentation to make the expression easier to read. You could have also written the expression as follows:

    if (time < 2) Balloon.scaleY = time;

    This isn’t as easy to read. Consistent, organized spacing is a good habit to develop. This lesson attempts to show examples of good spacing style whenever possible.

  7. Click Create to compile the expression.

    The ball flattens.

    The expression executes when you click Create. Because the animation is at frame 0, animation time is 0. Because time is less than 2, Maya sets Balloon.scaleY equal to the value of time, which equals 0.

    A scaleY value of 0 flattens the object in the Y dimension.

  8. Playback the animation.

    The flattened Balloon’s scale increases along its Y-axis. It inflates as the animation plays.

    At 2 seconds and thereafter, Balloon no longer inflates.

    When time equals 2 or more, the if condition is no longer true. The statement that follows it, Balloon.scaleY = time, no longer executes. The value of the scaleY attribute stays at the last value it had before time became 2, specifically, 1.9583.

    Recall that this example uses a frame rate of 24 frames per second. The time and Balloon.scaleY have these values at various frames:

    Frame Time (seconds) Balloon.scaleY (time)

























    The if statement’s condition, (time < 2), is a comparison. The condition must be surrounded by parentheses to isolate it from assignment that follows it.

    The < in the condition is a relational operator. A relational operator tests how one value relates to another. In the example, the < tested whether time is less than 2.

    Besides the < operator shown in this example, there are several other relational operators such as >, >=, ==, and so on. See the Maya Help for more details.

  9. Stop the animation and go to the start time. Balloon flattens again because the scaleY attribute becomes 0 when you go to the start time. Time is 0, so scaleY is 0.