VariantNode.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.Settings.ContainerRegistry import ContainerRegistry
  5. from UM.Settings.Interfaces import ContainerInterface
  6. from cura.Machines.ContainerNode import ContainerNode
  7. from cura.Machines.MachineNode import MachineNode
  8. from cura.Machines.MaterialNode import MaterialNode
  9. if TYPE_CHECKING:
  10. from typing import Dict
  11. ## This class represents an extruder variant in the container tree.
  12. #
  13. # The subnodes of these nodes are materials.
  14. #
  15. # This node contains materials with ALL filament diameters underneath it. The
  16. # tree of this variant is not specific to one global stack, so because the
  17. # list of materials can be different per stack depending on the compatible
  18. # material diameter setting, we cannot filter them here. Filtering must be
  19. # done in the model.
  20. class VariantNode(ContainerNode):
  21. def __init__(self, container_id: str, parent: MachineNode) -> None:
  22. super().__init__(container_id, parent)
  23. self.materials = {} # type: Dict[str, MaterialNode] # Mapping material base files to their nodes.
  24. container_registry = ContainerRegistry.getInstance()
  25. self.variant_name = container_registry.findContainersMetadata(id = container_id)[0]["name"] #Store our own name so that we can filter more easily.
  26. container_registry.allMetadataLoaded.connect(self._reloadAll)
  27. container_registry.containerAdded.connect(self._materialAdded)
  28. self._reloadAll()
  29. ## (Re)loads all materials under this variant.
  30. def _reloadAll(self):
  31. container_registry = ContainerRegistry.getInstance()
  32. # Find all the materials for this variant's name.
  33. if not self.parent.has_machine_materials: # Printer has no specific materials. Look for all fdmprinter materials.
  34. materials = container_registry.findInstanceContainersMetadata(type = "material", definition = "fdmprinter") # These are ONLY the base materials.
  35. else: # Printer has its own material profiles. Look for material profiles with this printer's definition.
  36. all_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = "fdmprinter")
  37. printer_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.parent.container_id)
  38. variant_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.parent.container_id, variant = self.variant_name)
  39. materials_per_base_file = {material["base_file"]: material for material in all_materials}
  40. materials_per_base_file.update({material["base_file"]: material for material in printer_specific_materials}) # Printer-specific profiles override global ones.
  41. materials_per_base_file.update({material["base_file"]: material for material in variant_specific_materials}) # Variant-specific profiles override all of those.
  42. materials = materials_per_base_file.values()
  43. for material in materials:
  44. base_file = material["base_file"]
  45. if base_file not in self.materials:
  46. self.materials[base_file] = MaterialNode(material["id"], parent = self)
  47. ## When a material gets added to the set of profiles, we need to update our
  48. # tree here.
  49. def _materialAdded(self, container: ContainerInterface):
  50. if container.getMetaDataEntry("type") != "material":
  51. return # Not interested.
  52. material_definition = container.getMetaDataEntry("definition")
  53. if not self.parent.has_machine_materials:
  54. if material_definition != "fdmprinter":
  55. return
  56. base_file = container.getMetaDataEntry("base_file")
  57. 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.
  58. if material_definition != "fdmprinter" and material_definition != self.parent.container_id:
  59. return
  60. material_variant = container.getMetaDataEntry("variant", "empty")
  61. if material_variant != "empty" and material_variant != self.variant_name:
  62. return
  63. else: # We already have this base profile. Replace the base profile if the new one is more specific.
  64. new_definition = container.getMetaDataEntry("definition")
  65. if new_definition == "fdmprinter":
  66. return # Just as unspecific or worse.
  67. if new_definition != self.parent.container_id:
  68. return # Doesn't match this set-up.
  69. original_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = self.materials[base_file].container_id)[0]
  70. original_variant = original_metadata.get("variant", "empty")
  71. if original_variant != "empty" or container.getMetaDataEntry("variant", "empty") == "empty":
  72. return # Original was already specific or just as unspecific as the new one.
  73. self.materials[base_file] = MaterialNode(container.getId(), parent = self)