13 February 2011

MEL and Expression : Operators

MEL has 4 kind of operators : Assignment, Arithmetic, Comparison, Conditional.

Assignment : =

Arithmetic : +, -, *, /, %, ^.
   * is multiply for integers and float and dot product for vectors.
   % is remainder of division.
   ^ is cross product for vector. it only works for vectors

Comparison : Relational Operator , Logical Operator
  • Relational Operator :  compares between two conditions
          <   : less than
          >   : greater than
          == : equal to
          !=  : not equal to
          >= : greater than or equal to
          <= : less than or equal to

If you use the "==" or "!=" between two vectors, they compares each component of vectors. The other operators compare the magnitude of two vectors. The magnitude of vector <<x, y, z>> is calculated by below expression.
  • Logical Operator :  decides True or False between two conditions
          ||    :  or  : True when only one of both conditions is true.
         &&: and : True when both conditions are true.
             : not : True when right handed condition is false.

Examples:
        if ( 0 || 1) : True
        if ( 0 && 1) : False
        if ( 2 && <<3, 7.7, 9>>) : True
        if ( ! 5.39 && 7) : False
        if ( <<0, 0, 0>> || 0) : False
        if ( ! <<0, 0, 0>>) :True

Conditional : It has 3 conditions.
 
        Expression : conditional statement ? condition 2 : condition 3

If the conditional statement is true, it operate condition 2. If the conditional statement is false, it operates condition 3.

Example:
        float $src = rand(5)<3 ? 15 : 25

rand(5) is picking a random floating point number within 5. If the random number is less than 3, which means true then $src assign 15. If the random number is greater than 3, which means false then $src assign 25.


Operator precedence

The precedence of operators in expression follows

Highest  ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯→ Lowest


( ) [ ]  →  !   →  * / % ^  →  + -  →  < <= > >=  →  == !=  →  &&  →  ||  →  =












References
  • 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]

No comments:

Post a Comment