Class: MEL Programming Basics
Instructor: Mike Harris email
Class: 4
Date: March 9, 2006
MEL Programming Basics
- Conditional Branching:
- An action will be performed only if a specified condition is
evaluated to be true
// if condition //
if ( condition
is true )
do something;
// if-else condition //
if ( first
condition
is true )
do something;
else
do something else;
// if-else if-else condition //
if ( first
condition
is true )
do something;
else if ( second condition is true
) // Multiple else if tests are optional and
valid
do something else;
else if ( third condition is true )
do something else;
else if ( fourth condition is true )
do something else;
else
do something else;
- Switch Case is a way of shortening the amount of typing for a
large if-else if-else conditional branching test, replacing the need
for multiple else if tests
- Syntax:
switch
(controlValue )
{
case value1:
statement;
break;
case value2:
statement;
break;
case value3:
statement;
break;
default:
statement;
}
- You can remove the "break;" line to "fall through" to the next
statement if the value is true. This means that if value2 was met and
there was no break in that group, the statements for both value2 and
value3 would execute until the "break;" of value3 was reached.
- Scope:
- Global vs. Local:
- Global variables are seen everywhere in Maya and can become
problematic
with naming conflicts and the possibility of changing someone else's
data.
- Local variables are seen only within the "nested" level they
are created in and all of that levels "nested" children.
- Nesting is a heirarchy way of making levels of code depend on
the parent level of code.
// Global scope!
grandparent level
{
// start gransparent
scope
parent level
{
// start parent scope
child1 level
{
// start child1 scope
}
// end child1 scope
child2 level
{
// start child2 scope
}
// end child2 scope
}
// end parent scope
}
// end gransparent
scope
- If a variable is declared inside the curly braces of a
multi-line statement (loop, conditional branch, or proceedure) the
value of that variable is only visible while you are inside the curly
braces. So when you leave the multi-line statement, it is removed from
memory.
- If you declare the variable outside the curly braces it will be
seen at it's level as well as all nested "child" levels of the script.
- Example:
for ( $i = 0; $i < 10; $i++ )
{
string $badscope = "Hello World!";
print ( "This is pass number: " +
$i + " through the loop\n" );
}
print (
$badScope ); // This will give you an error because Maya no
longer sees the variable $badScope
string
$goodScope = "Hello World!";
for ( $i = 0;
$i < 10; $i++ )
{
print ( $goodScope + "\n" );
print ( "This is pass number " +
$i + " through the loop.\n" );
}
print (
$goodScope ); // Because the variable was declared outside
the curly braces of the loop, Maya sees it both inside and outside of
the nested loop
- Functions vs. Procedures:
- A function is a built in piece of code that will perform a set
task based on the user input
- Examples of functions:
- size( )
- rand( )
- sphrand( )
- rad_to_deg( )
- deg_to_rad( )
- A proceedure is a small piece of code that performs a specific
task allowing you to moduralize your code into more managable pieces.
- Sometimes code is used more than one time in a script. This is
not a loop, but simply code that can serve more than one purpose at a
different time in the script.
- Basic syntax:
proc procName(
arguments ) // arguments are optional
{
// Tasks to perform...
}
- Passing Arguments to Procedures:
- Since variables are only seen within the block of code they were created, arguments are ways that we can pass variables from one procedure to another without having to make them global.
- Example:
proc
add2Numbers( float $numA, float $numB )
{
float $total = ( $numA + $numB );
print ( "The sum of " + $numA + "
and " + $numB + " is: " + $total + "\n" );
}
add2Numbers(
3.5, 25.63 );