0
$\begingroup$

After a few runs, it is quite certain that the overlap() function is causing blender to quit unexpectedly, I am not sure if the for loop is making blender to run on an infinite loop that cause that.

Or is there a way to write overlap() in a more elegant way to detect if an object is overlaping with another and get the the overlaping object in return.

import bpy
from mathutils.bvhtree import BVHTree
dgraph=bpy.context.evaluated_depsgraph_get()
for obj in bpy.context.scene.objects:
    if obj.name.startswith("M"):
        for obj2 in bpy.context.scene.objects:
            print(BVHTree.FromObject(obj, dgraph).overlap(BVHTree.FromObject(obj2, dgraph)))

there's only 6 objects in the scene with some cubes and cylinders for test.

$\endgroup$
3
  • $\begingroup$ What's your question? Please read: blender.stackexchange.com/help/how-to-ask and report bugs here: developer.blender.org $\endgroup$
    – brockmann
    Apr 6, 2020 at 7:38
  • $\begingroup$ edited the question and tried to be a little bit more specific... and i am not sure if that's a bug or me coding bad. $\endgroup$
    – adrian li
    Apr 7, 2020 at 11:59
  • $\begingroup$ Can confirm, this crashes. The script also tries to determine the overlap for non-mesh objects, which you shouldn't do. $\endgroup$
    – Robert Gützkow
    Apr 7, 2020 at 15:10

1 Answer 1

3
$\begingroup$

Updated answer

The original answer only addressed the problem of the script causing a crash. However, there was another issue that was not addressed. The initialization of the BVHTree does not apply the world matrix for the intersection tests. Thus, instead of identifying intersections in global space, objects were compared as if their origins were in the same location.

The following script fixes the issue by creating a BMesh for each object and applying the world matrix prior to the construction of the BVHTree.

import bpy
import bmesh
from mathutils.bvhtree import BVHTree

depsgraph = bpy.context.evaluated_depsgraph_get()

for obj1 in bpy.context.scene.objects:
    if obj1.type == "MESH" and obj1.name.startswith("M"):
        for obj2 in bpy.context.scene.objects:
            if obj2.type == "MESH":
                bm1 = bmesh.new()
                bm2 = bmesh.new()
                bm1.from_object(obj1, depsgraph)
                bm2.from_object(obj2, depsgraph)
                bm1.transform(obj1.evaluated_get(depsgraph).matrix_world)
                bm2.transform(obj2.evaluated_get(depsgraph).matrix_world)
                bvh1 = BVHTree.FromBMesh(bm1)
                bvh2 = BVHTree.FromBMesh(bm2)
                if bvh1.overlap(bvh2):
                    print(f"Intersection between {obj1.name} and {obj2.name}")
                bm1.free()
                bm2.free()

Note: The scripts are currently not optimized and check the intersection between each pair twice.


Original answer

The script iterates over all objects, including those that aren't meshes. Only determine the overlap for objects with type set to "MESH" and it will work properly.

Your script with the suggested fix:

import bpy
from mathutils.bvhtree import BVHTree

dgraph = bpy.context.evaluated_depsgraph_get()

for obj in bpy.context.scene.objects:
    if obj.name.startswith("M") and obj.type == "MESH":
        for obj2 in bpy.context.scene.objects:
            if obj2.type == "MESH":
                print(BVHTree.FromObject(obj, dgraph).overlap(BVHTree.FromObject(obj2, dgraph)))
$\endgroup$
2
  • $\begingroup$ In 3.2 this has the same output, whether the meshes (two Cubes) are overlapping or not. Could you please check that your answer is still valid? $\endgroup$
    – handle
    Jun 17, 2022 at 17:44
  • 1
    $\begingroup$ @handle I will update the answer shortly. The issue was that the script checked for an intersection in local space. The construction of the BVHTree does not apply the world matrix prior to the intersection test. Thus, if you have two cubes that did not have their location transform applied the script considered them overlapping, which is obviously incorrect for this use case. $\endgroup$
    – Robert Gützkow
    Jun 18, 2022 at 10:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .