View Full Version : sort array...
02-11-2010, 03:02 PM
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.
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.
02-11-2010, 03:56 PM
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 $pos = `xform -q -ws -translation $cubes[$i]`;
vector $vec = <<($targetPos - $pos), ($targetPos - $pos), ($targetPos - $pos)>>;
float $dist = mag($vec);
$array[$i] = $dist;
$array = `sort($array)`;
02-11-2010, 04:36 PM
Matt is right, you should ditch MEL now and switch to python before you learn any more ! :D
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);
02-11-2010, 05:57 PM
oooh, that's a tasty little nugget.
02-17-2010, 02: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;
02-18-2010, 08: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
02-18-2010, 08: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.
vBulletin v3.0.5, Copyright ©2000-2014, Jelsoft Enterprises Ltd.