slicer.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """Pydantic schemas for slice requests."""
  2. from typing import Literal
  3. from pydantic import BaseModel, Field, model_validator
  4. class PresetRef(BaseModel):
  5. """A source-aware reference to a printer / process / filament preset.
  6. The SliceModal pulls dropdown options from three tiers (cloud / local /
  7. standard). At submit time the client sends one of these per slot so the
  8. backend knows where to fetch the preset content from at slice time.
  9. """
  10. source: Literal["cloud", "local", "standard"]
  11. id: str = Field(..., description=("Cloud setting_id, local DB row id (stringified), or standard preset name."))
  12. class SliceBundleSpec(BaseModel):
  13. """Per-request reference to a Printer Preset Bundle stored on the slicer
  14. sidecar. When SliceRequest.bundle is set, the dispatch skips PresetRef
  15. resolution entirely and asks the sidecar to pick its inner JSON triplet
  16. by name from the bundle's extracted directory — much faster than
  17. re-uploading three profile JSONs every slice and matches the preset
  18. triplet the user actually slices with in BambuStudio.
  19. """
  20. bundle_id: str = Field(
  21. ...,
  22. min_length=1,
  23. description="Sidecar-side bundle id from POST /api/v1/slicer/bundles.",
  24. )
  25. printer_name: str = Field(
  26. ...,
  27. min_length=1,
  28. description="Preset name within the bundle's printer/ directory (with or without the BambuStudio '# ' prefix).",
  29. )
  30. process_name: str = Field(
  31. ...,
  32. min_length=1,
  33. description="Preset name within the bundle's process/ directory.",
  34. )
  35. filament_names: list[str] = Field(
  36. ...,
  37. min_length=1,
  38. description="Per-slot filament preset names within the bundle's filament/ directory. Index 0 = slot 1.",
  39. )
  40. class SliceRequest(BaseModel):
  41. """Body for `POST /library/files/{file_id}/slice`.
  42. Two preset shapes are accepted per slot for backwards-compatibility:
  43. - **Legacy** — bare integer ``*_preset_id`` fields point into the
  44. ``local_presets`` table. Existing clients (and stale browser tabs after
  45. a Bambuddy upgrade) keep working unchanged.
  46. - **Source-aware** — ``*_preset`` carries an explicit
  47. ``{source, id}``. Required for cloud / standard tiers; also accepted
  48. (and equivalent) for local presets when the client is on the new modal.
  49. Exactly one of each pair must be set; the validator normalises legacy
  50. integer ids into a ``PresetRef(source='local', id=str(id))`` so the
  51. downstream resolver only deals with one shape.
  52. """
  53. # Legacy fields — kept optional so older clients continue to work.
  54. printer_preset_id: int | None = Field(
  55. default=None,
  56. description="DEPRECATED: prefer printer_preset. LocalPreset id with preset_type='printer'.",
  57. )
  58. process_preset_id: int | None = Field(
  59. default=None,
  60. description="DEPRECATED: prefer process_preset. LocalPreset id with preset_type='process'.",
  61. )
  62. filament_preset_id: int | None = Field(
  63. default=None,
  64. description="DEPRECATED: prefer filament_preset. LocalPreset id with preset_type='filament'.",
  65. )
  66. # Source-aware fields — set by the new SliceModal.
  67. printer_preset: PresetRef | None = None
  68. process_preset: PresetRef | None = None
  69. filament_preset: PresetRef | None = None
  70. # Multi-color: one PresetRef per AMS slot the source plate uses. Order is
  71. # significant — the slicer matches index-by-index against the plate's
  72. # filament slots. Always preferred over the legacy singular field; the
  73. # validator promotes a singular field into ``[singular]`` when the list
  74. # is empty so older clients keep working.
  75. filament_presets: list[PresetRef] = Field(default_factory=list)
  76. # Bundle dispatch alternative — when set, presets above are ignored and
  77. # the slicer dispatch picks per-category JSONs from a previously-imported
  78. # .bbscfg on the sidecar. Validator below short-circuits the
  79. # presets-required check when this is non-None.
  80. bundle: SliceBundleSpec | None = Field(
  81. default=None,
  82. description="When set, slice via a sidecar-side bundle instead of resolved preset refs.",
  83. )
  84. plate: int | None = Field(
  85. default=None,
  86. ge=0,
  87. description=(
  88. "Plate number to slice. ``None`` defaults to plate 1 on the sidecar "
  89. "(matches the pre-multi-plate behaviour). ``0`` is the sidecar's "
  90. "'all plates' sentinel — produces a single multi-plate 3MF whose "
  91. "``Metadata/plate_N.gcode`` entries cover every plate in the "
  92. "source. ``>= 1`` slices that one plate."
  93. ),
  94. )
  95. export_3mf: bool = Field(
  96. default=False,
  97. description="If true, request a 3MF response with embedded G-code instead of raw G-code.",
  98. )
  99. bed_type: str | None = Field(
  100. default=None,
  101. max_length=64,
  102. description=(
  103. "Override the process preset's curr_bed_type for this slice. Canonical "
  104. "BambuStudio / OrcaSlicer values: 'Cool Plate', 'Engineering Plate', "
  105. "'High Temp Plate', 'Textured PEI Plate', 'Smooth PEI Plate', "
  106. "'Cool Plate (SuperTack)', 'Supertack Plate'. None ⇒ inherit from the "
  107. "process preset unchanged (#1337)."
  108. ),
  109. )
  110. @model_validator(mode="after")
  111. def normalise_preset_refs(self) -> "SliceRequest":
  112. """Each slot must end up with a `PresetRef` set. Legacy integer ids
  113. become `(source='local', id=str(int))` so the route handler only
  114. deals with the canonical shape. For filament: a non-empty
  115. ``filament_presets`` list satisfies the requirement on its own; an
  116. empty list falls back to the singular fields, which then promote
  117. into a one-element list.
  118. When ``bundle`` is set, the dispatch picks the JSON triplet from
  119. the sidecar bundle directly so PresetRef resolution is skipped —
  120. return early before the presets-required checks below.
  121. """
  122. if self.bundle is not None:
  123. return self
  124. for slot, ref_attr, legacy_attr in (
  125. ("printer", "printer_preset", "printer_preset_id"),
  126. ("process", "process_preset", "process_preset_id"),
  127. ):
  128. ref = getattr(self, ref_attr)
  129. legacy_id = getattr(self, legacy_attr)
  130. if ref is None and legacy_id is None:
  131. raise ValueError(
  132. f"{slot} preset is required: provide '{ref_attr}' (preferred) or legacy '{legacy_attr}'"
  133. )
  134. if ref is None:
  135. setattr(self, ref_attr, PresetRef(source="local", id=str(legacy_id)))
  136. # Filament accepts THREE shapes, in priority order:
  137. # 1. filament_presets — multi-color array (new clients)
  138. # 2. filament_preset — source-aware singular (single-color new clients)
  139. # 3. filament_preset_id — legacy bare integer (old clients)
  140. # The first non-empty shape wins; missing all three raises.
  141. if not self.filament_presets:
  142. if self.filament_preset is not None:
  143. self.filament_presets = [self.filament_preset]
  144. elif self.filament_preset_id is not None:
  145. fallback = PresetRef(source="local", id=str(self.filament_preset_id))
  146. self.filament_preset = fallback
  147. self.filament_presets = [fallback]
  148. else:
  149. raise ValueError(
  150. "filament preset is required: provide 'filament_presets' (preferred), "
  151. "'filament_preset', or legacy 'filament_preset_id'"
  152. )
  153. elif self.filament_preset is None:
  154. # Multi-color caller: backfill the singular from the first slot
  155. # so callers that still read the legacy field see a stable value.
  156. self.filament_preset = self.filament_presets[0]
  157. return self
  158. class SliceResponse(BaseModel):
  159. """Response from `POST /library/files/{file_id}/slice`. The result lands
  160. in the user's library as a new ``LibraryFile`` (in the same folder as
  161. the source)."""
  162. library_file_id: int
  163. name: str
  164. print_time_seconds: int
  165. filament_used_g: float
  166. filament_used_mm: float
  167. used_embedded_settings: bool = False
  168. class SliceArchiveResponse(BaseModel):
  169. """Response from `POST /archives/{archive_id}/slice`. The result lands
  170. in the user's archives as a new ``PrintArchive`` row, inheriting
  171. printer / project metadata from the source archive."""
  172. archive_id: int
  173. name: str
  174. print_time_seconds: int
  175. filament_used_g: float
  176. filament_used_mm: float
  177. used_embedded_settings: bool = False