PDA

View Full Version : "setAttr " how is right form ?


ducvemt
03-20-2009, 01:59 PM
hi every one!
here is my code
the first , i created one cube named pCube1.
After that , i got "ctrl + shift + D " to clone it to 10 cube , pcube1 , pCube2, pCube3 ....
After that , i wrote some codes. it here.

int $i;

$i = frame % 5;

if ($i == 0 )

{

int $j;

$j=frame/5+1;

setAttr pCube'$j'.translateY 2;

}


You see , i wanna make pCube translateY automatic but alway error with line

setAttr pCube'$j'.translateY 2;

i think " pCube'$j' " is wrong! but i dont know how to get it right?
help me! how do I must write ?

Keilun
03-20-2009, 05:03 PM
Everything is a string, so you must treat it like that. So build your string then pass that in as your parameter.

Eg.


int $i;
$i = frame % 5;
if ($i == 0 )
{
int $j;
$j=frame/5+1;
string $obj = "pCube" + $j;
string $attr = $obj + ".translateY"
setAttr $attr 2;
}

ducvemt
03-20-2009, 06:24 PM
ok! that is right!
thank you so much!

ewerybody
03-21-2009, 01:04 PM
might work this way as well. didn't test.
if ((frame % 5) == 0 )
setAttr ("pCube" + (frame/5+1) + ".translateY") 2;


but this is expression code right?
so do you need to use setAttr at all?

if ((frame % 5) == 0 )
("pCube" + (frame/5+1) + ".translateY") = 2;


nah I don't know. too lazy to check now. But you know what I mean. In expressions yu can write like:
pCube1.tx = pCube3.rz * 4;
no problem...

Addendum@ vanviet:

to prevent mistakes like that look at the inputs of the command. You see: setAttr awaits 2 things:
1. The attribute to set. Its a string so like said, you need to pass one single string. You can either build it together in another variable (like shown) or build it up in brackets (like I did^) so the enclosed stuff is one string again.
2. The value.

CGTalk Moderation
03-21-2009, 01:04 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.