Introduction

  • Maya offers several programming language.
    • MEL

    • maya.cmds (Python)

    • PyMEL (Python)

    • OpenMaya (Python)

    • C++

  • Except for C++, all of them can be used for scripting

  • Only C++ and OpenMaya (Python) can be used for Plugins

  • You can use all the Python flavors (i.e. Pymel + OpenMaya) in one python file. Although you might have to wrangle some variables if you want to pass data not native to python (i.e. custom objects)

  • C++ was the oldest (as it is the language Maya is made of).

  • MEL was made available for easy user programming. C++ can’t be used for scripting since it needed to be compiled.

  • maya.cmds was made available as Python was becoming popular. maya.cmds is just a wrapper of MEL commands.

  • PyMEL was initially made as a third party library by Luma Pictures studio and was made available for public. Autodesk eventually picked it up and became a native language. PyMEL makes an object-oriented implementation (i.e. Pythonic) of the Maya API. maya.cmds, since just a wrapper, is still heavily procedural oriented.

  • OpenMaya is heavily based on C++ API, which makes it capable of creating plug-ins. It’s also harder to write since the commands are not as abstracted compared to other Python implementations.

  • Despite having several languages, none of them are deprecated. The most logical language to learn and invest to is Python, as it is available not only to Maya but to all other major DCC.

  • There is not much changes in Maya in the past decade, a blessing and a curse. Scripts you can find even on abandoned forums just works. Makes it easy to pick up any Maya language.

MEL

  • Variables have $ prefix.

  • ++$aa is different from $aa++

  • To invoke the previous MEL Command, in the MEL Command tab press the up arrow key.

  • Multi line comment /*. Single line comment //

  • Name cannot be start with a number

  • For file paths, MEL reads forward slashes and not back lashes.

General Syntax
1$my_Values[] = {1,2,3,4,5,6};
2// You can access individual elements by numbers (starts with zero) (i.e $my_Values[2])
3
4matrix $m[3][5] = <<1,2,3,4,5;6,7,8,9,10;11,12,13,14,15>>
Flow Syntax
 1// If/Else Block
 2
 3if ( $a == $b) {
 4    ...
 5} else if ($a > $b) {
 6    ...
 7} else {
 8    ...
 9}
10
11//Compare $a with the result of whether $b is greater than 10.
12//If true, then $c is used.
13//If false, then $c is subtracted by 10.
14$a = ($b > 10) ? $c : ($c - 10);
15
16//Basic for loop
17int $i;
18for ($i = 10; $i > 0; $i--) {
19    print($i+"...\n");
20}
21print("Blastoff!!!");
22string $arry[3] = {"red","green","blue"};
23
24//Iterate through each k variable in arry
25for ($k in $arry) {
26    ...
27}
28
29// Procedures or Functions
30
31global proc <return type> <name>(<arg list>) {
32    ...
33    return <exp>;
34}
35global proc float squareAndAdd(float $x, float $y) {
36    return $x * $x + $y;
37}
38square(5.0, 2.0);
3927
40
41
42setAttr("mySphere1.translateX",10); // Function syntax
43setAttr mySphere1.translateX 10; // Command syntax
44
45$a = getAttr("mySphere.translateX"); // Function syntax
46$b = `getAttr mySphere.translateY`; // Command syntax
Do something on selected objects
1string $sel[] = `ls -sl`;
2for ($each in $sel){
3    $rescale = $rescale + 1;
4    scale $rescale $rescale $rescale $each;
5}
Execute a MEL file inside a procedure
1global proc savePose(){
2rehash;
3eval ("source savePoseUI_DT.mel");
4}
5savePose;
Creating stairs
1float $stepH = 4;
2float $stepW = 24;
3float $stepD = 8;
4int $numSteps = 6;
5
6for ($i = 0 ; $i < $numSteps ; ++$i) {
7polyCube -h $stepH -w $stepW -d $stepD;
8move -r 0 ($i * $stepH) ($i * $stepD);
9}
By default, MEL commands are in creation mode. Need specify query or edit mode on existing objects
1polySphere -name "Fred"
2polySphere -query -radius
3polySphere -edit -radius 5 "Fred"
General syntax
 1for x in ls( type='transform'):
 2    # object oriented design
 3    print x.longName()
 4
 5    # make and break some connections
 6    x.sx.connect(x.sy)
 7    x.sx.connect(x.sz)
 8
 9x.sx.disconnect()
10
11# add and set a string array attribute with the histor
12x.setAttr('newAt', x.getShape().history(), force=1)

Quirks

  • You can access an object directly through a name. Blessing as it is convenient, but a curse since if you rename the object, the code will break. In this case, you’ll end up using PyMel and storing it as a proper object variable

  • Some weird instance where you need to slice a list from a Python methods.

  • In python for constraints, the affected object are shown first while in MEL it is last.