VariantNode.py 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.containerAdded.connect(self._materialAdded)
  27. self._loadAll()
  28. ## (Re)loads all materials under this variant.
  29. def _loadAll(self):
  30. container_registry = ContainerRegistry.getInstance()
  31. # Find all the materials for this variant's name.
  32. if not self.parent.has_machine_materials: # Printer has no specific materials. Look for all fdmprinter materials.
  33. materials = container_registry.findInstanceContainersMetadata(type = "material", definition = "fdmprinter") # These are ONLY the base materials.
  34. else: # Printer has its own material profiles. Look for material profiles with this printer's definition.
  35. all_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = "fdmprinter")
  36. printer_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.parent.container_id)
  37. variant_specific_materials = container_registry.findInstanceContainersMetadata(type = "material", definition = self.parent.container_id, variant = self.variant_name)
  38. materials_per_base_file = {material["base_file"]: material for material in all_materials}
  39. materials_per_base_file.update({material["base_file"]: material for material in printer_specific_materials}) # Printer-specific profiles override global ones.
  40. materials_per_base_file.update({material["base_file"]: material for material in variant_specific_materials}) # Variant-specific profiles override all of those.
  41. materials = materials_per_base_file.values()
  42. for excluded_material in self.parent.exclude_materials:
  43. del materials[excluded_material]
  44. for material in materials:
  45. base_file = material["base_file"]
  46. if base_file not in self.materials:
  47. self.materials[base_file] = MaterialNode(material["id"], parent = self)
  48. ## When a material gets added to the set of profiles, we need to update our
  49. # tree here.
  50. def _materialAdded(self, container: ContainerInterface):
  51. if container.getMetaDataEntry("type") != "material":
  52. return # Not interested.
  53. material_definition = container.getMetaDataEntry("definition")
  54. if not self.parent.has_machine_materials:
  55. if material_definition != "fdmprinter":
  56. return
  57. base_file = container.getMetaDataEntry("base_file")
  58. if base_file in self.parent.exclude_materials:
  59. return # Material is forbidden for this printer.
  60. 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.
  61. if material_definition != "fdmprinter" and material_definition != self.parent.container_id:
  62. return
  63. material_variant = container.getMetaDataEntry("variant", "empty")
  64. if material_variant != "empty" and material_variant != self.variant_name:
  65. return
  66. else: # We already have this base profile. Replace the base profile if the new one is more specific.
  67. new_definition = container.getMetaDataEntry("definition")
  68. if new_definition == "fdmprinter":
  69. return # Just as unspecific or worse.
  70. if new_definition != self.parent.container_id:
  71. return # Doesn't match this set-up.
  72. original_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = self.materials[base_file].container_id)[0]
  73. original_variant = original_metadata.get("variant", "empty")
  74. if original_variant != "empty" or container.getMetaDataEntry("variant", "empty") == "empty":
  75. return # Original was already specific or just as unspecific as the new one.
  76. self.materials[base_file] = MaterialNode(container.getId(), parent = self)