Slicing from the command line with Bambu Studio (flatpak), including two-color prints
bambubambu-studioslicercliflatpak3mfmulticolorx2d
Bambu Studio can slice headlessly, which lets a script turn STLs into a ready-to-print .gcode.3mf for a Bambu printer (here Justin's Bambu Lab X2D, two nozzles). The CLI is poorly documented and fails in confusing ways; these are the rules that made it work with the flatpak build (com.bambulab.BambuStudio, 2.8.x).
Basic recipe
- Run it inside the sandbox:
flatpak run --command=bambu-studio com.bambulab.BambuStudio ...(or the app's entry point) with--env=DISPLAY=:Nso it can render the embedded plate thumbnail. A GLFW error is still printed; the slice succeeds ifresult.jsonreportsreturn_code 0. - System profiles live at
/app/share/BambuStudio/profiles/BBL/inside the sandbox. Host paths under~/.local/share/flatpak/.../files/share/...fail. --load-settingsaccepts only one process JSON, and it must be standalone. Bambu profiles inherit ("inherits": "...") through several files; partial overrides, or machine + process + override together, all error out. Walk theinheritschain inprofiles/BBL/process(andmachine,filament) yourself, merge parent first, and pass the flattened result. Do the flattening with a small Python script run inside the flatpak so it sees the same paths.- The CLI reports filament weight as 0.00; ignore it and estimate from the model volume.
Two colors (or more)
The CLI's filament count follows filament_colour, and system filament presets carry no filament_colour, so every part collapses onto filament 1: no tool changes in the G-code and only one filament in the exported project. (This was first misdiagnosed as "the CLI ignores per-part extruders".) Fix: pass flattened filament JSONs that each set filament_colour (one per color). Then either works:
- Per-part assignment: --assemble --load-filament-ids "1,2,1" with the part STLs in order; they become one "Assembly" object with per-part filaments.
- Height-based color changes on one STL: --load-custom-gcodes changes.json with
json
{"mode":"MultiAsSingle","gcodes":[{"print_z":2.2,"type":"ToolChange","extruder":2,"color":"#FFFFFF","extra":""}]}
type is a string enum; print_z is the first layer printed in the new filament.
Example: a 101.6 x 101.6 x 38.1 mm Paycom "p" logo with 2 mm green caps and a white core sliced to 1h32m, about 20 g green + 69 g white, two filament changes with a prime tower, both filaments on one nozzle (the CLI left filament_map at 1,1; forcing one color per X2D nozzle was not attempted). Without an AMS, the same print works by pausing at the layer heights and swapping filament by hand.
3MF pitfalls
- A 3MF that carries
<metadata name="Application">BambuStudio-...but lacksMetadata/project_settings.configsegfaults the CLI (exit 139). Drop that tag and it loads as a generic 3MF, keeping part names and per-part extruders (set inMetadata/model_settings.config). - An export made without
--slicecannot be re-sliced by the CLI later ("No valid nozzle found", emptyextruder_nozzle_stats). Ship the sliced export: it opens in the GUI as an editable project, and the same bytes renamed.3mfserve as the project file.
Generating projects that slice without help (3d site and orrery, 2026-10)
Generated Bambu projects for the X2D (3d.jhampton.io /create downloads and the orrery kit) needed more than flattened presets. These were found the hard way.
- The CLI does not resolve inherits/include, and this build has no *_full presets. Leaf presets saved by the CLI are padded with program defaults: start G-code of 46 characters instead of 14,848, no prime tower, infill density 0. Shipping such CLI-saved settings and calling them "genuine" was wrong by about 100 settings. Diff a few load-bearing values (start G-code length, a density, a toggle) against the source preset before claiming fidelity.
- Segfault cause, precisely: a Bambu project with no printer profile, meaning no project_settings.config or one without printer_settings_id. That is what the CLI writes itself when --load-settings is not passed. It does not crash on every Bambu project.
- It leaves printer and window state blank, and slicing fails on each in turn:
- extruder_nozzle_stats: [] gives "No valid nozzle found"; fill in Standard#1 per extruder.
- A 4x4 flush_volumes_matrix gives "Flush volumes matrix do not match"; it needs filaments squared times extruders entries, plus flush_multiplier per extruder.
- A prime tower at x = 15 on the X2D gives "Found G-code in unprintable area of multi-extruder printers" because nozzle 2 starts at x = 20.5; place it near (205, 205).
- Error messages go to stderr unprefixed between log lines. Grep for lines not starting with [; otherwise a failure looks like it had no message.
- With those fixed, generated two- and three-filament projects slice with one filament per X2D nozzle (three filaments need an AMS).
Generating settings data
tools/3d-bambu-x2d.py drives the CLI about 80 times to emit a base plus per-filament-type deltas (X2D 0.4 nozzle, "0.20mm Standard @BBL X2D", 1 to 3 filaments, ~380 KB JSON) that the browser merges into each download. Per-template overrides (enable_support: '1', brim_type: 'outer_only', wipe_tower_x/y, enable_prime_tower: 0) are merged in and listed in different_settings_to_system[0], which marks the project as modified from the system preset. See 3MF files and the Bambu Studio project layout.
More sandbox facts
- The flatpak cannot see
/tmp; work under$HOME. - "Invalid T command (T65279)" and GLFW/OpenGL errors are harmless;
--estimate-modefails ("cannot determine BBL machine tag"); host gdb cannot follow into the sandbox. - Parallel runs collide in
/tmp/bamboo_modelinside the sandbox ("Directory not empty"); serialize or retry. - Slice times repeat only to within a few seconds; re-saved projects position parts by their center, not their corner.
Measured examples (X2D, 0.4, standard)
Flat two-color cat tag 2.3 g + 0.2 g, ~15 min; ball tag in PETG with supports 13 g + 1.8 g, ~50 min; Gridfinity 2x1x3 labeled bin 24.6 g, ~50 min; orrery kit on five plates 7 h 41 min, 232 g. Whether the GUI honors embedded overrides on open, and any real print, remain unverified.
Gotchas
- Keep the slice command in a script (
slice_cmd.sh,slice_x2d.sh) next to the model generator so any geometry change can be resliced in one step. - A stray
result.jsonin the working directory is just CLI output. - Bambu Studio segfaulting repeatedly in the GUI on Linux happens; it was unrelated to CLI use.
See also
- Designing a printable replacement part from photos and calipers
- 3d.jhampton.io (print library and parametric template maker)
- Bambu Lab X2D (Justin's printer)
- 3MF files and the Bambu Studio project layout
- Blender Python scripting, safety audit and gotchas
Linked from
- 3d.jhampton.io (print library and parametric template maker)
- 3MF files and the Bambu Studio project layout
- Bambu Lab X2D (Justin's printer)
- Blender Python scripting, safety audit and gotchas
- Designing a printable replacement part from photos and calipers
- Designing printable involute gear trains (the orrery case)
- Working with Justin as an AI agent (ask first, confirm the target, precise verbs, artifacts over essays)