bambu-printer-mcp 1.1.93 → 1.1.94

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.
@@ -36,6 +36,22 @@ export interface PlateFilament {
36
36
  name?: string;
37
37
  }
38
38
 
39
+ /**
40
+ * One part on a plate, as the open 3MF standard places it, so a client draws it in its filament's colour without
41
+ * reading slicer files: the mesh object `object_id`, in the model part `path` when that is not the root
42
+ * 3D/3dmodel.model (production extension p:path, as the file names it), and the 1-based project filament it prints
43
+ * with. `painted` is true when the slicer's painting (paint_color) recolours some of its facets.
44
+ */
45
+ export interface PlatePart {
46
+ object_id: number;
47
+ /** The root object (a build item's objectid) this part belongs to: copies of one mesh may print in other filaments. */
48
+ build_object_id: number;
49
+ path?: string;
50
+ name: string;
51
+ filament: number;
52
+ painted?: true;
53
+ }
54
+
39
55
  const MAX_TEXT_BYTES = 8 * 1024 * 1024;
40
56
  const MAX_MESH_BYTES = 64 * 1024 * 1024;
41
57
  const MAX_MESH_TOTAL = 256 * 1024 * 1024;
@@ -54,11 +70,12 @@ const metadataOf = (block: string): Settings => new Map([...block.matchAll(/<met
54
70
  /** model_settings.config objects with their own settings and parts (parts never nest). */
55
71
  export function readModelObjects(settings: string): ObjectEntry[] {
56
72
  return [...settings.matchAll(/<object\b[^>]*\bid="(\d+)"[^>]*>([\s\S]*?)<\/object>/g)].map(match => {
57
- const parts = [...match[2].matchAll(/<part\b([^>]*)>([\s\S]*?)<\/part>/g)].map(part => {
58
- const own = metadataOf(part[2]);
73
+ // A part may be self-closing (<part id="1" subtype="normal_part"/>): it has no settings of its own.
74
+ const parts = [...match[2].matchAll(/<part\b([^>]*?)(?:\/>|>([\s\S]*?)<\/part>)/g)].map(part => {
75
+ const own = metadataOf(part[2] ?? "");
59
76
  return { id: /\bid="(\d+)"/.exec(part[1])?.[1] ?? "", subtype: /\bsubtype="([^"]+)"/.exec(part[1])?.[1] ?? "normal_part", name: words(own.get("name") ?? ""), settings: own };
60
77
  });
61
- const own = metadataOf(match[2].replace(/<part\b[^>]*>[\s\S]*?<\/part>/g, ""));
78
+ const own = metadataOf(match[2].replace(/<part\b[^>]*?(?:\/>|>[\s\S]*?<\/part>)/g, ""));
62
79
  return { id: match[1], name: words(own.get("name") ?? ""), settings: own, parts };
63
80
  });
64
81
  }
@@ -176,6 +193,8 @@ export interface PlateLayout {
176
193
  instances?: { objectId: string; instanceId: number }[];
177
194
  /** Metadata/plate_N.json filament_ids (0-based) when the file holds this plate's slice. */
178
195
  slicedFilamentIds?: number[];
196
+ /** The file has no plate metadata: this one plate holds every build item. */
197
+ allItems?: boolean;
179
198
  }
180
199
 
181
200
  /**
@@ -183,7 +202,7 @@ export interface PlateLayout {
183
202
  * 3D/3dmodel.model text.
184
203
  */
185
204
  export async function plateFilaments(zip: JSZip, settings: string, model: string, plates: PlateLayout[], limits: PaintLimits = PAINT_LIMITS)
186
- : Promise<{ plates: Map<number, PlateFilament[]>; incomplete: Set<number>; project: PlateFilament[] }> {
205
+ : Promise<{ plates: Map<number, PlateFilament[]>; incomplete: Set<number>; project: PlateFilament[]; buildItems: Map<number, number[]>; parts: Map<number, PlatePart[]> }> {
187
206
  const text = async (name: string) => {
188
207
  const file = zip.file(name);
189
208
  if (!file) return "";
@@ -203,7 +222,10 @@ export async function plateFilaments(zip: JSZip, settings: string, model: string
203
222
  const objects = new Map(readModelObjects(settings).map(object => [object.id, object]));
204
223
  // Root objects' components: which sub-object mesh in which file each part is (part id = component objectid).
205
224
  const components = new Map<string, Map<string, string>>();
225
+ // Root objects that are meshes themselves (a plain 3MF): their own id is the part.
226
+ const rootMeshes = new Set<string>();
206
227
  for (const object of model.matchAll(/<(?:\w+:)?object\b[^>]*\bid="(\d+)"[^>]*>([\s\S]*?)<\/(?:\w+:)?object>/g)) {
228
+ if (/<(?:\w+:)?mesh[\s>]/.test(object[2])) rootMeshes.add(object[1]);
207
229
  const parts = new Map<string, string>();
208
230
  for (const component of object[2].matchAll(/<(?:\w+:)?component\b([^>]*)\/?>/g)) {
209
231
  const path = /\b(?:\w+:)?path="\/?([^"]+)"/.exec(component[1])?.[1];
@@ -251,10 +273,14 @@ export async function plateFilaments(zip: JSZip, settings: string, model: string
251
273
  .map(layer => Number(/\bextruder="(\d+)"/.exec(layer[1])?.[1])));
252
274
  }
253
275
 
254
- // Painting only matters for parts on a plate that is not sliced yet.
276
+ // Painting decides an unsliced plate's filaments, and marks painted parts on every plate. Unsliced plates' meshes
277
+ // come first, so the read budget goes to the filament lists before the painted marks.
255
278
  const paintPaths = new Set<string>();
256
- for (const plate of plates) if (!plate.slicedFilamentIds) for (const objectId of plate.objectIds)
279
+ for (const sliced of [false, true]) for (const plate of plates) if (!!plate.slicedFilamentIds === sliced) for (const objectId of plate.objectIds)
257
280
  for (const path of components.get(objectId)?.values() ?? []) paintPaths.add(path);
281
+ // A build item whose object is itself the mesh (a plain 3MF) keeps its painting in the root model.
282
+ const ROOT_MODEL = "3D/3dmodel.model";
283
+ if (buildItems.some(item => rootMeshes.has(/\bobjectid="(\d+)"/.exec(item)?.[1] ?? ""))) paintPaths.add(ROOT_MODEL);
258
284
  const { painting, unread } = await readPainting(zip, paintPaths, limits);
259
285
  // Plates with a part whose painting couldn't be read: they list every declared filament and say they're incomplete.
260
286
  const incomplete = new Set<number>();
@@ -262,7 +288,43 @@ export async function plateFilaments(zip: JSZip, settings: string, model: string
262
288
  const global = (key: string) => intOf(config[key]) ?? 0;
263
289
  const FEATURES = ["wall_filament", "sparse_infill_filament", "solid_infill_filament"] as const;
264
290
  const result = new Map<number, PlateFilament[]>();
291
+ const plateItems = new Map<number, number[]>();
292
+ const plateParts = new Map<number, PlatePart[]>();
293
+ const itemObject = buildItems.map(item => /\bobjectid="(\d+)"/.exec(item)?.[1]);
294
+ // The plate's copies as root build items (0-based, in build order): a copy is its object's instance_id-th item.
295
+ const plateItemsOf = (plate: PlateLayout) => {
296
+ const items = new Set<number>();
297
+ if (plate.instances?.length) for (const copy of plate.instances) {
298
+ let seen = -1;
299
+ const index = itemObject.findIndex(objectId => objectId === copy.objectId && ++seen === copy.instanceId);
300
+ if (index >= 0) items.add(index);
301
+ }
302
+ else itemObject.forEach((objectId, index) => { if (objectId && (plate.allItems || plate.objectIds.includes(objectId))) items.add(index); });
303
+ return items;
304
+ };
265
305
  for (const plate of plates) {
306
+ const items = plateItemsOf(plate);
307
+ plateItems.set(plate.number, [...items].sort((a, b) => a - b));
308
+ // Each model part of the plate's objects once, in its own filament: what a client colours when it draws the plate.
309
+ const drawn: PlatePart[] = [];
310
+ for (const objectId of [...new Set([...items].map(index => itemObject[index]!))]) {
311
+ const object = objects.get(objectId);
312
+ const meshes = components.get(objectId);
313
+ const objectFilament = intOf(object?.settings.get("extruder")) || (global("wall_filament") > 0 ? global("wall_filament") : 1);
314
+ const entry = (id: string, name: string, filament: number) => {
315
+ const path = meshes?.get(id) ?? (id === objectId && rootMeshes.has(objectId) ? ROOT_MODEL : undefined);
316
+ const paint = path ? painting.get(`${path}#${id}`) : undefined;
317
+ drawn.push({ object_id: Number(id), build_object_id: Number(objectId), ...(path && path !== "3D/3dmodel.model" ? { path: `/${path}` } : {}), name,
318
+ filament: valid(filament) ? filament : 1, ...(paint?.states.size ? { painted: true as const } : {}) });
319
+ };
320
+ if (object?.parts.length) {
321
+ for (const part of object.parts) if (part.subtype === "normal_part" && /^\d+$/.test(part.id))
322
+ entry(part.id, part.name || object.name, intOf(part.settings.get("extruder")) || objectFilament);
323
+ } else if (meshes) for (const id of meshes.keys()) entry(id, object?.name ?? "", objectFilament);
324
+ else if (rootMeshes.has(objectId)) entry(objectId, object?.name ?? "", objectFilament);
325
+ }
326
+ plateParts.set(plate.number, drawn.slice(0, 1000));
327
+
266
328
  const used = new Map<number, string | undefined>();
267
329
  const add = (index: number | undefined, name?: string) => {
268
330
  if (!valid(index)) return;
@@ -329,6 +391,12 @@ export async function plateFilaments(zip: JSZip, settings: string, model: string
329
391
  // else filament 1 (ModelVolume::extruder_id's default).
330
392
  if (!used.size && (plate.objectIds.some(id => !objects.has(id)) || (!objects.size && buildItems.length)))
331
393
  for (const key of FEATURES) add(global(key) > 0 ? global(key) : 1);
394
+ // Painting on such an object's own mesh prints its painted filaments.
395
+ for (const index of items) {
396
+ const objectId = itemObject[index];
397
+ if (objectId && !objects.get(objectId)?.parts.length && rootMeshes.has(objectId))
398
+ for (const state of painting.get(`${ROOT_MODEL}#${objectId}`)?.states ?? []) add(state, objects.get(objectId)?.name || undefined);
399
+ }
332
400
  // A mixed filament slot prints as its component filaments (PartPlate.cpp:1437-1449, expand_mixed_filaments in
333
401
  // FilamentMixer.cpp:460-478; components are 1-based, comma-separated, FilamentMixer.cpp:165-180).
334
402
  const mixed = Array.isArray(config.filament_is_mixed) ? (config.filament_is_mixed as unknown[]).map(value => boolOf(value) === true) : [];
@@ -350,5 +418,5 @@ export async function plateFilaments(zip: JSZip, settings: string, model: string
350
418
  result.set(plate.number, [...used.entries()].sort((a, b) => a[0] - b[0]).map(([index, name]) => describe(index, project, name)));
351
419
  }
352
420
  // Every filament the project declares, used on a plate or not: slice_stl takes one profile per declared filament.
353
- return { plates: result, incomplete, project: Array.from({ length: declared }, (_, i) => describe(i + 1, project)) };
421
+ return { plates: result, incomplete, project: Array.from({ length: declared }, (_, i) => describe(i + 1, project)), buildItems: plateItems, parts: plateParts };
354
422
  }
package/src/index.ts CHANGED
@@ -2603,7 +2603,7 @@ class BambuPrinterMCPServer {
2603
2603
  },
2604
2604
  {
2605
2605
  name: "list_3mf_plates",
2606
- description: "List a Bambu Studio or OrcaSlicer 3MF's plates as its designer laid them out, before slicing: plate_count, and for each plate its index (from 0; slice_stl's slice_plate is index + 1, inspect_print_file's plate_index is index), name, the objects on it, each object copy's identify_id (instances: the ids slice_stl's skip_objects takes) whether the file already holds its G-code (sliced), and filaments: the project filaments that plate prints with and only those (index from 1 = slice_stl's load_filaments/filament_colours position and print_3mf's ams_mapping position + 1; colour, material, filament_id, and the first part using it), from the slice when there is one, otherwise worked out as Bambu Studio does from the plate's parts, painting, support, layer ranges and tool changes. filaments_complete is false when a part's mesh was too large to read its painting: that plate then lists every declared filament, so offer them all rather than miss one that prints. project_filaments lists every filament the project declares, used or not (slice_stl takes one load_filaments profile per declared filament). A plain 3MF without plate metadata is one plate. include_thumbnails adds each plate's own preview from the file (thumbnail_png_base64). Read-only.",
2606
+ description: "List a Bambu Studio or OrcaSlicer 3MF's plates as its designer laid them out, before slicing: plate_count, and for each plate its index (from 0; slice_stl's slice_plate is index + 1, inspect_print_file's plate_index is index), name, the objects on it, each object copy's identify_id (instances: the ids slice_stl's skip_objects takes) whether the file already holds its G-code (sliced), and filaments: the project filaments that plate prints with and only those (index from 1 = slice_stl's load_filaments/filament_colours position and print_3mf's ams_mapping position + 1; colour, material, filament_id, and the first part using it), from the slice when there is one, otherwise worked out as Bambu Studio does from the plate's parts, painting, support, layer ranges and tool changes. filaments_complete is false when a part's mesh was too large to read its painting: that plate then lists every declared filament, so offer them all rather than miss one that prints. project_filaments lists every filament the project declares, used or not (slice_stl takes one load_filaments profile per declared filament). build_items: the root 3D/3dmodel.model build items on the plate (0-based, build order); parts: each model part of the plate's objects once, with object_id (the mesh object), path when that mesh is in another model part (p:path), build_object_id (the root object a build item places), name, filament (1-based) and painted: true when slicer painting recolours some facets; draw a plate from these with the 3MF's own meshes. A plain 3MF without plate metadata is one plate. include_thumbnails adds each plate's own preview from the file (thumbnail_png_base64). Read-only.",
2607
2607
  inputSchema: {
2608
2608
  type: "object",
2609
2609
  properties: {