This tutorial covers the basics of making your own MEL procedures, MEL
procedures allow you to define your own language in MEL, creating your
own commands instead of using the built in commands like 'move', 'rotate'
and 'polyCube'. The power of being able to make your own procedures is
the ablity to create a simple interface to a vast array of functionality
and decision making. MEL procedures do this by allowing you to take a
series of complicated commands, group them together and give them a name.
In this simple tutorial we are going to create a new MEL procedures called
'stair', this procedure will perform all of the calculations necessary
to create a set of stairs based on height, number of steps and starting
location. Once you have made this function, you can save it to a file
and source it whenever you start Maya. If you do that, you can use it
over and over again, wherever you need a staircase.
Step 1

Making stairs with a procedure |
The first step in the tutorial is just to make a basic looping script
that will create a set of stairs. After you have made this, you can encapsulate
it in a MEL procedure that gives it a name. The script uses a 'while'
loop, to reptetively call the 'polyCube' command and the 'move' command,
creating 10 steps and moving them into a stair configuration. If you cut-and-paste
the following code into your MEL scripting window you should get something
resembling the image on the right.
$i = 0;
//loop through while $i is less that ten and create some cubes
while($i < 10){
polyCube;
scale 10 .1 1;
move 0 $i $i;
$i++;
}
Step 2
The second step in this step is to take the loop from step 1 and wrap
it inside of a procedure. To create a procedure, just surround the loop
with a procedure declaration. Place the statement 'proc stair(){' at the
beginning and '}' at the end. This defines it as a procedure named 'stair()'
(the parthesis will be explained later in the tutorial). When you copy-and-paste
this script into the MEL scripting window, nothing will happen on the
screen. However, once you have cut-and-paste the script, you will have
a new function called 'stair()' that you can execute. Once you copy-and-paste,
type 'stair();' into the MEL scripting window and it will create a stair.
proc stair(){
//create a function called 'stair()' that will perform the loop
//loop through while $i is less that ten and create some cubes
$i = 0;
while($i < 10){
polyCube;
scale 10 .1 1;
move 0 $i $i;
$i++;
}
}
If you add the word 'global' before your procedure declaration, then if
you save it to a file you can use the files as a function library. Add
a 'global' declararion before the 'proc' statement and save your script
as a text-only file called 'stair.mel'. To load the stair function at
any time, choose 'Source Script' from the 'File' menu in the MEL Scripting
window and select you file.
Step 3
Now that you have defined your 'stair' procedure, you can refine its definition
by providing for 'arguments'. 'Arguments' allow you to feed
information or varaibles into your function so that it can have different
behaviors. These arguments are the variables that come between the parenthesis
when you call a procedure.
In this step you will redefine your function so that the number of stairs
is variable. To do this change the function declaration to be 'global
proc stair(float $numberofsteps)', this tells the function that is will
receive a variable (which will be a floating point decimal) and that number
will define how many steps should be in the stairs. Use the '$numberofsteps"
variable inside of the function to limit the iterations of the loop.
global proc stair(float $numberofsteps){
//create a function called 'stair()' that will perform the loop
//this function will take the argument 'numberofsteps'
//loop through while $i is less that $numberofsteps and create some cubes
$i = 0;
//change the < to be less that $numberofsteps
while($i < $numberofsteps){
polyCube;
scale 10 .1 1;
move 0 $i $i;
$i++;
}
}
Arguments have to be defined by naming the variable, and defining its
type. In the example above we define the variable as being '$numberofsteps'
and we can use that variable inside of the procedure by referring to its
name. The variable name is preceded by its type definition, there are
many different type definitions in MEL, but the 3 most common are:
float - Floating point number. This is a decimal number, like 2.3457
or 0.0000045.
int - Integer. This is a whole number, like 2 or 345839.
If you define a definition as an 'int' and pass the procedure decimal
number, it will automatically be rounded down to the closest whole number.
For example. If you have a function defined as 'proc box(int $size)' and
you call it with the following syntax 'proc box(4.56342)' the $size will
be rounded down to 4 inside of the procedure.
string - Text string. This is a word or series of words, like "Hello"
or "My name is ....".
Once you have cut-and-pasted the code into the MEL scripting window, you
will have to call the stair command with a number as an argument, try
'stair(20);' and it will make 20 steps, 'stair(40);' will make 40 steps.
Step 4
The stair procedure can be further refined by adding additional arguments.
In this step you will add arguments of '$locationx', '$locationy' and
'$locationz'. These additional arguments will be used to position the
staircase anywhere in space (instead of always being at 0,0,0), making
the 'stair' procedure much more self-contained and usable in more situations.
global proc stair(float $numberofsteps,float $locationx,float $locationy,float
$locationz){
//make function called 'stair' that takes arguments of 'numberofsteps',
plus x,y,z coordinates of a starting point.
//loop through while $i is less than the number of steps.
$i = 0;
while($i < $numberofsteps){
polyCube;
scale 10 .1 1;
//use the arguments of locationx, locationy and locationz to position
the steps.
$x = $locationx; // the x position
$y = $locationy + $i; // the y position is the starting y position, plus
and offset for each step
$z = $locationz + $i; // the z position is the starting z position, plus
and offset for each step
//move the step to x,y,z
move $x $y $z;
$i++;
}
}
When you call the stair command now in the MEL scripting window you will
have to give it 4 arguments, such as 'stair(20,10,10,10)', which would
make a staircase with 20 steps at location 10,10,10.
Step 6
The last step is to add some more intelligence to your stair function.
Aadd the argument of '$height' to the function and use the height and
the number of steps to determine the spacing between each individual step.
Once you have done this you can make stairs anywhere you need and you
only have to know the number of steps, height and location.
global proc stair(float $numberofsteps,float $height, float $locationx,
float $locationy, float $locationz){
//add and argument of 'height' to the stair function.
//use the 'height' variable to calculate the step riser height. The riser
height is equal to
//the overall height divided by the number of steps.
float $step = $height / $numberofsteps;
$i = 0;
while($i < $numberofsteps){
polyCube;
scale 10 .1 1;
$x = $locationx;
//use the 'step variable as a multiplier so that each step is based on
the riser height
$y = $locationy + ($i * $step);
$z = $locationz + $i;
move $x $y $z;
$i++;
}
}
One of the real values of procedures is to provide a simple interface
ot complex functionality and calculations like this stair. As a modeler,
you do not want to have to calculate the stair spacing and other things
to create a stair, all you know is how big you want the stairs and where
they should be - the procedure should handle the rest. You can further
refine your procedure to calculate for proper landings, railings and ballustrades.
Remember, save your stair procedure to a file and you can load it by selecting
'Source Script' each time you start Maya. If you quit Maya and restart,
your custom function will not be available until you source the file again.