header
Welcome, Guest. Please login or register.
Did you miss your activation email?
   
FillerFiller

+ Chapter - 05 - MEL - Values & Variables +



Instructor - Pradeep Mamgain

+ Overview +

Variable is essentially a placeholder for data. Once we store data in the placeholder we manipulate and change the value of that data, whenever we want to. Don't get confused. A variable stores a value, such as text string "Hello Word", or the integer value 1. Variables can hold different values at different points in a script. A variable can then be reused throughout your script, instead of having to type out the actual value over and over again for the entire life of the variable, which can be frustrating and tedious.

+ Variable Naming +

Variable name always start with a $ sign. Variable names are case sensitive. MEL considers $name, $Name and $NAME to be different variables. Some valid examples or variables name are as seen in Example 5.1.

Example - 5.1 - Valid Variable Names

$age
$newEmail
$name01
$new_email
$_city


It is good practice to use sensible name for the variables for example, if you have an array of email ids; use $email_id_Array instead of something like $ea or $eArray.

+ Variable types +

Variables all store certain types of data. PHP automatically picks a data variable based on the value assigned. These data types include strings, numbers, and more complex elements, such as arrays.


TYPES OF MEL VARIABLES

int - An integer number—used to represent a whole number, such as 98 or –6.

float - A decimal number—used to represent a real number, such as –6.98 or 223.76.

string - A set of characters surrounded by quotes—used to store text, such as “hi there”.

vector - Three float numbers separated by commas, surrounded by angled brackets, such as <<01, 45, 20 >>.

array - A list of numbers—used to store lists of integers, floats, strings, or vectors. such as $color [3].

matrix - A two-dimensional array of floats or an array of float arrays.

+ Variable Declaration +

Take a look at Example - 5.2.

Example - 5.2, Variable Declaration.

string $myVariable;
$myVariable = "Hello World!";
print $myVariable;


Copy Example - 5.2 code and paste it in script editor, you'll see that Hello World! is printed in the last line in the history pan as seen in Figure - 5.1.

Variable Declaration
Figure - 5.1, Hello World Example.

The first line of code is called a Variable Declaration, where we declared that $myVarible is of type string. It will create a placeholder of variable in the memory. In next line script assigns value "Hello World" to the variable $myVariable. Third line tells script that, execute command print and print the value of the variable $myVariable.

You can also assign and declare variables in one line. MEL has this feature built in. Example 5.2 can be re-written as seen in Example 5.3.

Example - 5.3, One line declaration.

string $myVariable= "Hello World!";
print $myVariable;

+ Integers +

Integers are used to represent whole numbers, as 3 or -40. They are also very useful when you deal with boolean attributes. You might have seen on/off values for attributes in the channel box, for example visibility. When you perform mathematical operation on boolean attributes, Maya returns an integer value. For off, no and false it returns 0 and for on, yes and true it returns 1. Declaration of integer value is seen in Example 5.4.

Example - 5.4, Integer Declaration.

int $age=30;
+ Floats +

Floating Point Numbers or generally called floats have a fractional part, as 3.14, -2.6788, 0.23. Always remember that floats are more memory suckers, so whenever possible use integer numbers, they are computed faster. Declaration of float value is seen in Example 5.5.

Example - 5.5, Float Declaration.

float $height=2.67;
+ Vectors +

Vectors are used to represent the value in three dimensional grid. They are represented by three floats, enclosed in a angled brackets. They are mainly used with position and color attributes. Declaration of vector value is seen in Example 5.6.

Example - 5.6, Vector Declaration.

vector $position=<<5,39,10>>;
vector $colorRGB=<<0.2, 0.5, 0.1>>;

To get individual values from the vector variable use the .x, .y and .z accessors. Always surround the variable and accessors with parenthesis, as seen in Example 5.7.

Example - 5.7, Getting values from a Vector variable.

vector $color = <<0.8, 0.2, 0.5>>;
print($color.x);
print($color.y);
print($color.z);

You cannot use the accessors to set individual parts of a vector variable.

vector $color = <<0.8, 0.2, 0.5>>; $(color.x)=0.5; will return an error. However a trick you can use to set the accessors individually, as seen in Example - 5.8.

Example 5.8, Setting Individual part of a vector.

vector $color = <<0.8, 0.2, 0.5>>;
$color=<<$color.x, $color.y, 0.4>>;

New value of the variable color will be 0.8, 0.2, 0.4.

+ Strings +

String variable contains text information, a sequence of characters. It also contains text-formatting information like carriage return or tab.

STRING CODES

String variable can contain some special codes. List is seen below.

\" - Quotation Mark
\n - New Line
\t - Tab
\r - Carriage Return
\\ - Backslash

As seen in Example 5.9, newline and quotation mark codes are used in print command.

Example 5.9 - String Codes

string $name="Names is Bond \n \"James Bond\" ";
print $name;


Example 5.9 when executed will display..

Names is Bond
"James Bond"

You can concatenate strings using the + operator, print "Name is Bond" + "James Bond";

+ Arrays +

An array is an ordered list of values. It allows us to store multiple values in a single variable. When declaring arrays we first use variable type keyword (int, float...) then variable name with $ sign, followed by the square brackets [].

int $objectID[];
float $oHeight[];


You can also set the initial size of the array.

int $objectID[5];
float $oHeight[8];


To assign a value to a certain element, use the index number of the element. Numbering of the elements start with 0. If size of your array is 5, then it will contain 6 elements from index number 0 to index number 6.

$objectID[4]=1098;

The literal representation of an array is a list of values separated by commas, surrounded by curly brackets.

string $color={"red", "blue", "green"} or int $id[3]={1, 2, 3};

To clear an array to free the memory use the clear function.

clear($colors);
To get the size of an array use the size function.

print (size($colors));
Example 5.10, when executed will print

Size : 3
New Size : 0

Example 5.10, size and clear functions.

string $color[] = {"blue", "red", "black"};
print ("\n Size : " + size($color));
clear($color);
print ("\n New Size : " + size($color));

The size of an array is increased automatically as needed. In Example 5.11, first line sets the size of array to 3, as it has three elements, when you assign a fourth value for color, size of the array will be automatically increased.

Example - 5.11, Changing the size of an Array.

string $color[] = {"blue", "red", "black"};
print ("\n Size : " + size($color));
$color[3]="Pink";
print ("\n New Size : " + size($color));

Size : 3
New Size : 4

+ Matrices +

You can think of matrices as an array of arrays. It is two dimensional table of floating point values. use matrix keyword to declare the variable. Unlike arrays, you must specify the the size of matrix while declaring the variable. You can not expand the size of a matrix.

matrix $color[3][4]=<<0.4, 0.5,0.5 ; 0.2, 0.3, 0.6 ; 0.2, 0.5, 0.7>>;

Matrix is a series of value separated by commas representing rows and rows separated by the semicolons.

+ Explicit Typing +


Normally, MEL figures out whether a number is an integer or float, however you can override this default behavior by declaring its type explicitly.

print (int(8.5)); // Will print 8, MEL will truncate 8.5 to 8.
print (string(345)); // Will print 345, same as "345"


+ Implicit Typing +

When you use numbers and strings together, MEL converts numbers to strings.

print ("298" + 2); // Will print 2982.
print (298 + 2); // Will print 300.

SOME MORE WAYS TO DECLARE VARIABLES

int $age=25; weight=56;
int $age=$weight=76;

float $width, $height=20; $depth=34;

string $hi="Hello World!",
$hi="Hey ! Whats up?";

+ The difference between = and == +

New MEL programmers generally get confused with these two similar looking operators. The single equal sign (=) assigns value to a variable. For example, $color="red", assigns the value red to the variable $color. The double equal (==) sign, tests whether two values are equal. For example, $color=="blue"; tests whether $color is equal to blue.

+ What Next ? +

We have covered lots of stuff here, in next chapter we will look at the Syntax of MEL.

+ Download Project +

Dear guest, Only registered members can download projects/footages/scripts and Video files.Kindly get registered to download the files. It will also help us in informing you that when new tutorial/article/resource or cg news is available. Once you get registered downlaod link will appear here. - Downloaded times.

+ Save / Promote This Article/Tutorial/Information: +

If you enjoyed this article your vote is always highly appreciated.

+ Other Info +

+ Discuss in Forums

# Forum Info #

Chat with cgRokrs - Online

Supports forum boards as channels, a public channel, multiple languages, multiple themes and much more !!

# Board Topics #

Recent Topics Recent CG News

# Board Stats #

Boards : 33, Categories : 7 , Members : 1222, Topics : 135, Posts : 242

# Users Info #

Please welcome our newest friend blackmamba.

Currently (5) users online in forums; (5) guests and (0) users.

Users Online :
Fusion®, Maya® and After Effects® are registered trademarks of eyeon, Autodesk and Adobe.

Content Copyrights © 2008, CG Sutra.

Copyright © 2007 Free Templates by Zymic - Free Web Hosting - Best Viewed in Mozilla Firefox