Archive for the ‘MEL’ Category

Check for Existence of a Variable in MEL

Thursday, June 16th, 2011

Error-catching in MEL with the catch command won’t catch a “variable does not exist” error, so use whatIs instead:

if (`whatIs "$test"` == "Unknown") {
print "yes";
} else {
print "no";

There ya go.

Importing Expressions in Maya

Sunday, April 24th, 2011

Writing expressions in Maya is a huge pain. The error messages are vague, there’s no stack trace, and the editor is a text entry field with no line numbers, syntax highlighting, or line wraps. And Maya forbid you try to write a script with a dynamically-generated expression in it — then everything has to be a giant encoded string. And when you try to run it, it squirts venom at you from its eyeballs.

However, it’s possible to write and edit your expression in the text editor of your choice, save it to a file, and import that file into your script at runtime. This is much, much nicer, and comes with less venom.

There’s a way using fread to put the entire contents of a file into a string at once, but I had trouble with it — this way, reading a line at a time, seems to work better. [Based on this tutorial from Jay Grenier’s Script Swell.]

Code follows:

selectTrigger v01

Thursday, December 23rd, 2010

Here’s a Maya script I wrote to trigger the selection of one object with the selection of another, inspired by Hamish McKenzie‘s Trigger UI, as seen in Andrew Silke‘s venerable Generi rig.

The script makes a scriptNode, which creates and tracks a scriptJob. The scriptJob checks selected objects each time something is selected, and if the control object is first in the selection list, the target objects will be selected as well. Since scriptJobs only last until the scene is closed, the scriptNode also re-creates the scriptJob when the scene is reopened.

I’d like to draw your particular attention to line 34, wherein I escape a backslash six times.

Download script here: selectTrigger_v01.mel

Code follows:

Mondo shrub

Wednesday, April 29th, 2009

10000 pyramids in 26 minutes. This is much better.

I found a bit of optimization I missed; now I’ve got one more list, which marks the position of each pyramid, obviating a calculation step. Now it can do 5000 in 11 minutes, and 1000 in a minute-twenty. The collision detection is still only mostly polite, but the sheer speed of this code boggles the mind.

Mind-boggling follows:

Smooth shrub

Tuesday, April 28th, 2009

Behold a 5000-pyramid smoothly-growing shrub! That’s it! Keep beholding!

I’ve been working with a lot of largish loops lately, iterating a series of nested processes over lists of ever-increasing size, and bumping into some soft walls.

However! Whether through inspired genius or cargo-cult pigeon-pecking, I have arrived at a superior method.

First, I will bore you with the setup, followed by code and cake.

Polite shrub

Wednesday, April 22nd, 2009

Back to MEL, because this would have taken for-freakin-ever to run in Pymel. This is 500 pyramids.

Figured out the strategic flaw in my collision-detection. I need two lists: one of objects available for growing from, the other with everything in it, for collision-checking.

…that didn’t have anything to do with the code, it was a personal revelation. Although now I think about it, it applies to the code too. There’s still the occasional sneaky intersection, but they’re much rarer now.

Code follows:

PyMEL Python Module for Maya

Tuesday, March 31st, 2009

If Maya is the giant killer robot, the Maya interface is the control panel in the underground bunker, miles from the action. The interface is a limited set of tools, but when you push the buttons on the panel, mushroom clouds form on the horizon, and for most people that’s good enough.

If you really want to get your hands dirty with Maya, and feel the rush of punching through buildings yourself, you’re going to have to learn to code. Maya gives you a few options for getting that buzz, but the most promising thing I’ve found so far is a third-party open-source project called PyMEL. But first, a strained metaphor for my artist homeboys.

The Ghosts in the Machine

A mushroom cloud

If you use Maya’s interface for very long, you start to notice relationships between the buttons you push and the motion of the gears and levers visible through the portholes in the control panel. Eventually you might be brave enough to start pulling pieces of the panel off to get at the guts. Inside you’d find a Rube Goldberg device of gears and rubber bands, maintained by dancing Keebler elves. This is MEL, the Maya Embedded Language, a scripting language with which the whole Maya interface is built.


Semi-polite worm

Sunday, February 15th, 2009

This is a first stab at a worm which doesn’t self-intersect. This has led me into the valley of collision detection, which turns out to be deep, dark, and cluttered with the bones of many programmers wiser than I.

So far the code prevents any pyramid’s points from landing in the volume of any other pyramids, and starts another branch if the main branch runs into a blind alley (which happens at about 12 secs in the above video).

The resulting worm is semi-polite — it exhibits fewer blatant intersections and more coiling knots, but the loopholes in the code allow very sneaky intersections, including pyramids sharing an edge and overlapping edges. Tackling that will be the next step; hopefully it won’t triple the length of the code again.

MEL code follows:

Sloppy worm knot

Tuesday, February 10th, 2009

Here’s a knot of 20 worms of 500 segments each, branching from the same seed segment. The code below has smaller, child-proof default values.

MEL code follows:

50,000 Tetrahedrons

Monday, February 9th, 2009

After some tweaking (including getting re-aquainted with the dot product) the generative code is faster and the geo is 25% lighter… in fact they’re not really tetrahedrons anymore. Actually, each of them is as much as it ever was, but more aren’t as many as they used to be.

Using that logic, I can reliably get up to about 50,000 iterations before Maya starts getting cranky. It only takes a couple of minutes, and here’s what I get:

MEL code follows:

Sloppy twisty worm

Monday, February 9th, 2009

In this worm, the pieces are all chained together and each piece is rotated 180 degrees on its base.

MEL code follows:

Sloppy pyramid worm

Saturday, February 7th, 2009

A growing pyramid worm, with faces and corners aligned, mostly. It doesn’t line up exactly, and I’m not sure if that’s because of the shape of the pyramid or my dodgy code.

It’s easy to find out how far away something is, but how do you tell left from right? Things I thought I knew, until I tried to explain them. I may have to resort to particle physics.

MEL code follows…

Aligned faces pyramid worm

Friday, February 6th, 2009

More random walkies. In this one, the faces are aligned, but the corners are not.

Turns out there’s no shortcut to find the center of a face via MEL. The interface does it all the time — there’s a dot in the middle of each face, and lots of snap tools, so I know it’s buried in there somewhere… but I couldn’t find it. So I took a procedure from Seth Hall and cleaned it up.

MEL code follows:

Random walk pyramids

Thursday, February 5th, 2009

MEL code follows:

Random walk keyframed blocks in MEL

Wednesday, February 4th, 2009

I’m starting a project, and I thought I’d do it in the window of my little shop, here on this sidestreet of the Internet. That way everyone wandering by can peek in, gaze upon the array of parts strewn all over my workbench, and wonder what I’m up to.

MEL code follows: