PerObjectContainerStack.py 3.0 KB

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