ExtruderManager.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 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. from UM.Application import Application #To get the global container stack to find the current machine.
  6. from UM.Logger import Logger
  7. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  8. from UM.Scene.SceneNode import SceneNode
  9. from UM.Scene.Selection import Selection
  10. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  11. from UM.Settings.ContainerRegistry import ContainerRegistry #Finding containers by ID.
  12. from UM.Settings.InstanceContainer import InstanceContainer
  13. from UM.Settings.SettingFunction import SettingFunction
  14. from UM.Settings.ContainerStack import ContainerStack
  15. from UM.Settings.DefinitionContainer import DefinitionContainer
  16. from typing import Optional, List
  17. ## Manages all existing extruder stacks.
  18. #
  19. # This keeps a list of extruder stacks for each machine.
  20. class ExtruderManager(QObject):
  21. ## Signal to notify other components when the list of extruders for a machine definition changes.
  22. extrudersChanged = pyqtSignal(QVariant)
  23. ## Signal to notify other components when the global container stack is switched to a definition
  24. # that has different extruders than the previous global container stack
  25. globalContainerStackDefinitionChanged = pyqtSignal()
  26. ## Notify when the user switches the currently active extruder.
  27. activeExtruderChanged = pyqtSignal()
  28. ## Registers listeners and such to listen to changes to the extruders.
  29. def __init__(self, parent = None):
  30. super().__init__(parent)
  31. self._extruder_trains = { } #Per machine, a dictionary of extruder container stack IDs.
  32. self._active_extruder_index = 0
  33. self._selected_object_extruders = []
  34. Application.getInstance().globalContainerStackChanged.connect(self.__globalContainerStackChanged)
  35. self._global_container_stack_definition_id = None
  36. self._addCurrentMachineExtruders()
  37. Selection.selectionChanged.connect(self.resetSelectedObjectExtruders)
  38. ## Gets the unique identifier of the currently active extruder stack.
  39. #
  40. # The currently active extruder stack is the stack that is currently being
  41. # edited.
  42. #
  43. # \return The unique ID of the currently active extruder stack.
  44. @pyqtProperty(str, notify = activeExtruderChanged)
  45. def activeExtruderStackId(self) -> Optional[str]:
  46. if not Application.getInstance().getGlobalContainerStack():
  47. return None # No active machine, so no active extruder.
  48. try:
  49. return self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()][str(self._active_extruder_index)].getId()
  50. 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.
  51. return None
  52. ## Return extruder count according to extruder trains.
  53. @pyqtProperty(int, notify = extrudersChanged)
  54. def extruderCount(self):
  55. if not Application.getInstance().getGlobalContainerStack():
  56. return 0 # No active machine, so no extruders.
  57. try:
  58. return len(self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()])
  59. except KeyError:
  60. return 0
  61. @pyqtProperty("QVariantMap", notify=extrudersChanged)
  62. def extruderIds(self):
  63. map = {}
  64. for position in self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()]:
  65. map[position] = self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()][position].getId()
  66. return map
  67. @pyqtSlot(str, result = str)
  68. def getQualityChangesIdByExtruderStackId(self, id: str) -> str:
  69. for position in self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()]:
  70. extruder = self._extruder_trains[Application.getInstance().getGlobalContainerStack().getId()][position]
  71. if extruder.getId() == id:
  72. return extruder.findContainer(type = "quality_changes").getId()
  73. ## The instance of the singleton pattern.
  74. #
  75. # It's None if the extruder manager hasn't been created yet.
  76. __instance = None
  77. ## Gets an instance of the extruder manager, or creates one if no instance
  78. # exists yet.
  79. #
  80. # This is an implementation of singleton. If an extruder manager already
  81. # exists, it is re-used.
  82. #
  83. # \return The extruder manager.
  84. @classmethod
  85. def getInstance(cls) -> "ExtruderManager":
  86. if not cls.__instance:
  87. cls.__instance = ExtruderManager()
  88. return cls.__instance
  89. ## Changes the active extruder by index.
  90. #
  91. # \param index The index of the new active extruder.
  92. @pyqtSlot(int)
  93. def setActiveExtruderIndex(self, index: int) -> None:
  94. self._active_extruder_index = index
  95. self.activeExtruderChanged.emit()
  96. @pyqtProperty(int, notify = activeExtruderChanged)
  97. def activeExtruderIndex(self) -> int:
  98. return self._active_extruder_index
  99. ## Gets the extruder name of an extruder of the currently active machine.
  100. #
  101. # \param index The index of the extruder whose name to get.
  102. @pyqtSlot(int, result = str)
  103. def getExtruderName(self, index):
  104. try:
  105. return list(self.getActiveExtruderStacks())[index].getName()
  106. except IndexError:
  107. return ""
  108. ## Emitted whenever the selectedObjectExtruders property changes.
  109. selectedObjectExtrudersChanged = pyqtSignal()
  110. ## Provides a list of extruder IDs used by the current selected objects.
  111. @pyqtProperty("QVariantList", notify = selectedObjectExtrudersChanged)
  112. def selectedObjectExtruders(self) -> List[str]:
  113. if not self._selected_object_extruders:
  114. object_extruders = set()
  115. # First, build a list of the actual selected objects (including children of groups, excluding group nodes)
  116. selected_nodes = []
  117. for node in Selection.getAllSelectedObjects():
  118. if node.callDecoration("isGroup"):
  119. for grouped_node in BreadthFirstIterator(node):
  120. if grouped_node.callDecoration("isGroup"):
  121. continue
  122. selected_nodes.append(grouped_node)
  123. else:
  124. selected_nodes.append(node)
  125. # Then, figure out which nodes are used by those selected nodes.
  126. for node in selected_nodes:
  127. extruder = node.callDecoration("getActiveExtruder")
  128. if extruder:
  129. object_extruders.add(extruder)
  130. else:
  131. global_stack = Application.getInstance().getGlobalContainerStack()
  132. object_extruders.add(self._extruder_trains[global_stack.getId()]["0"].getId())
  133. self._selected_object_extruders = list(object_extruders)
  134. return self._selected_object_extruders
  135. ## Reset the internal list used for the selectedObjectExtruders property
  136. #
  137. # This will trigger a recalculation of the extruders used for the
  138. # selection.
  139. def resetSelectedObjectExtruders(self) -> None:
  140. self._selected_object_extruders = []
  141. self.selectedObjectExtrudersChanged.emit()
  142. def getActiveExtruderStack(self) -> ContainerStack:
  143. global_container_stack = Application.getInstance().getGlobalContainerStack()
  144. if global_container_stack:
  145. if global_container_stack.getId() in self._extruder_trains:
  146. if str(self._active_extruder_index) in self._extruder_trains[global_container_stack.getId()]:
  147. return self._extruder_trains[global_container_stack.getId()][str(self._active_extruder_index)]
  148. return None
  149. ## Get an extruder stack by index
  150. def getExtruderStack(self, index):
  151. global_container_stack = Application.getInstance().getGlobalContainerStack()
  152. if global_container_stack:
  153. if global_container_stack.getId() in self._extruder_trains:
  154. if str(index) in self._extruder_trains[global_container_stack.getId()]:
  155. return self._extruder_trains[global_container_stack.getId()][str(index)]
  156. return None
  157. ## Get all extruder stacks
  158. def getExtruderStacks(self):
  159. result = []
  160. for i in range(self.extruderCount):
  161. result.append(self.getExtruderStack(i))
  162. return result
  163. ## Adds all extruders of a specific machine definition to the extruder
  164. # manager.
  165. #
  166. # \param machine_definition The machine definition to add the extruders for.
  167. # \param machine_id The machine_id to add the extruders for.
  168. def addMachineExtruders(self, machine_definition: DefinitionContainer, machine_id: str) -> None:
  169. changed = False
  170. machine_definition_id = machine_definition.getId()
  171. if machine_id not in self._extruder_trains:
  172. self._extruder_trains[machine_id] = { }
  173. changed = True
  174. container_registry = ContainerRegistry.getInstance()
  175. if container_registry:
  176. # Add the extruder trains that don't exist yet.
  177. for extruder_definition in container_registry.findDefinitionContainers(machine = machine_definition_id):
  178. position = extruder_definition.getMetaDataEntry("position", None)
  179. if not position:
  180. Logger.log("w", "Extruder definition %s specifies no position metadata entry.", extruder_definition.getId())
  181. if not container_registry.findContainerStacks(machine = machine_id, position = position): # Doesn't exist yet.
  182. self.createExtruderTrain(extruder_definition, machine_definition, position, machine_id)
  183. changed = True
  184. # Gets the extruder trains that we just created as well as any that still existed.
  185. extruder_trains = container_registry.findContainerStacks(type = "extruder_train", machine = machine_id)
  186. for extruder_train in extruder_trains:
  187. self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train
  188. # regardless of what the next stack is, we have to set it again, because of signal routing.
  189. extruder_train.setNextStack(Application.getInstance().getGlobalContainerStack())
  190. changed = True
  191. if changed:
  192. self.extrudersChanged.emit(machine_id)
  193. def registerExtruder(self, extruder_train, machine_id):
  194. changed = False
  195. if machine_id not in self._extruder_trains:
  196. self._extruder_trains[machine_id] = {}
  197. changed = True
  198. if extruder_train:
  199. self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train
  200. changed = True
  201. if changed:
  202. self.extrudersChanged.emit(machine_id)
  203. ## Creates a container stack for an extruder train.
  204. #
  205. # The container stack has an extruder definition at the bottom, which is
  206. # linked to a machine definition. Then it has a variant profile, a material
  207. # profile, a quality profile and a user profile, in that order.
  208. #
  209. # The resulting container stack is added to the registry.
  210. #
  211. # \param extruder_definition The extruder to create the extruder train for.
  212. # \param machine_definition The machine that the extruder train belongs to.
  213. # \param position The position of this extruder train in the extruder slots of the machine.
  214. # \param machine_id The id of the "global" stack this extruder is linked to.
  215. def createExtruderTrain(self, extruder_definition: DefinitionContainer, machine_definition: DefinitionContainer,
  216. position, machine_id: str) -> None:
  217. # Cache some things.
  218. container_registry = ContainerRegistry.getInstance()
  219. machine_definition_id = Application.getInstance().getMachineManager().getQualityDefinitionId(machine_definition)
  220. # Create a container stack for this extruder.
  221. extruder_stack_id = container_registry.uniqueName(extruder_definition.getId())
  222. container_stack = ContainerStack(extruder_stack_id)
  223. container_stack.setName(extruder_definition.getName()) # Take over the display name to display the stack with.
  224. container_stack.addMetaDataEntry("type", "extruder_train")
  225. container_stack.addMetaDataEntry("machine", machine_id)
  226. container_stack.addMetaDataEntry("position", position)
  227. container_stack.addContainer(extruder_definition)
  228. # Find the variant to use for this extruder.
  229. variant = container_registry.findInstanceContainers(id = "empty_variant")[0]
  230. if machine_definition.getMetaDataEntry("has_variants"):
  231. # First add any variant. Later, overwrite with preference if the preference is valid.
  232. variants = container_registry.findInstanceContainers(definition = machine_definition_id, type = "variant")
  233. if len(variants) >= 1:
  234. variant = variants[0]
  235. preferred_variant_id = machine_definition.getMetaDataEntry("preferred_variant")
  236. if preferred_variant_id:
  237. preferred_variants = container_registry.findInstanceContainers(id = preferred_variant_id, definition = machine_definition_id, type = "variant")
  238. if len(preferred_variants) >= 1:
  239. variant = preferred_variants[0]
  240. else:
  241. Logger.log("w", "The preferred variant \"%s\" of machine %s doesn't exist or is not a variant profile.", preferred_variant_id, machine_id)
  242. # And leave it at the default variant.
  243. container_stack.addContainer(variant)
  244. # Find a material to use for this variant.
  245. material = container_registry.findInstanceContainers(id = "empty_material")[0]
  246. if machine_definition.getMetaDataEntry("has_materials"):
  247. # First add any material. Later, overwrite with preference if the preference is valid.
  248. machine_has_variant_materials = machine_definition.getMetaDataEntry("has_variant_materials", default = False)
  249. if machine_has_variant_materials or machine_has_variant_materials == "True":
  250. materials = container_registry.findInstanceContainers(type = "material", definition = machine_definition_id, variant = variant.getId())
  251. else:
  252. materials = container_registry.findInstanceContainers(type = "material", definition = machine_definition_id)
  253. if len(materials) >= 1:
  254. material = materials[0]
  255. preferred_material_id = machine_definition.getMetaDataEntry("preferred_material")
  256. if preferred_material_id:
  257. global_stack = ContainerRegistry.getInstance().findContainerStacks(id = machine_id)
  258. if global_stack:
  259. approximate_material_diameter = round(global_stack[0].getProperty("material_diameter", "value"))
  260. else:
  261. approximate_material_diameter = round(machine_definition.getProperty("material_diameter", "value"))
  262. search_criteria = { "type": "material", "id": preferred_material_id, "approximate_diameter": approximate_material_diameter}
  263. if machine_definition.getMetaDataEntry("has_machine_materials"):
  264. search_criteria["definition"] = machine_definition_id
  265. if machine_definition.getMetaDataEntry("has_variants") and variant:
  266. search_criteria["variant"] = variant.id
  267. else:
  268. search_criteria["definition"] = "fdmprinter"
  269. preferred_materials = container_registry.findInstanceContainers(**search_criteria)
  270. if len(preferred_materials) >= 1:
  271. # In some cases we get multiple materials. In that case, prefer materials that are marked as read only.
  272. read_only_preferred_materials = [preferred_material for preferred_material in preferred_materials if preferred_material.isReadOnly()]
  273. if len(read_only_preferred_materials) >= 1:
  274. material = read_only_preferred_materials[0]
  275. else:
  276. material = preferred_materials[0]
  277. else:
  278. Logger.log("w", "The preferred material \"%s\" of machine %s doesn't exist or is not a material profile.", preferred_material_id, machine_id)
  279. # And leave it at the default material.
  280. container_stack.addContainer(material)
  281. # Find a quality to use for this extruder.
  282. quality = container_registry.getEmptyInstanceContainer()
  283. search_criteria = { "type": "quality" }
  284. if machine_definition.getMetaDataEntry("has_machine_quality"):
  285. search_criteria["definition"] = machine_definition_id
  286. if machine_definition.getMetaDataEntry("has_materials") and material:
  287. search_criteria["material"] = material.id
  288. else:
  289. search_criteria["definition"] = "fdmprinter"
  290. preferred_quality = machine_definition.getMetaDataEntry("preferred_quality")
  291. if preferred_quality:
  292. search_criteria["id"] = preferred_quality
  293. containers = ContainerRegistry.getInstance().findInstanceContainers(**search_criteria)
  294. if not containers and preferred_quality:
  295. Logger.log("w", "The preferred quality \"%s\" of machine %s doesn't exist or is not a quality profile.", preferred_quality, machine_id)
  296. search_criteria.pop("id", None)
  297. containers = ContainerRegistry.getInstance().findInstanceContainers(**search_criteria)
  298. if containers:
  299. quality = containers[0]
  300. container_stack.addContainer(quality)
  301. empty_quality_changes = container_registry.findInstanceContainers(id = "empty_quality_changes")[0]
  302. container_stack.addContainer(empty_quality_changes)
  303. user_profile = container_registry.findInstanceContainers(type = "user", extruder = extruder_stack_id)
  304. if user_profile: # There was already a user profile, loaded from settings.
  305. user_profile = user_profile[0]
  306. else:
  307. user_profile = InstanceContainer(extruder_stack_id + "_current_settings") # Add an empty user profile.
  308. user_profile.addMetaDataEntry("type", "user")
  309. user_profile.addMetaDataEntry("extruder", extruder_stack_id)
  310. user_profile.setDefinition(machine_definition)
  311. container_registry.addContainer(user_profile)
  312. container_stack.addContainer(user_profile)
  313. # regardless of what the next stack is, we have to set it again, because of signal routing.
  314. container_stack.setNextStack(Application.getInstance().getGlobalContainerStack())
  315. container_registry.addContainer(container_stack)
  316. def getAllExtruderValues(self, setting_key):
  317. return self.getAllExtruderSettings(setting_key, "value")
  318. ## Gets a property of a setting for all extruders.
  319. #
  320. # \param setting_key \type{str} The setting to get the property of.
  321. # \param property \type{str} The property to get.
  322. # \return \type{List} the list of results
  323. def getAllExtruderSettings(self, setting_key, property):
  324. global_container_stack = Application.getInstance().getGlobalContainerStack()
  325. if global_container_stack.getProperty("machine_extruder_count", "value") <= 1:
  326. return [global_container_stack.getProperty(setting_key, property)]
  327. result = []
  328. for index in self.extruderIds:
  329. extruder_stack_id = self.extruderIds[str(index)]
  330. stack = ContainerRegistry.getInstance().findContainerStacks(id = extruder_stack_id)[0]
  331. result.append(stack.getProperty(setting_key, property))
  332. return result
  333. ## Gets the extruder stacks that are actually being used at the moment.
  334. #
  335. # An extruder stack is being used if it is the extruder to print any mesh
  336. # with, or if it is the support infill extruder, the support interface
  337. # extruder, or the bed adhesion extruder.
  338. #
  339. # If there are no extruders, this returns the global stack as a singleton
  340. # list.
  341. #
  342. # \return A list of extruder stacks.
  343. def getUsedExtruderStacks(self):
  344. global_stack = Application.getInstance().getGlobalContainerStack()
  345. container_registry = ContainerRegistry.getInstance()
  346. if global_stack.getProperty("machine_extruder_count", "value") <= 1: #For single extrusion.
  347. return [global_stack]
  348. used_extruder_stack_ids = set()
  349. #Get the extruders of all meshes in the scene.
  350. support_enabled = False
  351. support_bottom_enabled = False
  352. support_roof_enabled = False
  353. scene_root = Application.getInstance().getController().getScene().getRoot()
  354. meshes = [node for node in DepthFirstIterator(scene_root) if type(node) is SceneNode and node.isSelectable()] #Only use the nodes that will be printed.
  355. for mesh in meshes:
  356. extruder_stack_id = mesh.callDecoration("getActiveExtruder")
  357. if not extruder_stack_id: #No per-object settings for this node.
  358. extruder_stack_id = self.extruderIds["0"]
  359. used_extruder_stack_ids.add(extruder_stack_id)
  360. #Get whether any of them use support.
  361. per_mesh_stack = mesh.callDecoration("getStack")
  362. if per_mesh_stack:
  363. support_enabled |= per_mesh_stack.getProperty("support_enable", "value")
  364. support_bottom_enabled |= per_mesh_stack.getProperty("support_bottom_enable", "value")
  365. support_roof_enabled |= per_mesh_stack.getProperty("support_roof_enable", "value")
  366. else: #Take the setting from the build extruder stack.
  367. extruder_stack = container_registry.findContainerStacks(id = extruder_stack_id)[0]
  368. support_enabled |= extruder_stack.getProperty("support_enable", "value")
  369. support_bottom_enabled |= extruder_stack.getProperty("support_bottom_enable", "value")
  370. support_roof_enabled |= extruder_stack.getProperty("support_roof_enable", "value")
  371. #The support extruders.
  372. if support_enabled:
  373. used_extruder_stack_ids.add(self.extruderIds[str(global_stack.getProperty("support_infill_extruder_nr", "value"))])
  374. used_extruder_stack_ids.add(self.extruderIds[str(global_stack.getProperty("support_extruder_nr_layer_0", "value"))])
  375. if support_bottom_enabled:
  376. used_extruder_stack_ids.add(self.extruderIds[str(global_stack.getProperty("support_bottom_extruder_nr", "value"))])
  377. if support_roof_enabled:
  378. used_extruder_stack_ids.add(self.extruderIds[str(global_stack.getProperty("support_roof_extruder_nr", "value"))])
  379. #The platform adhesion extruder. Not used if using none.
  380. if global_stack.getProperty("adhesion_type", "value") != "none":
  381. used_extruder_stack_ids.add(self.extruderIds[str(global_stack.getProperty("adhesion_extruder_nr", "value"))])
  382. try:
  383. return [container_registry.findContainerStacks(id = stack_id)[0] for stack_id in used_extruder_stack_ids]
  384. except IndexError: # One or more of the extruders was not found.
  385. Logger.log("e", "Unable to find one or more of the extruders in %s", used_extruder_stack_ids)
  386. return []
  387. ## Removes the container stack and user profile for the extruders for a specific machine.
  388. #
  389. # \param machine_id The machine to remove the extruders for.
  390. def removeMachineExtruders(self, machine_id):
  391. for extruder in self.getMachineExtruders(machine_id):
  392. containers = ContainerRegistry.getInstance().findInstanceContainers(type = "user", extruder = extruder.getId())
  393. for container in containers:
  394. ContainerRegistry.getInstance().removeContainer(container.getId())
  395. ContainerRegistry.getInstance().removeContainer(extruder.getId())
  396. ## Returns extruders for a specific machine.
  397. #
  398. # \param machine_id The machine to get the extruders of.
  399. def getMachineExtruders(self, machine_id):
  400. if machine_id not in self._extruder_trains:
  401. Logger.log("w", "Tried to get the extruder trains for machine %s, which doesn't exist.", machine_id)
  402. return []
  403. return [self._extruder_trains[machine_id][name] for name in self._extruder_trains[machine_id]]
  404. ## Returns a list containing the global stack and active extruder stacks.
  405. #
  406. # The first element is the global container stack, followed by any extruder stacks.
  407. # \return \type{List[ContainerStack]}
  408. def getActiveGlobalAndExtruderStacks(self):
  409. global_stack = Application.getInstance().getGlobalContainerStack()
  410. if not global_stack:
  411. return None
  412. result = [global_stack]
  413. result.extend(self.getActiveExtruderStacks())
  414. return result
  415. ## Returns the list of active extruder stacks.
  416. #
  417. # \return \type{List[ContainerStack]} a list of
  418. def getActiveExtruderStacks(self):
  419. global_stack = Application.getInstance().getGlobalContainerStack()
  420. result = []
  421. if global_stack:
  422. for extruder in sorted(self._extruder_trains[global_stack.getId()]):
  423. result.append(self._extruder_trains[global_stack.getId()][extruder])
  424. return result
  425. def __globalContainerStackChanged(self) -> None:
  426. self._addCurrentMachineExtruders()
  427. global_container_stack = Application.getInstance().getGlobalContainerStack()
  428. if global_container_stack and global_container_stack.getBottom() and global_container_stack.getBottom().getId() != self._global_container_stack_definition_id:
  429. self._global_container_stack_definition_id = global_container_stack.getBottom().getId()
  430. self.globalContainerStackDefinitionChanged.emit()
  431. self.activeExtruderChanged.emit()
  432. self.resetSelectedObjectExtruders()
  433. ## Adds the extruders of the currently active machine.
  434. def _addCurrentMachineExtruders(self) -> None:
  435. global_stack = Application.getInstance().getGlobalContainerStack()
  436. if global_stack and global_stack.getBottom():
  437. self.addMachineExtruders(global_stack.getBottom(), global_stack.getId())
  438. ## Get all extruder values for a certain setting.
  439. #
  440. # This is exposed to SettingFunction so it can be used in value functions.
  441. #
  442. # \param key The key of the setting to retieve values for.
  443. #
  444. # \return A list of values for all extruders. If an extruder does not have a value, it will not be in the list.
  445. # If no extruder has the value, the list will contain the global value.
  446. @staticmethod
  447. def getExtruderValues(key):
  448. global_stack = Application.getInstance().getGlobalContainerStack()
  449. result = []
  450. for extruder in ExtruderManager.getInstance().getMachineExtruders(global_stack.getId()):
  451. value = extruder.getRawProperty(key, "value")
  452. if value is None:
  453. continue
  454. if isinstance(value, SettingFunction):
  455. value = value(extruder)
  456. result.append(value)
  457. if not result:
  458. result.append(global_stack.getProperty(key, "value"))
  459. return result
  460. ## Get all extruder values for a certain setting.
  461. #
  462. # This is exposed to qml for display purposes
  463. #
  464. # \param key The key of the setting to retieve values for.
  465. #
  466. # \return String representing the extruder values
  467. @pyqtSlot(str, result="QVariant")
  468. def getInstanceExtruderValues(self, key):
  469. return ExtruderManager.getExtruderValues(key)
  470. ## Get the value for a setting from a specific extruder.
  471. #
  472. # This is exposed to SettingFunction to use in value functions.
  473. #
  474. # \param extruder_index The index of the extruder to get the value from.
  475. # \param key The key of the setting to get the value of.
  476. #
  477. # \return The value of the setting for the specified extruder or for the
  478. # global stack if not found.
  479. @staticmethod
  480. def getExtruderValue(extruder_index, key):
  481. extruder = ExtruderManager.getInstance().getExtruderStack(extruder_index)
  482. if extruder:
  483. value = extruder.getRawProperty(key, "value")
  484. if isinstance(value, SettingFunction):
  485. value = value(extruder)
  486. else: #Just a value from global.
  487. value = Application.getInstance().getGlobalContainerStack().getProperty(key, "value")
  488. return value
  489. ## Get the resolve value or value for a given key
  490. #
  491. # This is the effective value for a given key, it is used for values in the global stack.
  492. # This is exposed to SettingFunction to use in value functions.
  493. # \param key The key of the setting to get the value of.
  494. #
  495. # \return The effective value
  496. @staticmethod
  497. def getResolveOrValue(key):
  498. global_stack = Application.getInstance().getGlobalContainerStack()
  499. resolved_value = global_stack.getProperty(key, "resolve")
  500. if resolved_value is not None:
  501. user_container = global_stack.findContainer({"type": "user"})
  502. quality_changes_container = global_stack.findContainer({"type": "quality_changes"})
  503. if user_container.hasProperty(key, "value") or quality_changes_container.hasProperty(key, "value"):
  504. # Normal case
  505. value = global_stack.getProperty(key, "value")
  506. else:
  507. # We have a resolved value and we're using it because of no user and quality_changes value
  508. value = resolved_value
  509. else:
  510. value = global_stack.getRawProperty(key, "value")
  511. return value