From 5a4661db988558b34dc2ece849258c98ab62ed90 Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Tue, 28 Jan 2020 16:36:06 +0100 Subject: [PATCH 1/2] Add valueFromContainer() and extruderValueFromContainer() functions --- cura/CuraApplication.py | 2 ++ cura/Settings/CuraFormulaFunctions.py | 32 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 221ccf9fb0..ff563c8f2d 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -393,6 +393,8 @@ class CuraApplication(QtApplication): SettingFunction.registerOperator("extruderValues", self._cura_formula_functions.getValuesInAllExtruders) SettingFunction.registerOperator("resolveOrValue", self._cura_formula_functions.getResolveOrValue) SettingFunction.registerOperator("defaultExtruderPosition", self._cura_formula_functions.getDefaultExtruderPosition) + SettingFunction.registerOperator("valueFromContainer", self._cura_formula_functions.getValueFromContainerAtIndex) + SettingFunction.registerOperator("extruderValueFromContainer", self._cura_formula_functions.getValueFromContainerAtIndexInExtruder) # Adds all resources and container related resources. def __addAllResourcesAndContainerResources(self) -> None: diff --git a/cura/Settings/CuraFormulaFunctions.py b/cura/Settings/CuraFormulaFunctions.py index f39435be60..e4846a9da9 100644 --- a/cura/Settings/CuraFormulaFunctions.py +++ b/cura/Settings/CuraFormulaFunctions.py @@ -133,6 +133,38 @@ class CuraFormulaFunctions: context = self.createContextForDefaultValueEvaluation(global_stack) return self.getResolveOrValue(property_key, context = context) + # Gets the value for the given setting key starting from the given container index. + def getValueFromContainerAtIndex(self, property_key: str, container_index: int, + context: Optional["PropertyEvaluationContext"] = None) -> Any: + machine_manager = self._application.getMachineManager() + global_stack = machine_manager.activeMachine + + context = self.createContextForDefaultValueEvaluation(global_stack) + context.context["evaluate_from_container_index"] = container_index + + return global_stack.getProperty(property_key, "value", context = context) + + # Gets the extruder value for the given setting key starting from the given container index. + def getValueFromContainerAtIndexInExtruder(self, extruder_index: int, property_key: str, container_index: int, + context: Optional["PropertyEvaluationContext"] = None) -> Any: + machine_manager = self._application.getMachineManager() + global_stack = machine_manager.activeMachine + + if extruder_position == -1: + extruder_position = int(machine_manager.defaultExtruderPosition) + + global_stack = machine_manager.activeMachine + try: + extruder_stack = global_stack.extruderList[int(extruder_position)] + except IndexError: + Logger.log("w", "Value for %s of extruder %s was requested, but that extruder is not available. " % (property_key, extruder_position)) + return None + + context = self.createContextForDefaultValueEvaluation(extruder_stack) + context.context["evaluate_from_container_index"] = container_index + + return self.getValueInExtruder(extruder_index, property_key, context) + # Creates a context for evaluating default values (skip the user_changes container). def createContextForDefaultValueEvaluation(self, source_stack: "CuraContainerStack") -> "PropertyEvaluationContext": context = PropertyEvaluationContext(source_stack) From 5f67331bac6490bd0a1a34a199d8ab9448c93441 Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Thu, 30 Jan 2020 18:20:28 +0100 Subject: [PATCH 2/2] Fix "typo" caused by cleanup before push --- cura/Settings/CuraFormulaFunctions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cura/Settings/CuraFormulaFunctions.py b/cura/Settings/CuraFormulaFunctions.py index e4846a9da9..13e3dfb26e 100644 --- a/cura/Settings/CuraFormulaFunctions.py +++ b/cura/Settings/CuraFormulaFunctions.py @@ -145,7 +145,7 @@ class CuraFormulaFunctions: return global_stack.getProperty(property_key, "value", context = context) # Gets the extruder value for the given setting key starting from the given container index. - def getValueFromContainerAtIndexInExtruder(self, extruder_index: int, property_key: str, container_index: int, + def getValueFromContainerAtIndexInExtruder(self, extruder_position: int, property_key: str, container_index: int, context: Optional["PropertyEvaluationContext"] = None) -> Any: machine_manager = self._application.getMachineManager() global_stack = machine_manager.activeMachine @@ -163,7 +163,7 @@ class CuraFormulaFunctions: context = self.createContextForDefaultValueEvaluation(extruder_stack) context.context["evaluate_from_container_index"] = container_index - return self.getValueInExtruder(extruder_index, property_key, context) + return self.getValueInExtruder(extruder_position, property_key, context) # Creates a context for evaluating default values (skip the user_changes container). def createContextForDefaultValueEvaluation(self, source_stack: "CuraContainerStack") -> "PropertyEvaluationContext":