Class: MEL Programming Basics
Instructor: Mike Harris email
Class: 3
Date: March 7, 2006
MEL Programming Basics
- Homework Review:
- Loops:
- While Loop:
- Do something while "conditionStatement" is true.
- Syntax:
initialize condition;
while ( conditionStatement )
{
block of code;
increment/decrement condition;
}
- For Loop:
- Repeat a block of code until the "valueTest" is no longer true
- Syntax:
for ( startValue; valueTest; incrementValue )
{
block of code;
}
- For-in Loop:
- Repeat a block of code for each element in an array
- Syntax
initialize arrayList;
for ( currentElement in arrayList )
{
block of code;
}
- Infinite Loops:
- Never evaluate a false test condition and continue to loop endlessly causing Maya to lock up and crash.
- Example:
int $x = 1;
while ( $x > 0 ) // $x will always be greater than 0, causing an infinite loop!
{
print ("Keep looping...\n");
$x++;
}
- Nested Looping
- You can have loops inside of other loops, adding an infinite possibility of control over your code.
- Example:
string $selected[] = `ls -selection`;
for ( $current in $selected )
{
string $relatives[] = `listRelatives -shapes $current`;
if ( $relatives[0] != "" )
{
for ( $object in $relatives )
{
print ( "The shape node for " + $current + " is: " + $object + "\n" );
}
}
else
print ( $current + " is a transform node.\n" );
}
string $sceneList[] = `ls -selection`;
int $i = 1;
print ( "This is the While Loop example.\n" );
while ( $i <= size( $sceneList ))
{
print ( "\tItem " + $i + " is: " + $sceneList[$i - 1] + "\n" );
$i++;
}
print ( "That is the end of the selection list." );
string $sceneList[] = `ls -selection`;
print ( "This is the For Loop example.\n" );
for ( $i = 1; $i <= size( $sceneList ); $i++)
{
print ( "\tItem " + $i + " is: " + $sceneList[$i - 1] + "\n" );
}
print ( "That is the end of the selection list." );
string $sceneList[] = `ls -selection`;
int $i = 1;
print ( "This is the For-in Loop example.\n" );
for ( $current in $sceneList )
{
print ( "\tItem " + $i + " is: " + $current + "\n" );
$i++;
}
print ( "That is the end of the selection list." );