The Assignment Statement and Variable

The Assignment Statement

An assignment statement is most important element is the singular equal sign = . After evaluation of an assignment statement, the variable on the left side of the equal sign is set to the value on the right side. Assignment is not limited to being used during declaration. In fact, we are able to set the value of a variable at anytime after declaration and can do so repeatedly. Whenever possible, reuse variables to save on memory usage, seen in Example 4.10.

Example 4.10: Re-declaring a variable value.

    float $tempFloat = 3.48;
    $tempFloat = 3.21212;

Note that when we reassign a new value to the variable $tempFloat, we do not re-declare its type as a float.

The humble assignment statement is the basis for all data gathering within MEL. In Chapter 3, MEL Basics, we learned that most commands contain an aspect called a return value . By putting a MEL command on the right side of the = in an assignment statement, we can capture that return value and assign it to a variable. In order to capture the return value, we enclose the command within single left-hand quotes (`). In Example 4.11, we capture the value of the translate X value of an object called ball, and store that value in a float variable called $translateX.

Example 4.11: Capturing the value returned by the getAttr statement.

    float $translateX = `getAttr ball.translateX` ;

When we use an assignment statement to capture the return value of a command, it is important to assign it to a variable that can actually hold that data. For many beginning scripters, the most frustrating aspect of this comes with array variables when they want to catch only one item. A perfect example is building a selection list when we have only one object selected, seen in Example 4.12.

Example 4.12: Attempting to assign captured data to the wrong type of variable.

    string $object = `ls selected` ;
//ERROR : Cannot cast data of type string[] to string.

The command ls always returns a string array, even if that array is empty or has just one object in it. Data return types for each command can be found within the documentation for that command.



What $ (Dollar) means in Regular Expressions?

When working with MEL, everybody knows that $ means to declare a variable, but when it’s included in a string what it does?

It simply check the strings(characters) before the dollar sign in the ending of the second strings.

Or as Jeffry Friedl mentions in his book:

The $ (Dollar) the position at the end of the line

Therefore let’s check the following script in MEL:


string $testString = “This is a test string”;

match “string$” $testString;


We have now learnt how to use $ in regExp!!



Rounding numbers

Image and video hosting by TinyPic

###1 Did you know how you can round numbers?

Rounding numbers in arithmetic is excessively useful, specially for graphic artist who aim to write expressions, such as After Effects’ users.

The way you round numbers follows a primary rule. For rounding numbers to the nearest ten, you must pay attention to the last digit, if it’s less or equal to 4, you turn it down, if it’s bigger than 4 you round it up.

For instance, you have 82, the last digit is 2, and since it’s less than 4, you round it down, which becomes 0, and then add it to 8, makes 80, in other words, turning it down lest ten which is 80, while if it was 86 we would turn it up to bigger ten which would result in 90.

Following this rule, for nearest hundred we pay attention to last two digits, and if the number was equal or less than 49, we repeat the foresaid process.

e.g :   157 → 200