"""Combine STL parts into one multi-part 3MF for OrcaSlicer / PrusaSlicer. python export_3mf.py (from hardware/openscad, after the STL export) Writes stl/remote_v2/remote_v2_top_multicolor.3mf: the top shell with its two inlays as ONE object with three parts, in the same frame, each part on its own extruder/filament (1 = case, 2 = honeycomb, 3 = ON/OFF text). The part list is in PARTS below. Uses the PrusaSlicer 3MF layout (one mesh, the parts as triangle ranges in Metadata/Slic3r_PE_model.config), which OrcaSlicer and PrusaSlicer both read. """ import os import zipfile HERE = os.path.dirname(os.path.abspath(__file__)) OUT_DIR = os.path.join(HERE, "stl", "remote_v2") # (file, part name, extruder) PARTS = [ ("remote_v2_top.stl", "top shell (black)", 1), ("remote_v2_inlay.stl", "honeycomb inlay (red)", 2), ("remote_v2_text.stl", "ON-OFF inlay (white)", 3), ] OUT = "remote_v2_top_multicolor.3mf" def read_stl(path): """Triangles of an ASCII or binary STL as lists of 3 vertex tuples.""" data = open(path, "rb").read() tris = [] if data[:5] == b"solid" and b"facet" in data[:400]: verts = [] for line in data.decode("ascii", "replace").splitlines(): t = line.split() if t and t[0] == "vertex": verts.append(tuple(float(x) for x in t[1:4])) if len(verts) == 3: tris.append(verts) verts = [] else: import struct n = struct.unpack_from("', '', ' 1', ' remote_v2 top shell with inlays', ' ', ' ', ' ', ' '] model += [' ' % v for v in vertices] model += [' ', ' '] model += [' ' % tuple(t) for t in triangles] model += [' ', ' ', ' ', ' ', ' ', ' ', ' ', ''] config = ['', '', ' ', ' '] for first, last, name, extruder in ranges: config += [' ' % (first, last), ' ' % name, ' ', ' ' % extruder, ' '] config += [' ', ''] content_types = ('\n' '\n' ' \n' ' \n' ' \n' '\n') rels = ('\n' '\n' ' \n' '\n') path = os.path.join(OUT_DIR, OUT) with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z: z.writestr("[Content_Types].xml", content_types) z.writestr("_rels/.rels", rels) z.writestr("3D/3dmodel.model", "\n".join(model) + "\n") z.writestr("Metadata/Slic3r_PE_model.config", "\n".join(config) + "\n") print("%s: %d vertices, %d triangles, parts %s" % (OUT, len(vertices), len(triangles), ", ".join("%s %d-%d" % (r[2], r[0], r[1]) for r in ranges))) if __name__ == "__main__": main()