PerObjectSettingsTool.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Uranium is released under the terms of the AGPLv3 or higher.
  3. from UM.Tool import Tool
  4. from UM.Scene.Selection import Selection
  5. from UM.Application import Application
  6. from UM.Preferences import Preferences
  7. from cura.SettingOverrideDecorator import SettingOverrideDecorator
  8. ## This tool allows the user to add & change settings per node in the scene.
  9. # The settings per object are kept in a ContainerStack, which is linked to a node by decorator.
  10. class PerObjectSettingsTool(Tool):
  11. def __init__(self):
  12. super().__init__()
  13. self._model = None
  14. self.setExposedProperties("SelectedObjectId", "ContainerID", "SelectedActiveExtruder")
  15. Preferences.getInstance().preferenceChanged.connect(self._onPreferenceChanged)
  16. Selection.selectionChanged.connect(self.propertyChanged)
  17. self._onPreferenceChanged("cura/active_mode")
  18. def event(self, event):
  19. return False
  20. def getSelectedObjectId(self):
  21. try:
  22. selected_object = Selection.getSelectedObject(0)
  23. if selected_object.getParent().callDecoration("isGroup"):
  24. selected_object = selected_object.getParent()
  25. except:
  26. selected_object = None
  27. selected_object_id = id(selected_object)
  28. return selected_object_id
  29. def getContainerID(self):
  30. selected_object = Selection.getSelectedObject(0)
  31. if selected_object.getParent().callDecoration("isGroup"):
  32. selected_object = selected_object.getParent()
  33. try:
  34. return selected_object.callDecoration("getStack").getId()
  35. except AttributeError:
  36. return ""
  37. ## Gets the active extruder of the currently selected object.
  38. #
  39. # \return The active extruder of the currently selected object.
  40. def getSelectedActiveExtruder(self):
  41. selected_object = Selection.getSelectedObject(0)
  42. return selected_object.callDecoration("getActiveExtruder")
  43. ## Changes the active extruder of the currently selected object.
  44. #
  45. # \param extruder_stack_id The ID of the extruder to print the currently
  46. # selected object with.
  47. def setSelectedActiveExtruder(self, extruder_stack_id):
  48. selected_object = Selection.getSelectedObject(0)
  49. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  50. if not stack:
  51. selected_object.addDecorator(SettingOverrideDecorator())
  52. selected_object.callDecoration("setActiveExtruder", extruder_stack_id)
  53. def _onPreferenceChanged(self, preference):
  54. if preference == "cura/active_mode":
  55. enabled = Preferences.getInstance().getValue(preference)==1
  56. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, enabled)