FORUMS: list search recent posts

"Variable Variable Names"?

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Jason Frasier
"Variable Variable Names"?
on Feb 9, 2012 at 10:48:19 pm

I'm writing an expression to take care of a "chasing lights" effect. Each row of lights has its own surfaceShader, and the expression uses a sine wav to bring the lights' outColor up and down. So far so good.

The problem I'm encountering now is that I want to use a for loop to set each of the surfaceShaders with one expression. The loop works fine, but I have yet to figure out how to use the loop counter to "generate" the surfaceShader name, then feed that into an assignment statement.

Example: let's say I need to assign 1 to outColorR on 5 nodes, named testLight0, testLight1, ..., testLight4. Like this:

for ( $i =0; $i < 5, $i++) {

[?????].outColorR = 1;
}

I've tried concatening $i to the end of a "testLight" string with the + operator, and no luck. Any ideas?
I guess what I really need to know is how to turn a string into a reference to a node.

Jason Frasier


Return to posts index

Steve Sayer
Re: "Variable Variable Names"?
on Feb 10, 2012 at 4:03:49 pm

First, check your syntax: you've got a comma after the '$i < 5' statement, instead of a semicolon.

Next, the problem here is that you can't just set an attribute directly with the '=' operator in an expression. You need to use setAttr:

for ($i = 0; $i < 5, $i++) {
setAttr ("testLight" + $i + ".outColorR") 1;
}


For more complex cases where you need to construct a command from a string, look up the 'eval' command.


Return to posts index

Jason Frasier
Re: "Variable Variable Names"?
on Feb 10, 2012 at 4:38:45 pm

Eval looks like just what I need.

Sorry about the syntax mistake, I jotted that down in a hurry just as an example.

(By the way, you can set attributes directly, but only in expressions, not in basic MEL scripts. I don't know why that is, but that's how the manual explains it and it definitely works.)


Return to posts index


Steve Sayer
Re: "Variable Variable Names"?
on Feb 10, 2012 at 6:47:27 pm

Oh, of course--silly of me.

I had the 'setAttr' vs. '=' thing backwards, because I'd tried a version of your code where I constructed the string on the left side of the '='. That doesn't work, but a straightforward command on a fixed name does. So yeah, in this case the eval command should do the trick.

Sorry about that.


Return to posts index

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