PDA

View Full Version : Distance between locator and multi objects


fatsumo
10-01-2009, 05:24 PM
Okay, think this is my last scripty question for a while so anyone in the know, would be great to pick your brains.

Have a locator and have let's say 20 cubes. Want to turn the cubes vis off if the locator is within a certain distance.

I have something, but it doesnt work at present, but maybe a good start?


string $sObjectA = "locator1";

string $sObjectB = "cube_";

$attrib = ($sObjectB + $i + ".visibility");

$someGivenAmount = 10;



for ($frame = 1; $frame <= 100; $frame ++)

currentTime $frame;

for( $i = 1; $i < size( $sObjectB ); $i++ )

{

int $i;

float $faObjectA_Xforms[] = `xform -q -ws -t $sObjectA`;

float $faObjectB_Xforms[] = `xform -q -ws -t $sObjectB`;



float $dx = ($faObjectA_Xforms[0] - $faObjectB_Xforms[0]);

float $dy = ($faObjectA_Xforms[1] - $faObjectB_Xforms[1]);

float $dz = ($faObjectA_Xforms[2] - $faObjectB_Xforms[2]);



$fDistance = sqrt(($dx*$dx) + ($dy*$dy) + ($dz*$dz));

if ($fDistance < $someGivenAmount)

{

setAttr $attrib (0);

}

else

{

setAttr $attrib (1);

}

};

phix314
10-01-2009, 06:44 PM
This should do the trick. Only thing I'm thinkin is this bases the distances off the object center, so if you have some large, strange shapes it won't be accurate in terms of distance.


proc gdistance(string $master){
int $i;

string $master = $master;

float $mCenter[] = `objectCenter $master`;
string $kids[] = `ls -sl -sn`;
for($i = 0; $i < size($kids); $i++){
float $dist[] = `objectCenter $kids[$i]`;
float $dx = $mCenter[0] - $dist[0];
float $dy = $mCenter[1] - $dist[1];
float $dz = $mCenter[2] - $dist[2];
float $delta = sqrt ( ($dx*$dx) + ($dy*$dy) + ($dz*$dz) );

if($delta <= 20){
setAttr ($kids[$i]+".visibility") 0;
}
}
}
gdistance("locator1")

fatsumo
10-02-2009, 10:11 AM
Dude, absolutely fantastic! cheers.
Adapted it a little for my needs but you have just saved me a ton of headaches.

Can I just ask what $master is? As it is not defined anywhere as far as I know.

Scripting is not my strong point :)

CGTalk Moderation
10-02-2009, 10:11 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.