Mel: Basics

From wikinotes

Variables

Standard:

More Detail: String Variable Conversion
string $variable = "string";            //text variable. You can check if it is empty: if ($variable == ""){};

int $variable = -1;                     //whole, non- fractional numbers

float $variable = -5.86;                //4 bit precision

double $variable = -5.86;               //8 bit precision

matrix $variable[2][3] = << 3, 4, 5;
									3, 9, 0>>;   //matrix variable (size must be pre-defined)
													 //matrix index numbers start at 0, but when declaring them
													 //you must declare the actual number you want.

Arrays:

2D Arrays

Arrays start counting from 0

string $array[] = {"a", "b", "c"}; //A basic array
print  $array[0];



size($array);                               //Total of all variables stored (count starts at 1)

sort($array);                               //Alphabetizes all entries in that array (even works with paths!)

clear($array);                              //Resets Array

Vectors:

vector $vector = <<1.0, 5.0, 2.5>>;         //Declares a Vector

print ($vector.x);                          //How to print a single value of the vector

vector $myvector = $myVector1 - $myvector2; //You can evaluate arithmetic with multiple vectors

Vectors cannot have values assigned to them in Mel simply by stating $vector.x = 3.0; instead you must redeclare the entire vector:

$vector = <<10.0, 5.0, 3.0>>;
$vector = <<5.0, $vector.y, $vector.z>>;

Global Variables:

You can make variables global by preceding the variable declaration with "global". global variables must be declared before being assigned a value.

ex: 
global string $variable;
$variable = "abcd";

Operators

Operators

x == 4                     //check if x = 4

x != 4                     //check if x does not = 4

>=  or <=                  //bigger/smaller than or equal to

>   or >                   //bigger/smaller than

Logic Operators

||                         //OR

&&                         //AND

!                          //NOT ex: (x=4 && ! y=5)


Standard Operators

*                          //multiply

/                          //divide

%                          //remainder (ex: 5 goes into 12 twice, the remainder is 2)

+                          //add

-                          //subtract

Shortcuts

$score *= 2                //$score = $score * 2 (works with all operators)

$i++                       //$i = $i + 1

$i--                       //$i = $i -1

If Statements

if ($a == "abc") {
   print $a;
} else if ($a == "def") {
   print "abc" + $a;
} else {
   print "$a is not abc or def";
}

Loops

Loop Types

for ($i = 0; $i < 10; $i++){       //for loop
	                                //commands to execute
}

string $varList = {"A", "B", "C"};
for ($var in $varList) {
//command
}


while ($i <= 10){                  //executes while a condition is met
//commands to execute
}



do {                               //do loop (same as while loop, except
//commands to execute              //it executes once before condition is evaluated
} while($i <=10);

Loop Tools

continue;                          //Skips remaining code, and proceeds to the next loop cycle

break;                             //exits current loop only (not all nested loops. cannot be stacked)

Procedures

Procedures

proc myProcedure(){               //myProcedure; can only be called from procs in this melscript file
    //contents of procedure go here
}




global proc myGlobalProcedure() { //myGlobalProcedure; can be called from maya or any other script
    //contents of procedure go here
}
proc string[] listnames(string $ids[]){
    string $names[];
    return $names;
}

Processing Data in Procedures

global proc string myProcedure(){			//returning information from a called procedure
	return "this is the return value";
}




proc myProcedure(int $numOne, string $text, string $array[] ){		//bringing information into a procedure
	//contents of procedure go here			      //in this example, the procedure would be called like this:
}							                           //myProcedure(2,"this is my text");



return;                                                 //can also be used similar to break; to exit the procedure

Any variable declared between the { and } brackets is local to that procedure. Even if it is global, it must be declared within a new procedure if you want to be able to use it. This is called scope.

Comments

// indicates a comment

/*

everything written between these will be commented out

*/

Misc

evalDeferred

Occasionally, you need to wait for maya to finish doing everything to run a particular command. This is hacky, but sometimes it is a lifesaver.

evalDeferred "mycommand";