"""Reopen every delivered .blend and compare physical meshes with the snapshot.""" import bpy import bmesh import hashlib import json import math from pathlib import Path from mathutils import Vector from mathutils.bvhtree import BVHTree ROOT=Path(__file__).resolve().parents[4] SOURCE=Path(__file__).resolve().parent/'snapshot' catalog=json.loads((ROOT/'assets/collections/storybook/catalog.json').read_text()) bpy.ops.wm.open_mainfile(filepath=str(SOURCE/'source_snapshot.blend')) def geometry(obj): # Blender may reorder polygons when rebuilding an otherwise identical mesh. # Compare face connectivity and winding, independent of list order/start vertex. faces=[] for polygon in obj.data.polygons: face=list(polygon.vertices) start=face.index(min(face)) faces.append(face[start:]+face[:start]) return ([list(v.co) for v in obj.data.vertices], sorted(faces)) def solid_base(obj, data): bm=bmesh.new();bm.from_mesh(obj.data) closed=all(e.is_manifold for e in bm.edges) volume=abs(bm.calc_volume(signed=True));bm.free() # Local mesh queries avoid the collection's display translations. tree=BVHTree.FromPolygons([v.co for v in obj.data.vertices], [list(p.vertices) for p in obj.data.polygons]) low=data['boundsBlender']['min'];high=data['boundsBlender']['max'] bottom_hits=[] for u in (.05,.25,.5,.75,.95): for v in (.05,.25,.5,.75,.95): origin=Vector((low[0]+u*(high[0]-low[0]),low[1]+v*(high[1]-low[1]),-.1)) hit,normal,_,_=tree.ray_cast(origin,Vector((0,0,1))) bottom_hits.append(hit is not None and abs(hit.z)<1e-5 and normal.z<-.99) filled=[] for path in data['upperPaths']: for p in path[4:-4:4]: point=Vector((p[0],-p[2],p[1]-data['grooveRadius']-.04)) hit,normal,_,_=tree.find_nearest(point) filled.append(hit is not None and (point-hit).dot(normal)<-1e-5) return {'passed':closed and volume>0 and all(bottom_hits) and all(filled), 'closedManifold':closed,'solidVolume':volume,'bottomSamples':len(bottom_hits), 'bottomClosed':all(bottom_hits),'grooveUndersideSamples':len(filled), 'grooveUndersideFilled':all(filled)} source_names={data.get('geometrySource',kind) for kind,data in catalog['models'].items()} baseline={kind:geometry(bpy.data.objects[kind]) for kind in source_names if kind in bpy.data.objects} checks=[] for kind,data in catalog['models'].items(): if data.get('solidBase'): checks.append({'id':'baseline_'+kind,**solid_base(bpy.data.objects[data.get('geometrySource',kind)],data)}) for kind,data in catalog['models'].items(): path=ROOT/data['sourceBlend'] bpy.ops.wm.open_mainfile(filepath=str(path)) obj=bpy.data.objects[kind] physical=[o for o in bpy.context.scene.objects if not o.name.startswith('ink_')] authored=data.get('geometryOrigin')=='authored' mesh_match=None if authored else geometry(obj)==baseline[data.get('geometrySource',kind)] correct=(len(physical)==1 and physical[0].name==kind and (authored or mesh_match) and obj.location.length<1e-6 and all(abs(x-1)<1e-6 for x in obj.scale)) check={'id':kind,'passed':correct,'objects':[o.name for o in bpy.context.scene.objects], 'geometryMatchesSnapshot':mesh_match, 'origin':list(obj.location),'dimensions':list(obj.dimensions), 'materials':[m.name for m in obj.data.materials]} if data.get('solidBase'): solid=solid_base(obj,data) check['solidBase']=solid check['passed']=correct and solid['passed'] if kind in ('iron_ball','wood_ball','ceramic_ball'): radius=max(v.co.length for v in obj.data.vertices) check['measuredRadius']=radius correct=correct and abs(radius-data['radius'])<1e-6 textures=[n.image for m in obj.data.materials for n in m.node_tree.nodes if n.type=='TEX_IMAGE'] check['packedTextureCount']=sum(image.packed_file is not None for image in textures) check['passed']=correct and len(textures)==2 and check['packedTextureCount']==2 if authored: bm=bmesh.new();bm.from_mesh(obj.data) volume=abs(bm.calc_volume(signed=True));closed=all(e.is_manifold for e in bm.edges);bm.free() correct=correct and closed and all(abs(d-1)<1e-6 for d in obj.dimensions) if kind in ('wall','directional_boost'): correct=correct and abs(volume-1)<1e-6 if kind=='directional_boost': arrow=bpy.data.objects.get('ink_boost_arrow_fill') correct=correct and arrow is not None if arrow is not None: points=[v.co for v in arrow.data.vertices] tip=max(points,key=lambda v:v.x) correct=correct and tip.x>0 and abs(tip.y)<1e-6 and all(1.999) axis_open=tree.ray_cast(Vector((0,0,.1)),Vector((0,0,-1)))[0] is None throat=tree.ray_cast(Vector((0,0,-.7)),Vector((1,0,0)))[0] throat_open=throat is not None and abs(throat.x-.32)<1e-6 # The regular 96-sided circular cavity uses the unchanged inlet. cavity=(.35*(.46**2+.46*.32+.32**2)/3+.65*.32**2)*48*math.sin(math.tau/96) expected_volume=1-cavity correct=correct and corners and all(side_hits) and axis_open and throat_open and abs(volume-expected_volume)<1e-6 check['cubeWithOpenFunnel']={'eightCorners':corners,'flatSideSamples':len(side_hits), 'flatSides':all(side_hits),'verticalPassageOpen':axis_open, 'throatRadiusCorrect':throat_open,'expectedSolidVolume':expected_volume} check.update({'passed':correct,'closedManifold':closed,'solidVolume':volume}) checks.append(check) manifest=json.loads((SOURCE/'source_manifest.json').read_text()) source_status={p:hashlib.sha256((ROOT/p).read_bytes()).hexdigest()==sha for p,sha in manifest['files'].items()} result={'passed':sum(c['passed'] for c in checks),'failed':sum(not c['passed'] for c in checks), 'baselineStillMatchesSnapshot':source_status,'checks':checks} (ROOT/'playtest/artifacts/styled/reopened_assets.json').write_text(json.dumps(result,indent=2)) if result['failed']:raise RuntimeError('Standalone source asset verification failed') print('REOPENED_ASSETS_VERIFIED',result['passed'])