filament_finish.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """The finish a filament's wording names: Basic, Matte, Silk, ...
  2. A slot preset of one finish is not the preset of another, even for the same
  3. material: a Silk spool must not take a slot's Matte preset. The same words pick
  4. the generic preset for a finish that has one (Generic PLA Silk, PETG HF).
  5. """
  6. from backend.app.utils.filament_ids import GENERIC_FILAMENT_IDS
  7. # Finishes Bambu names in a sub-brand ("PLA Matte", "PLA Silk+", "PETG HF").
  8. KNOWN_FINISHES = (
  9. "basic",
  10. "matte",
  11. "silk",
  12. "glow",
  13. "marble",
  14. "metal",
  15. "sparkle",
  16. "galaxy",
  17. "translucent",
  18. "tough",
  19. "wood",
  20. "aero",
  21. "lite",
  22. "gradient",
  23. "high speed",
  24. "dual color",
  25. "hf",
  26. )
  27. def finishes(text: str | None) -> list[str]:
  28. """The known finishes named in ``text``, "+" and "-" read as spaces
  29. ("Silk+", "Silk Multi-Color")."""
  30. words = f" {(text or '').lower().replace('+', ' ').replace('-', ' ')} "
  31. return [finish for finish in KNOWN_FINISHES if f" {finish} " in words]
  32. def different_finish(spool_text: str | None, slot_text: str | None) -> bool:
  33. """True when both name a finish and they share none."""
  34. spool_finishes = set(finishes(spool_text))
  35. slot_finishes = set(finishes(slot_text))
  36. return bool(spool_finishes and slot_finishes and not spool_finishes & slot_finishes)
  37. def finish_generic_id(material: str | None, text: str | None) -> str:
  38. """The generic preset for ``material`` in a finish ``text`` names, e.g.
  39. PLA + "Silk+" -> GFL96 (Generic PLA Silk); "" when there is none."""
  40. base = (material or "").upper().strip()
  41. for finish in finishes(text):
  42. generic = GENERIC_FILAMENT_IDS.get(f"{base} {finish.upper()}")
  43. if generic:
  44. return generic
  45. return ""