Files
workinf_Blender_Wasm/tools/web/generate-decimate-golden.py
mes123456 fe071c1249
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
feat(web): decimate modifier stack evaluation, desktop entries and headed suite
- engine: publish evaluated modifier-stack meshes (triangleCount, base counts,
  EVALUATED status) for object-mode single-user meshes with enabled modifiers
- UI: 添加修改器 ▸ 生成 ▸ 简化 (Decimate) menu and edit-mode
  网格 ▸ 清理 ▸ 简化几何 dialog (mesh.decimate with ratio)
- tests: four-case headed suite (modifier/edit × web/native) on the monkey GLB
- tools: headed runner, receipt checker, desktop golden generator
- docs: MESH-DECIMATE-HEADED-TESTS.md and status completion record

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-28 20:57:16 +08:00

168 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Desktop Blender 5.2 reference for the Web Blender Decimate suite.
Run through the wrapper (recommended):
node tools/web/generate-decimate-golden.mjs
or directly:
<blender> --background --python tools/web/generate-decimate-golden.py -- \
--blend web/app/public/tutorial/pbr/studio.blend \
--monkey "re/猴头.glb" \
--out tests/golden/MESH-DECIMATE-DESKTOP/desktop-decimate.json
The script reproduces the two desktop entries the headed suite drives in the
browser and records the triangle counts the browser must match:
1. Properties ▸ Modifier ▸ Add Modifier ▸ Generate ▸ Decimate ▸ Collapse,
ratio 0.1 — evaluated through the dependency graph, original mesh intact.
2. Edit Mode ▸ Mesh ▸ Clean Up ▸ Decimate Geometry, ratio 0.5 — the
`bpy.ops.mesh.decimate` operator applied to the selected faces.
Output is a golden record, never a pass claim: `acceptance` is NOT_CLAIMED.
"""
import hashlib
import json
import os
import sys
import bpy
def parse_args():
argv = sys.argv
argv = argv[argv.index("--") + 1:] if "--" in argv else []
options = {
"blend": os.path.abspath("web/app/public/tutorial/pbr/studio.blend"),
"monkey": os.path.abspath("re/猴头.glb"),
"out": os.path.abspath("tests/golden/MESH-DECIMATE-DESKTOP/desktop-decimate.json"),
"modifier-ratio": 0.1,
"edit-ratio": 0.5,
}
index = 0
while index < len(argv):
key = argv[index].lstrip("-")
if key in ("blend", "monkey", "out", "modifier-ratio", "edit-ratio"):
value = argv[index + 1]
options[key] = float(value) if key.endswith("ratio") else os.path.abspath(value)
index += 2
else:
raise SystemExit(f"unknown argument: {argv[index]}")
return options
def sha256(path):
digest = hashlib.sha256()
with open(path, "rb") as handle:
for chunk in iter(lambda: handle.read(1 << 20), b""):
digest.update(chunk)
return digest.hexdigest()
def evaluated_triangles(obj):
depsgraph = bpy.context.evaluated_depsgraph_get()
depsgraph.update()
evaluated = obj.evaluated_get(depsgraph)
mesh = evaluated.to_mesh()
try:
mesh.calc_loop_triangles()
return len(mesh.loop_triangles)
finally:
evaluated.to_mesh_clear()
def import_monkey(path):
before = set(bpy.data.objects)
bpy.ops.import_scene.gltf(filepath=path)
added = [obj for obj in bpy.data.objects if obj not in before and obj.type == "MESH"]
if not added:
raise SystemExit(f"the monkey GLB produced no mesh objects: {path}")
obj = added[0]
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
return obj, [item.name for item in added]
def main():
options = parse_args()
for key in ("blend", "monkey"):
if not os.path.exists(options[key]):
raise SystemExit(f"missing fixture: {options[key]}")
bpy.ops.wm.open_mainfile(filepath=options["blend"])
monkey, imported_names = import_monkey(options["monkey"])
baseline = evaluated_triangles(monkey)
modifier = monkey.modifiers.new(name="Decimate", type="DECIMATE")
modifier.decimate_type = "COLLAPSE"
modifier.ratio = options["modifier-ratio"]
bpy.context.view_layer.update()
modifier_triangles = evaluated_triangles(monkey)
baked = None
monkey.modifiers.remove(modifier)
bpy.context.view_layer.update()
restored = evaluated_triangles(monkey)
bpy.ops.object.select_all(action="DESELECT")
monkey.select_set(True)
bpy.context.view_layer.objects.active = monkey
edit_before = evaluated_triangles(monkey)
bpy.ops.object.mode_set(mode="EDIT")
try:
bpy.ops.mesh.select_all(action="SELECT")
result = bpy.ops.mesh.decimate(ratio=options["edit-ratio"])
finally:
bpy.ops.object.mode_set(mode="OBJECT")
mesh = monkey.data
mesh.calc_loop_triangles()
edit_faces = len(mesh.polygons)
edit_triangles = len(mesh.loop_triangles)
baked = evaluated_triangles(monkey)
record = {
"schemaVersion": 1,
"acceptance": "NOT_CLAIMED",
"source": "desktop-blender",
"blender": {
"version": bpy.app.version_string,
"binary": os.path.abspath(bpy.app.binary_path),
},
"fixtures": {
"studio": {"path": options["blend"], "sha256": sha256(options["blend"])},
"monkey": {"path": options["monkey"], "sha256": sha256(options["monkey"])},
},
"object": {"name": monkey.name, "imported": imported_names},
"modifier": {
"type": "DECIMATE",
"decimateType": "COLLAPSE",
"ratio": options["modifier-ratio"],
"trianglesBefore": baseline,
"trianglesAfter": modifier_triangles,
"trianglesAfterRemoval": restored,
},
"edit": {
"operator": "mesh.decimate",
"ratio": options["edit-ratio"],
"trianglesBefore": edit_before,
"trianglesAfter": edit_triangles,
"faces": edit_faces,
"trianglesAfterObjectMode": baked,
"operatorResult": list(result),
},
}
directory = os.path.dirname(options["out"])
os.makedirs(directory, exist_ok=True)
with open(options["out"], "w", encoding="utf-8") as handle:
json.dump(record, handle, ensure_ascii=False, indent=2)
handle.write("\n")
print(json.dumps({"status": "WRITTEN", "path": options["out"], "record": record}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()