printer_models.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. """Printer model normalization utilities.
  2. Converts 3MF printer model names (e.g., "Bambu Lab X1 Carbon") to
  3. normalized short names (e.g., "X1C") that match database storage.
  4. """
  5. # Map from 3MF printer_model strings to normalized short names
  6. PRINTER_MODEL_MAP = {
  7. "Bambu Lab X1 Carbon": "X1C",
  8. "Bambu Lab X1": "X1",
  9. "Bambu Lab X1E": "X1E",
  10. "Bambu Lab P1S": "P1S",
  11. "Bambu Lab P1P": "P1P",
  12. "Bambu Lab P2S": "P2S",
  13. "Bambu Lab A1": "A1",
  14. "Bambu Lab A1 Mini": "A1 Mini",
  15. "Bambu Lab A1 mini": "A1 Mini",
  16. # Bambu cloud rolled out a terse model-code rename mid-2026 (#1649);
  17. # 3MFs prepared with newer cloud presets may carry this short form.
  18. "Bambu Lab A1M": "A1 Mini",
  19. "Bambu Lab H2D": "H2D",
  20. "Bambu Lab H2D Pro": "H2D Pro",
  21. "Bambu Lab H2C": "H2C",
  22. "Bambu Lab H2S": "H2S",
  23. "Bambu Lab X2D": "X2D",
  24. "Bambu Lab A2L": "A2L",
  25. }
  26. # Map from printer_model_id (internal codes in slice_info.config) to short names
  27. # These are the codes Bambu Studio uses internally, and the same codes the
  28. # printers announce over SSDP (DevModel), so Printer.model can hold them too.
  29. # A real P1P 3MF carries printer_model_id "C11" next to "Bambu Lab P1P"; the
  30. # X1 Carbon's code is "BL-P001", not a C-code.
  31. PRINTER_MODEL_ID_MAP = {
  32. # X1 series
  33. "BL-P001": "X1C",
  34. "BL-P002": "X1",
  35. "C13": "X1E",
  36. # P1 series
  37. "C11": "P1P",
  38. "C12": "P1S",
  39. "P1P": "P1P",
  40. "P1S": "P1S",
  41. # P2 series
  42. "N7": "P2S",
  43. "P2S": "P2S",
  44. # X2 series
  45. "N6": "X2D",
  46. # A2 series (A2L is single-FDM + integrated cutter/plotter — single nozzle)
  47. "N9": "A2L",
  48. # A1 series
  49. "A11": "A1",
  50. "A12": "A1 Mini",
  51. "N1": "A1 Mini",
  52. "N2S": "A1",
  53. "A04": "A1 Mini",
  54. # H2 series (Office/H series)
  55. "O1D": "H2D",
  56. "O1E": "H2D Pro", # Some devices report O1E
  57. "O2D": "H2D Pro", # Some devices report O2D
  58. "O1C": "H2C",
  59. "O1C2": "H2C",
  60. "O1S": "H2S",
  61. }
  62. # Rod/rail type classification for maintenance tasks.
  63. # Carbon rods: X1, P1 series (CoreXY with carbon fiber rods)
  64. # Steel rods: P2S, X2D series (hardened steel linear shafts)
  65. # Linear rails: A1, H2 series (linear rail motion system)
  66. # Values must be uppercase with spaces stripped for normalized comparison.
  67. CARBON_ROD_MODELS = frozenset(
  68. [
  69. # Display names (uppercase, no spaces)
  70. "X1",
  71. "X1C",
  72. "X1E",
  73. "P1P",
  74. "P1S",
  75. # Internal codes (dashes stripped, as the lookup strips them)
  76. "BLP001", # X1C (BL-P001)
  77. "BLP002", # X1 (BL-P002)
  78. "C13", # X1E
  79. "C11", # P1P
  80. "C12", # P1S
  81. ]
  82. )
  83. STEEL_ROD_MODELS = frozenset(
  84. [
  85. # Display names (uppercase, no spaces)
  86. "P2S",
  87. "X2D",
  88. # Internal codes
  89. "N7", # P2S
  90. "N6", # X2D
  91. ]
  92. )
  93. LINEAR_RAIL_MODELS = frozenset(
  94. [
  95. # Display names (uppercase, no spaces)
  96. "A1",
  97. "A1MINI",
  98. "A2L",
  99. "H2D",
  100. "H2DPRO",
  101. "H2C",
  102. "H2S",
  103. # Internal codes
  104. "N1", # A1 Mini
  105. "N2S", # A1
  106. "N9", # A2L
  107. "A04", # A1 Mini (alternate)
  108. "A11", # A1
  109. "A12", # A1 Mini
  110. "O1D", # H2D
  111. "O1E", # H2D Pro
  112. "O2D", # H2D Pro (alternate)
  113. "O1C", # H2C
  114. "O1C2", # H2C (dual nozzle variant)
  115. "O1S", # H2S
  116. ]
  117. )
  118. # Models sold with a single nozzle flow variant, so a Standard / High Flow
  119. # choice on a K-profile is meaningless there. Derived from the slicer's own
  120. # rule (len(nozzle_volume) // len(nozzle_diameter) > 1 over the bundled Bambu
  121. # machine presets), not from nozzle count — P1P/P1S/P2S/X1/X1C/X1E/H2S are
  122. # single-nozzle and all carry two variants. Only the A-series has one.
  123. SINGLE_NOZZLE_FLOW_MODELS = frozenset(
  124. [
  125. # Display names (uppercase, no spaces)
  126. "A1",
  127. "A1MINI",
  128. "A2L",
  129. # Internal codes
  130. "N1", # A1 Mini
  131. "N2S", # A1
  132. "N9", # A2L
  133. "A04", # A1 Mini (alternate)
  134. "A11", # A1
  135. "A12", # A1 Mini
  136. ]
  137. )
  138. # Models without any external storage (MicroSD / SD card slot).
  139. # The A1 and A1 Mini ship with internal storage only — there is no
  140. # firmware-side "Store sent files on external storage" toggle and no
  141. # slicer-side equivalent surfaces one. The connection diagnostic's
  142. # external_storage check (printer_diagnostic.py) must skip on these
  143. # models instead of reporting fail from a 0-valued home_flag bit.
  144. NO_EXTERNAL_STORAGE_MODELS = frozenset(
  145. [
  146. # Display names (uppercase, no spaces)
  147. "A1",
  148. "A1MINI",
  149. # Internal codes
  150. "N1", # A1 Mini
  151. "N2S", # A1
  152. "A04", # A1 Mini (alternate)
  153. "A11", # A1
  154. "A12", # A1 Mini
  155. ]
  156. )
  157. # Models that HAVE a MicroSD slot but expose NO reachable control to enable
  158. # the "Store sent files on external storage" option. The toggle only renders
  159. # in Bambu Studio when the printer publishes the
  160. # `support_save_remote_print_file_to_storage` capability in its live status;
  161. # current P1-series firmware (through 01.10.00.00) never publishes it, and
  162. # the P1S/P1P have no on-printer screen, so `store_to_sdcard` (home_flag bit
  163. # 11) is stuck at False with no way for the user to change it. The
  164. # external_storage diagnostic must therefore skip (not fail) on these models
  165. # — a hard fail would be permanently unresolvable (#2524). If a future
  166. # firmware surfaces the capability, remove the model here and the check
  167. # reactivates. Bambu Lab's own storage-cache wiki lists P1 Series as "Not
  168. # Supported", corroborating this.
  169. NO_REMOTE_STORAGE_TOGGLE_MODELS = frozenset(
  170. [
  171. # Display names (uppercase, no spaces)
  172. "P1S",
  173. "P1P",
  174. # Internal codes
  175. "C11", # P1P
  176. "C12", # P1S
  177. ]
  178. )
  179. # Models with an ethernet port.
  180. # X1, P1P, A1, A1 Mini do NOT have ethernet.
  181. ETHERNET_MODELS = frozenset(
  182. [
  183. # Display names (uppercase, no spaces)
  184. "X1C",
  185. "X1E",
  186. "X2D",
  187. "P1S",
  188. "P2S",
  189. "H2D",
  190. "H2DPRO",
  191. "H2C",
  192. "H2S",
  193. # Internal codes (dashes stripped, as the lookup strips them)
  194. "BLP001", # X1C (BL-P001)
  195. "C13", # X1E
  196. "N6", # X2D
  197. "C12", # P1S
  198. "N7", # P2S
  199. "O1D", # H2D
  200. "O1E", # H2D Pro
  201. "O2D", # H2D Pro (alternate)
  202. "O1C", # H2C
  203. "O1C2", # H2C (dual nozzle variant)
  204. "O1S", # H2S
  205. ]
  206. )
  207. # Dual-nozzle (dual-extruder) printers. Single source of truth for nozzle
  208. # class — consumed by ``BambuMQTTClient.start_print``, the K-profile routes,
  209. # and the re-slice nozzle-class guard (previously an inline model tuple
  210. # duplicated across all three). Re-slicing a model laid out for a single-nozzle
  211. # printer onto one of these — or vice versa — is not yet supported: the source
  212. # 3MF's embedded single-nozzle filament/extruder layout is not a valid
  213. # dual-nozzle project and BambuStudio's multi-extruder validator rejects it.
  214. DUAL_NOZZLE_MODELS = frozenset(
  215. [
  216. # Display names (uppercase, no spaces)
  217. "H2D",
  218. "H2DPRO",
  219. "H2C",
  220. "X2D",
  221. # Internal codes
  222. "O1D", # H2D
  223. "O1E", # H2D Pro
  224. "O2D", # H2D Pro (alternate)
  225. "O1C", # H2C
  226. "O1C2", # H2C (dual nozzle variant)
  227. "N6", # X2D
  228. ]
  229. )
  230. # Printers with a swappable nozzle rack ("Vortek"): the H2C carries six
  231. # hotends in a rack and mounts one of them on its right extruder at a time.
  232. #
  233. # Why this needs its own set rather than reusing DUAL_NOZZLE_MODELS: on every
  234. # other dual-nozzle printer the dispatch `nozzle_mapping` values ARE the MQTT
  235. # extruder indices (0 = right, 1 = left). On a rack model the wire wants the
  236. # *physical* nozzle position for both carriages: the rack positions the
  237. # firmware reports as IDs 16-21 — see `device.nozzle.info` handling in
  238. # bambu_mqtt — and 1 for the fixed hotend, which is not its extruder index.
  239. # Note the H2C does not follow the 0 = right convention either: extruder
  240. # index 1 is the rack side, confirmed on hardware in #2800.
  241. # Sending an extruder index where a physical position is expected makes the
  242. # printer clean and level with one nozzle and then print with another, at the
  243. # wrong Z (#2800).
  244. NOZZLE_RACK_MODELS = frozenset(
  245. [
  246. # Display names (uppercase, no spaces)
  247. "H2C",
  248. # Internal codes
  249. "O1C", # H2C
  250. "O1C2", # H2C (dual nozzle variant)
  251. ]
  252. )
  253. # Models where Bambu's own firmware/UI names the enclosure fan (big_fan2 /
  254. # airduct part id 3) "Exhaust" rather than "Chamber". On these the printer's
  255. # touchscreen and Bambu Studio both call it the exhaust fan, and on the P2S it
  256. # is an add-on kit rather than built-in hardware. Other enclosed models
  257. # (X1 / P1S / H2 series) keep the "Chamber" naming.
  258. EXHAUST_FAN_LABEL_MODELS = frozenset(
  259. [
  260. # Display names (uppercase, no spaces)
  261. "P2S",
  262. "X2D",
  263. # Internal codes
  264. "N7", # P2S
  265. "N6", # X2D
  266. ]
  267. )
  268. def uses_exhaust_fan_label(model: str | None) -> bool:
  269. """Return True if this model calls the big_fan2 enclosure fan "Exhaust".
  270. P2S/X2D name that fan "Exhaust" in Bambu's firmware/UI; everything else
  271. enclosed calls it the chamber fan. Used so the UI badge and the API
  272. response message agree on what the user sees.
  273. """
  274. if not model:
  275. return False
  276. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  277. return normalized in EXHAUST_FAN_LABEL_MODELS
  278. # Ceiling for every chamber-temperature target the UI and API accept (manual
  279. # M141, the preheat filament map, the per-item preheat override, the chamber
  280. # quick-select presets). The H2 series (H2C / H2D / H2D Pro / H2S) and X2D
  281. # heat the chamber to 65 °C; X1E tops out at 60. We validate against the
  282. # highest of those and let the firmware clamp on the lower-ceiling models —
  283. # the preheat filament map is global rather than per printer, so a per-model
  284. # maximum could not be expressed there anyway.
  285. MAX_CHAMBER_TEMP_C = 65
  286. def has_ethernet(model: str | None) -> bool:
  287. """Return True if the printer model has an ethernet port."""
  288. if not model:
  289. return False
  290. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  291. return normalized in ETHERNET_MODELS
  292. def has_external_storage(model: str | None) -> bool:
  293. """Return True if the printer model can have a MicroSD / external storage slot.
  294. Defaults to True when the model is unknown — the diagnostic only flips
  295. its check on for the explicit no-storage list. New models added to the
  296. Bambu lineup without a slot must be added to ``NO_EXTERNAL_STORAGE_MODELS``
  297. or the diagnostic will continue to evaluate ``store_to_sdcard`` against
  298. a hardware feature the printer doesn't have.
  299. """
  300. if not model:
  301. return True
  302. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  303. return normalized not in NO_EXTERNAL_STORAGE_MODELS
  304. def has_remote_storage_toggle(model: str | None) -> bool:
  305. """Return True if the model exposes a reachable control for the
  306. "Store sent files on external storage" option.
  307. False for P1-series (has an SD slot, but no on-printer screen and no
  308. published `support_save_remote_print_file_to_storage` capability, so the
  309. Bambu Studio toggle never renders). The external_storage diagnostic uses
  310. this to skip rather than report an unresolvable fail (#2524). Defaults to
  311. True for unknown models so the check keeps working on anything not
  312. explicitly listed.
  313. """
  314. if not model:
  315. return True
  316. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  317. return normalized not in NO_REMOTE_STORAGE_TOGGLE_MODELS
  318. def is_dual_nozzle_model(model: str | None) -> bool:
  319. """Return True if the printer model has two nozzles (H2D family / X2D)."""
  320. if not model:
  321. return False
  322. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  323. return normalized in DUAL_NOZZLE_MODELS
  324. def is_nozzle_rack_model(model: str | None) -> bool:
  325. """Return True if the model mounts its nozzles from a swappable rack (H2C).
  326. Accepts both the display name and the internal SSDP code, because
  327. ``BambuMQTTClient.model`` carries whichever the printer row happens to
  328. hold — the same reason the P2S dispatch tweak checks ``("P2S", "N7")``.
  329. """
  330. if not model:
  331. return False
  332. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  333. return normalized in NOZZLE_RACK_MODELS
  334. def supports_nozzle_flow_type(model: str | None) -> bool:
  335. """Return True if the model offers a Standard / High Flow nozzle choice.
  336. A K-profile is filed under a ``nozzle_id`` of the form ``HS00-0.4``
  337. (Standard) or ``HH00-0.4`` (High Flow), so the flow type is part of the
  338. profile's identity on any printer where both exist — and meaningless noise
  339. on one where only a single variant is sold.
  340. The split is NOT the nozzle count: P1S, P2S, X1C and H2S are single-nozzle
  341. and all offer both flows. BambuStudio/OrcaSlicer derive the same capability
  342. from the machine preset — ``support_nozzle_volume()`` is
  343. ``len(nozzle_volume) // len(nozzle_diameter) > 1`` — and every bundled
  344. Bambu profile evaluated against that formula puts only the A-series on the
  345. "one variant" side (A1 and A1 Mini at 1, A2L at 1; everything from P1P
  346. upward at 2 or more per extruder).
  347. Defaults to True for unknown models: offering the choice on a printer that
  348. turns out to have one flow type costs the user a redundant dropdown, while
  349. hiding it on one that has two makes half its calibration table
  350. unreachable.
  351. """
  352. if not model:
  353. return True
  354. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  355. return normalized not in SINGLE_NOZZLE_FLOW_MODELS
  356. def get_rod_type(model: str | None) -> str | None:
  357. """Return the rod/rail type for a printer model.
  358. Returns:
  359. "carbon" for X1/P1 series (carbon fiber rods),
  360. "steel_rod" for P2S/X2D series (hardened steel rods),
  361. "linear_rail" for A1/H2 series (linear rails),
  362. None for unknown models.
  363. """
  364. if not model:
  365. return None
  366. normalized = model.strip().upper().replace(" ", "").replace("-", "")
  367. if normalized in CARBON_ROD_MODELS:
  368. return "carbon"
  369. if normalized in STEEL_ROD_MODELS:
  370. return "steel_rod"
  371. if normalized in LINEAR_RAIL_MODELS:
  372. return "linear_rail"
  373. return None
  374. # G-code interchange families (#2578). A sliced 3MF may target a different
  375. # model ONLY within its family: same kinematics, build volume and G-code
  376. # dialect. X1/P1 series are the one proven-interchangeable group (256mm
  377. # CoreXY, single nozzle — mixed farms intentionally run X1-sliced jobs on
  378. # P1S/P1P). Everything else is exact-match only; extend deliberately, never
  379. # by assumption — a wrong entry here dispatches G-code onto hardware it was
  380. # not sliced for.
  381. # Short display names only (uppercase, no spaces) — is_gcode_compatible()
  382. # resolves internal codes (C11, O1D, ...) to short names before lookup.
  383. GCODE_COMPAT_FAMILIES = (frozenset(["X1", "X1C", "X1E", "P1P", "P1S"]),)
  384. def is_gcode_compatible(sliced_for_model: str | None, target_model: str | None) -> bool:
  385. """Return True when G-code sliced for one model may be dispatched to the other.
  386. Unknown/missing metadata on either side returns True — we can only
  387. validate what the 3MF declares, and legacy files without
  388. ``sliced_for_model`` must keep working.
  389. """
  390. if not sliced_for_model or not target_model:
  391. return True
  392. def _norm(model: str) -> str:
  393. # Internal codes (e.g. "C11") → short names first, so "C11" vs "X1C"
  394. # compares equal instead of leaning on family membership.
  395. resolved = PRINTER_MODEL_ID_MAP.get(model.strip(), model)
  396. return resolved.strip().upper().replace(" ", "").replace("-", "")
  397. a = _norm(sliced_for_model)
  398. b = _norm(target_model)
  399. if a == b:
  400. return True
  401. return any(a in family and b in family for family in GCODE_COMPAT_FAMILIES)
  402. def normalize_printer_model_id(model_id: str | None) -> str | None:
  403. """Convert printer_model_id (internal code) to normalized short name.
  404. Args:
  405. model_id: The printer_model_id from slice_info.config (e.g., "C11", "O1D")
  406. Returns:
  407. Normalized short name (e.g., "X1C", "H2D") or the original ID if unknown.
  408. """
  409. if not model_id:
  410. return None
  411. # Check known mappings
  412. if model_id in PRINTER_MODEL_ID_MAP:
  413. return PRINTER_MODEL_ID_MAP[model_id]
  414. # Return original if unknown (might already be a short name)
  415. return model_id
  416. def normalize_printer_model(raw_model: str | None) -> str | None:
  417. """Convert 3MF printer_model to normalized short name.
  418. Args:
  419. raw_model: The printer_model string from 3MF metadata
  420. (e.g., "Bambu Lab X1 Carbon")
  421. Returns:
  422. Normalized short name (e.g., "X1C") or None if input is empty.
  423. Unknown models have "Bambu Lab " prefix stripped.
  424. """
  425. if not raw_model:
  426. return None
  427. # Check known mappings first
  428. if raw_model in PRINTER_MODEL_MAP:
  429. return PRINTER_MODEL_MAP[raw_model]
  430. # Strip "Bambu Lab " prefix for unknown models
  431. stripped = raw_model.replace("Bambu Lab ", "").strip()
  432. return stripped or None