FORUMS: list search recent posts

beginning MEL help

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
keithkjeith
beginning MEL help
on Feb 8, 2007 at 5:28:24 am

hey guys, been using maya for a while, but just now venturing into MEL. My question is as such: lets say i make a "thing," object_1. i add an attribute "attribute" to it. I then duplicate it 100 times (just using regular maya dup, no MEL). so now i've got object_1, object_2, etc. how would i go about selecting each of them in turn and giving a random number (lets say between 1 and 2) to "attribute"?

I know all the basic concepts of coding, but the details are escaping me. i know i have to set up a for loop selecting each of them and using setAttr with rand. how do i let for ($i=1; $i<100; $i++) into the numeric portion of "object_1" (basically object_$i)? The other option would be to just select all the objects and do something like...list the objects and put them in a string array and then go through the array...yes?

Any help on this would be great! feel free to get technical, as i've done a bit of JAVA(ish) coding in the past and want to know as much as i can on this!

Thanks!
Keith


Return to posts index

Steve Sayer
Re: beginning MEL help
on Feb 8, 2007 at 6:18:03 pm

[keithkjeith] "The other option would be to just select all the objects and do something like...list the objects and put them in a string array and then go through the array...yes?"

Yes! If what you're doing is a one-time operation, this is probably the simplest way. Select the objects manually, then just run a for-in loop:

string $selectionArray[] = `ls -sl`;
for ($item in $selectionArray)
    setAttr .yourCustomAttribute (rand(1,2)) $item;


The first line lists ('ls') the items you have selected ('-sl'), and stores the results in a string array (the single forward quotes ` ` execute the contained command and report the returned value of that command.

The 'for in' loop is a handy way to perform an operation on a series of items. Note that the variable I called '$item' can be called anything, and you actually don't need to declare it before using it--one of those rare exceptions to the rule.

Finally, the setAttr command will explicitly set an attribute. Don't forget the period in front of the attribute's name, and don't forget the parentheses around the random function: Maya will be expecting a number here, and if it sees the 'r' of 'rand()' first, it will report an error. The parentheses force Maya to evaluate the expression first, and make use of the resulting number.

As for the long way around...

It is possible to execute a command several times, replacing part of it with an incremented number each time. For this you want to use string tools to create your command as a text string, then use the eval command to evaluate the resulting string as a command. Very handy; in many cases there's no way around doing it. Here's an example, which will do the same as above, without needing you to select all the objects first:

for ($i = 1; $i < 10; $i++)
    {
    string $myCommand = ("setAttr object_" + $i + ".myCustomAttribute (rand(1,2))");
    eval $myCommand;
    }


The first iteration will construct the string "setAttr object_1.myCustomAttribute (rand(1,2))", and then evaluate that string as a command. The second iteration will construct the string "setAttr object_2.myCustomAttribute (rand(1,2))", evaluate it, and so on.

If you're confident, you can construct and execute the string in one step as follows:

eval ("setAttr object_" + $i + ".myCustomAttribute (rand(1,2))");.

Any more questions, feel free to ask. Good luck!


Return to posts index

keithkjeith
Re: beginning MEL help
on Feb 8, 2007 at 6:43:52 pm

great, thanks! all in the syntax it seems. the last section seems especially handy, did not know about the eval command. appreciate the assistance, i'm sure i'll have more questions down the line.

-Keith


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]