VariantNode.py 9.3 KB

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