PerObjectSettingsTool.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_visibility_changed = False
  71. settings = stack.getTop()
  72. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  73. if property_key != mesh_type:
  74. if settings.getInstance(property_key):
  75. settings.removeInstance(property_key)
  76. else:
  77. if not (settings.getInstance(property_key) and settings.getProperty(property_key, "value")):
  78. definition = stack.getSettingDefinition(property_key)
  79. new_instance = SettingInstance(definition, settings)
  80. new_instance.setProperty("value", True)
  81. new_instance.resetState() # Ensure that the state is not seen as a user state.
  82. settings.addInstance(new_instance)
  83. for property_key in ["top_bottom_thickness", "wall_thickness"]:
  84. if mesh_type == "infill_mesh":
  85. if settings.getInstance(property_key) is None:
  86. definition = stack.getSettingDefinition(property_key)
  87. new_instance = SettingInstance(definition, settings)
  88. new_instance.setProperty("value", 0)
  89. new_instance.resetState() # Ensure that the state is not seen as a user state.
  90. settings.addInstance(new_instance)
  91. settings_visibility_changed = True
  92. elif old_mesh_type == "infill_mesh" and settings.getInstance(property_key) and settings.getProperty(property_key, "value") == 0:
  93. settings.removeInstance(property_key)
  94. settings_visibility_changed = True
  95. if settings_visibility_changed:
  96. self.visibility_handler.forceVisibilityChanged()
  97. self.propertyChanged.emit()
  98. return True
  99. def getMeshType(self):
  100. selected_object = Selection.getSelectedObject(0)
  101. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  102. if not stack:
  103. return ""
  104. settings = stack.getTop()
  105. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  106. if settings.getInstance(property_key) and settings.getProperty(property_key, "value"):
  107. return property_key
  108. return ""
  109. def _onGlobalContainerChanged(self):
  110. global_container_stack = Application.getInstance().getGlobalContainerStack()
  111. if global_container_stack:
  112. # used for enabling or disabling per extruder settings per object
  113. self._multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
  114. extruder_stack = ExtruderManager.getInstance().getExtruderStack(0)
  115. if extruder_stack:
  116. root_node = Application.getInstance().getController().getScene().getRoot()
  117. for node in DepthFirstIterator(root_node):
  118. new_stack_id = extruder_stack.getId()
  119. # Get position of old extruder stack for this node
  120. old_extruder_pos = node.callDecoration("getActiveExtruderPosition")
  121. if old_extruder_pos is not None:
  122. # Fetch current (new) extruder stack at position
  123. new_stack = ExtruderManager.getInstance().getExtruderStack(old_extruder_pos)
  124. if new_stack:
  125. new_stack_id = new_stack.getId()
  126. node.callDecoration("setActiveExtruder", new_stack_id)
  127. self._updateEnabled()
  128. def _updateEnabled(self):
  129. selected_objects = Selection.getAllSelectedObjects()
  130. if len(selected_objects)> 1:
  131. self._single_model_selected = False
  132. elif len(selected_objects) == 1 and selected_objects[0].callDecoration("isGroup"):
  133. self._single_model_selected = False # Group is selected, so tool needs to be disabled
  134. else:
  135. self._single_model_selected = True
  136. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, self._single_model_selected)