Files
Diabolo/tools/measure_equipment_sources.py
T

45 lines
2.2 KiB
Python

"""Measure connected islands and ray-cast openings before game preparation."""
import bpy,json
from pathlib import Path
from mathutils import Vector
from mathutils.bvhtree import BVHTree
ROOT=Path(__file__).resolve().parents[1]
out={}
for name in ['pillory','rack','wheel_vat','musical_chair','cauldron']:
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.import_scene.gltf(filepath=str(ROOT/'tools/model_sources/equipment_meshy_v01'/name/'model.glb'))
o=next(o for o in bpy.context.scene.objects if o.type=='MESH')
bpy.context.view_layer.objects.active=o;o.select_set(True)
bpy.ops.object.transform_apply(location=True,rotation=True,scale=True)
mesh=o.data;neighbors=[[] for _ in mesh.vertices]
for e in mesh.edges:
a,b=e.vertices;neighbors[a].append(b);neighbors[b].append(a)
seen=set();islands=[]
for start in range(len(neighbors)):
if start in seen:continue
stack=[start];seen.add(start);group=[]
while stack:
v=stack.pop();group.append(v)
for n in neighbors[v]:
if n not in seen:seen.add(n);stack.append(n)
if len(group)<20:continue
vs=[mesh.vertices[v].co for v in group]
islands.append({'vertices':len(group),'lo':[round(min(v[i] for v in vs),5) for i in range(3)],'hi':[round(max(v[i] for v in vs),5) for i in range(3)]})
bvh=BVHTree.FromObject(o,bpy.context.evaluated_depsgraph_get())
rays=[]
if name=='pillory':
# Horizontal slices expose the three holes without trusting the picture.
for iz in range(-2,23):
z=iz*.01;empty=[]
for ix in range(-32,33):
x=ix*.01
if bvh.ray_cast(Vector((x,-1,z)),Vector((0,1,0)),2)[0] is None:empty.append(round(x,3))
if empty:rays.append([round(z,3),empty])
if name in ['wheel_vat','cauldron']:
for x in [0,.1,.2,.3]:
hit=bvh.ray_cast(Vector((x,-.1,1)),Vector((0,0,-1)),2)[0]
rays.append([x,list(hit) if hit is not None else None])
out[name]={'islands':sorted(islands,key=lambda x:-x['vertices'])[:18],'rays':rays}
(ROOT/'qa_v19/equipment-sources/measurements.json').write_text(json.dumps(out,indent=2))
print(json.dumps(out))