# 3MF reference values through lib3mf's Python binding (ctypes over lib3mf.so; both from nixpkgs), run by 3mf.py as # `python3 -I _3mf_reference.py < paths.json` -> JSON list, one entry per file: # { unit, items, meshObjects = [ [vertices, triangles] ... sorted ], triangles, area, min, max (world space, every # build item through its components), baseGroups, colorGroups, textures, texGroups } or { failed = message } import json, math, sys sys.path.insert(0, sys.argv[1]) import Lib3MF # noqa: E402 W = Lib3MF.Wrapper(sys.argv[2]) UNITS = {0: "micron", 1: "millimeter", 2: "centimeter", 3: "inch", 4: "foot", 5: "meter"} def mat(t): f = t.Fields return [[f[r][c] for c in range(3)] for r in range(4)] def mul(a, b): # row-vector 4x3 affine: p * a * b out = [] for r in range(4): row = [] for c in range(3): s = sum(a[r][k] * b[k][c] for k in range(3)) if r == 3: s += b[3][c] row.append(s) out.append(row) return out IDENT = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]] def count(it): n = 0 while it.MoveNext(): n += 1 return n def info(path): model = W.CreateModel() reader = model.QueryReader("3mf") reader.SetStrictModeActive(False) reader.ReadFromFile(path) res = {"unit": UNITS.get(model.GetUnit().value if hasattr(model.GetUnit(), "value") else model.GetUnit(), "?")} meshes = [] it = model.GetMeshObjects() while it.MoveNext(): m = it.GetCurrentMeshObject() meshes.append([m.GetVertexCount(), m.GetTriangleCount()]) res["meshObjects"] = sorted(meshes) tot = {"tri": 0, "area": 0.0, "min": [math.inf] * 3, "max": [-math.inf] * 3} def place(rid, M, depth): if depth > 32: return try: mo = model.GetMeshObjectByID(rid) except Exception: mo = None if mo is not None: verts = mo.GetVertices() tris = mo.GetTriangleIndices() P = [] for v in verts: x, y, z = v.Coordinates[0], v.Coordinates[1], v.Coordinates[2] P.append([x * M[0][c] + y * M[1][c] + z * M[2][c] + M[3][c] for c in range(3)]) for p in P: for c in range(3): tot["min"][c] = min(tot["min"][c], p[c]) tot["max"][c] = max(tot["max"][c], p[c]) for t in tris: a, b, c = (P[i] for i in t.Indices) e1 = [b[k] - a[k] for k in range(3)] e2 = [c[k] - a[k] for k in range(3)] cx = e1[1] * e2[2] - e1[2] * e2[1] cy = e1[2] * e2[0] - e1[0] * e2[2] cz = e1[0] * e2[1] - e1[1] * e2[0] tot["area"] += math.sqrt(cx * cx + cy * cy + cz * cz) / 2 tot["tri"] += 1 return co = model.GetComponentsObjectByID(rid) for i in range(co.GetComponentCount()): comp = co.GetComponent(i) T = mat(comp.GetTransform()) if comp.HasTransform() else IDENT place(comp.GetObjectResourceID(), mul(T, M), depth + 1) items = model.GetBuildItems() n = 0 while items.MoveNext(): bi = items.GetCurrent() n += 1 T = mat(bi.GetObjectTransform()) if bi.HasObjectTransform() else IDENT place(bi.GetObjectResourceID(), T, 0) res["items"] = n res["triangles"] = tot["tri"] res["area"] = tot["area"] if tot["tri"]: res["min"], res["max"] = tot["min"], tot["max"] res["baseGroups"] = count(model.GetBaseMaterialGroups()) res["colorGroups"] = count(model.GetColorGroups()) res["textures"] = count(model.GetTexture2Ds()) res["texGroups"] = count(model.GetTexture2DGroups()) return res out = [] for p in json.loads(sys.stdin.read()): try: out.append(info(p)) except Exception as e: # noqa: BLE001 out.append({"failed": str(e)[:200]}) print(json.dumps(out))