beginning MEL help
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!
[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:
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:
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:
Any more questions, feel free to ask. Good luck!
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.