16 February 2011

MEL and Expression : Conditional Statement

Conditional statement make the attribute or variable follow the expression in condition of another attribute or variable. For instance, I can make object move at only certain point such as frame 50 to 100.
The if and if - else statements are the most commonly used conditional statements in expressions.

if statements

The if conditional statement has below format.

       if ( condition ) 
             statement;

 meaning : if condition is true, statement executes. The statement can be a single line or
                  a multi-line in a block.

Example : The Ball's scaleY attribute to 2 after the animation plays 3 seconds.

      if (time > 3)
           Ball.scaleY = 2;


else statements

else conditional statement only works with if statement and has below format.

      if ( condition )
           statement 1;
      else
           statement 2;

meaning : if condition is true, executes statement 1, otherwise execute statement 2.
                The statement can be a single line or a multi-line in a block.

Example : When time is less than 2, Ball.translateY =0 and Ball.scaleY = time.
                 When time is greater than 2, Ball.traslateY = time-2 and Ball.scaleY = 1.

      if ( time < 2 )
      {
           Ball.translateY = 0;
           Ball. scaleY = time;
       }
      else
      {
           Ball.translateY = time-2;
           Ball.scaleY = 1;
       }


else if statements

else if statement works with else statement and else has conditions.

       if ( condition 1 )
            statement 1; 
       else if ( condition 2 )
            statement 2;

meaning : If condition 1 is true, executes statement 1 and skip the else if statement.
                 If condition 1 is false, not executing statement 1 and check if condition 2 is true.
                 If it is, execute condition 2.
                 If neither condition is true, neither statement executes.
                 The statement can be a single line or a multi-line in a block.

Example : If time is less than 3, Ball.scaleY = 1, if time is greater than 3 or equal and
                 less than 6 or equal, Ball.scaleY = 2. If time is greater than 6, Ball.scaleY = 3.

       If ( time < 3 )
             Ball.scaleY = 1;
       else if ((time >= 3) && (time =< 6))
             Ball.scaleY = 2;
       else
             Ball.scaleY = 3;

You can use as many else if statement as you can but only one else statement in one if statement.











Reference
  • Jo Sang Bum., 2002. programming with MAYA MEL & Expression. Seoul: Ahn Dae Sik
  • Autodesk Maya 2011, 2010. Autodesk Maya Online Help : Introduction. [online] Available at : < file:///Applications/Autodesk/maya2011/docs/Maya2011/en_US/index.html?url=./files/GS_Introduction.htm,topicNumber=d0e1838 > [Accessed 25 February 2011].
  • Jeon Gye do, 2000. MEL 4 Artists. [online] Available at: <http://gyedo.pe.kr/zbxe/mayaExpression> [Accessed 25 February 2011]

1 comment: