PerObjectSettingsTool.py 6.6 KB

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