Damn it, Maya. Just… Damn it.

Maya Hates Me: The Best Way to Learn MEL… maybe.

MEL, Maya Embedded Language, is the backbone of the Maya system.  In fact, almost all of what we call “Maya” (the interface, the menus, the buttons and gizmos)  is generated by MEL.  The actual application of Maya is essentially just a MEL interpreter.  You could completely rewrite the Maya interface using MEL.  Everything you model, animate, or configure in Maya, no matter how complicated, could potentially be replicated through a MEL script.  This makes MEL quite possibly the most powerful tool in the toolbox of a dedicated Maya artist.  You can automate repetitive tasks, and procedurally generate models and animations that would otherwise take hours or days to achieve — if not be impossible to do so at all.  As I continue to learn MEL, I’ll be posting some tips, tricks, annoyances, and the like.  Might even throw in my scripts as a philanthropic gesture.

So, the question is… what is the best way to go about learning this stuff?  Honestly, I don’t know.  But if you’re keen on teaching yourself, here’s some advice.

STEP ONE: Get Inspired!

For me, this fellow here did the trick:

http://richsunproductions.info/vsfx705/modelingsuite.html

Some of the stuff he has on there is damned impressive.  I actually emailed him asking if I could take a peek at his code, but he’s planning on putting together a commercial version, so he has to protect it. So I figured, by gum, I’ll do it myself!

Check out websites, YouTube, portfolios, see what other people are doing with MEL.  Get some ideas rolling around.  You’ll soon be envious of these artists ability to bend Maya to their will, I guarantee it.

I still don’t know how Richard Sun managed nine-tenths of his stuff, but I’m faking my way through it fairly well, just trying to replicate some of the functionality of his scripts.  Which leads me neatly into step 2.

STEP TWO: Have a Specific Goal

It may seem a bit odd, but this is probably the single thing that makes most people give up on MEL scripting.  If you don’t have a set of goals in hand, then the learning process can’t be very well directed.  There is a mind-numbingly huge number of things that MEL can do; it would be a complete waste of time to try to learn every command.

Here’s how I recommend setting your goals.  Write out a list of repetitive things that you do in Maya a lot — “move an object pivot to the origin and mirror on the X-axis”, for example.  Also write down a few Maya commands that you use a lot, but don’t use a hotkey or can’t set a hotkey for.  This can be especially helpful for some commands, such as CenterPivot, which can only be executed on one object at a time through the default Maya command in the Modify menu.  These kinds of simple commands are extremely easy to translate into MEL, and you can whip up a script for easy access to these in no time.

After that, the rest is just figuring out what exact simple steps add up to a more complicated goal.  I started with a simple “move an object to another selected object” and ended up with the  more complicated “duplicate and move 12 copies of one object to the bottom-most vertices of another object”.  Every complex MEL script is based on a string of simple commands and a little bit of clever planning.

STEP THREE: Research

There are a plethora of great MEL scripts out there.  Not only can they be extremely useful tools, but you can learn all kinds of tricks by reading other programmers’ codes.  If you haven’t heard of it before, try Creative Crash (formerly Hi End 3D).  They’ve got hundreds of scripts free to download.

Also, try this: Find a good movie with visual effects; Look up the names of some of the Technical Directors; Find their home pages, if they have any; Look for the scripts page.  Most programming TDs will keep a page where they share useful scripts, shaders, or other stuff — free to whomever wants it.  You can learn a huge amount just by looking at these guys’ code.

Use Google.  Use the Maya Help applet — in the “technical documentation” category, there is a list of Maya MEL commands with decent explanations of functionality and snippets of code examples.  Use anyone who’ll help you.  Many of the 3D forums have Maya programming sections where some very experienced and philanthropic souls reside.

The real trick is figuring out exactly what you need in a given situation. At one point, I was trying to figure out how to determine the global positioning of a single vertex in an object.  Turns out what I SHOULD have been searching for was “world space position” instead of “global position”.  Once I figured that out, the xform command query was suggested by a helpful fellow on a closed forum thread from back in 2004.  Google found it, but it needed a little prodding with the right keywords.

STEP FOUR: Research Some More

I can’t emphasize this enough.  You’ll find all kinds of cool stuff.

STEP FIVE: Breathe.

Maya and MEL can be a little finicky. “Maya Hates Me” isn’t just a catchy phrase, it’s a code that I’m forced to live by.  Sometimes, the Script Editor will decide to throw errors your way for seemingly no reason at all.  Sometimes variables will carry over between iterations of the code, and really gum up the works.  Sometimes Maya is adamant that you declare your variables outside of a procedure, while other times it doesn’t really care.  There’s always an explanation behind this, but it can be pretty frustrating.  Take a break.  Restart Maya, or restart your computer.  If the script editor disappears and refuses to come back, just delete your WindowPrefs file in My Documents and restart Maya.  I’ve even had Maya refuse to do something, and it’s working just fine by the time I get back from getting a drink or taking a pee-break.

STEP SIX: Brush up on Your Math

Math sucks.  You know it, I know it, sadistic math teachers know it.  It’s hard, it’s annoying, and it’s utterly pointless.  Until you need it.  Especially geometry and linear algebra, essential for modeling and animating with MEL.  I’m sure a few physics equations couldn’t hurt either.  The good news is that you don’t have to do any of the calculations by hand.  The bad news is that you have to know enough to know when you’ve got it wrong.  Matrices, vector math, all kinds of averages, velocities, areas, all of these mathematical concepts WILL be used for building complex modeling and animating scripts.  It sucks, but here’s the fantastic thing about this: when you’re done, you aren’t getting a grade, you aren’t turning in a worksheet, you’ve made a script that actually does something.  It’s useful, it’s visible, and it’s yours.  I think if math were paired with programming early on, people would be much more willing to learn.

In Conclusion: I hope you weren’t expecting any MEL code in this post.

So yeah.  Scripting can be a very rewarding, very useful thing.  I’ve only really been scripting for a couple of weeks now, and I’m already turning out some really cool stuff that’s saving me time in my modeling workflow.  I’ll be posting more here as I continue to learn!

Advertisements

3 responses

  1. Roger

    hey ED,

    Nice post, Im really enjoying going through your site. Consider your site bookmarked.
    keep the posts coming….

    June 15, 2010 at 4:06 pm

  2. Personally i recommend Pymel. Maya has python bindings for all of their Mel commands, but let us be honest… they suck. The new versions of Maya however include Pymel (http://code.google.com/p/pymel/) turns the ugly python bindings into object oriented code just like you would expect.

    Plus instead of just learning Mel you are learning Python which is applicable in nearly every major Animation or Visual Effects package being released today.

    To get started just go to the python terminal in maya and type:

    from pymel.core import *

    ^^^ There are people who believe that it is better to import to a name space, and they are probably right. just while I am in maya, I like working at the root namespace and I have yet to come across anything that breaks due to importing directly into the root.

    And example of the ease of syntax, that make this really nice:

    mySphere = sphere()
    myCube = polyCube()
    sphere.tx >> mySphere.tx
    #This creates a sphere, and a cube and stores them into names as objects. The “>>” #connects the translation x attribute of the sphere to the cube. Now you have a sphere #and a cube with a direct connection with no confusion or cryptic commands.

    ^^ofcourse you gain the benefit of having all the other pythonic commands, like list comprehensions, honestly better list and tuple managements and others.

    Too long; didn’t read?

    Go Here:
    http://www.luma-pictures.com/tools/pymel/docs/1.0/index.html
    Learn Pymel instead of just mel.

    July 26, 2011 at 11:49 pm

  3. David

    Great post. I have been wanting to learn MEL scripting for a while, but it wasn’t until I started researching my own project that I realized I’m gonna need some help, scripting help that is. Modeling out what I have in mind will take a truck load of time. The good thing is, your Kludge City script helps and cuts some of the time.

    Best damn city generation script I’ve seen. Thanks!

    February 1, 2012 at 4:10 am

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s