MachineNode.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # Copyright (c) 2024 UltiMaker
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Dict, List
  4. from UM.Decorators import deprecated
  5. from UM.Logger import Logger
  6. from UM.Signal import Signal
  7. from UM.Util import parseBool
  8. from UM.Settings.ContainerRegistry import ContainerRegistry # To find all the variants for this machine.
  9. import cura.CuraApplication # Imported like this to prevent circular dependencies.
  10. from cura.Machines.ContainerNode import ContainerNode
  11. from cura.Machines.QualityChangesGroup import QualityChangesGroup # To construct groups of quality changes profiles that belong together.
  12. from cura.Machines.QualityGroup import QualityGroup # To construct groups of quality profiles that belong together.
  13. from cura.Machines.QualityNode import QualityNode
  14. from cura.Machines.VariantNode import VariantNode
  15. from cura.Machines.MaterialNode import MaterialNode
  16. import UM.FlameProfiler
  17. class MachineNode(ContainerNode):
  18. """This class represents a machine in the container tree.
  19. The subnodes of these nodes are variants.
  20. """
  21. def __init__(self, container_id: str) -> None:
  22. super().__init__(container_id)
  23. self.variants = {} # type: Dict[str, VariantNode] # Mapping variant names to their nodes.
  24. self.global_qualities = {} # type: Dict[str, QualityNode] # Mapping quality types to the global quality for those types.
  25. self.materialsChanged = Signal() # Emitted when one of the materials underneath this machine has been changed.
  26. container_registry = ContainerRegistry.getInstance()
  27. try:
  28. my_metadata = container_registry.findContainersMetadata(id = container_id)[0]
  29. except IndexError:
  30. Logger.log("Unable to find metadata for container %s", container_id)
  31. my_metadata = {}
  32. # Some of the metadata is cached upon construction here.
  33. # ONLY DO THAT FOR METADATA THAT DOESN'T CHANGE DURING RUNTIME!
  34. # Otherwise you need to keep it up-to-date during runtime.
  35. self.has_materials = parseBool(my_metadata.get("has_materials", "true"))
  36. self.has_variants = parseBool(my_metadata.get("has_variants", "false"))
  37. self.has_machine_quality = parseBool(my_metadata.get("has_machine_quality", "false"))
  38. self.quality_definition = my_metadata.get("quality_definition", container_id) if self.has_machine_quality else "fdmprinter"
  39. self.exclude_materials = my_metadata.get("exclude_materials", [])
  40. self.preferred_variant_name = my_metadata.get("preferred_variant_name", "")
  41. self.preferred_material = my_metadata.get("preferred_material", "")
  42. self.preferred_quality_type = my_metadata.get("preferred_quality_type", "")
  43. self.supports_abstract_color = parseBool(my_metadata.get("supports_abstract_color", "false"))
  44. self._loadAll()
  45. def getQualityGroups(self, variant_names: List[str], material_bases: List[str], extruder_enabled: List[bool]) -> Dict[str, QualityGroup]:
  46. """Get the available quality groups for this machine.
  47. This returns all quality groups, regardless of whether they are available to the combination of extruders or
  48. not. On the resulting quality groups, the is_available property is set to indicate whether the quality group
  49. can be selected according to the combination of extruders in the parameters.
  50. :param variant_names: The names of the variants loaded in each extruder.
  51. :param material_bases: The base file names of the materials loaded in each extruder.
  52. :param extruder_enabled: Whether or not the extruders are enabled. This allows the function to set the
  53. is_available properly.
  54. :return: For each available quality type, a QualityGroup instance.
  55. """
  56. if len(variant_names) != len(material_bases) or len(variant_names) != len(extruder_enabled):
  57. Logger.log("e", "The number of extruders in the list of variants (" + str(len(variant_names)) + ") is not equal to the number of extruders in the list of materials (" + str(len(material_bases)) + ") or the list of enabled extruders (" + str(len(extruder_enabled)) + ").")
  58. return {}
  59. # For each extruder, find which quality profiles are available. Later we'll intersect the quality types.
  60. qualities_per_type_per_extruder = [{}] * len(variant_names) # type: List[Dict[str, QualityNode]]
  61. for extruder_nr, variant_name in enumerate(variant_names):
  62. if not extruder_enabled[extruder_nr]:
  63. continue # No qualities are available in this extruder. It'll get skipped when calculating the available quality types.
  64. material_base = material_bases[extruder_nr]
  65. if variant_name not in self.variants or material_base not in self.variants[variant_name].materials:
  66. # The printer has no variant/material-specific quality profiles. Use the global quality profiles.
  67. qualities_per_type_per_extruder[extruder_nr] = self.global_qualities
  68. else:
  69. # Use the actually specialised quality profiles.
  70. qualities_per_type_per_extruder[extruder_nr] = {node.quality_type: node for node in self.variants[variant_name].materials[material_base].qualities.values()}
  71. # Create the quality group for each available type.
  72. quality_groups = {}
  73. for quality_type, global_quality_node in self.global_qualities.items():
  74. if not global_quality_node.container:
  75. Logger.log("w", "Node {0} doesn't have a container.".format(global_quality_node.container_id))
  76. continue
  77. quality_groups[quality_type] = QualityGroup(name = global_quality_node.getMetaDataEntry("name", "Unnamed profile"), quality_type = quality_type)
  78. quality_groups[quality_type].node_for_global = global_quality_node
  79. for extruder_position, qualities_per_type in enumerate(qualities_per_type_per_extruder):
  80. if quality_type in qualities_per_type:
  81. quality_groups[quality_type].setExtruderNode(extruder_position, qualities_per_type[quality_type])
  82. available_quality_types = set(quality_groups.keys())
  83. for extruder_nr, qualities_per_type in enumerate(qualities_per_type_per_extruder):
  84. if not extruder_enabled[extruder_nr]:
  85. continue
  86. available_quality_types.intersection_update(qualities_per_type.keys())
  87. for quality_type in available_quality_types:
  88. quality_groups[quality_type].is_available = True
  89. return quality_groups
  90. def getQualityChangesGroups(self, variant_names: List[str], material_bases: List[str], extruder_enabled: List[bool]) -> List[QualityChangesGroup]:
  91. """Returns all of the quality changes groups available to this printer.
  92. The quality changes groups store which quality type and intent category they were made for, but not which
  93. material and nozzle. Instead for the quality type and intent category, the quality changes will always be
  94. available but change the quality type and intent category when activated.
  95. The quality changes group does depend on the printer: Which quality definition is used.
  96. The quality changes groups that are available do depend on the quality types that are available, so it must
  97. still be known which extruders are enabled and which materials and variants are loaded in them. This allows
  98. setting the correct is_available flag.
  99. :param variant_names: The names of the variants loaded in each extruder.
  100. :param material_bases: The base file names of the materials loaded in each extruder.
  101. :param extruder_enabled: For each extruder whether or not they are enabled.
  102. :return: List of all quality changes groups for the printer.
  103. """
  104. machine_quality_changes = ContainerRegistry.getInstance().findContainersMetadata(type = "quality_changes", definition = self.quality_definition) # All quality changes for each extruder.
  105. groups_by_name = {} #type: Dict[str, QualityChangesGroup] # Group quality changes profiles by their display name. The display name must be unique for quality changes. This finds profiles that belong together in a group.
  106. for quality_changes in machine_quality_changes:
  107. name = quality_changes["name"]
  108. if name not in groups_by_name:
  109. # CURA-6599
  110. # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to
  111. # a QObject. Setting the object ownership to QQmlEngine.ObjectOwnership.CppOwnership doesn't work, but setting the object
  112. # parent to application seems to work.
  113. from cura.CuraApplication import CuraApplication
  114. groups_by_name[name] = QualityChangesGroup(name, quality_type = quality_changes["quality_type"],
  115. intent_category = quality_changes.get("intent_category", "default"),
  116. parent = CuraApplication.getInstance())
  117. elif groups_by_name[name].intent_category == "default": # Intent category should be stored as "default" if everything is default or as the intent if any of the extruder have an actual intent.
  118. groups_by_name[name].intent_category = quality_changes.get("intent_category", "default")
  119. if quality_changes.get("position") is not None and quality_changes.get("position") != "None": # An extruder profile.
  120. groups_by_name[name].metadata_per_extruder[int(quality_changes["position"])] = quality_changes
  121. else: # Global profile.
  122. groups_by_name[name].metadata_for_global = quality_changes
  123. quality_groups = self.getQualityGroups(variant_names, material_bases, extruder_enabled)
  124. for quality_changes_group in groups_by_name.values():
  125. if quality_changes_group.quality_type not in quality_groups:
  126. if quality_changes_group.quality_type == "not_supported":
  127. # Quality changes based on an empty profile are always available.
  128. quality_changes_group.is_available = True
  129. else:
  130. quality_changes_group.is_available = False
  131. else:
  132. # Quality changes group is available iff the quality group it depends on is available. Irrespective of whether the intent category is available.
  133. quality_changes_group.is_available = quality_groups[quality_changes_group.quality_type].is_available
  134. return list(groups_by_name.values())
  135. def preferredGlobalQuality(self) -> "QualityNode":
  136. """Gets the preferred global quality node, going by the preferred quality type.
  137. If the preferred global quality is not in there, an arbitrary global quality is taken. If there are no global
  138. qualities, an empty quality is returned.
  139. """
  140. return self.global_qualities.get(self.preferred_quality_type, next(iter(self.global_qualities.values())))
  141. def isExcludedMaterialBaseFile(self, material_base_file: str) -> bool:
  142. """Returns whether the material should be excluded from the list of materials."""
  143. for exclude_material in self.exclude_materials:
  144. if exclude_material in material_base_file:
  145. return True
  146. return False
  147. @deprecated("Use isExcludedMaterialBaseFile instead.", since = "5.9.0")
  148. def isExcludedMaterial(self, material: MaterialNode) -> bool:
  149. """Returns whether the material should be excluded from the list of materials."""
  150. return self.isExcludedMaterialBaseFile(material.base_file)
  151. @UM.FlameProfiler.profile
  152. def _loadAll(self) -> None:
  153. """(Re)loads all variants under this printer."""
  154. container_registry = ContainerRegistry.getInstance()
  155. if not self.has_variants:
  156. self.variants["empty"] = VariantNode("empty_variant", machine=self)
  157. self.variants["empty"].materialsChanged.connect(self.materialsChanged)
  158. else:
  159. # Find all the variants for this definition ID.
  160. variants = container_registry.findInstanceContainersMetadata(type = "variant", definition = self.container_id, hardware_type = "nozzle")
  161. for variant in variants:
  162. variant_name = variant["name"]
  163. if variant_name not in self.variants:
  164. self.variants[variant_name] = VariantNode(variant["id"], machine = self)
  165. self.variants[variant_name].materialsChanged.connect(self.materialsChanged)
  166. else:
  167. # Force reloading the materials if the variant already exists or else materals won't be loaded
  168. # when the G-Code flavor changes --> CURA-7354
  169. self.variants[variant_name]._loadAll()
  170. if not self.variants:
  171. self.variants["empty"] = VariantNode("empty_variant", machine = self)
  172. # Find the global qualities for this printer.
  173. global_qualities = container_registry.findInstanceContainersMetadata(type = "quality", definition = self.quality_definition, global_quality = "True") # First try specific to this printer.
  174. if not global_qualities: # This printer doesn't override the global qualities.
  175. global_qualities = container_registry.findInstanceContainersMetadata(type = "quality", definition = "fdmprinter", global_quality = "True") # Otherwise pick the global global qualities.
  176. if not global_qualities: # There are no global qualities either?! Something went very wrong, but we'll not crash and properly fill the tree.
  177. global_qualities = [cura.CuraApplication.CuraApplication.getInstance().empty_quality_container.getMetaData()]
  178. for global_quality in global_qualities:
  179. self.global_qualities[global_quality["quality_type"]] = QualityNode(global_quality["id"], parent = self)