Deborah R. Fowler


MEL Examples - continued notes

Updated on April 14   2013

Class 7

Open up window->general editors -> script editors

// First try creating a cube

polyCube;

// Next try scaling the cube

polyCube;
scale 20 .5 20;



// Now try variables

$i = 0;
polyCube;
scale 10 .5 10;
move 0 $i 0;



// Now try looping

$i = 0;
while ( $i < 10 )
{
    polyCube;
    scale 20 .5 20;
    move 0 $i 0;
    $i++;
}



// now try a function

proc drawBox( float $width, float $height, float $depth )
{
    $i = 0;
    while ( $i < 10 )
    {
        polyCube;
        scale $width $height $depth;
        move 0 $i 0;
        $i++;
    };
};


drawBox(20, .5, 20);


// Now draw a grid of these
select -all;
delete;

proc drawBox( float $x, float $z, float $width, float $height, float $depth )
{
    int $i;
    $i = 0;
    while ( $i < 10 )
    {
        polyCube;
        scale $width $height $depth;
        move $x $i $z;
        $i++;
    }
}

for ($i = 0; $i < 10; $i++)
{
    for ($j = 0; $j < 10; $j++)
    {
        drawBox( $i * 40, $j * 40, 20, .5, 20 );
    }
}


// Or the one in the manual

int $i;
for ($i = 10; $i > 0; $i--) {
        print($i+"...\n");
}
print("Blastoff!!!");



// Introducting lists
// Didn't talk about this specifically in python (some of you discovered them of course)
// lists are ways of keeping track of things

///Mel has lists that are less general than python lists:

// vector, arrays and matrix
// A vector (in c it is a list, not just a 3-tuple)
// An array is declared with  type variableName[]
For example
int $arrayOfInts[];
float $arrayOfFloats[4];

Access is thru subscripts:
$arrayOfFloats[2] = 4.0;


vector $v= << 1.2, 3.4, 6.5>>;
float $ar[] = {1.2, 3.4, 4.5};
matrix $myTable[3][2];  // A 3 x 3 table of floats


// ALL OF THESE HAVE THE SAME TYPE
// In MEL unlike other languages -  you cannot make an array of arrays


So far we have been running the scripts from the window, we can also create files with .mel extension. Type in the blastoff loop above into a file called blastoff.mel. You can then select this from the dialog box that your run your scripts from by selecting File->Source Script

For example run blastoff.mel (download here.)



// Random
// rand command generates a random float
rand(100); // gives a number between 0 and the argument
rand(0,1) // if you give two arguments, it returns a number between the two