View Full Version : Is it possible to execute nested commands?

11 November 2011, 01:56 AM
Like this for instance?
string $jnt[] = `ls -long `joint` `;
then I'd know $jnt[0] is my joint

instead of writing this

joint -n "jnt_0#";
string $sel[] = `ls -long -sl`;
string $jnt = $sel[0];

Thanks and god night!

dah. Once I figured out to call it nested commands when writing this post I found it =P

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

On second thought, I don't fully grasp it though.

How would you add another attribute in there?
for instance:
print (size(ls("-selection -long")));

Obviously this throws an error. :hmm:

11 November 2011, 05:19 AM
An individual MEL command (in your example, 'ls') that has to have parameters to be processed needs to be bounded by back-ticks (the key left of the digit 1) in order to all be interpreted as one command.

Here is a working version of what you were trying to do:
print (size(`ls -selection -long`));
Note that instead of parentheses, the whole of ls -selection -long is grouped together between back-ticks, so the interpreter knows to take all that and process it as one expression, feed that to size() and then feed the result of that to print().

<* Wes *>

11 November 2011, 06:51 AM
Use python, it makes more sense, and has infinite more features. If you'll miss mel you could always call it using the mel package inside python.

11 November 2011, 12:56 PM
functions in MEL can be (and still often are) called using the more traditional coding style:
string $array[] = ls("-selection", "-long", "-r", 1);
so to make your example slightly more pythonic and uniform (forego the `backticks` completely):
print(size(ls("-selection","-long")));gets a little harsh on the eyes though...

11 November 2011, 07:17 PM
Thanks alot guys!
Yeah, I have big plans to start using python instead. Or addition and delving into plugins scripting.
I just tried some basic, but since I'm still so far fluent with mel, jumping to python without learning the basics of object oriented programming seems the wrong way to go? like using python to write mel? Sure you'd have easier to learn the syntax, but I got the hang of that so in due time I will make the big leap over to python. Waiting for a copy of the new maya python book as of writing this.

Thanks guys!

11 November 2011, 07:57 PM
Listen, you don't have to use python OOP, nor its other functionalities. Each mel line correspond to each python line, and if you do a little exercise and write your code in python, you'll see there's not much difference and it makes much more sense. You can use python's hints or a one hour crash course:

No offense, but you doesn't sound like a mel expert, so the transition shouldn't be that hard. Just learn the new stuff in python instead of mel.

11 November 2011, 08:26 PM
I just haven't done things this way :O and have a hard time understanding how to connect it properly.

MEL in itself is not that hard. Seems I know enough to move to the next step, but stuff like this that seems to be very basic things you learn as a programmer is not something I've ever come across before.

string $sel[] = ls ("-sl","-long") polyCube ("-w", 100, "-h", 100, "-d", 100);

Was messing with parentheses, but can't seem to get it right =/

Thanks for being there!

11 November 2011, 11:17 PM
I'm not sure what you are trying to do in that last code example, but this nesting is very easy to understand and get a grip on. the clue is in the name "nesting", Your last example has neither command inside the parameter braces/parenthesis of the other, so there isn't any nesting happening...
Just remember that every function has it's own parameters and ALL it's parameters have to go inside it's own SINGLE set of parenthesis:
function(ALL, params, go, inside, these, braces) // this is the basic principle, ONE command, ONE set of braces
// examples:
print("some text")
ls("-sl", "-l")
hideAllLights() // this one HAS NO parameters, but it still has it's own pair of braces.
Notice how there is always one command, and only one pair of braces, one right at the start or it's own params, one right at the end of it's parameter list. If it helps, don't put a space between the function and the open brace. You never have more (or less) than one set of braces per command. Of course there may be other braces anywhere inside a commands parameters, but these are part of the parameters, and not related to the functions braces e.g.:
ls("-type", "nurbsCurve", "-tail", 3, ("edgeAlign"+$i) );
When you nest these commands inside each other, all you are doing is using a command(braces) combo as one parameter inside another command. lets colour code them to make it easier to see:
string $allMeshes[] = ls("-type","mesh");
string $parentTransforms[] = listRelatives("-parent", $allMeshes);
// and in nested format:
string $parentTransforms[] = listRelatives("-parent", ls("-type","mesh") )
If it helps, simply write out each individual command on it's own line, then copy/paste it wholesale into the appropriate place in the other command. e.g.
ls("-selection", "-long") // get the selection
size( input ) // get the size of the selection
print( input ) // print it all out

size( ls("-selection", "-long") ) // step 1
print( size( ls("-selection", "-long") ) ) // step 2
I hope that makes sense and I've not just complicated it!! :D

11 November 2011, 09:52 AM
Yes, make total sense!

string $sel[] = ls("-sl", "-long", (polyCube("-w", "100", "-h", "100", "-d", "100", "-n", "sphere") ) )

string $sphere = $sel[0];

Thanks alot! Now I get it

CGTalk Moderation
11 November 2011, 09:52 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.