Class: MEL Programming Basics
Teacher: Mike Harris email
Class: 2
Date: March 2, 2006
MEL Programming Basics
- Special Characters:
- \" - Used when a quotation mark needs to be part of a string
- \r - Used to add a carriage return into a string
- \t - Used to add a tab into a string
- \n - Used to add a new line feed into a string
- \\ - Used to add a backslash into a string. Mostly used for
system paths
- Data types (cont'd):
- Array
- A zero-based series of variables stored in a single
variable container
- Denoted by braces [ ] at the end of the variable name
- Each element is seperated by a comma
- All variable types in the array must be of the same data
type
- Size is dynamic and expands as you add more elements
- Syntax:
- float $myArray[] = { 0.3, 6.325, 71.6 };
- print $myArray[1]; // Result: 6.325 //
- Matrix
- A zero-based two dimensional array that holds floating
point numbers
- Denoted by double braces [ ] [ ] at the end of the variable
name containing the number of rows and columns
- Each row is seperated by a semi-colon while each column is
seperated by a comma
- Size is static and must be initialized when created
- Syntax:
- matrix $myMatrix[3][4] = << 1.3, 0.25, 3.625, 2.25;
41.2, 15.625, 6.335, 23.63; 2.228, 32.3, 54.2, 0.352 >>;
- print $myMatrix[2][1]; // Result: 32.3 //
- Casting Values:
- Backticks are used when capturing a command return value.
- Remember to use the accent key ( ` ), located in the top left
corner on your keyboard, and not the the apostrophe key ( ' ).
- Operators:
- All data on the right side of the
operator is evaluated first, and then applied to the variable on the
left side of the operator
- Operators use basic math "nesting" styles
in determining evaluation order
- ((3 + 4) * 6) / 2 would result in 21 but
3 + (4 * 6) / 2 would result in 15
- Assignment Oerators: ( = )
- Used to set two items equal to eachother
- Syntax:
- int $temp = 5;
- Arithmetic Operators: ( +, -, *, / )
- Basic math functionality
- Syntax:
- int $myAddInt = 5 + 1; // Result: $myAddInt = 6 //
- float $mySubFloat = 6 - 1.7; // Result: $mySubFloat = 4.3 //
- float $myMultFloat = 16 * 3.31; // Result: $myMultFloat =
52.96 //
- float $myDivFloat = 2.25 / 1.3; // Result: $myDivFloat =
1.730769 //
- Note: When using the division arithmetic operator, at
least one of the numbers ( numerator or denominator ) must contain a
decimal point.
- float $myDivFloat = 3 / 2; // Result: $myDivFloat = 1 //
- float $myDivFloat = 3.0 / 2; // Result: $myDivFloat =
1.5 //
- float $myDivFloat = 3 / 2.0; // Result: $myDivFloat =
1.5 //
- In-place Arithmetic Operators: ( +=, -=, *=, /= )
- Changes value to an item
- Syntax:
- int $temp = 1;
- $temp += 5; // Result: $temp = 6 //
- $temp *= 3; // Result: $temp = 18 //
- $temp /= 2; // Result: $temp = 9 //
- Increment and Decrement Operators: ( ++, -- )
- ++ adds 1 to the value of an item
- -- subtracts 1 from the value of an item
- Syntax:
- $temp++; ( post-operation +1 after the entire line is
executed )
- ++$temp; (pre-operation +1 before the entire line is
executed )
- $temp--; ( post-operation -1 after the entire line is
executed )
- --$temp; ( pre-operation -1 before the entire line is
executed )
- Modulous Operator: ( % )
- Used to find the remainder of division in integers
- Syntax:
- int $val1 = 26;
- int $val2 = 8;
- int $modVal = $val1 % $val2; // Result: $modVal = 2 //
- Relational Operators: ( <, <=, >, >=, ==, != )
- Tests relationship conditions in if statements
- < Less than
- <= Less than or equal to
- > Greater than
- >= Greater than or equal to
- == Equal to ( Note: Do not confuse with assignment = operator
)
- != Not equal to
- Syntax:
- int $a = 3;
- int $b = 4;
- if ( $a < $b ) // Results in a true statement //
- if ( $a == $b ) // Results in a false statement //
- if ( $a != $b ) // Results in a true statement //
- Logical Operators: ( &&, ||, ! )
- Tests conditions in if statements
- && And - Needs both evaluations to be true
- || Or - Needs at least one evaluation to be true
- ! Not - Toggles the current state
- Syntax:
- int $a = false;
- int $b = true;
- if ( $a && $b ) // Results in false statement //
- if ( $a || $b ) // Results in true statement //
- if ( !$a ) // Results in true statement //
- Conditional Operator: ( ?: )
- This is a shorthand way of testing a condition and populating
a variable depending on the results
- Syntax:
- int $val1 = 5;
- int $val2 = 4;
- float $newVal = ( $val1 > $val2 ) ? 13.25 : 36.375; //
Result: $newVal = 13.25 //
- Operator Order of Precedence:
- Operators in the same precedence group are evaluated from
left to right in the order they appear in the statement
- Precedence Groups:
- ( ), [ ]
- !, ++, --
- *, /, %, ^
- +, -
- <. <=, >, >=
- ==, !=
- &&
- ||
- ?:
- =, +=, -=, *=, /=
- Conditional Statements:
- An action will be performed only if a specified condition is
evaluated to be true
if ( condition
is true )
do something;
else
do something else;
- Looping:
- Used for repeating a block of code a specified number of times
- While Loop: Repeats a block of code until a
"conditionStatement" is
no longer evaluated as true
initialize condition;
while (
conditionStatement )
{
block of code;
increment value;
}
- For Loop: Repeats a block of code until the "valueTest" is no
longer evaluated as true
for (
startValue; valueTest; incrementValue )
{
block of code;
}
- For-in Loop: Repeats a block of code for each element in an
array
initialize
arrayList;
for (
currentElement in arrayList )
{
block of code;
}
- Infinite Loops: Always evaluate as true and never stop looping,
causing Maya to lock up and crash!