Maya MEL Introduction – part 1

MEL Intro - part 1 MEL stands for Maya Embedded Language, it is a scripting language used in Maya, it is similar to Perl.

The magic of MEL scripting is, that you don’t have to touch mouse or use a single menu and yet create, manipulate and animate the whole scene. This is exactly what we will do. The only keyboard shortcut you will ever need is ctrl+c, ctrl+v and ctrl+Enter (assuming you are using Windows) 🙂

MEL opens way to endless possibilities; it allows you to see what is going on underneath the surface. You will be amazed how simple or (sometimes) how complicated matters really are.

The basic idea behind Maya is, that every single mouse click or keyboard shortcut you perform is transformed into a command or series of commands. Those are then executed and manipulate your scene or your environment. You can easily check this by opening the Script Editor. To do so click on the icon just right from the Command Line, if you cannot see the Command Line, go to menu Display -> UI Elements -> Command Line and make sure the checkbox is checked.

Introduction- running the script for the first time

First steps before running the script:

Download the file: scriptSPAFi.mel

The prerequisites for this example are these:

Start with a new scene, in Preferences, set your Up axis Y, Working Units –> Linear centimeter

MEL Intro - Preferences

Preferences

Another setting that will later help you orient in the scene is to set the grid. Set its Size to 10, Grid lines every 1 unit and Subdivisions 1. Also Perspective grid numbers: On Axes.

MEL Intro - Grid settings

Grid settings

Before opening and running the script from a file, I would recommend you backing up your preferences folder. These can be found in a directory Documents\maya. There you will find a folder called “7.0” or “8.0” or something like that, depending on your version of Maya. Just copy this directory somewhere else (its size shouldn’t exceed 10MB). When something goes wrong with your Maya preferences, you can always return to this backup copy and copy the files back

It is usually better to run the script after a fresh start of Maya, so that everything is in its starting state. Even after setting the preferences it is better to close Maya and open it again, so the first thing you do in the program is opening and running the script file.

Opening and running the script (.mel) file:

To run a script in Maya you use the Script Editor (S.E.). The way to access it is to click on the small icon with a scaled depiction of the Script Editor. It is located next to your Command Line, as mentioned before.

MEL Intro - Opening the Script editor

Opening the Script Editor

MEL Intro - Script Editor (Maya 8.5)

MEL Intro - Script Editor (Maya 7.0)
Script Editor (Maya 8.5) Script Editor (Maya 7.0)

Script Editor has its own menu; go to File–>Load Script… (8.5) (or File–>Open Script… – 7.0).

Locate the downloaded file scriptSPAFi.mel and open it.

The file will load itself into the input area of S.E. Now move your Script Editor window to the bottom of your screen, so you can watch presentation of what the script does.

To execute loaded script hit ctrl+Enter on your keyboard. (Or the Enter on the numerical keyboard will do the same job for you.)

After the start, this script makes pauses, waiting for your acknowledgement. Just click OK.

Writing your own script- general information

Setting up the environment:

We will start by arranging our workspace. My favorite layout for MEL activities is Three Panes Split Right. Script Editor on the left side, top right viewport- perspective view and the last panel either Outliner or Hypershade.

MEL Intro - MEL Layout

MEL Layout

Where to type the commands?

All the commands throughout the way will be typed in the Script Editor and run by ctrl+Enter shortcut key. The final script is saved as an external MEL file that can later be opened and executed as the whole.

Note about the semicolon;

One very important note for all people starting with scripting- do not forget the semicolon at the end of every command! Although in Command Line (works as well with a single command in S.E.) Maya will be tolerant and will run the command for you as expected and desired, in the script you must end every command with the obligatory semicolon. It is how Maya distinguishes two commands apart.

After some time you will automate this habit and soon you will find yourself ending every sentence by a semicolon 😉

Getting help:

The first command you should familiarize yourself with is also one of the most important ones. It is help. Not only does it help you opening the help window- type help and hit CTRL+Enter in S.E., or Enter in Command Line. It also explains you syntax of every command. Try typing for example this: help move (CTRL+Enter / Enter). This will list all the options (flags) you can use with “move” command.

MEL Intro - The results of help on help

The results of help on help

Testing:

Testing only certain parts of your script can be achieved by selecting that part in S.E. input area and pressing ctrl+Enter. This will execute only the selected commands and it still leaves everything you typed in its position as it is in the Script Editors’ input area.

The nice thing is that when you execute piece of your code, that doesn’t work and only creates undesired mess in your scene, you can always hit ctrl+z . But make sure, your camera view (or any other window except the S.E.) is selected, because otherwise you would be undoing your typing and editing you did in Script Editor.

Description of the script “scriptSPAFi.mel”

Intro:

The script is divided into many separate parts, each taking care of a different job. Some parts even repeat and will be discussed only once, on the first occasion of their occurrence.

In this short space, there is not enough room to go into every little detail, so only the important and interesting commands will be noted.

The description will be brief, usually describing how you would perform this task in workflow without using MEL.

You certainly don’t have to understand all the math in the script, just try to follow the main principles. And, of course, the best way to discover and learn something new is to experiment for yourself- feel free to change any part of the code and see, what happens  🙂

Before running the script it is best and recommended to restart Maya, start with the new file and newly started Maya.

Environment:
In this part we will setup our working environment, layout and viewports.

Maya Environment:

ThreeRightSplitViewArrangement;

Sets arrangement to 3 window, the one on right side is split in two halves horizontally.

//scriptedPanel -e -rp modelPanel4 scriptEditorPanel1;  // IF YOU ARE USING MAYA 8 AND ABOVE REMOVE THE FIRST TWO SLASHES ON THIS LINE

Two slashes work as a sign, that the text after the two slashes is not executed. Simply- a note for you or for others.

As the note says- if you are Maya 8 or 8.5 user, then remove the first pair of slashes.

lookThroughModelPanel persp modelPanel2;
outlinerPanel -e -rp modelPanel3 outlinerPanel1;

Sets up appropriate working windows in the layout.

HideUIElements;

As the name says- hides all UI elements. We start with no UI element visible.

ToggleTimeSlider;
ToggleRangeSlider;
ToggleHelpLine;
ToggleCommandLine;
ToggleStatusLine;
ToggleChannelsLayers;

The name says it all- these lines toggle the elements we do want to see.

MEL Intro - What should the layout look like (Maya 8, 8.5)

MEL Intro – What should the layout look like (Maya 8, 8.5)

Viewport Setup:

select persp;

Selects persp camera. This works just like clicking on the object.

Just like in the usual “clicking” way, each command can be executed on currently selected object. Or another way is that the values are assigned directly to the objects’ attributes. We will use the first way. At first we select the camera, then we apply the command.

move -10 4.5 0.5;

This command moves selected object (camera persp) to this location (x=-10 y=4.5 z=0.5), the coordinates are in XYZ order, separated by a space. The point used here is a decimal point. And of course everything ends with a semicolon. It does what would be achieved by typing the coordinates in coordinate box with move tool active.

In this case those are absolute coordinates. To use relative you would have to put a flag after the word move (for example: move -r 0 1 0 moves selected objects 1 unit in the upper direction).

rotate 165 -72 180;

The same as with move. Values can also be either positive or negative. Also the possibility of relative values is available.

modelEditor -edit -da “smoothShaded” -displayTextures on -dl “default” modelPanel2;

Just like pressing 6 on your keyboard- the viewport will use smoothshading.

setAttr “perspShape.backgroundColor” -type double3 0.20525 0.775742 0.821 ;

This is the second way to manipulate object – without first selecting it- value is assigned directly. This will turn the background of your possible render to a bluish color. The three numbers represent values of components of RGB model, in the order Red Green Blue. Range for the value is from 0 to 1 (for example 1 0 0 would be red color).

setFocus modelPanel2;

Just like right-clicking in the viewport, it sets focus to model panel.

MEL Intro - Moved and rotated camera "persp"

Moved and rotated camera “persp”

In the next part of MEL Intro we will describe you more about the cubes and their movements using the variable matrix for the animation, so stay tuned up.