PerObjectSettingsTool.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from UM.Tool import Tool
  5. from UM.Scene.Selection import Selection
  6. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  7. from UM.Application import Application
  8. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  9. from cura.Settings.ExtruderManager import ExtruderManager
  10. from UM.Settings.SettingInstance import SettingInstance
  11. from UM.Event import Event
  12. ## This tool allows the user to add & change settings per node in the scene.
  13. # The settings per object are kept in a ContainerStack, which is linked to a node by decorator.
  14. class PerObjectSettingsTool(Tool):
  15. def __init__(self):
  16. super().__init__()
  17. self._model = None
  18. self.setExposedProperties("SelectedObjectId", "ContainerID", "SelectedActiveExtruder", "MeshType")
  19. self._multi_extrusion = False
  20. self._single_model_selected = False
  21. self.visibility_handler = None
  22. Selection.selectionChanged.connect(self.propertyChanged)
  23. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  24. self._onGlobalContainerChanged()
  25. Selection.selectionChanged.connect(self._updateEnabled)
  26. def event(self, event):
  27. super().event(event)
  28. if event.type == Event.MousePressEvent and self._controller.getToolsEnabled():
  29. self.operationStopped.emit(self)
  30. return False
  31. def getSelectedObjectId(self):
  32. selected_object = Selection.getSelectedObject(0)
  33. selected_object_id = id(selected_object)
  34. return selected_object_id
  35. def getContainerID(self):
  36. selected_object = Selection.getSelectedObject(0)
  37. try:
  38. return selected_object.callDecoration("getStack").getId()
  39. except AttributeError:
  40. return ""
  41. ## Gets the active extruder of the currently selected object.
  42. #
  43. # \return The active extruder of the currently selected object.
  44. def getSelectedActiveExtruder(self):
  45. selected_object = Selection.getSelectedObject(0)
  46. return selected_object.callDecoration("getActiveExtruder")
  47. ## Changes the active extruder of the currently selected object.
  48. #
  49. # \param extruder_stack_id The ID of the extruder to print the currently
  50. # selected object with.
  51. def setSelectedActiveExtruder(self, extruder_stack_id):
  52. selected_object = Selection.getSelectedObject(0)
  53. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  54. if not stack:
  55. selected_object.addDecorator(SettingOverrideDecorator())
  56. selected_object.callDecoration("setActiveExtruder", extruder_stack_id)
  57. ## Returns True when the mesh_type was changed, False when current mesh_type == mesh_type
  58. def setMeshType(self, mesh_type: str) -> bool:
  59. old_mesh_type = self.getMeshType()
  60. if old_mesh_type == mesh_type:
  61. return False
  62. selected_object = Selection.getSelectedObject(0)
  63. if selected_object is None:
  64. Logger.log("w", "Tried setting the mesh type of the selected object, but no object was selected")
  65. return False
  66. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  67. if not stack:
  68. selected_object.addDecorator(SettingOverrideDecorator())
  69. stack = selected_object.callDecoration("getStack")
  70. settings = stack.getTop()
  71. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  72. if property_key != mesh_type:
  73. if settings.getInstance(property_key):
  74. settings.removeInstance(property_key)
  75. else:
  76. if not (settings.getInstance(property_key) and settings.getProperty(property_key, "value")):
  77. definition = stack.getSettingDefinition(property_key)
  78. new_instance = SettingInstance(definition, settings)
  79. new_instance.setProperty("value", True)
  80. new_instance.resetState() # Ensure that the state is not seen as a user state.
  81. settings.addInstance(new_instance)
  82. for property_key in ["top_bottom_thickness", "wall_thickness"]:
  83. if mesh_type == "infill_mesh":
  84. if not settings.getInstance(property_key):
  85. definition = stack.getSettingDefinition(property_key)
  86. new_instance = SettingInstance(definition, settings)
  87. new_instance.setProperty("value", 0)
  88. new_instance.resetState() # Ensure that the state is not seen as a user state.
  89. settings.addInstance(new_instance)
  90. visible = self.visibility_handler.getVisible()
  91. visible.add(property_key)
  92. self.visibility_handler.setVisible(visible)
  93. elif old_mesh_type == "infill_mesh" and settings.getInstance(property_key) and settings.getProperty(property_key, "value") == 0:
  94. settings.removeInstance(property_key)
  95. self.propertyChanged.emit()
  96. return True
  97. def getMeshType(self):
  98. selected_object = Selection.getSelectedObject(0)
  99. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  100. if not stack:
  101. return ""
  102. settings = stack.getTop()
  103. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  104. if settings.getInstance(property_key) and settings.getProperty(property_key, "value"):
  105. return property_key
  106. return ""
  107. def _onGlobalContainerChanged(self):
  108. global_container_stack = Application.getInstance().getGlobalContainerStack()
  109. if global_container_stack:
  110. # used for enabling or disabling per extruder settings per object
  111. self._multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
  112. extruder_stack = ExtruderManager.getInstance().getExtruderStack(0)
  113. if extruder_stack:
  114. root_node = Application.getInstance().getController().getScene().getRoot()
  115. for node in DepthFirstIterator(root_node):
  116. new_stack_id = extruder_stack.getId()
  117. # Get position of old extruder stack for this node
  118. old_extruder_pos = node.callDecoration("getActiveExtruderPosition")
  119. if old_extruder_pos is not None:
  120. # Fetch current (new) extruder stack at position
  121. new_stack = ExtruderManager.getInstance().getExtruderStack(old_extruder_pos)
  122. if new_stack:
  123. new_stack_id = new_stack.getId()
  124. node.callDecoration("setActiveExtruder", new_stack_id)
  125. self._updateEnabled()
  126. def _updateEnabled(self):
  127. selected_objects = Selection.getAllSelectedObjects()
  128. if len(selected_objects)> 1:
  129. self._single_model_selected = False
  130. elif len(selected_objects) == 1 and selected_objects[0].callDecoration("isGroup"):
  131. self._single_model_selected = False # Group is selected, so tool needs to be disabled
  132. else:
  133. self._single_model_selected = True
  134. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, self._single_model_selected)