MEL


Misc.


Renaming Tabs in Script Editor

Makes life that much better.
Commands --> Rename Tabs


How to add an element to an array

ex:  $myArray[`size $myArray`] = $objString;

TIPS

when you are assigning variables, check the 'Return Value' in Maya help to see what your command is expected to return. 

Get the size of an array?


How to select a command/option from a list of choices in GUI at runtime?

For this, use the 'optionMenuGrp' command..

in your main procedure, create the commands.

Write a separate procedure that queries the optionMenuGrp, and call it from within the main procedure:

def getMySelectedOption (optionMenuGrp):
    getSel = mc.optionMenuGrp(optionMenuName, q=1, sl=1)
    return getSel

def getMyCommand (optionMenuName, command1, command2):
    getSel = getMySelectedOption (optionMenuName)
    #print 'the option menu is ' + optionMenuName
    if getSel == 1: 
        print 'the option selected is command1'
        eval(command1)  
    elif getSel == 2: 
        print 'the option selected is command2'
        eval(command2)

def mainProc():

    optionMenuName = mc.optionMenuGrp (label='blah')

    command1 = 'does something'
    command2 = 'does something even cooler'

    myCommand = getMyCommand (optionMenuName, command1, command2)

    mc.button(label = 'Do Something Based On What's Selected in the Option Menu', c = myCommand)
    

Surely there's a better way, but this seems to work for now..

Namespaces


When people ask if I can show them how to properly "script" for Maya... sure, namespaces 101.

When I first started working my current position they had 200+ scripts in 15+ directories and about 100+ in a root directory. You want to talk about ugly?

You know how many times a day I was doing whatIs just to find the collisions?

Namespaces are simply good coding standards =p
(Alexander Morano)

What are Namespaces?

Make an Object Unselectable in the Viewport

How to do this while maintaining override color?  (ie., not using template/reference).  

Fun and Useful Mel Commands

that you may not be using yet.

connectControl - This command attaches a UI widget, specified as the first argument, to one or more dependency node attributes. The attributes/nodes don't have to exist yet, they will get looked up as needed.

headsUpMessage - prints a message on screen that is erased w/screen redraw

createNode script -n "welcomeMessage";
setAttr ".b" -type "string" "headsUpMessage \"Look at this neat headUp message!\";";
setAttr ".st" 1;

hudButton - makes a button in heads up display

sets -addElement [name of set] [thing you want in set] ;

substituteGeometry - This command can be used to replace the geometry which is already connected to deformers with a new geometry. The weights on the old geometry will be retargeted to the new geometry.

toggle - toggles things, like local rotation axis

polyToCurve - takes your selected edges and makes them into a nice curve (thanks Paul L.)


Operators

Assignment (=)

The assignment operator assigns a value to a variable.
$a = 5;

The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from right to left, and never the other way.

 

Arithmetic operators ( +, -, *, /, % )

The five arithmetical operations supported by the C++ language are:

+addition
-subtraction
*multiplication
/division
%modulo

Operations of addition, subtraction, multiplication and division literally correspond with their respective mathematical operators. The only one that you might not be so used to see may be modulo; whose operator is the percentage sign ( %). Modulo is the operation that gives the remainder of a division of two values. For example, if we write:

a = 11 % 3;

the variable a will contain the value 2, since 2 is the remainder from dividing 11 between 3.

Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:

expressionis equivalent to
value += increase;value = value + increase;
a -= 5;a = a - 5;
a /= b;a = a / b;
price *= units + 1;price = price * (units + 1);

and the same for all other operators. For example:

// compound assignment operators

#include <iostream>
using namespace std;

int main ()
{
  int a, b=3;
  a = b;
  a+=2;             // equivalent to a=a+2
  cout << a;
  return 0;
}
5

Increase and decrease (++, --)

Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:

c++;
c+=1;
c=c+1;

are all equivalent in its functionality: the three of them increase by one the value of c.

In the early C compilers, the three previous expressions probably produced different executable code depending on which one was used. Nowadays, this type of code optimization is generally done automatically by the compiler, thus the three expressions should produce exactly the same executable code.

A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluated as a value in an outer expression they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated and therefore the increased value is considered in the outer expression; in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the outer expression. Notice the difference:

Example 1Example 2
B=3;
A=++B;
// A contains 4, B contains 4
B=3;
A=B++;
// A contains 3, B contains 4

In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.

 

==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
 

Logical operators ( !, &&, || )

The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:

!(5 == 5)    // evaluates to false because the expression at its right (5 == 5) is true. 
!(6 <= 4)    // evaluates to true because (6 <= 4) would be false. 
!true        // evaluates to false
!false       // evaluates to true. 

The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a && b:

&& OPERATOR

aba && b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b:

|| OPERATOR

aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

For example:

( (5 == 5) && (3 > 6) )  // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) )  // evaluates to true ( true || false ).

Mathematical Expression Operators

great page here

abs, ceil, floor, clamp, min, max, etc.

Resources

Comments