5
$\begingroup$

I need to add new geometry to my scene and it should fit inside a large box without any intersections.

Is it possible to calculate the shortest distance between two geometry objects in space, and check if the objects are intersecting each other via python?

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Sounds possible, but please define your requirements a bit more. What exactly you want to achieve? Do you want to calculate the distance between the bounding boxes or the distance of the center points? Can you share your code with us? An Image would also be helpful to understand your issue. Be aware: asking multiple questions is not allowed - for the ASCII part, the code of this addon: github.com/p2or/blender-vfxtoolbox could help you. $\endgroup$ – p2or Dec 22 '14 at 13:55
  • 2
    $\begingroup$ This answer may help you. $\endgroup$ – David Dec 22 '14 at 17:14
  • 1
    $\begingroup$ Short answer is: 2 objects have intersection or not, Long answer is: Distance 'X' between minimal of face/edge/point of 2 objects. bankfotek.pl/image/1832980 $\endgroup$ – bardzo Dec 22 '14 at 19:30
  • 1
    $\begingroup$ @bardzo Please edit your question with this additional information to reopen it. :) $\endgroup$ – p2or Dec 22 '14 at 19:43
  • 1
    $\begingroup$ mathutils.geometry should help. distance_point_to_plane or intersect_point_line would be the starting point. $\endgroup$ – sambler Dec 24 '14 at 11:25
3
$\begingroup$

Going by the question in your title this is my answer.

To get the distance between two objects use this formula.

$$d = \sqrt{\left(x_2-x_1\right)^2+\left(y_2-y_1\right)^2+\left(z_2-z_1\right)^2}$$

Then the python code is as follow. Comments are included in the code to help you understand what is going on.

import bpy
from math import sqrt

lst = []  # create list to store the location info

for obj in bpy.context.selected_objects:  # iterate over the selection NOTE: two object should be selected
    lst.append(obj.location)  # populate the lst with the location info

# calulate the distance of the two objects
distance = sqrt((lst[0][0] - lst[1][0])**2 + (lst[0][1] - lst[1][1])**2 + (lst[0][2] - lst[1][2])**2)

# display the distance
print(distance)
$\endgroup$
  • $\begingroup$ With vector math, included in blender, there is no reason to ever use the Euclidean formula as above. (obj1.matrix.world.translation - obj2.matrix_world.translation).length is the global distance between the origins of two objects obj1 and obj2 $\endgroup$ – batFINGER Nov 2 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.