"""Check the exported files as a slicer would see them, not as Blender does. python3 verify.py out/ Manifold check works on the triangle soup alone (STL carries no topology, so vertices are welded by exact coordinate match). Signed volume is the cheap global test: a closed, consistently outward-oriented surface gives a positive volume close to the intended body volume. """ import glob import json import os import struct import sys from array import array import xml.etree.ElementTree as ET import zipfile import numpy as np CORE_NS = "{http://schemas.microsoft.com/3dmanufacturing/core/2015/02}" def read_stl(path): with open(path, "rb") as f: f.read(80) (count,) = struct.unpack("= 0 and tris.max() < len(verts), \ f"{path}: triangle index out of range" return unit, verts, tris def topology(verts, tris, weld=True): """Edge census, plus signed volume. STL carries no topology, so vertices have to be welded by coordinate, which costs a sort over N x 3 keys -- at 8M triangles that is enough to get the process OOM-killed. A 3MF already carries shared indices, so there is nothing to weld and the census runs directly. """ if weld: key = np.round(verts / 1e-6).astype(np.int64) unique, index = np.unique(key, axis=0, return_inverse=True) tris = index[tris] welded = unique.astype(np.float64) * 1e-6 else: welded = verts degenerate = int(((tris[:, 0] == tris[:, 1]) | (tris[:, 1] == tris[:, 2]) | (tris[:, 0] == tris[:, 2])).sum()) # Every edge appears once per incident triangle. A closed, consistently # oriented surface therefore shows each undirected edge exactly twice, # once in each direction. directed = np.vstack([tris[:, [0, 1]], tris[:, [1, 2]], tris[:, [2, 0]]]) forward = directed[:, 0] < directed[:, 1] undirected = np.sort(directed, axis=1) edges, inverse, counts = np.unique(undirected, axis=0, return_inverse=True, return_counts=True) forward_per_edge = np.bincount(inverse, weights=forward, minlength=len(edges)) boundary = int((counts == 1).sum()) nonmanifold = int((counts > 2).sum()) misoriented = int(((counts == 2) & (forward_per_edge != 1)).sum()) a, b, c = welded[tris[:, 0]], welded[tris[:, 1]], welded[tris[:, 2]] signed_vol = float(np.einsum("ij,ij->i", a, np.cross(b, c)).sum() / 6.0) area = float(np.linalg.norm(np.cross(b - a, c - a), axis=1).sum() / 2.0) return { "triangles": int(len(tris)), "welded_vertices": int(len(welded)), "boundary_edges": boundary, "nonmanifold_edges": nonmanifold, "misoriented_edges": misoriented, "degenerate_triangles": degenerate, "signed_volume_mm3": round(signed_vol, 1), "area_mm2": round(area, 1), "bbox_mm": [round(float(v), 2) for v in np.ptp(verts, axis=0)], } def main(out_dir, only=None): report = {} paths = sorted(glob.glob(os.path.join(out_dir, "*.stl")) + glob.glob(os.path.join(out_dir, "*.3mf"))) for path in paths: name = os.path.basename(path) kind = "3mf" if path.endswith(".3mf") else "stl" if only and kind != only: continue if kind == "stl": tris = read_stl(path) verts = tris.reshape(-1, 3) shape = (verts, np.arange(len(verts)).reshape(-1, 3), True) unit = "millimetre (implied)" else: unit, verts, idx = read_3mf(path) shape = (verts, idx, False) info = topology(*shape) info["unit"] = unit info["file_mb"] = round(os.path.getsize(path) / 1e6, 2) report[name] = info print(f"\n== {name} [{info['unit']}]") for key, value in info.items(): if key != "unit": print(f" {key:22s} {value}") with open(os.path.join(out_dir, "verify.json"), "w") as f: json.dump(report, f, indent=2) if __name__ == "__main__": directory = sys.argv[1] if len(sys.argv) > 1 else "out" only = sys.argv[2] if len(sys.argv) > 2 else None main(directory, only)