Thursday, August 20, 2009

MEL Notes

In this section, I'm going to keep some general notes on MEL and how it works. These are things I slowly discovered through trial and error, or shortcuts & explanations I picked up from forums. Hopefully these will be of some use.

∞ Operators =========================
$x++ // adds 1 to the value of $x, and makes $x that result.
$x-- // subtracts 1 from the value of $x, and makes $x that result.
$x += 5 // adds 5 to the value of $x, and makes $x that result.
$x -= 3 // subtracts 3 from the value of $x, and makes $x that result.

Example: If $x = 5, then $x -= 3, will make $x = 2.

∞ The # Key =========================
*note: the following came from a post on the MEL forums at, and relate to an example of controlling window visibility. But, one function of "#" is described here:

============ Window Visibility ==================

window -title "Vis Test" -s 0 -w 200 -h 200 VisTestWin;

rowLayout -nc 2;
checkBox -label "click me" -vis 1 -cc "floatFieldGrp -e -vis #1 -v 1 scaleVal" myCheckBox;
floatFieldGrp -vis 0 scaleVal;
setParent ..;
showWindow VisTestWin;

Note: #1 reverts the value back to its first value.

yeah, the # feature of control commands is a kind of "hidden" feature, i.e: it's definitely in the docs somewhere but good luck finding it.. smile.gif

the #1 represents the FIRST VALUE of the control, and likewise #2, #3 and #4 represent the 2nd, 3rd, 4th, etc..
it doesn't matter what type of control it is, as long as it has a value or values, so you can do stuff like this:

intFieldGrp -nf 3 -cc "optionVar -iv pref1 #1;optionVar -iv pref2 #2;optionVar -iv pref2 #3";

so that whenever you change any of the 3 int field values, it stores all three in respective optionVars...

not *all* controls support this feature (e.g. optionMenu) but most of the useful ones do (bool, int, float and slider controls)

it's a very useful shortcut as it allows you to do away with lots of pointless global procs and keeps your UI code more self-contained (which I personally prefer in most cases)



It can also be used as below (also from HighEnd3D):

string $window = `window`;
floatField -minValue -10 -maxValue 10 -value 0
-dragCommand "theFloatFieldChangeCommand #1"
-changeCommand "theFloatFieldChangeCommand #1";
showWindow $window;

global proc theFloatFieldChangeCommand(float $input)
print("$input: " + $input + "\n");

you could do
floatField -changeCommand "theFloatFieldChangeCommand (`floatField -q -v floatFieldName`)" floatFieldName;
as well but #1 is real good for floats ints and checks!
Works with string-fields as well until you enter a "


So, here, it is used to pass the value from the floatField to the necessary input for the, "theFloatFieldChangeCommand" command. Its a pretty useful little tool.


∞ Regular Expressions=================
∞ A good RegEx link:

∞ Regular expressions are used to define search strings. These can be most helpful
with MEL's match, gmatch, strcmp and other string based commands.

∞The basic building blocks of a regular expression are as follows:

. Matches any single character
* Match zero or more occurances of the preceeding expression
+ Match one or more occurances of the preceeding expression
^ Matches (anchors) the expression to the start of a line
$ Matches (anchors) the expression to the end of a line
\ escape character. Use this in front of a special character (e.g. '*') ............ when you wish to match the actual character. Note that ............ MEL strings resolve control characters that use '\' when ............ they are first parsed. So, to use a '\' character in an ............ expression, you must escape it (e.g. "\\") so that it is ............ passed through to match.
[...] Matches any one of the enclosed characters. A pair of characters separated by - matches any character lexically between the pair, inclusive. If the first character following the opening "[ " is a "^" any character not enclosed is matched. A - can be included in the character set by putting it as the first or last character.
(...) Used to group part of an expression together.

∞ 'ls -sl'============================
This will error out:

string $ctrlName = `ls -sl`;
$ctrlName = anyOtherString;

Maya will make 'ls -sl' a string array, even though I declared it above as a 'string' because 'ls -sl' ALWAYS returns a string array, even if its the empty string "".

So, the above code is trying to make a string array a string which is a no-no.

∞ When Making Windows ==============
There is a setting in the Settings and preferences menu that tells Maya to remember the size and placement of all windows you open in the interface. Look under Windows > Settings and Preferences > Preferences and on the top tab, the 'Interface' tab there should be a checkBox labeled 'Windows: [ ] Remember Size and Position.' If this is checked, then once the window is created, Maya remembers what size it was, its contents (mostly) and where it was in the interface. So, any changes you may make in a MEL script, even if you source it again, will not show up if they affect the size of the window. To see your code actually updated, uncheck this box until you're done creating the window. Or, you can use a specific windowPref command in the MEL to tell Maya what to remember about the window.


No comments:

Post a Comment