Building a Simple MEL Window


One of the most satisfying elements of MEL scripting is the creation of custom windows, or GUIs.
If you’re relatively new to the world of scripting, however, the MEL code can be very intimidating.
With that in mind, this section boils down MEL windows into their most basic components.
The following MEL script makes a simple window with two drop-down menu options,
two buttons, and a short text message:
window -title “Simple Window” -menuBar true newWindow;
menu -label “Options”;
menuItem -label “Save File” -command “file -f -save”;
menuItem -label “Exit Maya” -command “quit”;
columnLayout;
button -label “Scale Up” -command “scale -r 2 2 2”;
button -label “Scale Down” -command “scale -r .5 .5 .5”;
text -label “Click me!”;

showWindow newWindow;

This script is saved as simple.mel in the Chapter 1 mel folder on the CD. To use it, open
the Script Editor, choose File ➔ Source Script, and browse for the file. The MEL window pops
up immediately. You can also paste the text into the work area of the Script Editor, highlight
it, and MMB drag it onto a shelf to create a shelf icon.
You’re free to use any command within the quotes after each -command flag, whether it
is used for a menu item or a button. If you’re wondering what commands are available, look
no further than the Script Editor. Every single transform, operation, and tool within Maya has a
MEL line associated with it. For example, if you transform a sphere, a line similar to this appears:
move -r -16.289322 8.110931 10.206124;
If you create a default sphere, this line appears:
sphere -p 0 0 0 -ax 0 1 0 -ssw 0 -esw 360 -r 1 -d 3 -ut
-tol 0.01 -s 8 -nsp 4 -ch 1;objectMoveCommand;
Even though the sphere command has a huge number of option flags (a flag has a dash
and several letters, such as -p or -ax), you do not have to use them all. sphere by itself will
suffice. The same holds true for tools. For example, the Rebuild Surfaces tool prints out this:
rebuildSurface -ch 1 -rpo 1 -rt 0 -end 1 -kr 0 -kcp 0
-kc 0 -su 4 -du 3 -sv 4 -dv 3 -tol 0.01 -fr 0
-dir 2 “nurbsSphere”;
With any tool, you can pick and choose the flags you need. For example, rebuildSurface
-su 12 will rebuild the surface with 12 spans in U direction with all the other settings left
at default. Rest assured, memorizing what each and every f lag does is close to impossible.
Luckily, you can look up the f lags and their functions by choosing Help ➔ MEL Command
Reference in the Script Editor. All Maya commands, including all tools, are listed with a
detailed explanation of all possible f lags. Keep in mind that f lags have a short form and a
long form. For instance, -su is the same as -spansU.
Commands used by buttons and menus are not limited to tools and such operations as
file -save and quit. You can also launch Maya windows. For example, HypergraphWindow
opens the Hypergraph window and GraphEditor opens the Graph Editor. To see the MEL
lines associated with windows, choose History ➔ Echo All Commands in the Script Editor.
Note that MEL scripting is case sensitive.
Returning to the simple.mel script, the columnLayout command allows you to add as many
buttons as you’d like to the layout. By default, they stack vertically. A layout command is
mandatory for basic MEL windows. You have the choice of rowColumnLayout, rowLayout,
or columnLayout, each of which organizes the window according to its name. You can add
extra menu items by inserting additional menuItem lines. If you’d like more than one dropdown
menu, add additional menu commands. The text command offers a simple way to
add a message to a window. Whatever message you would like to appear should be inserted
between the quotation marks after the -label flag.

On the first line of the script, the window command describes the GUI window. On the
last line, showWindow newWindow launches the described window. A variation of the window
command is mandatory if a pop-up window is desired.


(Top) A red MEL error on the Command line. (Bottom) A MEL error message in the Script Editor. In
this example, a quotation mark is missing before the ending semicolon.

If you write a new MEL script, or adapt an example, and the script fails to run, a red
error message appears on the Command line.



Post a Comment

Previous Post Next Post