Files
IntoHole/assets/collections/storybook/source/verify.py
T
Yanchen bf6c355fa6 feat: 切换至滚动阻力运行模型并更新资产、编辑器与验证
- 物理规则:现行模型仅保留滚动阻力耗能与恢复系数碰撞,风力、空气阻力、静摩擦、动摩擦不再参与运行;确认三球质量 60/330/约890 kg 及水密度、流阻参数
- 定向加速改为真实顶面接触触发的一次性 +5 m/s 速度事件,模型新增渐进式 U 形导槽出口
- 更新球体、滑槽、水/冰/墙、球洞模型与 storybook 目录、物理、实体配置
- 工坊摆放、编辑器、运行适配与检查脚本同步;删除过时的 baseline 三组检查
- 新增谜题设计参考文档、关卡草稿与本轮实验产物
2026-12-05 10:37:31 +08:00

136 lines
7.6 KiB
Python

"""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 (obj.dimensions-Vector(data['boundsBlender']['size'])).length<1e-6
if kind=='wall':
correct=correct and abs(volume-1)<1e-6
elif kind=='directional_boost':
base=data['baseHeight']
radius=catalog['models']['straight']['grooveRadius']
mouth=[v.co for v in obj.data.vertices if abs(v.co.x-.5)<1e-6]
profile=[Vector((.5,radius*math.sin(i*math.pi/32),base+radius*(1-math.cos(i*math.pi/32)))) for i in range(-16,17)]
correct=correct and all(any((vertex-target).length<1e-6 for vertex in mouth) for target in profile)
arrow=bpy.data.objects.get('ink_boost_arrow_fill')
correct=correct and arrow is not None
if arrow is not None:
tip=max((v.co for v in arrow.data.vertices),key=lambda v:v.x)
correct=correct and tip.x>0 and abs(tip.y)<1e-6 and base<tip.z<base+.01
check['arrowTipBlender']=list(tip)
elif kind=='goal_hole':
tree=BVHTree.FromPolygons([v.co for v in obj.data.vertices],
[list(p.vertices) for p in obj.data.polygons])
corners=all(any((v.co-Vector((x,y,z))).length<1e-6 for v in obj.data.vertices)
for x in (-.5,.5) for y in (-.5,.5) for z in (-1,0))
side_hits=[]
for axis in (0,1):
for sign in (-1,1):
for tangent in (-.45,0,.45):
for height in (-.05,-.9):
origin=Vector((tangent,tangent,height));origin[axis]=sign
direction=Vector((0,0,0));direction[axis]=-sign
hit,normal,_,_=tree.ray_cast(origin,direction)
side_hits.append(hit is not None and abs(hit[axis]-sign*.5)<1e-6
and normal[axis]*sign>.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'])