bambu-printer-mcp 1.1.71 → 1.1.73

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.
@@ -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
  }