"""Import a browser-produced binary STL in pinned Blender 5.2.""" import hashlib import json import sys from pathlib import Path import bpy def sha256_file(path): digest = hashlib.sha256() with open(path, "rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def main(stl_path, report_path): stl_path = Path(stl_path).resolve() report_path = Path(report_path).resolve() bpy.ops.wm.read_factory_settings(use_empty=True) result = bpy.ops.wm.stl_import( filepath=str(stl_path), directory=str(stl_path.parent), forward_axis="NEGATIVE_Z", up_axis="Y", global_scale=1.0, use_scene_unit=False, use_facet_normal=True, use_mesh_validate=True, ) objects = [obj for obj in bpy.context.scene.objects if obj.type == "MESH"] if "FINISHED" not in result or not objects: raise RuntimeError("Blender Web STL import did not produce a mesh") mesh = objects[0].data mesh.calc_loop_triangles() report = { "schemaVersion": 1, "operation": "DESKTOP_IMPORT_WEB_STL", "sourceSha256": sha256_file(stl_path), "sourceBytes": stl_path.stat().st_size, "objectCount": len(objects), "vertexCount": len(mesh.vertices), "polygonCount": len(mesh.polygons), "triangleCount": len(mesh.loop_triangles), "polygonNormals": [[float(value) for value in polygon.normal] for polygon in mesh.polygons], } report_path.parent.mkdir(parents=True, exist_ok=True) report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8") print("stl-web-roundtrip-desktop-imported triangles=%s" % report["triangleCount"]) if __name__ == "__main__": args = sys.argv[sys.argv.index("--") + 1 :] if "--" in sys.argv else [] if len(args) != 2: raise SystemExit("usage: blender --background --python check-stl-web-roundtrip.py -- STL REPORT") main(args[0], args[1])