PDA

View Full Version : Dynamic attribute


qkuotidian
11-10-2010, 08:37 AM
Hello,
I want to add an attribute (sphereCount), which will control the number of created spheres.
Does anyone know how to make this attr more dynamic? I mean change it value and see changes immediately, and not executing the script again.
Thanks an advance for help.

sphere;
addAttr -longName sphereCount -attributeType long;
setAttr nurbsSphere1.sphereCount 10;
int $sphereCount = `getAttr nurbsSphere1.sphereCount`;

float $distance=2.0;
int $counter=0;

while ($counter <= $sphereCount)
{
sphere;
move 0 0 ($distance*$counter);
$counter++;
}

mduvekot
11-10-2010, 03:29 PM
use a scriptJob to execute a proc when the attribute changes as in

scriptJob -ac nurbsSphere1.sphereCount myProc;

setup the proc to delete and recreate all the other spheres.

like this:

global proc string[] myProc (){
global string $spheres[];
delete $spheres;
$spheres = {};
int $sphereCount = `getAttr nurbsSphere1.sphereCount`;
float $distance=2.0;
int $counter=0;
while ($counter <= $sphereCount)
{
string $sph[] = `sphere`;
$spheres = stringArrayCatenate($spheres, $sph);
move 0 0 ($distance*$counter);
$counter++;
}
return $spheres;
}

scriptJob -ac nurbsSphere1.sphereCount myProc;

dastcher
11-10-2010, 03:43 PM
You can try and add it as an expression. You just need a dummy attribute to ensure that it runs.
Add a control object, add the sphereCount and dummy attributes to it and then the expression.

Here's a "working" expression that I tested quickly.

int $sphereCount = pCube1.sphereCount;
pCube1.dummy = $sphereCount;
float $distance=2.0;
int $counter=0;
while ($counter <= $sphereCount)
{
sphere;
move 0 0 ($distance*$counter);
$counter++;
};


Don't drag the attribute with this expression though, otherwise your gonna get ALOT of spheres.

Your gonna have to go a lot more complex though if you want to delete and add spheres properly. You could try and use the objExists command to check whether a spheres exists and go from there.

qkuotidian
11-10-2010, 05:07 PM
Mduvekot, thanks for reply.
My goal is to have a slider-type attribute, and in your script (of course it does what is needed) I have to type new sphereCount each time again. I want it to act sketchy, because I donít know sphereCount in advance as well as how to put it in script.

qkuotidian
11-10-2010, 08:42 PM
Mduvekot, thanks for reply.
Is it possible to make sphereCount attribute behave like slider? I want this attribute behave more sketchy, because I donít know in advance exact number of objects to be created.

mduvekot
11-10-2010, 10:54 PM
Sure.


string $sph[] = `sphere`;
addAttr -ln "sphereCount" -at long -min 1 -max 100 -dv 10 $sph[0];

qkuotidian
11-11-2010, 06:41 AM
mduvekot and dastcher - thank you

CGTalk Moderation
11-11-2010, 06:41 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.