ExtruderManager.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtSignal, pyqtProperty, QObject, QVariant # For communicating data and events to Qt.
  4. from UM.FlameProfiler import pyqtSlot
  5. import cura.CuraApplication # To get the global container stack to find the current machine.
  6. from cura.Settings.GlobalStack import GlobalStack
  7. from UM.Logger import Logger
  8. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  9. from UM.Scene.SceneNode import SceneNode
  10. from UM.Scene.Selection import Selection
  11. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  12. from UM.Settings.ContainerRegistry import ContainerRegistry # Finding containers by ID.
  13. from UM.Settings.SettingFunction import SettingFunction
  14. from UM.Settings.ContainerStack import ContainerStack
  15. from UM.Settings.PropertyEvaluationContext import PropertyEvaluationContext
  16. from typing import Any, cast, Dict, List, Optional, TYPE_CHECKING, Union
  17. if TYPE_CHECKING:
  18. from cura.Settings.ExtruderStack import ExtruderStack
  19. ## Manages all existing extruder stacks.
  20. #
  21. # This keeps a list of extruder stacks for each machine.
  22. class ExtruderManager(QObject):
  23. ## Registers listeners and such to listen to changes to the extruders.
  24. def __init__(self, parent = None):
  25. if ExtruderManager.__instance is not None:
  26. raise RuntimeError("Try to create singleton '%s' more than once" % self.__class__.__name__)
  27. ExtruderManager.__instance = self
  28. super().__init__(parent)
  29. self._application = cura.CuraApplication.CuraApplication.getInstance()
  30. # Per machine, a dictionary of extruder container stack IDs. Only for separately defined extruders.
  31. self._extruder_trains = {} # type: Dict[str, Dict[str, "ExtruderStack"]]
  32. self._active_extruder_index = -1 # Indicates the index of the active extruder stack. -1 means no active extruder stack
  33. # TODO; I have no idea why this is a union of ID's and extruder stacks. This needs to be fixed at some point.
  34. self._selected_object_extruders = [] # type: List[Union[str, "ExtruderStack"]]
  35. self._addCurrentMachineExtruders()
  36. Selection.selectionChanged.connect(self.resetSelectedObjectExtruders)
  37. ## Signal to notify other components when the list of extruders for a machine definition changes.
  38. extrudersChanged = pyqtSignal(QVariant)
  39. ## Notify when the user switches the currently active extruder.
  40. activeExtruderChanged = pyqtSignal()
  41. ## Gets the unique identifier of the currently active extruder stack.
  42. #
  43. # The currently active extruder stack is the stack that is currently being
  44. # edited.
  45. #
  46. # \return The unique ID of the currently active extruder stack.
  47. @pyqtProperty(str, notify = activeExtruderChanged)
  48. def activeExtruderStackId(self) -> Optional[str]:
  49. if not self._application.getGlobalContainerStack():
  50. return None # No active machine, so no active extruder.
  51. try:
  52. return self._extruder_trains[self._application.getGlobalContainerStack().getId()][str(self._active_extruder_index)].getId()
  53. except KeyError: # Extruder index could be -1 if the global tab is selected, or the entry doesn't exist if the machine definition is wrong.
  54. return None
  55. ## Return extruder count according to extruder trains.
  56. @pyqtProperty(int, notify = extrudersChanged)
  57. def extruderCount(self) -> int:
  58. if not self._application.getGlobalContainerStack():
  59. return 0 # No active machine, so no extruders.
  60. try:
  61. return len(self._extruder_trains[self._application.getGlobalContainerStack().getId()])
  62. except KeyError:
  63. return 0
  64. ## Gets a dict with the extruder stack ids with the extruder number as the key.
  65. @pyqtProperty("QVariantMap", notify = extrudersChanged)
  66. def extruderIds(self) -> Dict[str, str]:
  67. extruder_stack_ids = {} # type: Dict[str, str]
  68. global_container_stack = self._application.getGlobalContainerStack()
  69. if global_container_stack:
  70. extruder_stack_ids = {position: extruder.id for position, extruder in global_container_stack.extruders.items()}
  71. return extruder_stack_ids
  72. ## Changes the active extruder by index.
  73. #
  74. # \param index The index of the new active extruder.
  75. @pyqtSlot(int)
  76. def setActiveExtruderIndex(self, index: int) -> None:
  77. self._active_extruder_index = index
  78. self.activeExtruderChanged.emit()
  79. @pyqtProperty(int, notify = activeExtruderChanged)
  80. def activeExtruderIndex(self) -> int:
  81. return self._active_extruder_index
  82. ## Gets the extruder name of an extruder of the currently active machine.
  83. #
  84. # \param index The index of the extruder whose name to get.
  85. @pyqtSlot(int, result = str)
  86. def getExtruderName(self, index: int) -> str:
  87. try:
  88. return self.getActiveExtruderStacks()[index].getName()
  89. except IndexError:
  90. return ""
  91. ## Emitted whenever the selectedObjectExtruders property changes.
  92. selectedObjectExtrudersChanged = pyqtSignal()
  93. ## Provides a list of extruder IDs used by the current selected objects.
  94. @pyqtProperty("QVariantList", notify = selectedObjectExtrudersChanged)
  95. def selectedObjectExtruders(self) -> List[Union[str, "ExtruderStack"]]:
  96. if not self._selected_object_extruders:
  97. object_extruders = set()
  98. # First, build a list of the actual selected objects (including children of groups, excluding group nodes)
  99. selected_nodes = [] # type: List["SceneNode"]
  100. for node in Selection.getAllSelectedObjects():
  101. if node.callDecoration("isGroup"):
  102. for grouped_node in BreadthFirstIterator(node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  103. if grouped_node.callDecoration("isGroup"):
  104. continue
  105. selected_nodes.append(grouped_node)
  106. else:
  107. selected_nodes.append(node)
  108. # Then, figure out which nodes are used by those selected nodes.
  109. current_extruder_trains = self.getActiveExtruderStacks()
  110. for node in selected_nodes:
  111. extruder = node.callDecoration("getActiveExtruder")
  112. if extruder:
  113. object_extruders.add(extruder)
  114. elif current_extruder_trains:
  115. object_extruders.add(current_extruder_trains[0].getId())
  116. self._selected_object_extruders = list(object_extruders) # type: List[Union[str, "ExtruderStack"]]
  117. return self._selected_object_extruders
  118. ## Reset the internal list used for the selectedObjectExtruders property
  119. #
  120. # This will trigger a recalculation of the extruders used for the
  121. # selection.
  122. def resetSelectedObjectExtruders(self) -> None:
  123. self._selected_object_extruders = [] # type: List[Union[str, "ExtruderStack"]]
  124. self.selectedObjectExtrudersChanged.emit()
  125. @pyqtSlot(result = QObject)
  126. def getActiveExtruderStack(self) -> Optional["ExtruderStack"]:
  127. return self.getExtruderStack(self._active_extruder_index)
  128. ## Get an extruder stack by index
  129. def getExtruderStack(self, index) -> Optional["ExtruderStack"]:
  130. global_container_stack = self._application.getGlobalContainerStack()
  131. if global_container_stack:
  132. if global_container_stack.getId() in self._extruder_trains:
  133. if str(index) in self._extruder_trains[global_container_stack.getId()]:
  134. return self._extruder_trains[global_container_stack.getId()][str(index)]
  135. return None
  136. def registerExtruder(self, extruder_train: "ExtruderStack", machine_id: str) -> None:
  137. changed = False
  138. if machine_id not in self._extruder_trains:
  139. self._extruder_trains[machine_id] = {}
  140. changed = True
  141. # do not register if an extruder has already been registered at the position on this machine
  142. if any(item.getId() == extruder_train.getId() for item in self._extruder_trains[machine_id].values()):
  143. Logger.log("w", "Extruder [%s] has already been registered on machine [%s], not doing anything",
  144. extruder_train.getId(), machine_id)
  145. return
  146. if extruder_train:
  147. self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train
  148. changed = True
  149. if changed:
  150. self.extrudersChanged.emit(machine_id)
  151. ## Gets a property of a setting for all extruders.
  152. #
  153. # \param setting_key \type{str} The setting to get the property of.
  154. # \param property \type{str} The property to get.
  155. # \return \type{List} the list of results
  156. def getAllExtruderSettings(self, setting_key: str, prop: str) -> List:
  157. result = []
  158. for extruder_stack in self.getActiveExtruderStacks():
  159. result.append(extruder_stack.getProperty(setting_key, prop))
  160. return result
  161. def extruderValueWithDefault(self, value: str) -> str:
  162. machine_manager = self._application.getMachineManager()
  163. if value == "-1":
  164. return machine_manager.defaultExtruderPosition
  165. else:
  166. return value
  167. ## Gets the extruder stacks that are actually being used at the moment.
  168. #
  169. # An extruder stack is being used if it is the extruder to print any mesh
  170. # with, or if it is the support infill extruder, the support interface
  171. # extruder, or the bed adhesion extruder.
  172. #
  173. # If there are no extruders, this returns the global stack as a singleton
  174. # list.
  175. #
  176. # \return A list of extruder stacks.
  177. def getUsedExtruderStacks(self) -> List["ContainerStack"]:
  178. global_stack = self._application.getGlobalContainerStack()
  179. container_registry = ContainerRegistry.getInstance()
  180. used_extruder_stack_ids = set()
  181. # Get the extruders of all meshes in the scene
  182. support_enabled = False
  183. support_bottom_enabled = False
  184. support_roof_enabled = False
  185. scene_root = self._application.getController().getScene().getRoot()
  186. # If no extruders are registered in the extruder manager yet, return an empty array
  187. if len(self.extruderIds) == 0:
  188. return []
  189. # Get the extruders of all printable meshes in the scene
  190. meshes = [node for node in DepthFirstIterator(scene_root) if isinstance(node, SceneNode) and node.isSelectable()] #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  191. for mesh in meshes:
  192. extruder_stack_id = mesh.callDecoration("getActiveExtruder")
  193. if not extruder_stack_id:
  194. # No per-object settings for this node
  195. extruder_stack_id = self.extruderIds["0"]
  196. used_extruder_stack_ids.add(extruder_stack_id)
  197. # Get whether any of them use support.
  198. stack_to_use = mesh.callDecoration("getStack") # if there is a per-mesh stack, we use it
  199. if not stack_to_use:
  200. # if there is no per-mesh stack, we use the build extruder for this mesh
  201. stack_to_use = container_registry.findContainerStacks(id = extruder_stack_id)[0]
  202. support_enabled |= stack_to_use.getProperty("support_enable", "value")
  203. support_bottom_enabled |= stack_to_use.getProperty("support_bottom_enable", "value")
  204. support_roof_enabled |= stack_to_use.getProperty("support_roof_enable", "value")
  205. # Check limit to extruders
  206. limit_to_extruder_feature_list = ["wall_0_extruder_nr",
  207. "wall_x_extruder_nr",
  208. "roofing_extruder_nr",
  209. "top_bottom_extruder_nr",
  210. "infill_extruder_nr",
  211. ]
  212. for extruder_nr_feature_name in limit_to_extruder_feature_list:
  213. extruder_nr = int(global_stack.getProperty(extruder_nr_feature_name, "value"))
  214. if extruder_nr == -1:
  215. continue
  216. used_extruder_stack_ids.add(self.extruderIds[str(extruder_nr)])
  217. # Check support extruders
  218. if support_enabled:
  219. used_extruder_stack_ids.add(self.extruderIds[self.extruderValueWithDefault(str(global_stack.getProperty("support_infill_extruder_nr", "value")))])
  220. used_extruder_stack_ids.add(self.extruderIds[self.extruderValueWithDefault(str(global_stack.getProperty("support_extruder_nr_layer_0", "value")))])
  221. if support_bottom_enabled:
  222. used_extruder_stack_ids.add(self.extruderIds[self.extruderValueWithDefault(str(global_stack.getProperty("support_bottom_extruder_nr", "value")))])
  223. if support_roof_enabled:
  224. used_extruder_stack_ids.add(self.extruderIds[self.extruderValueWithDefault(str(global_stack.getProperty("support_roof_extruder_nr", "value")))])
  225. # The platform adhesion extruder. Not used if using none.
  226. if global_stack.getProperty("adhesion_type", "value") != "none":
  227. extruder_str_nr = str(global_stack.getProperty("adhesion_extruder_nr", "value"))
  228. if extruder_str_nr == "-1":
  229. extruder_str_nr = self._application.getMachineManager().defaultExtruderPosition
  230. used_extruder_stack_ids.add(self.extruderIds[extruder_str_nr])
  231. try:
  232. return [container_registry.findContainerStacks(id = stack_id)[0] for stack_id in used_extruder_stack_ids]
  233. except IndexError: # One or more of the extruders was not found.
  234. Logger.log("e", "Unable to find one or more of the extruders in %s", used_extruder_stack_ids)
  235. return []
  236. ## Removes the container stack and user profile for the extruders for a specific machine.
  237. #
  238. # \param machine_id The machine to remove the extruders for.
  239. def removeMachineExtruders(self, machine_id: str) -> None:
  240. for extruder in self.getMachineExtruders(machine_id):
  241. ContainerRegistry.getInstance().removeContainer(extruder.userChanges.getId())
  242. ContainerRegistry.getInstance().removeContainer(extruder.getId())
  243. if machine_id in self._extruder_trains:
  244. del self._extruder_trains[machine_id]
  245. ## Returns extruders for a specific machine.
  246. #
  247. # \param machine_id The machine to get the extruders of.
  248. def getMachineExtruders(self, machine_id: str) -> List["ExtruderStack"]:
  249. if machine_id not in self._extruder_trains:
  250. return []
  251. return [self._extruder_trains[machine_id][name] for name in self._extruder_trains[machine_id]]
  252. ## Returns the list of active extruder stacks, taking into account the machine extruder count.
  253. #
  254. # \return \type{List[ContainerStack]} a list of
  255. def getActiveExtruderStacks(self) -> List["ExtruderStack"]:
  256. global_stack = self._application.getGlobalContainerStack()
  257. if not global_stack:
  258. return []
  259. result_tuple_list = sorted(list(global_stack.extruders.items()), key = lambda x: int(x[0]))
  260. result_list = [item[1] for item in result_tuple_list]
  261. machine_extruder_count = global_stack.getProperty("machine_extruder_count", "value")
  262. return result_list[:machine_extruder_count]
  263. def _globalContainerStackChanged(self) -> None:
  264. # If the global container changed, the machine changed and might have extruders that were not registered yet
  265. self._addCurrentMachineExtruders()
  266. self.resetSelectedObjectExtruders()
  267. ## Adds the extruders of the currently active machine.
  268. def _addCurrentMachineExtruders(self) -> None:
  269. global_stack = self._application.getGlobalContainerStack()
  270. extruders_changed = False
  271. if global_stack:
  272. container_registry = ContainerRegistry.getInstance()
  273. global_stack_id = global_stack.getId()
  274. # Gets the extruder trains that we just created as well as any that still existed.
  275. extruder_trains = container_registry.findContainerStacks(type = "extruder_train", machine = global_stack_id)
  276. # Make sure the extruder trains for the new machine can be placed in the set of sets
  277. if global_stack_id not in self._extruder_trains:
  278. self._extruder_trains[global_stack_id] = {}
  279. extruders_changed = True
  280. # Register the extruder trains by position
  281. for extruder_train in extruder_trains:
  282. extruder_position = extruder_train.getMetaDataEntry("position")
  283. self._extruder_trains[global_stack_id][extruder_position] = extruder_train
  284. # regardless of what the next stack is, we have to set it again, because of signal routing. ???
  285. extruder_train.setParent(global_stack)
  286. extruder_train.setNextStack(global_stack)
  287. extruders_changed = True
  288. self._fixSingleExtrusionMachineExtruderDefinition(global_stack)
  289. if extruders_changed:
  290. self.extrudersChanged.emit(global_stack_id)
  291. self.setActiveExtruderIndex(0)
  292. # After 3.4, all single-extrusion machines have their own extruder definition files instead of reusing
  293. # "fdmextruder". We need to check a machine here so its extruder definition is correct according to this.
  294. def _fixSingleExtrusionMachineExtruderDefinition(self, global_stack: "GlobalStack") -> None:
  295. container_registry = ContainerRegistry.getInstance()
  296. expected_extruder_definition_0_id = global_stack.getMetaDataEntry("machine_extruder_trains")["0"]
  297. extruder_stack_0 = global_stack.extruders.get("0")
  298. # At this point, extruder stacks for this machine may not have been loaded yet. In this case, need to look in
  299. # the container registry as well.
  300. if not global_stack.extruders:
  301. extruder_trains = container_registry.findContainerStacks(type = "extruder_train",
  302. machine = global_stack.getId())
  303. if extruder_trains:
  304. for extruder in extruder_trains:
  305. if extruder.getMetaDataEntry("position") == "0":
  306. extruder_stack_0 = extruder
  307. break
  308. if extruder_stack_0 is None:
  309. Logger.log("i", "No extruder stack for global stack [%s], create one", global_stack.getId())
  310. # Single extrusion machine without an ExtruderStack, create it
  311. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  312. CuraStackBuilder.createExtruderStackWithDefaultSetup(global_stack, 0)
  313. elif extruder_stack_0.definition.getId() != expected_extruder_definition_0_id:
  314. Logger.log("e", "Single extruder printer [{printer}] expected extruder [{expected}], but got [{got}]. I'm making it [{expected}].".format(
  315. printer = global_stack.getId(), expected = expected_extruder_definition_0_id, got = extruder_stack_0.definition.getId()))
  316. extruder_definition = container_registry.findDefinitionContainers(id = expected_extruder_definition_0_id)[0]
  317. extruder_stack_0.definition = extruder_definition
  318. ## Get all extruder values for a certain setting.
  319. #
  320. # This is exposed to SettingFunction so it can be used in value functions.
  321. #
  322. # \param key The key of the setting to retrieve values for.
  323. #
  324. # \return A list of values for all extruders. If an extruder does not have a value, it will not be in the list.
  325. # If no extruder has the value, the list will contain the global value.
  326. @staticmethod
  327. def getExtruderValues(key: str) -> List[Any]:
  328. global_stack = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()) #We know that there must be a global stack by the time you're requesting setting values.
  329. result = []
  330. for extruder in ExtruderManager.getInstance().getActiveExtruderStacks():
  331. if not extruder.isEnabled:
  332. continue
  333. # only include values from extruders that are "active" for the current machine instance
  334. if int(extruder.getMetaDataEntry("position")) >= global_stack.getProperty("machine_extruder_count", "value"):
  335. continue
  336. value = extruder.getRawProperty(key, "value")
  337. if value is None:
  338. continue
  339. if isinstance(value, SettingFunction):
  340. value = value(extruder)
  341. result.append(value)
  342. if not result:
  343. result.append(global_stack.getProperty(key, "value"))
  344. return result
  345. ## Get all extruder values for a certain setting. This function will skip the user settings container.
  346. #
  347. # This is exposed to SettingFunction so it can be used in value functions.
  348. #
  349. # \param key The key of the setting to retrieve values for.
  350. #
  351. # \return A list of values for all extruders. If an extruder does not have a value, it will not be in the list.
  352. # If no extruder has the value, the list will contain the global value.
  353. @staticmethod
  354. def getDefaultExtruderValues(key: str) -> List[Any]:
  355. global_stack = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()) #We know that there must be a global stack by the time you're requesting setting values.
  356. context = PropertyEvaluationContext(global_stack)
  357. context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  358. context.context["override_operators"] = {
  359. "extruderValue": ExtruderManager.getDefaultExtruderValue,
  360. "extruderValues": ExtruderManager.getDefaultExtruderValues,
  361. "resolveOrValue": ExtruderManager.getDefaultResolveOrValue
  362. }
  363. result = []
  364. for extruder in ExtruderManager.getInstance().getActiveExtruderStacks():
  365. # only include values from extruders that are "active" for the current machine instance
  366. if int(extruder.getMetaDataEntry("position")) >= global_stack.getProperty("machine_extruder_count", "value", context = context):
  367. continue
  368. value = extruder.getRawProperty(key, "value", context = context)
  369. if value is None:
  370. continue
  371. if isinstance(value, SettingFunction):
  372. value = value(extruder, context = context)
  373. result.append(value)
  374. if not result:
  375. result.append(global_stack.getProperty(key, "value", context = context))
  376. return result
  377. ## Return the default extruder position from the machine manager
  378. @staticmethod
  379. def getDefaultExtruderPosition() -> str:
  380. return cura.CuraApplication.CuraApplication.getInstance().getMachineManager().defaultExtruderPosition
  381. ## Get all extruder values for a certain setting.
  382. #
  383. # This is exposed to qml for display purposes
  384. #
  385. # \param key The key of the setting to retrieve values for.
  386. #
  387. # \return String representing the extruder values
  388. @pyqtSlot(str, result="QVariant")
  389. def getInstanceExtruderValues(self, key) -> List:
  390. return ExtruderManager.getExtruderValues(key)
  391. ## Get the value for a setting from a specific extruder.
  392. #
  393. # This is exposed to SettingFunction to use in value functions.
  394. #
  395. # \param extruder_index The index of the extruder to get the value from.
  396. # \param key The key of the setting to get the value of.
  397. #
  398. # \return The value of the setting for the specified extruder or for the
  399. # global stack if not found.
  400. @staticmethod
  401. def getExtruderValue(extruder_index: int, key: str) -> Any:
  402. if extruder_index == -1:
  403. extruder_index = int(cura.CuraApplication.CuraApplication.getInstance().getMachineManager().defaultExtruderPosition)
  404. extruder = ExtruderManager.getInstance().getExtruderStack(extruder_index)
  405. if extruder:
  406. value = extruder.getRawProperty(key, "value")
  407. if isinstance(value, SettingFunction):
  408. value = value(extruder)
  409. else:
  410. # Just a value from global.
  411. value = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()).getProperty(key, "value")
  412. return value
  413. ## Get the default value from the given extruder. This function will skip the user settings container.
  414. #
  415. # This is exposed to SettingFunction to use in value functions.
  416. #
  417. # \param extruder_index The index of the extruder to get the value from.
  418. # \param key The key of the setting to get the value of.
  419. #
  420. # \return The value of the setting for the specified extruder or for the
  421. # global stack if not found.
  422. @staticmethod
  423. def getDefaultExtruderValue(extruder_index: int, key: str) -> Any:
  424. extruder = ExtruderManager.getInstance().getExtruderStack(extruder_index)
  425. context = PropertyEvaluationContext(extruder)
  426. context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  427. context.context["override_operators"] = {
  428. "extruderValue": ExtruderManager.getDefaultExtruderValue,
  429. "extruderValues": ExtruderManager.getDefaultExtruderValues,
  430. "resolveOrValue": ExtruderManager.getDefaultResolveOrValue
  431. }
  432. if extruder:
  433. value = extruder.getRawProperty(key, "value", context = context)
  434. if isinstance(value, SettingFunction):
  435. value = value(extruder, context = context)
  436. else: # Just a value from global.
  437. value = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()).getProperty(key, "value", context = context)
  438. return value
  439. ## Get the resolve value or value for a given key
  440. #
  441. # This is the effective value for a given key, it is used for values in the global stack.
  442. # This is exposed to SettingFunction to use in value functions.
  443. # \param key The key of the setting to get the value of.
  444. #
  445. # \return The effective value
  446. @staticmethod
  447. def getResolveOrValue(key: str) -> Any:
  448. global_stack = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack())
  449. resolved_value = global_stack.getProperty(key, "value")
  450. return resolved_value
  451. ## Get the resolve value or value for a given key without looking the first container (user container)
  452. #
  453. # This is the effective value for a given key, it is used for values in the global stack.
  454. # This is exposed to SettingFunction to use in value functions.
  455. # \param key The key of the setting to get the value of.
  456. #
  457. # \return The effective value
  458. @staticmethod
  459. def getDefaultResolveOrValue(key: str) -> Any:
  460. global_stack = cast(GlobalStack, cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack())
  461. context = PropertyEvaluationContext(global_stack)
  462. context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  463. context.context["override_operators"] = {
  464. "extruderValue": ExtruderManager.getDefaultExtruderValue,
  465. "extruderValues": ExtruderManager.getDefaultExtruderValues,
  466. "resolveOrValue": ExtruderManager.getDefaultResolveOrValue
  467. }
  468. resolved_value = global_stack.getProperty(key, "value", context = context)
  469. return resolved_value
  470. __instance = None # type: ExtruderManager
  471. @classmethod
  472. def getInstance(cls, *args, **kwargs) -> "ExtruderManager":
  473. return cls.__instance