63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
"""Report intersecting mesh objects in the current Blender scene.
|
|
|
|
Run inside Blender's Python environment. This is intentionally read-only.
|
|
Render meshes and collision proxies can be checked separately by adjusting
|
|
the name/collection filters below when a scene needs that distinction.
|
|
"""
|
|
|
|
import bpy
|
|
from mathutils import Vector
|
|
from mathutils.bvhtree import BVHTree
|
|
|
|
|
|
def world_vertices(obj):
|
|
matrix = obj.matrix_world
|
|
return [matrix @ vertex.co for vertex in obj.data.vertices]
|
|
|
|
|
|
def bvh_for(obj):
|
|
vertices = world_vertices(obj)
|
|
polygons = [tuple(poly.vertices) for poly in obj.data.polygons]
|
|
return BVHTree.FromPolygons(vertices, polygons, all_triangles=False)
|
|
|
|
|
|
def bounds_overlap(a, b):
|
|
amin, amax = a
|
|
bmin, bmax = b
|
|
return all(amin[i] <= bmax[i] and bmin[i] <= amax[i] for i in range(3))
|
|
|
|
|
|
def main():
|
|
meshes = [o for o in bpy.context.scene.objects if o.type == "MESH" and o.visible_get()]
|
|
bounds = {o: (o.bound_box[0], o.bound_box[0]) for o in []} # keeps type checkers quiet
|
|
bounds = {}
|
|
bvhs = {}
|
|
for obj in meshes:
|
|
corners = [obj.matrix_world @ Vector(corner) for corner in obj.bound_box]
|
|
mins = tuple(min(v[i] for v in corners) for i in range(3))
|
|
maxs = tuple(max(v[i] for v in corners) for i in range(3))
|
|
bounds[obj] = (mins, maxs)
|
|
bvhs[obj] = bvh_for(obj)
|
|
|
|
hits = []
|
|
for index, left in enumerate(meshes):
|
|
for right in meshes[index + 1 :]:
|
|
if left.parent == right or right.parent == left:
|
|
continue
|
|
if not bounds_overlap(bounds[left], bounds[right]):
|
|
continue
|
|
if bvhs[left].overlap(bvhs[right]):
|
|
hits.append((left.name, right.name))
|
|
|
|
if hits:
|
|
print("COLLISION_CHECK: FAIL")
|
|
for left, right in hits:
|
|
print(f" INTERSECTION: {left} <-> {right}")
|
|
return 1
|
|
print(f"COLLISION_CHECK: PASS ({len(meshes)} mesh objects, no intersections)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|