PDA

View Full Version : sort array...


pdrewello
02-11-2010, 02:02 PM
Hi all,


I need to establish a manor in which to sort data in an array. What I need todo is the following:

$array = all cubes in scene.

for ($i=0;$i<size($array);$i++){
for ($j=0;$j<size($array);$j++){
sort $array by distance of object from $i with all objects in $array
reorder $array by closest object[$j] from object [$i]
}
}

I can get the axes of two points i.e. objectsA and objectB but I am unsure how to measure the distance between these two, and then to repeat this task -and re-order the array according to the distance of ojbectB from objectA.

Please help...

mlefevre
02-11-2010, 02:56 PM
Hey,

I'm not sure if I'm understanding you correctly, but you'd be iterating over each object, and checking the distance between that object and every other object, then creating an array from each object, sorted by distance.
In that case you'd need as many arrays as there are objects.

Unless you want to find the distance of every other object from ONE specific object which would make a bit more sense?

Python dictionaries would come in handy for this.


string $target[] = `ls -sl`;
float $targetPos[] = `xform -q -ws -translation $target`;
string $cubes[] = `ls -sl`;
int $num = size($cubes);
float $array[];
for($i=0;$i<$num;$i++){
float $pos[] = `xform -q -ws -translation $cubes[$i]`;
vector $vec = <<($targetPos[0] - $pos[0]), ($targetPos[1] - $pos[1]), ($targetPos[2] - $pos[2])>>;

float $dist = mag($vec);
$array[$i] = $dist;
}
$array = `sort($array)`;
print($array);

NaughtyNathan
02-11-2010, 03:36 PM
Matt is right, you should ditch MEL now and switch to python before you learn any more ! :D

:nathaN

p.s. Matt, if you ditch floats[] in favour of vectors in MEL you can use normal arithmetic directly on them without breaking them into individual components.. looks neater too.. ;)


vector $targetPos = `xform -q -ws -translation $target`;
vector $pos = `xform -q -ws -translation $cubes[$i]`;
vector $vec = ($targetPos - $pos);

mlefevre
02-11-2010, 04:57 PM
oooh, that's a tasty little nugget.
cheers

rafoarc
02-17-2010, 01:32 AM
in order to get the distance between two objects, you can also use the distanceNode. That way you can avoid the vector stuff...

createNode distanceBetween -n myDistance;
connectAttr -f box1.translate myDistance.point1;
connectAttr -f box2.translate myDistance.point2;
print myDistance.distance;

safakoner
02-18-2010, 07:45 PM
global proc float distance(string $dag1, string $dag2)
{
vector $vector1 = `xform -q -ws -rp $dag1`;
vector $vector2 = `xform -q -ws -rp $dag2`;
return `mag($vector1 - $vector2)`;
}
distance "locator1" "locator2";

make your calculation to get the closest one to your object, create a new array don't bother trying to change existing one and store your objects in new array

CGTalk Moderation
02-18-2010, 07:45 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.