I have a script to determine the distance between two objects, but it’s just creating a “distanceBetween” node, connecting, getAttr(obj.distance) and then deleting it. Seems very inefficient.

Is there a way to extract the distance from a matrix or a vector with math? I’m sure there’s some elegant way to calculate that!

And then how would I code this in python cmds? or with OpenMaya?

also doing the math manually is pretty simple the subtraction of 2 vectors is just subtract each component from each other to create a new vector. then to get the length of the vector you square each component, add it to the previous then get the square root. sqrt(x^2 + y^2 + z^2)

FWIW, square roots are comparatively expensive – if you are checking one distance, or even a thousand distances, it’s not a big deal… but if you are checking a hundred thousand it will add up. At that point a common trick is to do the “manhattan distance” , the sum of the differences in X, Y and Z. It won’t be correct but it will be a decent approximation, so you can use it to cut down the number of real sqrt(x^2 + y^2 + z^2) checks for speed – you can skip detail checks when the approximate distance is way too big or way too small to bother checking in detail