8

In addition to this discussion:

How to check if two meshes intersect in python?

I would like to know if the new mathutils.bvhtree module may help to detect intersections of two meshes.

Peter Hilgers
  • 199
  • 4
  • 14

1 Answers1

11

yes, overlap in mathutils.bvhtree does show pairs of polygons that are intersecting if any.

You will have to create 2 bvh trees, for each mesh, using one of the methods given in the api (from Object, from Bmesh, from Polygons), then use those as pairs = bvhA.overlap(bvhB)

So a basic check would just see if that list is [] or something. Or may further use the pairs of poly to do something.


Note that there are some limitations

  • it does not account for other info, if one obj is inside another, normals etc, only if they touch
  • it does not work for coplanar poly well, for example 2 blender default plane objects, one slightly shifted x (not reliable on this)
o.g.
  • 1,063
  • 6
  • 7
  • For a test I used the standard objects Suzanne and Cone with this code:import bpy from mathutils.bvhtree import BVHTree scene = bpy.context.scene

    Suzanne = bpy.data.objects["Suzanne"] Cone = bpy.data.objects["Cone"]

    Tree_Suzanne = BVHTree.FromObject(Suzanne, scene, deform = False,
    render = False, cage = False, epsilon = 0.01)

    Tree_Cone = BVHTree.FromObject(Cone, scene, deform = True,
    render = False, cage = False, epsilon = 0.01)

    overlap_pairs = Tree_Cone.overlap(Tree_Suzanne)

    print(len(overlap_pairs)). The result: Always 96 independent of the position of objects ??

    – Peter Hilgers Jan 29 '16 at 09:20
  • 2
    I only used from polygon. Where you have vectors and poly indices. And I used obj.matrix_world * vector. And it worked very well. I guess this also goes for bmesh, as there is a bmesh transform (with matrix) that u can use first. – o.g. Jan 29 '16 at 09:56
  • You are completely right. If I use the bmesh transform it works! Thank you – Peter Hilgers Jan 29 '16 at 13:02