FilamentChange.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # This PostProcessing Plugin script is released
  2. # under the terms of the AGPLv3 or higher
  3. from ..Script import Script
  4. class FilamentChange(Script):
  5. def __init__(self):
  6. super().__init__()
  7. def getSettingDataString(self):
  8. return """{
  9. "name":"Filament Change",
  10. "key": "FilamentChange",
  11. "metadata": {},
  12. "version": 2,
  13. "settings":
  14. {
  15. "layer_number":
  16. {
  17. "label": "Layer",
  18. "description": "At what layer should color change occur. This will be before the layer starts printing. Specify multiple color changes with a comma.",
  19. "unit": "",
  20. "type": "str",
  21. "default_value": "1"
  22. },
  23. "initial_retract":
  24. {
  25. "label": "Initial Retraction",
  26. "description": "Initial filament retraction distance. The filament will be retracted with this amount before moving the nozzle away from the ongoing print.",
  27. "unit": "mm",
  28. "type": "float",
  29. "default_value": 30.0
  30. },
  31. "later_retract":
  32. {
  33. "label": "Later Retraction Distance",
  34. "description": "Later filament retraction distance for removal. The filament will be retracted all the way out of the printer so that you can change the filament.",
  35. "unit": "mm",
  36. "type": "float",
  37. "default_value": 300.0
  38. }
  39. }
  40. }"""
  41. def execute(self, data: list):
  42. """data is a list. Each index contains a layer"""
  43. layer_nums = self.getSettingValueByKey("layer_number")
  44. initial_retract = self.getSettingValueByKey("initial_retract")
  45. later_retract = self.getSettingValueByKey("later_retract")
  46. color_change = "M600"
  47. if initial_retract is not None and initial_retract > 0.:
  48. color_change = color_change + (" E%.2f" % initial_retract)
  49. if later_retract is not None and later_retract > 0.:
  50. color_change = color_change + (" L%.2f" % later_retract)
  51. color_change = color_change + " ; Generated by FilamentChange plugin"
  52. layer_targets = layer_nums.split(",")
  53. if len(layer_targets) > 0:
  54. for layer_num in layer_targets:
  55. layer_num = int(layer_num.strip())
  56. if layer_num < len(data):
  57. layer = data[layer_num - 1]
  58. lines = layer.split("\n")
  59. lines.insert(2, color_change)
  60. final_line = "\n".join(lines)
  61. data[layer_num - 1] = final_line
  62. return data