slicer.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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=1,
  87. description="Plate number to slice (1-indexed). Defaults to plate 1 on the sidecar.",
  88. )
  89. export_3mf: bool = Field(
  90. default=False,
  91. description="If true, request a 3MF response with embedded G-code instead of raw G-code.",
  92. )
  93. bed_type: str | None = Field(
  94. default=None,
  95. max_length=64,
  96. description=(
  97. "Override the process preset's curr_bed_type for this slice. Canonical "
  98. "BambuStudio / OrcaSlicer values: 'Cool Plate', 'Engineering Plate', "
  99. "'High Temp Plate', 'Textured PEI Plate', 'Smooth PEI Plate', "
  100. "'Cool Plate (SuperTack)', 'Supertack Plate'. None ⇒ inherit from the "
  101. "process preset unchanged (#1337)."
  102. ),
  103. )
  104. @model_validator(mode="after")
  105. def normalise_preset_refs(self) -> "SliceRequest":
  106. """Each slot must end up with a `PresetRef` set. Legacy integer ids
  107. become `(source='local', id=str(int))` so the route handler only
  108. deals with the canonical shape. For filament: a non-empty
  109. ``filament_presets`` list satisfies the requirement on its own; an
  110. empty list falls back to the singular fields, which then promote
  111. into a one-element list.
  112. When ``bundle`` is set, the dispatch picks the JSON triplet from
  113. the sidecar bundle directly so PresetRef resolution is skipped —
  114. return early before the presets-required checks below.
  115. """
  116. if self.bundle is not None:
  117. return self
  118. for slot, ref_attr, legacy_attr in (
  119. ("printer", "printer_preset", "printer_preset_id"),
  120. ("process", "process_preset", "process_preset_id"),
  121. ):
  122. ref = getattr(self, ref_attr)
  123. legacy_id = getattr(self, legacy_attr)
  124. if ref is None and legacy_id is None:
  125. raise ValueError(
  126. f"{slot} preset is required: provide '{ref_attr}' (preferred) or legacy '{legacy_attr}'"
  127. )
  128. if ref is None:
  129. setattr(self, ref_attr, PresetRef(source="local", id=str(legacy_id)))
  130. # Filament accepts THREE shapes, in priority order:
  131. # 1. filament_presets — multi-color array (new clients)
  132. # 2. filament_preset — source-aware singular (single-color new clients)
  133. # 3. filament_preset_id — legacy bare integer (old clients)
  134. # The first non-empty shape wins; missing all three raises.
  135. if not self.filament_presets:
  136. if self.filament_preset is not None:
  137. self.filament_presets = [self.filament_preset]
  138. elif self.filament_preset_id is not None:
  139. fallback = PresetRef(source="local", id=str(self.filament_preset_id))
  140. self.filament_preset = fallback
  141. self.filament_presets = [fallback]
  142. else:
  143. raise ValueError(
  144. "filament preset is required: provide 'filament_presets' (preferred), "
  145. "'filament_preset', or legacy 'filament_preset_id'"
  146. )
  147. elif self.filament_preset is None:
  148. # Multi-color caller: backfill the singular from the first slot
  149. # so callers that still read the legacy field see a stable value.
  150. self.filament_preset = self.filament_presets[0]
  151. return self
  152. class SliceResponse(BaseModel):
  153. """Response from `POST /library/files/{file_id}/slice`. The result lands
  154. in the user's library as a new ``LibraryFile`` (in the same folder as
  155. the source)."""
  156. library_file_id: int
  157. name: str
  158. print_time_seconds: int
  159. filament_used_g: float
  160. filament_used_mm: float
  161. used_embedded_settings: bool = False
  162. class SliceArchiveResponse(BaseModel):
  163. """Response from `POST /archives/{archive_id}/slice`. The result lands
  164. in the user's archives as a new ``PrintArchive`` row, inheriting
  165. printer / project metadata from the source archive."""
  166. archive_id: int
  167. name: str
  168. print_time_seconds: int
  169. filament_used_g: float
  170. filament_used_mm: float
  171. used_embedded_settings: bool = False