PerObjectContainerStack.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Optional
  4. from UM.Application import Application
  5. from UM.Decorators import override
  6. from UM.Settings.Interfaces import PropertyEvaluationContext
  7. from UM.Settings.SettingInstance import InstanceState
  8. from .CuraContainerStack import CuraContainerStack
  9. class PerObjectContainerStack(CuraContainerStack):
  10. @override(CuraContainerStack)
  11. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  12. if context is None:
  13. context = PropertyEvaluationContext()
  14. context.pushContainer(self)
  15. global_stack = Application.getInstance().getGlobalContainerStack()
  16. if not global_stack:
  17. return None
  18. # Return the user defined value if present, otherwise, evaluate the value according to the default routine.
  19. if self.getContainer(0).hasProperty(key, property_name):
  20. if self.getContainer(0).getProperty(key, "state") == InstanceState.User:
  21. result = super().getProperty(key, property_name, context)
  22. context.popContainer()
  23. return result
  24. # Handle the "limit_to_extruder" property.
  25. limit_to_extruder = super().getProperty(key, "limit_to_extruder", context)
  26. if limit_to_extruder is not None:
  27. limit_to_extruder = str(limit_to_extruder)
  28. # if this stack has the limit_to_extruder "not overriden", use the original limit_to_extruder as the current
  29. # limit_to_extruder, so the values retrieved will be from the perspective of the original limit_to_extruder
  30. # stack.
  31. if limit_to_extruder == "-1":
  32. if "original_limit_to_extruder" in context.context:
  33. limit_to_extruder = context.context["original_limit_to_extruder"]
  34. if limit_to_extruder is not None and limit_to_extruder != "-1" and limit_to_extruder in global_stack.extruders:
  35. # set the original limit_to_extruder if this is the first stack that has a non-overriden limit_to_extruder
  36. if "original_limit_to_extruder" not in context.context:
  37. context.context["original_limit_to_extruder"] = limit_to_extruder
  38. if super().getProperty(key, "settable_per_extruder", context):
  39. result = global_stack.extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
  40. if result is not None:
  41. context.popContainer()
  42. return result
  43. result = super().getProperty(key, property_name, context)
  44. context.popContainer()
  45. return result
  46. @override(CuraContainerStack)
  47. def setNextStack(self, stack: CuraContainerStack) -> None:
  48. super().setNextStack(stack)
  49. # trigger signal to re-evaluate all default settings
  50. for key in self.getContainer(0).getAllKeys():
  51. # only evaluate default settings
  52. if self.getContainer(0).getProperty(key, "state") != InstanceState.Default:
  53. continue
  54. self._collectPropertyChanges(key, "value")
  55. self._emitCollectedPropertyChanges()