I'm trying to make a script that'll find if any points of an objects bounding box is inside the area of a different objects bounding box.
My thought was to cancel out the rotation of the object I'm checking against so that I can just do a simple point.x > minX and point.x < maxX check to see if its inside. I would essentially move everything into the relative space of the object I'm checking against.
I've almost got a solution working, I'm using a matrix invert on the active object (the one I'm using as the area to check against) so everything is axis absolute and then I translate each other selected object into that relative space.
The problem is that I don't understand how to keep the relative rotation of the other objects intact as currently it removes all rotation for all objects:

Here's the code I'm using to get the positions of the corners (also spawning empties to make it easier to see)
import bpy
context = bpy.context
act_obj = context.active_object
sel_obj = context.selected_objects
#this section is to make sure the scale of the active object is applied
originalSel = sel_obj
for obj in sel_obj:
obj.select_set(False)
act_obj.select_set(True)
bpy.ops.object.transform_apply(
location=False, rotation=False, scale=True, properties=False)
#debug create an empty to see where the points are
def createEmpty():
obj = bpy.data.objects.new( "empty", None )
bpy.context.scene.collection.objects.link( obj )
obj.empty_display_size = 1
obj.empty_display_type = 'PLAIN_AXES'
return obj
#get the relative locations of the corners of the bounding box
def calculateBounds(matrix_world_inverted, obj):
local_pos = matrix_world_inverted @ obj.matrix_world.translation.copy()
allVertPositions = []
for vert in obj.bound_box:
empty = createEmpty()
vertPos = []
for i in range(3):
vertPos.append(local_pos[i] + (vert[i] * obj.scale[i]))
empty.location[i] = local_pos[i] + (vert[i] * obj.scale[i])
allVertPositions.append(vertPos)
return allVertPositions
#get the active objects inverted matrix (so we can work with absolute axis)
matrix_world_inverted = act_obj.matrix_world.copy().inverted()
#create empties for each corner of all selected objects
for obj in sel_obj:
objVerts = calculateBounds(matrix_world_inverted, obj)
How would I go about making sure that the relative (to the bix box) location of the small box stays intact? Is there an easier way than what I'm doing?
Just in case you are not looking for the most elegant and efficient solution though, you could also work in global space and use a BVHTree. https://blender.stackexchange.com/questions/173356/blender-crashes-when-running-trying-to-overlap/173527
– Robert Gützkow Jun 28 '22 at 14:38