Editing slicer 3MF projects (lib3mf-slicer-settings)
Repo: https://github.com/gmarceau/lib3mf-slicer-settings (GPL-3.0, pure
Python, mypy --strict, plumbum + pydash). Working copies live under
~/code/ (e.g. ~/code/dragon-claude-3mf uses its step1–4 pipeline).
Why
General 3MF libraries (lib3mf, trimesh) drop the slicer's own entries
(Metadata/project_settings.config, Metadata/model_settings.config,
thumbnails) when they write. This library edits the 3MF zip in place —
only rewritten entries change; every other entry keeps its exact bytes and
order.
Core recipe (OpenSCAD export → printable project)
from lib3mf_slicer_settings import (
apply_print_settings, is_slicer_project, private_paths,
put_3mf, read_mesh, read_print_settings, scrub_paths, swap_mesh,
)
report = swap_mesh("hanger_print.3mf", read_mesh("hanger.stl"))
print(report.objects, report.new_size, report.dropped_paint)
# install a fresh 3MF without losing the slicer project's settings
if (report := put_3mf("build/hanger.3mf", "hanger_print.3mf")) is not None:
print(report.objects, report.new_size, report.dropped_paint)
apply_print_settings("hanger_print.3mf", {
"wall_loops": 5, "sparse_infill_density": "40%", "layer_height": 0.2,
"outer_wall_speed": 100, # per-extruder list: value fans out to every entry
"fan_max_speed": [50],
})
print(read_print_settings("hanger_print.3mf")["wall_loops"]) # "5"
scrub_paths("hanger_print.3mf") # cut absolute source_file paths to bare names
assert private_paths("hanger_print.3mf") == [] # verify before publishing!
Rules that bite
- Always
scrub_paths+private_paths-validate before publishing — slicers record absolute import paths that leak folder structure. - Print-settings keys are the slicer's own names from
Metadata/project_settings.config(wall_loops, notwall_loop— an unknown key errors with a "did you mean" and writes nothing). Values are stored the slicer's way:True→"1",0.2→"0.2". - Changed keys are filed into
different_settings_to_system(process/filament/printer slot via thesections.jsonclassifier cached in the user cache dir;LIB3MF_PROFILESpoints at slicer profiles elsewhere). Unknown-classification keys still get set and land inreport.unclassified. - Presets stay selected — you only override, you don't replace.
swap_meshrefuses projects with objects of different bounding-box sizes (can't tell which to replace); replaces all copies when sizes match. Per- object overrides (model_settings.config) and triangle painting (supports/seams/colour) don't survive a swap —report.dropped_paintflags it.- Project layout is shared by Creality/Orca/Bambu (PrusaSlicer differs):
3D/3dmodel.model(placements),3D/Objects/object_N.model(bbox-centred meshes),Metadata/project_settings.config(JSON),Metadata/model_settings.config(XML),Metadata/plate_N.png.
Environment conventions
is_slicer_project()first; a plain 3MF (OpenSCAD export) written withput_3mfis just copied fresh — the report isNonethen.- Verify with the repo's gates:
task(pytest + mypy --strict); test helpers inlib3mf_slicer_settings.testingbuild fake projects/boxes/STLs.