Nesting multiple command and procedure calls (mel)

There are really three core ways to call a command or procedure in mel.  Most scripts use a combination of all three however of the two that will allow you to store the result there’s only one which supports nesting (embedded command or procedure calls in the arguments of another command/procedure).


Method #1 – flat call:

command -option;

Method #2 – quoted call:

string $result[] = `command -option`;

Method #3 – bracketed call:

string $result[] = command("-option");


The most commonly used method to return the result is #2

int $curSel = `ls -selection`;

However if you want to nest multiple command executions this method can be problematic.


Lets say you you want to print the size of the current selection.

Typically you’d need to get the current selection and assign it to a variable then print the result from a ‘size’ query of that array. 

string $curSel = `ls -selection`;

int $selSize = `size $curSel`;

print $selSize;

However through the use of brackets you are able to nest these operations into one clean line.

print (size(ls("-selection")));


The main difference being the arguments that are being passed to the command have to be enclosed in brackets and each flag option (-selection) must be enclosed in quotes ("-selection").


int $connectSuccess = `connectAttr -f "locator1.translateX" "locator2.translateX"`;


int $connectSuccess = connectAttr("-f", "locator1.translateX", "locator2.translateX");


Note: The same restrictions on argument order apply to both methods.


There is no limit to the number of nested commands in one call.


Some people might argue that I’ve ignored the ‘eval’ method.  However the ‘eval’ group of commands are commands themselves – commands with a string input that gets executed. How that string is executed is subject to same three methods described here.


2 thoughts on “Nesting multiple command and procedure calls (mel)”

  1. Wow, I've been using mel for some time and feel quite confident about it, but I have wondered about this for some time and tried googling it before without success so thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *