PDA

View Full Version : Using For in Loop with set driven key? syntax


johnjoe
11-04-2008, 04:58 PM
Hi, it's me again! :argh:

I have a for in loop that works through and scales $n (any number) of polySpheres in order from 0 - 100% in size but I'd like to change this. I need the a value of say a locator to be 1 and all the spheres to be scaled like in the following script..



string $myselection[] = `ls -sl`;
int $numSelect = size($myselection);

float $sizeIcrament = 1.0/$numSelect;
float $size = $sizeIcrament;

for ($sel in $myselection)
{
setAttr ($sel + ".scaleX") $size;
setAttr ($sel + ".scaleY") $size;
setAttr ($sel + ".scaleZ") $size;
$size += $sizeIcrament;
}


here's the set driven key code..

setDrivenKeyframe
-currentDriver ($Locator1 + ".myControl")
-driverValue 1
-attribute ???
-value ???? ;

johnjoe
11-04-2008, 07:48 PM
PLEASE SOMEONE ANYONE :cry:

I've tried this but no joy... :sad:

float $sizeIcrament = 1.0/$num_of_balls;
float $size = $sizeIcrament;

for ($sel in $mshName)
{
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleX"
-value $size $mshName[0];
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleY"
-value $size $mshName[0];
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleZ"
-value $size $mshName[0];

//setAttr ($sel + ".scaleX") $size;
//setAttr ($sel + ".scaleY") $size;
//setAttr ($sel + ".scaleZ") $size;
$size += $sizeIcrament;
}


here's all of the script... simply select a load of polySpheres and a curve

/// Grab the number of dropslets and put it into a variable also get the curve name and put that into a variable.

string $crv_obj[] = `ls -sl -dag -type "nurbsCurve"`;
string $ball_meshes[] = `ls -sl -dag -type "mesh"`;

string $crvName[] =`listRelatives -parent $crv_obj`;
string $mshName[] =`listRelatives -parent $ball_meshes`;

// get the number of droplets in the selection
int $num_of_balls=size($mshName);

//print $num_of_balls;
//print $crvName;

///create the locator control

$animContol = `spaceLocator -p 0 0 0 -n $crvName `;

///create the motionPath for the controller and attach it to the web curve segment.

string $motionPathNode1 = `pathAnimation -fractionMode true -follow false -curve $crvName $animContol `;

//delete keyframes automatically created in the U value.
select -r ($motionPathNode1 + "_uValue");
delete;

//move the control locator to the middle of the curve
setAttr ($motionPathNode1 + ".uValue") 0.5;

//loop through droplets and set driven key for each one.

for ($i =0; $i<$num_of_balls; $i++){
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleX"
-value 1 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleY"
-value 1 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleZ"
-value 1 $mshName;



//sphere ;

}

//move the control locator down one quarter of the curve
setAttr ($motionPathNode1 + ".uValue") 0.25;

//string $mshName[]

//int $num_of_balls

float $sizeIcrament = 1.0/$num_of_balls;
float $size = $sizeIcrament;

for ($sel in $mshName)
{
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleX"
-value $size $mshName[0];
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleY"
-value $size $mshName[0];
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleZ"
-value $size $mshName[0];

//setAttr ($sel + ".scaleX") $size;
//setAttr ($sel + ".scaleY") $size;
//setAttr ($sel + ".scaleZ") $size;
$size += $sizeIcrament;
}

kattkieru
11-05-2008, 10:26 PM
You're going to hate me for saying this, but:

Don't use set driven keys.

There's a node in the utility nodes part of the hypershade called setRange that will make your life a lot easier once you learn to use it. Then all you need to do is run direct connections between it and the object where you want a driven key.

In fact, many driven keys can be done with a multiplyDivide node, since most end up being simple multiplication or division. I'm not exactly sure what you're doing in your script but I bet it'd be done better with utility nodes (near as I can tell all you need are some multiplyDivide nodes, perhaps one for each sphere, although it's late and I'm sleepy and not seeing straight ;) ), and as they execute faster than driven keys you'll also have a faster scene overall.

Have a look at the MEL command references for shadingNode -asUtility, and connectAttr -f.

johnjoe
11-06-2008, 06:59 AM
Thanks for your reply , I'm very interested to see your approach in action if thats possible?

Here's my script as it stands...

select and load of PolySpheres and a curve run the script and use the grp control located on the Locator control. I've not managed to get the offset spheres working just yet, so they all scale up and down the same.

Massive thanks :thumbsup:


/// Grab the number of dropslets and put it into a variable also get the curve name and put that into a variable.

string $crv_obj[] = `ls -sl -dag -type "nurbsCurve"`;
string $ball_meshes[] = `ls -sl -dag -type "mesh"`;

string $crvName[] =`listRelatives -parent $crv_obj`;
string $mshName[] =`listRelatives -parent $ball_meshes`;

// get the number of droplets in the selection
int $num_of_balls=size($mshName);

//print $num_of_balls;
//print $crvName;

///create the locator control

string $animControl[]= `spaceLocator -p 0 0 0 -n ($crvName[0] + "_ctrl") `;

///create the motionPath for the controller and attach it to the web curve segment.

string $motionPathNode1 = `pathAnimation -fractionMode true -follow false -curve $crvName $animControl `;

//delete keyframes automatically created in the U value.
select -r ($motionPathNode1 + "_uValue");
delete;

//move the control locator to the middle of the curve
setAttr ($motionPathNode1 + ".uValue") 0.5;

//loop through droplets and set driven key for each one at the following values 0 , 0.5 and 1.

for ($i =0; $i<$num_of_balls; $i++){
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleX"
-value 1 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleY"
-value 1 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.5
-attribute "scaleZ"
-value 1 $mshName;



//sphere ;

}

for ($i =0; $i<$num_of_balls; $i++){
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.0
-attribute "scaleX"
-value 0 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.0
-attribute "scaleY"
-value 0 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 0.0
-attribute "scaleZ"
-value 0 $mshName;



//sphere ;

}


for ($i =0; $i<$num_of_balls; $i++){
setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 1.0
-attribute "scaleX"
-value 0 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 1.0
-attribute "scaleY"
-value 0 $mshName;

setDrivenKeyframe
-currentDriver ($motionPathNode1 + ".uValue")
-driverValue 1.0
-attribute "scaleZ"
-value 0 $mshName;



//sphere ;

}

///Add the Attribute grp_control to the Locator control named (curveName_ctrl)

addAttr -ln "grp_control" -at double -min 0 -max 10 -dv 5 $animControl;

setAttr -e-keyable true ($animControl[0] + ".grp_control");


///connect the grp_control to the motionPath.uValue :making (0-1) now (0-10)

setDrivenKeyframe
-currentDriver ($animControl[0] + ".grp_control")
-driverValue 10
-attribute "scaleZ"
-value 1 ($motionPathNode1 + ".uValue");

setDrivenKeyframe
-currentDriver ($animControl[0] + ".grp_control")
-driverValue 0
-attribute "scaleZ"
-value 0 ($motionPathNode1 + ".uValue");

CGTalk Moderation
11-06-2008, 06:59 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.