PDA

View Full Version : Control Attribute of a List of Objects


edwardE
06-24-2008, 06:23 PM
Hey there; I've got some basic experience with scripting languages and I've set up simple relationships between attributes and objects in Maya, but I need a little help with this one:

I want to control an attribute of multiple objects in closely the same way, but independently of each other, and with some variation.

For this certain instance, I have a tree which has groups of leaves on it, and I want these groups to sway back and fourth using a sine on their rotation. Up until now I've been writing the expressions for each object, coming up with increasingly lengthy lists that are all basically the same lines, but a little variated.

In generic scripting terms (bear with me, I'm no expert) I want it do do something like this:

First, I want to define an key array as object -> rate for each element, where object is the object name, and rate is a value I can assign to each object so that I can evaluate each object in a slightly different manner so things don't look so mechanical. Something like this (reminder: I'm writing this only to illustrate my concept, I might be combining languages/syntax here)

$leaves[] = {
leafName1 -> 5,
leafName2 -> 3,
leafName3 -> 6
};

Then, I want to evaluate them as:

foreach ( $leaves as $leaf -> $rate ) {
$leaf.rotateX = sin( time ) * $rate;
}

That way, the only listing I need to do is in my object array, and things aren't messy and repetitive lines for each object. I just don't know how to implement this as MEL.

Knowing this would help speed up writing expressions and keeping them clean a great deal. Many thanks in advance for reading this; I hope it all made sense!

Norb
06-24-2008, 06:36 PM
changing attributes on an array of objects isn't terribly difficult, here's a template for you to follow:


for ($Item in $ItemList)
{
setAttr ($Item+ ".rotateX") $DesiredValue;
}

edwardE
06-24-2008, 07:17 PM
Thanks, man - that's just what I needed to get moving again. (:

davegreenwood
06-24-2008, 07:40 PM
Hi, not sure if you've just not used it in your example, but don't forget the rand function to give you a random number eg.

for ($Item in $ItemList)
{
setAttr ($Item+ ".rotateX") rand(-45,45);
}


Dave.

CGTalk Moderation
06-24-2008, 07:40 PM
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.