PDA

View Full Version : how to make unique values for variables and expression?


cgbeige
12-15-2010, 08:27 PM
I have a problem with this script that creates an expression. If I use it more than once, the separate objects connect to the same expression, which is a problem:


string $mySelection[] = `ls -sl`;
string $shapes[] = `listRelatives -s $mySelection[0]`;
string $sGroup[] = `listSets -type 1 -o $shapes[0]`;
string $dNode[] = `listConnections -d 0 -s 1 ($sGroup[0] + ".displacementShader")`;
string $file[] = `listConnections -d 0 -s 1 ($dNode[0] + ".displacement")`;
string $dispMap = $file[0];
float $gainValue = `getAttr ($dispMap + ".alphaGain")`;
float $alphaOffset = `getAttr ($dispMap + ".alphaOffset")`;

expression -string ($dispMap + ".alphaGain = $gainValue * " + $mySelection[0] + ".scaleX;\n" + $dispMap + ".alphaOffset = $alphaOffset * " + $mySelection[0] + ".scaleX;") -o $mySelection[0] -ae 1 -uc all;

fezz
12-15-2010, 09:58 PM
Give the expression a unique name with the "-n" flag:

expression -string ($dispMap + ".alphaGain = $gainValue * " + $mySelection[0] + ".scaleX;\n" + $dispMap + ".alphaOffset = $alphaOffset * " + $mySelection[0] + ".scaleX;") -n (mySelection[0]+"_exp") -o $mySelection[0] -ae 1 -uc all;

cgbeige
12-15-2010, 10:12 PM
throws an error:

// Error: Invalid use of Maya object "mySelection[0]". //

nevermind - caught it. was missing the $. works great. thanks.

fezz
12-15-2010, 10:17 PM
that should have read:

-n ($mySelection[0]+"_exp")

cgbeige
12-15-2010, 11:53 PM
hmm - it didn't seem to fix the problem. The problem is that the $gainValue and $alphaOffset variables are just tied to different expressions. How do you make a variable variable? :D

fezz
12-16-2010, 12:07 AM
Ah, I see the problem. You have to declare the variables $gainValue & $alphaOffset inside each expression, or else format the expression to take the values you've just defined:

expression -string ($dispMap + ".alphaGain = "+$gainValue+" * " + $mySelection[0] + ".scaleX;\n" + $dispMap + ".alphaOffset = "+$alphaOffset+" * " + $mySelection[0] + ".scaleX;") -o $mySelection[0] -ae 1 -uc all;

cgbeige
12-16-2010, 01:16 AM
ah - I didn't know you could do that. Nice. thanks again.

pixelranger
12-21-2010, 11:30 PM
You can also try this:

//create a sphere
string $sphere[] = `polySphere`;

//create an empty expression node
string $expr = `createNode expression -n "testExpression"`;

//define the actual expression. ".O[0]" is a temporary value that
//stands for "output[0]" (the first output target)
// the expression will just be "time".
setAttr ($expr + ".e") -type "string" ".O[0] = time;" ;

//connect it to the sphere's translateX attribute
connectAttr ($expr + ".out[0]") ($sphere[0] + ".tx") ;

So for multiple attributes in the same expression:

setAttr ($expr + ".e") -type "string" ".O[0] = time;\n.O[1] += 1;\n.O[2] = rand(2,5);";
connectAttr ($expr + ".out[0]") ($sphere[0] + ".tx ");
connectAttr ($expr + ".out[1]") ($sphere[0] + ".ty ");
connectAttr ($expr + ".out[2]") ($sphere[0] + ".tz ");

CGTalk Moderation
12-21-2010, 11:30 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.