# How to check if two meshes intersect in python with BVH Tree?

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.

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)
• 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 9:20
• 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 9:56
• You are completely right. If I use the bmesh transform it works! Thank you – Peter Hilgers Jan 29 '16 at 13:02