Mel - Maya Embedded Language


Data Types



An int is a whole integer number. You can not use decimal points in an int variable. If you were to declare an int without assigning a value to it, the default value will be set to 0

int $defaultInt;
int $myInt = 1;
int $myInt2 = -1;
int $myInt3 = 12345;


The main difference between float and int is that you can now use decimal points, also known as real numbers. Our default value is 0.0

float $defaultfloat;
float $myfloat1 = 1.123;
float $myfloat2 = -1.123;
float $myfloat3 = .123;
Below is an example of a random float value
float $myRandomFloat = rand(0,1000);
print $myRandomFloat;
  • The print command prints information to the screen.


    A string variable is a series of text characters. The default value of a string is empty.
    string $myEmptystring;
    string $myString = "Hello there my friend";
    A nice thing about text is that you can join several string variables together to make a longer piece of text. The technical term for this action is to Concatenation. Let's look at an example of this.
    string $empty;
    string $firstText = "Hello there";
    string $secondText = " my furry friend";

    $empty = "The combined sentence is: " + $firstText + $secondText;

    print $empty;

    The combined sentence is: Hello there my furry friend

    Notice that when I use a variable within my code, I start it off with the $ sign.
    To use special characters in a string you want to use a "\". Special characters include.
    \n newline
    \t tab
    \r carriage return
    \\ backslash character


    A vector variable allows you to hold 3 numbers. The default value of a vector will equal <<0.0,0.0,0.0>>

    vector $defaultvector;
    vector $myvector = <<1.3, 1.2, 1.1>>;
    One way to look at a vector is to look at the first through third slot respectfully as <<X, Y, Z>>. Vectors are usually used in storing directions, positions or color. If you wanted to use an individual component of a vector, you would call it as such:
    vector $V = <<1.3, 1.2, 1.1>>;
    print ($V.x);
    The resulting value should return 1.3. Lets move an object using a vector value:
    //assign random numbers to vector
    vector $V = <<(rand(0,10)), (rand(0,10)), (rand(0,10))>>;

    //replace yourObjName with the object name of what you want to move
    move ($V.x) ($V.y) ($V.z) yourObjName;

    //print the object's new position
    print ("\nthe object's position is: "+$V.x +" "+$V.y+" "+$V.z);


    Arrays, though they may appear daunting at first, are not all that hard to use.

    An array is simply a series of variables. This is actually a pretty nice thing. Think about it, we can use an arrays to store multiple values under one variable name. This comes in handy when trying to store multiple object names, etc.

    Let's see the format of a simple array:

    string $myArray[ ] = {"one","two","three","four"};
    int $myArray2[ ] = {1,2,3,4}
    To use an item in an array you would use the following format:
    string $myArray[ ] = {"polyCube","polyShpere","polyPlane","polyCylinder"};
    int $myObject = $myArray[0];
    print $myObject;
    //the result would be polyCube since the first element in the array is always [0]
    You can gather the length of your array using the size command.
    string $myArray[ ] = {"polyCube","polyShpere","polyPlane","polyCylinder"};
    int $myArraySize = size($myArray);
    print $myArraySize;
    //the result would be 4
    An easy way to add selected objects into an array is by using the ls command. To load an array with the names of the selected items use this format: (make sure you select some geometry first)
    string $myNewArray[] = `ls -sl`;
    When you use ls, put backquote marks around it. ` is located above the tab key. Using the backquote makes the program perform the action within it.

    Exercise: Create an array of four object names. Write the commands to create two different objects and then move them to another spot on the stage using the array.

    Array Funtions
    Operation: Empties the array's contents, freeing all memory reserved for the array
    Example: clear($myArray);

    Operation: Returns the number of elements in an array or the number of characters in a string.
    Example: size($myArray)

    Operation: Returns an array sorted in alphabetical or ascending numerical order.
    Example: sort($myArray)


    A matrix is a two dimensional array which consist of rows and columns of float values. When defining a matrix the user must always define the number of columns or rows the matrix holds.
    matrix $myMatrix[3][5];
    The dimension of this matrix would be 3 rows and 5 columns. The first square brackets are the rows, the second brackets are columns. If we were to plug in values, this is how it would look:
    matrix $m[3][5]= <<1,3,5,7,9; 2,4,6,8,1; 4,7,0,3,6>>;
    print $m;

    // Gives you a result of:
    << 1, 3, 5, 7, 9;
    2, 4, 6, 8, 1;
    4, 7, 0, 3, 6>>

    If I wanted to grab an individual component of the matrix, I would call it as such:
    matrix $m[3][5]= <<1,3,5,7,9; 2,4,6,8,1; 4,7,0,3,6>>;
    print $m[0][3];

    // Gives you a result of: 7
    //we are given a 7 because array start counting at 0