ColorChange.py 2.7 KB

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