Maya MEL Scripting: Episode 01

I know, “MEL is sh*t (not as good) compared to Python”.
Still, MEL can be really useful for artists (for you).

I decided to create a series to give some practical tips for you (Maya user) about MEL scripting.
My codes are not necessarily nice or well designed, and perhaps not even “correct” (in a way 🙂 ).
I’m not a developer or programmer, but I’m a generalist and I’m doing this for years as a professional.
Be brave, be creative and don’t give up.
Cheers, D

Tip nr. 0

For simple things (usually) MEL is shorter and simpler than Python.
For complex things, Python is much more suitable. You can learn both.

Tip nr. 0.5

Most useful:
Go to the Technical Documentation section and then choose MEL Commands.
You can find an extensive list of the commands, and a few examples for each as well.

And the other one:
I know, it’s old. Still good tho 🙂

For simple code editing software, I recommend Visual Studio Code.
Totally free and small. And you can add the MEL extension to it with a single click.

Tip nr. 1

Use { as a start and } to finish your codes. If you change a variable type in your code you’ll not get any special messages about that (it’s annoying).

Tip nr. 2

Store the name of the selected nodes in a variable called sel:
To store multiple things the variable will be a string array. (It’s become an array when you have [] at the end of the variable’s name. Without that it’s just a simple string that can contain one element/item.)

    string $sel[] = `ls -sl`;

Tip nr. 3

The most useful code part ever is this:
This for cycle is useful when you want to do things on each previously-stored node.
This version is a simple one.
It creates a variable called $each from every element of the $someVariable array.
Inside the for loop, you can do different things with those elements/items.

    for ($each in $someVariable)
        //the commands goes here, you can do something with $each node

For example: Rename each selected object, based on its parent’s name:

    // Store the selections
    string $sel[] = `ls -sl`;

    // For cycle to do something with each selected object
    for ($item in $sel)
        // The listrealtives command with the parent flag is used to get the name of the group. 
        // (A group is just a parent transform node without a shape node.)
        // The parent node's name is stored in a $parentName variable, yes it's an array as well. 
        // It has to be because of the listRelatives command.
        string $parentName[] = `listRelatives -parent $item`;

        // After we have the parent's name, just rename the current element with the rename command.
        // You can concatenate multiple “words” into one with a simple + sign.
        // In this case the first element (index 0) in the $parentName[] array plus the word Geo.
        // The Geo suffix after the group name is useful.
        // Don't worry, Maya won't let you use the same name at the same hierarchy level.
        // By the way you don't want to use the same name at any hierarchy level.
        rename $item ($parentName[0] + "Geo");

That’s it for today.

Cheers, D

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.