VariantNode.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import TYPE_CHECKING
  4. from UM.Logger import Logger
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.Interfaces import ContainerInterface
  7. from UM.Signal import Signal
  8. from cura.Machines.ContainerNode import ContainerNode
  9. from cura.Machines.MaterialNode import MaterialNode
  10. import UM.FlameProfiler
  11. if TYPE_CHECKING:
  12. from typing import Dict
  13. from cura.Machines.MachineNode import MachineNode
  14. ## This class represents an extruder variant in the container tree.
  15. #
  16. # The subnodes of these nodes are materials.
  17. #
  18. # This node contains materials with ALL filament diameters underneath it. The
  19. # tree of this variant is not specific to one global stack, so because the
  20. # list of materials can be different per stack depending on the compatible
  21. # material diameter setting, we cannot filter them here. Filtering must be
  22. # done in the model.
  23. class VariantNode(ContainerNode):
  24. def __init__(self, container_id: str, machine: "MachineNode") -> None:
  25. super().__init__(container_id)
  26. self.machine = machine
  27. self.materials = {} # type: Dict[str, MaterialNode] # Mapping material base files to their nodes.
  28. self.materialsChanged = Signal()
  29. container_registry = ContainerRegistry.getInstance()
  30. self.variant_name = container_registry.findContainersMetadata(id = container_id)[0]["name"] # Store our own name so that we can filter more easily.
  31. container_registry.containerAdded.connect(self._materialAdded)
  32. container_registry.containerRemoved.connect(self._materialRemoved)
  33. self._loadAll()
  34. ## (Re)loads all materials under this variant.
  35. @UM.FlameProfiler.profile
  36. def _loadAll(self) -> None:
  37. container_registry = ContainerRegistry.getInstance()
  38. if not self.machine.has_materials:
  39. self.materials["empty_material"] = MaterialNode("empty_material", variant = self)
  40. return # There should not be any materials loaded for this printer.
  41. # Find all the materials for this variant's name.
  42. else: # Printer has its own material profiles. Look for material profiles with this printer's definition.
  43. base_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = "fdmprinter")
  44. printer_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.machine.container_id)
  45. variant_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.machine.container_id, variant_name = self.variant_name) # If empty_variant, this won't return anything.
  46. materials_per_base_file = {material["base_file"]: material for material in base_materials}
  47. materials_per_base_file.update({material["base_file"]: material for material in printer_specific_materials}) # Printer-specific profiles override global ones.
  48. materials_per_base_file.update({material["base_file"]: material for material in variant_specific_materials}) # Variant-specific profiles override all of those.
  49. materials = list(materials_per_base_file.values())
  50. # Filter materials based on the exclude_materials property.
  51. filtered_materials = [material for material in materials if material["id"] not in self.machine.exclude_materials]
  52. for material in filtered_materials:
  53. base_file = material["base_file"]
  54. if base_file not in self.materials:
  55. self.materials[base_file] = MaterialNode(material["id"], variant = self)
  56. self.materials[base_file].materialChanged.connect(self.materialsChanged)
  57. if not self.materials:
  58. self.materials["empty_material"] = MaterialNode("empty_material", variant = self)
  59. ## Finds the preferred material for this printer with this nozzle in one of
  60. # the extruders.
  61. #
  62. # If the preferred material is not available, an arbitrary material is
  63. # returned. If there is a configuration mistake (like a typo in the
  64. # preferred material) this returns a random available material. If there
  65. # are no available materials, this will return the empty material node.
  66. # \param approximate_diameter The desired approximate diameter of the
  67. # material.
  68. # \return The node for the preferred material, or any arbitrary material
  69. # if there is no match.
  70. def preferredMaterial(self, approximate_diameter: int) -> MaterialNode:
  71. for base_material, material_node in self.materials.items():
  72. if self.machine.preferred_material == base_material and approximate_diameter == int(material_node.getMetaDataEntry("approximate_diameter")):
  73. return material_node
  74. # First fallback: Check if we should be checking for the 175 variant.
  75. if approximate_diameter == 2:
  76. preferred_material = self.machine.preferred_material + "_175"
  77. for base_material, material_node in self.materials.items():
  78. if preferred_material == base_material and approximate_diameter == int(material_node.getMetaDataEntry("approximate_diameter")):
  79. return material_node
  80. # Second fallback: Choose any material with matching diameter.
  81. for material_node in self.materials.values():
  82. if material_node.getMetaDataEntry("approximate_diameter") and approximate_diameter == int(material_node.getMetaDataEntry("approximate_diameter")):
  83. Logger.log("w", "Could not find preferred material %s, falling back to whatever works", self.machine.preferred_material)
  84. return material_node
  85. fallback = next(iter(self.materials.values())) # Should only happen with empty material node.
  86. Logger.log("w", "Could not find preferred material {preferred_material} with diameter {diameter} for variant {variant_id}, falling back to {fallback}.".format(
  87. preferred_material = self.machine.preferred_material,
  88. diameter = approximate_diameter,
  89. variant_id = self.container_id,
  90. fallback = fallback.container_id
  91. ))
  92. return fallback
  93. ## When a material gets added to the set of profiles, we need to update our
  94. # tree here.
  95. @UM.FlameProfiler.profile
  96. def _materialAdded(self, container: ContainerInterface) -> None:
  97. if container.getMetaDataEntry("type") != "material":
  98. return # Not interested.
  99. if not ContainerRegistry.getInstance().findContainersMetadata(id = container.getId()):
  100. # CURA-6889
  101. # containerAdded and removed signals may be triggered in the next event cycle. If a container gets added
  102. # and removed in the same event cycle, in the next cycle, the connections should just ignore the signals.
  103. # The check here makes sure that the container in the signal still exists.
  104. Logger.log("d", "Got container added signal for container [%s] but it no longer exists, do nothing.",
  105. container.getId())
  106. return
  107. if not self.machine.has_materials:
  108. return # We won't add any materials.
  109. material_definition = container.getMetaDataEntry("definition")
  110. base_file = container.getMetaDataEntry("base_file")
  111. if base_file in self.machine.exclude_materials:
  112. return # Material is forbidden for this printer.
  113. if base_file not in self.materials: # Completely new base file. Always better than not having a file as long as it matches our set-up.
  114. if material_definition != "fdmprinter" and material_definition != self.machine.container_id:
  115. return
  116. material_variant = container.getMetaDataEntry("variant_name")
  117. if material_variant is not None and material_variant != self.variant_name:
  118. return
  119. else: # We already have this base profile. Replace the base profile if the new one is more specific.
  120. new_definition = container.getMetaDataEntry("definition")
  121. if new_definition == "fdmprinter":
  122. return # Just as unspecific or worse.
  123. material_variant = container.getMetaDataEntry("variant_name")
  124. if new_definition != self.machine.container_id or material_variant != self.variant_name:
  125. return # Doesn't match this set-up.
  126. original_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = self.materials[base_file].container_id)[0]
  127. if "variant_name" in original_metadata or material_variant is None:
  128. return # Original was already specific or just as unspecific as the new one.
  129. if "empty_material" in self.materials:
  130. del self.materials["empty_material"]
  131. self.materials[base_file] = MaterialNode(container.getId(), variant = self)
  132. self.materials[base_file].materialChanged.connect(self.materialsChanged)
  133. self.materialsChanged.emit(self.materials[base_file])
  134. @UM.FlameProfiler.profile
  135. def _materialRemoved(self, container: ContainerInterface) -> None:
  136. if container.getMetaDataEntry("type") != "material":
  137. return # Only interested in materials.
  138. base_file = container.getMetaDataEntry("base_file")
  139. if base_file not in self.materials:
  140. return # We don't track this material anyway. No need to remove it.
  141. original_node = self.materials[base_file]
  142. del self.materials[base_file]
  143. self.materialsChanged.emit(original_node)
  144. # Now a different material from the same base file may have been hidden because it was not as specific as the one we deleted.
  145. # Search for any submaterials from that base file that are still left.
  146. materials_same_base_file = ContainerRegistry.getInstance().findContainersMetadata(base_file = base_file)
  147. if materials_same_base_file:
  148. most_specific_submaterial = None
  149. for submaterial in materials_same_base_file:
  150. if submaterial["definition"] == self.machine.container_id:
  151. if submaterial.get("variant_name", "empty") == self.variant_name:
  152. most_specific_submaterial = submaterial
  153. break # most specific match possible
  154. if submaterial.get("variant_name", "empty") == "empty":
  155. most_specific_submaterial = submaterial
  156. if most_specific_submaterial is None:
  157. Logger.log("w", "Material %s removed, but no suitable replacement found", base_file)
  158. else:
  159. Logger.log("i", "Material %s (%s) overridden by %s", base_file, self.variant_name, most_specific_submaterial.get("id"))
  160. self.materials[base_file] = MaterialNode(most_specific_submaterial["id"], variant = self)
  161. self.materialsChanged.emit(self.materials[base_file])
  162. if not self.materials: # The last available material just got deleted and there is nothing with the same base file to replace it.
  163. self.materials["empty_material"] = MaterialNode("empty_material", variant = self)
  164. self.materialsChanged.emit(self.materials["empty_material"])