+ Chapter - 06 - MEL - An Introduction to Programming Concepts +
Instructor -
Pradeep Mamgain
+ Overview +
In this chapter we will examine the process and techniques to control the flow of the script. Loops let us repeat a command over and over. Loops check a condition and then continuously loop as long as condition stands true. Before we dig deep into looping, let us first understand how to test and compare values in MEL.
+ Testing and Comparing Values +
MEL uses Comparison Operators to test and compare values. Following is the summary of Comparison Operator.

Apart form this MEL also has Logic Operators.

If you are confused about them, don't panic, we will use them with some real world examples later.
+ if .. else +
When you want to change the behavior of your script based on certain condition, use if control structure. Example - 6.1 demonstrate it.
Example - 6.1, Simple if structure.
string $fruit="orange";
if ($fruit=="orange")
{
print ("Wow !! Juicy");
}
Execute Example - 6.1, MEL will display Wow!! Juicy to be last line in History pan. Since condition is true, MEL will print Wow!! Juicy for you. Note that we have used == comparison operator. Statement(s) between curly braces is called block of code. Now if you want to print something or execute some other lines of codes when condition is false, use else with if, as seen in Example - 6.2.
Example - 6.2, if .. else structure.
string $fruit="apple";
if ($fruit=="orange")
{
print ("Wow !! Juicy");
}else{
print ("Naah !! Gimmie some Grapes");
}
Once you execute Example - 6.2, MEL considers condition as false and will display, Naah !! Gimmie some Grapes, in history pan.
You can specify several alternatives with the else if statement, as seen in Example - 6.3.
Example - 6.3, if..else if..else structure
string $fruit="apple";
if ($fruit=="orange")
{
print ("Wow !! Juicy");
}
else if ($fruit=="grapes")
{
print ("Yes !! I like Grapes");
}
else if ($fruit=="apple")
{
print ("Ok !! I will take one");
}
else
{ print ("Buy some Bananas for me");
}
Copy code in Example - 6.3 and execute it in script editor. Play with it and see how this structure works? Use four different values for variable $fruit in the first line and check out what MEL displays as output in history pan. For Example:
string $fruit="apple" // displays "Ok !! I will take one"
string $fruit="grapes"; // displays "Yes !! I like Grapes"
string $fruit="orange"; // displays "Wow !! Juicy"
string $fruit="mango"; // displays Buy some Bananas for me"
+ Switch .. Case +
A switch statement evaluates its control expression and then jump to case statement, if none of the case statements match the control value, default case statement is executed. Syntax :
switch (controlExp)
{ case value1:
![]()
exp1;
![]()
break;
case value2:
![]()
exp2;
![]()
break;
case value3:
![]()
exp3;
![]()
break;
...
default:
![]()
exp4;
![]()
break;
}
Example - 6.4, is switch version of if .. else structure.
Example - 6.4, Switch Control Structure in Action.
$fruit="mango";
switch ($fruit)
{
case "ORANGE":
case "orange":
print ("Wow !! Juicy");
break;
case "apple":
print ("Ok !! I will take one");
break;
default:
print ("Buy some Bananas");
break;
}
Don't forget to place break statement at the end of the case statement, else fall-though will occur and MEL will continuously evaluate the conditions until it reaches a break statement.
+ while loop +
The while loop sends MEL into a loop until a specific test condition is met. A while loop has the form:
while (condition)
{
statement1;
statement2;
...
}
while loop checks the condition, if it is true, the block continue to execute. If your script has an infinite loop, there is no way you can interrupt it. You have to restart the Maya application.
Example - 6.5, while loop.
int $i=0;
while($i<=5)
{
print ("Value of variable I is :" + $i + "\n");
$i++;
}
Example - 6.5, prints the following lines in the script editor.
Value of variable I is :0
Value of variable I is :1
Value of variable I is :2
Value of variable I is :3
Value of variable I is :4
Value of variable I is :5
+ do .. while loop +
do .. while loop is quite similar to while loop, but statement is evaluated before the test condition is evaluated. A do .. while loop has the form.
do
{
statement;
statement;
...
} while (condition);
Example - 6.6, do .. while loop
int $i=0;
do
{
$i++;
print ("Value of variable I is :" + $i + "\n");
}while ($i<=5);
Example - 6.6, prints the following lines in the script editor.
Value of variable I is :1
Value of variable I is :2
Value of variable I is :3
Value of variable I is :4
Value of variable I is :5
Value of variable I is :6
+ for loop +
for loop's conditional argument contains three aspects; initialization, test condition and update condition. Each aspect is separated by semicolon. A for loop has the form.
for (initialization; condition; update condition)
{
statement;
statement;
...
}
Example 6.7, for loop.
int $i;
for ($i=0; $i<=5; $i++)
{
print ("Value of variable I is :" + $i + "\n");
}
Example - 6.7, prints the following lines in the script editor.
Value of variable I is :0
Value of variable I is :1
Value of variable I is :2
Value of variable I is :3
Value of variable I is :4
Value of variable I is :5
+ for .. in loop +
This loop only works with arrays. for .. in loop is used extensively when programmer wants to iterate over each element of the array.
The for-in loop has the form:
for (array-element in array)
{
statement;
statement;
...
}
Example - 6.8, for .. in loop.
string $colors[2]={"red", "green", "blue"};
string $color;
for ($color in $colors)
{
print ("Paint the walls with " + $color + ".\n");
}
Example - 6.7, prints the following lines in the script editor.
Paint the walls with red.
Paint the walls with green.
Paint the walls with blue.
+ break and continue +
Sometime you wants to break a loop iteration immediately want to exit from any point in the block for this you can use the break statement and when you want to finish the current iteration of loop immediately but continue looping in that case use the continue statement.
+ How to test existence of commands, objects, and attributes? +
The exists, objExists and attributeExists functions are used to test existence of the commands, objects and attributes respectively. All three functions return true, when an entity exists. exists command is used to check the existence of the command and scripts. Example 6.8, will test the existence of the command, "polySphere" and if exists; MEL will create a polySphere with default options.
Example - 6.8, exists function.
if (exists("polySphere"))
{
print "polySphere Command Exists, polySphere created";
polySphere;
};
//polySphere Command Exists, polySphere created
We have just created a polySphere with default name pSphere1, now let's test its existence using objExists command as seen in Example - 6.9.
Example - 6.9, objExists function.
if (objExists("pSphere1"))
{
print ("Poly Sphere Exists");
};
//Poly Sphere Exists
General syntax for attributeExists function:
attributeExists("attributeName","nodeName")
Example 6.10, checks for the visibility attribute of the pSphere. It MEL finds sphere visible, next statement hides the sphere.
Example - 6.10, attributeExists function.
if (attributeExists("visibility","pSphere1"))
{
setAttr pSphere1.visibility off;
}
In Example 6.10, we have used setAttr command, don't get confused by that, we will cover attributes in-depth in next chapter.
+ What Next ? +
We have covered lots of things in this chapter. Now you are familiar with the basic programming concepts. In net chapter we will move ahead and talk about the Attributes.
+ 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