bambu-printer-mcp 1.1.70 → 1.1.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Every error code the Bambu Studio CLI can exit with, from libslic3r/Utils.hpp:21-75 of Bambu Studio
3
+ * 02.08.02.61 (OrcaSlicer's CLI shares them). The names and numbers are Studio's; the sentences are ours.
4
+ * The process exits with the code's low byte (-50 exits as 206). The CLI's own result.json, when present and
5
+ * fresh, is still the better reason; this table is for when it is missing or stale.
6
+ *
7
+ * tests/slicer/cli-exit-codes.test.mjs checks the names and numbers against Utils.hpp's list.
8
+ */
9
+ export const BAMBU_CLI_EXIT_CODES: Record<number, { name: string; sentence: string }> = {
10
+ [-1]: { name: "CLI_ENVIRONMENT_ERROR", sentence: "the slicer could not set up its working environment" },
11
+ [-2]: { name: "CLI_INVALID_PARAMS", sentence: "the slicer was given options it does not accept" },
12
+ [-3]: { name: "CLI_FILE_NOTFOUND", sentence: "an input file was not found" },
13
+ [-4]: { name: "CLI_FILELIST_INVALID_ORDER", sentence: "the input files were in the wrong order (a 3MF has to come first)" },
14
+ [-5]: { name: "CLI_CONFIG_FILE_ERROR", sentence: "a settings file could not be read" },
15
+ [-6]: { name: "CLI_DATA_FILE_ERROR", sentence: "the model file could not be read" },
16
+ [-7]: { name: "CLI_INVALID_PRINTER_TECH", sentence: "the printer settings are not for a filament (FDM) printer" },
17
+ [-8]: { name: "CLI_UNSUPPORTED_OPERATION", sentence: "the slicer does not support that operation" },
18
+ [-9]: { name: "CLI_COPY_OBJECTS_ERROR", sentence: "making copies of the objects failed" },
19
+ [-10]: { name: "CLI_SCALE_TO_FIT_ERROR", sentence: "scaling the model to fit the bed failed" },
20
+ [-11]: { name: "CLI_EXPORT_STL_ERROR", sentence: "writing the STL file failed" },
21
+ [-12]: { name: "CLI_EXPORT_OBJ_ERROR", sentence: "writing the OBJ file failed" },
22
+ [-13]: { name: "CLI_EXPORT_3MF_ERROR", sentence: "writing the 3MF file failed" },
23
+ [-14]: { name: "CLI_OUT_OF_MEMORY", sentence: "the slicer ran out of memory" },
24
+ [-15]: { name: "CLI_3MF_NOT_SUPPORT_MACHINE_CHANGE", sentence: "this 3MF can't be moved to the chosen printer" },
25
+ [-16]: { name: "CLI_3MF_NEW_MACHINE_NOT_SUPPORTED", sentence: "the chosen printer can't be used with this 3MF" },
26
+ [-17]: { name: "CLI_PROCESS_NOT_COMPATIBLE", sentence: "the process settings don't fit the chosen printer" },
27
+ [-18]: { name: "CLI_INVALID_VALUES_IN_3MF", sentence: "the 3MF holds settings values the slicer rejects" },
28
+ [-19]: { name: "CLI_POSTPROCESS_NOT_SUPPORTED", sentence: "the settings use post-processing scripts, which the slicer CLI doesn't run" },
29
+ [-20]: { name: "CLI_PRINTABLE_SIZE_REDUCED", sentence: "the chosen printer's bed is smaller than the one the settings were made for" },
30
+ [-21]: { name: "CLI_OBJECT_ARRANGE_FAILED", sentence: "arranging the objects on the plate failed" },
31
+ [-22]: { name: "CLI_OBJECT_ORIENT_FAILED", sentence: "turning the objects to print well failed" },
32
+ [-23]: { name: "CLI_MODIFIED_PARAMS_TO_PRINTER", sentence: "the printer settings change its bed area, height or excluded area, which isn't allowed" },
33
+ [-24]: { name: "CLI_FILE_VERSION_NOT_SUPPORTED", sentence: "the 3MF was saved by a slicer version this one can't read (often a beta)" },
34
+ [-25]: { name: "CLI_3MF_FEATURE_NOT_SUPPORTED", sentence: "the 3MF uses an experimental feature this slicer doesn't support yet" },
35
+ [-50]: { name: "CLI_NO_SUITABLE_OBJECTS", sentence: "a plate is empty or has no object fully inside it; arrange the model or place it on the plate" },
36
+ [-51]: { name: "CLI_VALIDATE_ERROR", sentence: "the slicing settings failed the slicer's own checks" },
37
+ [-52]: { name: "CLI_OBJECTS_PARTLY_INSIDE", sentence: "objects extend over the bed boundary" },
38
+ [-53]: { name: "CLI_EXPORT_CACHE_DIRECTORY_CREATE_FAILED", sentence: "the slicer couldn't create its cache folder" },
39
+ [-54]: { name: "CLI_EXPORT_CACHE_WRITE_FAILED", sentence: "the slicer couldn't write its cache" },
40
+ [-55]: { name: "CLI_IMPORT_CACHE_NOT_FOUND", sentence: "the slicer's cache wasn't found" },
41
+ [-56]: { name: "CLI_IMPORT_CACHE_DATA_CAN_NOT_USE", sentence: "the slicer's cache couldn't be read" },
42
+ [-57]: { name: "CLI_IMPORT_CACHE_LOAD_FAILED", sentence: "loading the slicer's cache failed" },
43
+ [-58]: { name: "CLI_SLICING_TIME_EXCEEDS_LIMIT", sentence: "slicing a plate took longer than the time limit; simplify the model or use a taller layer height" },
44
+ [-59]: { name: "CLI_TRIANGLE_COUNT_EXCEEDS_LIMIT", sentence: "a plate has more triangles than the limit; simplify the model" },
45
+ [-60]: { name: "CLI_NO_SUITABLE_OBJECTS_AFTER_SKIP", sentence: "no printable objects remain after skipping" },
46
+ [-61]: { name: "CLI_FILAMENT_NOT_MATCH_BED_TYPE", sentence: "a filament can't be printed on the chosen plate type" },
47
+ [-62]: { name: "CLI_FILAMENTS_DIFFERENT_TEMP", sentence: "the filaments' temperatures differ too much" },
48
+ [-63]: { name: "CLI_OBJECT_COLLISION_IN_SEQ_PRINT", sentence: "objects collide in print-by-object mode" },
49
+ [-64]: { name: "CLI_OBJECT_COLLISION_IN_LAYER_PRINT", sentence: "objects collide" },
50
+ [-65]: { name: "CLI_SPIRAL_MODE_INVALID_PARAMS", sentence: "some settings can't be used in spiral vase mode" },
51
+ [-66]: { name: "CLI_FILAMENT_CAN_NOT_MAP", sentence: "some filaments can't be assigned to the printer's nozzles" },
52
+ [-67]: { name: "CLI_ONLY_ONE_TPU_SUPPORTED", sentence: "only one TPU filament can be used in a print" },
53
+ [-68]: { name: "CLI_FILAMENTS_NOT_SUPPORTED_BY_EXTRUDER", sentence: "a filament can't be printed by the nozzle it is assigned to" },
54
+ [-100]: { name: "CLI_SLICING_ERROR", sentence: "slicing the model failed" },
55
+ [-101]: { name: "CLI_GCODE_PATH_CONFLICTS", sentence: "the print paths of two objects overlap; move them apart, or move the wipe tower away from the models" },
56
+ [-102]: { name: "CLI_GCODE_PATH_IN_UNPRINTABLE_AREA", sentence: "the print reaches an area one of the printer's nozzles can't print" },
57
+ [-103]: { name: "CLI_FILAMENT_UNPRINTABLE_ON_FIRST_LAYER", sentence: "a filament can't be printed on the first layer of this plate type" },
58
+ [-104]: { name: "CLI_GCODE_PATH_OUTSIDE", sentence: "the print goes outside the printable area, usually through a support, brim, skirt or the wipe tower" },
59
+ [-105]: { name: "CLI_GCODE_IN_WRAPPING_DETECT_AREA", sentence: "the print goes into the bed area the printer keeps clear for its clumping (wrapping) check" },
60
+ };
61
+
62
+ /** The table's entry for a process exit status (the code's low byte) or a full negative code. */
63
+ export function bambuCliExitCode(exitCode: unknown): { code: number; name: string; sentence: string } | undefined {
64
+ if (typeof exitCode !== "number" || !Number.isInteger(exitCode) || exitCode === 0) return undefined;
65
+ const code = Object.keys(BAMBU_CLI_EXIT_CODES).map(Number)
66
+ .find(value => value === exitCode || (value & 0xff) === exitCode);
67
+ return code === undefined ? undefined : { code, ...BAMBU_CLI_EXIT_CODES[code] };
68
+ }
@@ -7,11 +7,13 @@ import JSZip from "jszip";
7
7
  * (M400 U1 on Bambu printers) before the chosen layer, so the printer pauses
8
8
  * by itself there and waits for Resume; no remote command is involved.
9
9
  *
10
- * The CLI takes these as --load-custom-gcodes <json> (CustomGCode::Info). It
11
- * does not apply the pauses a project 3MF already carries in
12
- * Metadata/custom_gcode_per_print_z.xml, so those are read here and passed the
13
- * same way. A pause's print_z must be the exact top of its layer: a height
14
- * between layers lands on the layer below.
10
+ * The CLI takes these as --load-custom-gcodes <json> (CustomGCode::Info). A
11
+ * project 3MF keeps its own pauses and colour changes in
12
+ * Metadata/custom_gcode_per_layer.xml (Bambu Studio 02.08.02.61 bbs_3mf.cpp:184,
13
+ * written at :8943-8990), which the CLI loads with the project. The JSON
14
+ * replaces that whole map (BambuStudio.cpp:4005-4008), so the plate's own
15
+ * entries are read here and merged in. A pause's print_z must be the exact top
16
+ * of its layer: a height between layers lands on the layer below.
15
17
  */
16
18
 
17
19
  /** CustomGCode::Type, in the order bbs_3mf.cpp writes it as an integer. */
@@ -61,7 +63,7 @@ export async function readProjectCustomGcodes(threeMfPath: string, plate = 1): P
61
63
  } catch {
62
64
  return undefined;
63
65
  }
64
- const file = zip.file(/^Metadata\/custom_gcode_per_print_z\.xml$/i)[0];
66
+ const file = zip.file(/^Metadata\/custom_gcode_per_layer\.xml$/i)[0];
65
67
  if (!file) return undefined;
66
68
  const xml = await file.async("string");
67
69
  for (const block of xml.match(/<plate>[\s\S]*?<\/plate>/g) ?? []) {
@@ -11,8 +11,10 @@ import { EventEmitter } from 'events';
11
11
  import * as crypto from 'crypto';
12
12
  import { execFile } from 'child_process';
13
13
  import { keepSlice, restoreSlice, sliceCacheKey } from '../slicer/slice-cache.js';
14
+ import { listModelPlates } from '../counterpart/model-plates.js';
14
15
  import { flattenForCli, detectProfilesRoot, resolveBblMachineProfile } from '../slicer/profile-flatten.js';
15
16
  import { plainSlicerError, UNKNOWN_SLICER_ERROR } from '../slicer/slicer-messages.js';
17
+ import { bambuCliExitCode } from '../slicer/cli-exit-codes.js';
16
18
  import { applyProcessOverrides, type ProcessOverrides } from '../slicer/process-overrides.js';
17
19
  import { profileJsonFiles } from '../slicer/profile-files.js';
18
20
  import { materializeOrcaProfilesRoot, stageOrcaCliProfiles } from '../slicer/orca-profile-cache.js';
@@ -26,11 +28,13 @@ const BAMBU_PROFILE_ROOTS = [
26
28
  '/Applications/BambuStudio.app/Contents/Resources/profiles/BBL',
27
29
  '/Applications/OrcaSlicer.app/Contents/Resources/profiles/BBL',
28
30
  ];
31
+ // curr_bed_type's enum keys (Bambu Studio 02.08.02.61 libslic3r/PrintConfig.cpp:437-445).
29
32
  const BAMBU_CLI_BED_TYPES: Record<string, string> = {
30
33
  textured_plate: 'Textured PEI Plate',
31
34
  cool_plate: 'Cool Plate',
32
35
  engineering_plate: 'Engineering Plate',
33
36
  hot_plate: 'High Temp Plate',
37
+ supertack_plate: 'Supertack Plate',
34
38
  };
35
39
 
36
40
  // Define types for progress tracking
@@ -291,38 +295,14 @@ function describeSlicerFailure(
291
295
  return `Slicer failed: the slicer ${status}${reason ? ` (${reason})` : ""}. ${error.message.trim()}${output ? `\nSlicer output: ${output}` : ""}`;
292
296
  }
293
297
 
294
- // Bambu Studio and OrcaSlicer CLI errors (libslic3r/Utils.hpp). The process
295
- // exits with the low byte of the code; result.json names the error.
296
- const BAMBU_CLI_ERRORS: Record<number, string> = {
297
- [-2]: "invalid parameters to the slicer",
298
- [-3]: "input files not found",
299
- [-5]: "preset file cannot be parsed",
300
- [-6]: "model file cannot be parsed",
301
- [-9]: "copying objects failed",
302
- [-17]: "printer is not compatible with the process preset",
303
- [-21]: "auto-arranging objects failed",
304
- [-22]: "auto-orienting objects failed",
305
- [-50]: "a plate is empty or has no object fully inside it; arrange the model or place it on the plate",
306
- [-51]: "incorrect slicing parameters",
307
- [-52]: "objects extend over the bed boundary",
308
- [-60]: "no printable objects remain after skipping",
309
- [-61]: "filaments are not compatible with the plate type",
310
- [-62]: "the filaments' temperatures differ too much",
311
- [-63]: "objects collide in print-by-object mode",
312
- [-64]: "objects collide",
313
- [-100]: "slicing the model failed",
314
- [-101]: "G-code conflicts after slicing",
315
- [-102]: "G-code in an unprintable area",
316
- };
317
-
318
298
  function bambuCliFailureReason(exitCode: unknown, cli: CliResultSource): string | undefined {
319
299
  const result = readCliResult(cli);
320
300
  if (typeof result?.error_string === "string" && result.error_string.trim()) {
321
301
  return `${result.error_string.trim()} [${result.return_code}]`;
322
302
  }
323
303
  if (typeof exitCode !== "number") return undefined;
324
- const code = Object.keys(BAMBU_CLI_ERRORS).map(Number).find(value => (value & 0xff) === exitCode);
325
- return code === undefined ? undefined : `${BAMBU_CLI_ERRORS[code]} [${code}]`;
304
+ const known = bambuCliExitCode(exitCode);
305
+ return known ? `${known.sentence} [${known.code}]` : undefined;
326
306
  }
327
307
 
328
308
  export class STLManipulator extends EventEmitter {
@@ -816,11 +796,6 @@ export class STLManipulator extends EventEmitter {
816
796
  private resolveBambuStudioBedType(bedType?: string): string | undefined {
817
797
  if (!bedType) return undefined;
818
798
  const normalized = bedType.trim().toLowerCase();
819
- if (normalized === 'supertack_plate') {
820
- throw new Error(
821
- 'BambuStudio CLI SuperTack bed type is not verified; use a pre-sliced 3MF or choose textured_plate, cool_plate, engineering_plate, or hot_plate.'
822
- );
823
- }
824
799
  return BAMBU_CLI_BED_TYPES[normalized] || bedType;
825
800
  }
826
801
 
@@ -1786,34 +1761,38 @@ export class STLManipulator extends EventEmitter {
1786
1761
  this.sliceSTLOnce(stlFilePath, slicerType, slicerPath, slicerProfile, progressCallback, printerPreset, options);
1787
1762
  const layers = bambuOptions?.pauseAtLayers?.length ? bambuOptions.pauseAtLayers : undefined;
1788
1763
  const cli = slicerType === 'bambustudio' || slicerType === 'orcaslicer' || slicerType === 'orcaslicer-bambulab';
1789
- const plate = bambuOptions?.slicePlate && bambuOptions.slicePlate > 0 ? bambuOptions.slicePlate : 1;
1790
- // The CLI drops a project's own pauses and colour changes; carry them over.
1791
- const project = cli && /\.3mf$/i.test(stlFilePath) ? await readProjectCustomGcodes(stlFilePath, plate) : undefined;
1792
- if (!layers && !project?.gcodes.length) return once(bambuOptions);
1793
- if (layers && !cli) {
1764
+ // Without new pauses the CLI keeps the project's own pauses and colour changes itself: it loads a
1765
+ // Bambu 3MF's Metadata/custom_gcode_per_layer.xml with the project (Bambu Studio 02.08.02.61
1766
+ // BambuStudio.cpp:1882, bbs_3mf.cpp:2051), so nothing is passed.
1767
+ if (!layers) return once(bambuOptions);
1768
+ if (!cli) {
1794
1769
  throw new Error(`Pausing at a layer needs the Bambu Studio or OrcaSlicer CLI, not ${slicerType}.`);
1795
1770
  }
1771
+ const chosenPlate = bambuOptions?.slicePlate && bambuOptions.slicePlate > 0 ? bambuOptions.slicePlate : undefined;
1772
+ const plate = chosenPlate ?? 1;
1773
+ const threeMf = /\.3mf$/i.test(stlFilePath);
1774
+ // --load-custom-gcodes replaces the whole per-plate map and holds one plate (--slice N, or plate 1 for
1775
+ // --slice 0: BambuStudio.cpp:2224-2233, 4005-4008), so slicing every plate would drop plates 2..n's own.
1776
+ if (threeMf && chosenPlate === undefined && (await listModelPlates(stlFilePath)).plate_count > 1) {
1777
+ throw new Error('Pausing at a layer works on one plate at a time. Choose the plate with slice_plate.');
1778
+ }
1779
+ // The map replaces the project's own entries for this plate, so they are merged in.
1780
+ const project = threeMf ? await readProjectCustomGcodes(stlFilePath, plate) : undefined;
1796
1781
  const customGcodesFile = path.join(this.tempDir, `custom-gcodes-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
1797
1782
  try {
1798
- let tops: number[] = [];
1799
- if (layers) {
1800
- // A pause sits at its layer's exact top Z; learn the tops from a slice without pauses.
1801
- if (progressCallback) progressCallback(10, `Finding layer heights for the pause before layer ${layers.join(", ")}`);
1802
- const plain = await once({ ...bambuOptions, pauseAtLayers: undefined, customGcodesFile: undefined });
1803
- const gcode = await readPlateGcode(plain.path, plate);
1804
- if (!gcode) throw new Error('The slicer wrote no plate G-code, so the pause layers cannot be placed.');
1805
- tops = layerTops(gcode);
1806
- }
1807
- const info = layers ? customGcodesWithPauses(tops, layers, project) : project!;
1808
- await fs.promises.writeFile(customGcodesFile, JSON.stringify(info));
1783
+ // A pause sits at its layer's exact top Z; learn the tops from a slice without pauses.
1784
+ if (progressCallback) progressCallback(10, `Finding layer heights for the pause before layer ${layers.join(", ")}`);
1785
+ const plain = await once({ ...bambuOptions, pauseAtLayers: undefined, customGcodesFile: undefined });
1786
+ const plainGcode = await readPlateGcode(plain.path, plate);
1787
+ if (!plainGcode) throw new Error('The slicer wrote no plate G-code, so the pause layers cannot be placed.');
1788
+ const tops = layerTops(plainGcode);
1789
+ await fs.promises.writeFile(customGcodesFile, JSON.stringify(customGcodesWithPauses(tops, layers, project)));
1809
1790
  const result = await once({ ...bambuOptions, customGcodesFile });
1810
- if (layers) {
1811
- const gcode = await readPlateGcode(result.path, plate);
1812
- const placed = gcode ? pausedLayers(gcode, machinePauseGcode(gcode)) : [];
1813
- const missing = layers.filter(layer => !placed.includes(layer));
1814
- if (!gcode || missing.length || layerTops(gcode).length !== tops.length) {
1815
- throw new Error(`The slicer did not place the pause before layer ${(missing.length ? missing : layers).join(", ")}. Nothing was sent to the printer.`);
1816
- }
1791
+ const gcode = await readPlateGcode(result.path, plate);
1792
+ const placed = gcode ? pausedLayers(gcode, machinePauseGcode(gcode)) : [];
1793
+ const missing = layers.filter(layer => !placed.includes(layer));
1794
+ if (!gcode || missing.length || layerTops(gcode).length !== tops.length) {
1795
+ throw new Error(`The slicer did not place the pause before layer ${(missing.length ? missing : layers).join(", ")}. Nothing was sent to the printer.`);
1817
1796
  }
1818
1797
  return result;
1819
1798
  } finally {
package/src/types.ts CHANGED
@@ -87,13 +87,19 @@ export interface ThreeMFAmsRequirements {
87
87
  }
88
88
 
89
89
  export interface ThreeMFPlateObject {
90
+ /** The instance's identify_id: the id the printer's skip_objects command takes. */
90
91
  id: number;
91
92
  name: string;
93
+ /** Already skipped in this file (slice_info's skipped="true"). */
94
+ skipped: boolean;
95
+ /** First-layer area and box from plate_N.json, only when the name is unique on the plate. */
92
96
  area: number | null;
93
97
  bbox: unknown;
94
98
  }
95
99
 
96
100
  export interface ThreeMFPlateObjects {
97
101
  plateIndex: number;
102
+ /** The plate was sliced with object labels, which the printer needs to skip objects. */
103
+ labelObjectEnabled: boolean;
98
104
  objects: ThreeMFPlateObject[];
99
105
  }