MEL
MEL
MEL
Adrian Herbez
purplestatic@yahoo.com
MEL scripting
One of the great things about Maya is that, although it might not do everything you'd like it to straight out of the box, it
provides a way for you the user to add your own functionality via MEL (Maya Embedded Language).
Disclaimer: there is much too much here for a 2-pager. Refer to Maya's built-in documentation for more info
While you can make extremely complex scripts that add major functionality, you can also get a great deal of benefit
(increases speed, efficiency, and ease) from relatively simple scripts.
If you've downloading scripts, that's all there is to it. If you want to write you own, though....
Writing scripts
While you can write scripts as long as you like in the script editor, you'll generally want to use an external editor, as code
dissappears from the script editor once it executes. So, the workflow looks like this:
- write a script in an external editor (Notepad or similar)
- save it out to your script directory as Something.mel Make sure to save it as plain text
- execute it in Maya to test it by typing its name at the command line, and, most likely...
- go back to your text editor to make changes
Note that for Maya to see any changes to a script, you have to source the script. This will make Maya re-load it into
memory, and will reflect any changes you've made to what the script does. To source a script, use the File->Source script
command from within the script editor. Do that every time you make changes to your script. Also, before running a script
for the first time, you must use the rehash command to force Maya to re-examine its script directories and re-build the list
of known scripts.
Structure of scripts
All scripts must contain at leat one "global procedure" in order to be run. What that means is that you need to start a script
with the following:
and end it with a closing }. "myProcedure" is the name of the procdeure, and what you type to get Maya to run it. Scripts
are made up of a few kinds of elements, mainly variables, commands, control structure, and functions
Variables are bits of information, and can be one of several different kinds (integer, float, string, array, etc).
Commands are statements that either request information from Maya, or tell Maya to do something
Control Structures are specialized commands that affect the order in which statements are executed
Procedures are blocks of code, containing statments and variables that accomplish some task
Custom GUIs
MEL not only allows you to make your own tools, but it also allows you to wrap them up into a custom GUI (Graphical User
Interface) to make them easier to use. Making your own GUI consists of the following steps
- creating a window
- selecting a layout, or series of layouts
- creating controls and attaching them to the window
- attaching MEL commands to the controls
- showing the window
The last step is a bit counter-intuitive, but necessary. Though MEL has a window command to create a window, it doesn't
display it until you tell it to. Creating a window looks like this....
this specifies two things- the text that will appear in the top of the window (myTitle, in quotes), and the name by which
Maya will know the window (windowName). Notice that the line ends with a semicolon ( ; ). All MEL commands must end
with a semicolon.
Once you've got a window, you need to specify a layout, which will contorl how control items are placed within the window.
There are several different layout types available, here's an example.
rowColumnLayout -numberOfColumns 2;
...which will create a 2-wide grid (with as many rows as necessary) for control items to fit into. Now you're ready to add
control items. There are a variety of different kinds- let's start with a button;
floatSliderGrp -label "Y position" -min -10 -max 10 -value 0 -step 1 posY
...will create a slider that takes float values and ranges from -10 to 10, starting at 0, and names it "posY". To get this to
control something, we can use the connectControl command...
showWindow windowName;
This will make the window actually appear in Maya, et voila!
This just scratches the surface of what MEL is capable of. To really explore the possibilities, be sure to look at the MEL
pages in Maya's help documentation, as well as the MEL command reference (Help->MEL command reference) to see
what the available commands are.