PDA

View Full Version : Having trouble adding/setting attributes to successive objects


RoleXXX
06-26-2007, 08:27 PM
I'm writing a script where I need to create balls with the same attributes but with random variations of those attributes between each ball. i.e - Velocity, but different for each ball.

Right now I keep getting an error everytime I try to apply to successive balls (the user can press a button in my script and create as many balls as they want). It keeps saying "Cannot find attribute in object" or something very similar during the 'setAttr' part of my code. But as far as I know what I've written below should work. I'm not sure why it doesn't. Any help much appreciate.


//////////////////
// Create Ball //
////////////////
global proc CreateBall()
{
//Create Ball and turn into "active rigid body"
int $randLocationX = `rand -5 5`;
int $randLocationY = `rand -5 5`;
int $randLocationZ = `rand -5 5`;

float $randR = `rand 0.01 1`;
float $randG = `rand 0.01 1`;
float $randB = `rand 0.01 1`;

float $randScale =`rand 1 3`;
string $ball = "myBall";
int $counter = 0;
string $ballname = $ball+$counter;
polySphere -n $ballname -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1;

move $randLocationX $randLocationY $randLocationZ; //Randomize Position
polyColorPerVertex -rgb $randR $randG $randB -cdo; //Randomize Color
scale $randScale $randScale $randScale; //Randomize Size
addAttr -ln "VectorX" $ballname;
addAttr -ln "VectorY" $ballname;
addAttr -ln "VectorZ" $ballname;

addAttr -ln "Velocity" $ballname;

addAttr -ln "Drag" $ballname;

addAttr -ln "Gravity" $ballname;
float $VectorX = `floatField -q -value VectorXfield`;
float $VectorY = `floatField -q -value VectorYfield`;
float $VectorZ = `floatField -q -value VectorZfield`;
float $VelocityBall = `floatField -q -value VelocityField`;
float $DragBall = `floatField -q -value DragField`;
float $GravBall = `floatField -q -value GravField`;
print $VectorX;
print $VectorY;
print $VectorZ;

string $objs[] = `ls -sl`;
for($obj in objs)
{
setAttr $obj.VectorX $VectorX;
setAttr $obj.VectorY $VectorY;
setAttr $obj.VectorZ $VectorZ;
setAttr $obj.Velocity $VelocityBall;
setAttr $obj.Drag $DragBall;
setAttr $obj.Gravity $GravBall;
}
$counter++;
}

Robken
06-27-2007, 10:37 AM
for($obj in objs)
{
setAttr $obj.VectorX $VectorX;
setAttr $obj.VectorY $VectorY;
setAttr $obj.VectorZ $VectorZ;
setAttr $obj.Velocity $VelocityBall;
setAttr $obj.Drag $DragBall;
setAttr $obj.Gravity $GravBall;
}

should be

for($obj in objs)
{
setAttr ($obj + ".VectorX") $VectorX;
setAttr ($obj + ".VectorY") $VectorY;
setAttr ($obj + ".VectorZ") $VectorZ;
setAttr ($obj + ".Velocity") $VelocityBall;
setAttr ($obj + ".Drag") $DragBall;
setAttr ($obj + ".Gravity") $GravBall;
}

CGTalk Moderation
06-27-2007, 10:37 AM
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.