mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-11-28 13:21:10 -07:00
WIP: Rework Material management page
This commit is contained in:
parent
7fa218e9cd
commit
f024f45cfe
5 changed files with 104 additions and 114 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from collections import defaultdict
|
||||
from collections import defaultdict, OrderedDict
|
||||
from typing import Optional
|
||||
|
||||
from PyQt5.Qt import QTimer, QObject, pyqtSignal
|
||||
|
|
@ -17,6 +17,9 @@ class MaterialGroup:
|
|||
self.root_material_node = None
|
||||
self.derived_material_node_list = []
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "%s[%s]" % (self.__class__.__name__, self.name)
|
||||
|
||||
|
||||
class MaterialNode(ContainerNode):
|
||||
__slots__ = ("material_map", "children_map")
|
||||
|
|
@ -45,6 +48,9 @@ class MaterialManager(QObject):
|
|||
self._material_diameter_map = defaultdict() # root_material_id -> diameter -> root_material_id for that diameter
|
||||
self._diameter_material_map = dict() # material id including diameter (generic_pla_175) -> material root id (generic_pla)
|
||||
|
||||
# This is used in Legacy UM3 send material function and the material management page.
|
||||
self._guid_material_groups_map = defaultdict(list) # GUID -> a list of material_groups
|
||||
|
||||
# The machine definition ID for the non-machine-specific materials.
|
||||
# This is used as the last fallback option if the given machine-specific material(s) cannot be found.
|
||||
self._default_machine_definition_id = "fdmprinter"
|
||||
|
|
@ -63,7 +69,7 @@ class MaterialManager(QObject):
|
|||
# Find all materials and put them in a matrix for quick search.
|
||||
material_metadata_list = self._container_registry.findContainersMetadata(type = "material")
|
||||
|
||||
self._material_group_map = {}
|
||||
self._material_group_map = OrderedDict()
|
||||
self._diameter_machine_variant_material_map = {}
|
||||
|
||||
# Map #1
|
||||
|
|
@ -85,6 +91,14 @@ class MaterialManager(QObject):
|
|||
else:
|
||||
new_node = MaterialNode(material_metadata)
|
||||
group.derived_material_node_list.append(new_node)
|
||||
self._material_group_map = OrderedDict(sorted(self._material_group_map.items(), key = lambda x: x[0]))
|
||||
|
||||
# Map #1.5
|
||||
# GUID -> material group list
|
||||
self._guid_material_groups_map = defaultdict(list)
|
||||
for root_material_id, material_group in self._material_group_map.items():
|
||||
guid = material_group.root_material_node.metadata["GUID"]
|
||||
self._guid_material_groups_map[guid].append(material_group)
|
||||
|
||||
# Map #2
|
||||
# Lookup table for material type -> fallback material metadata
|
||||
|
|
@ -198,6 +212,9 @@ class MaterialManager(QObject):
|
|||
def getRootMaterialIDWithoutDiameter(self, root_material_id: str) -> str:
|
||||
return self._diameter_material_map.get(root_material_id)
|
||||
|
||||
def getMaterialGroupListByGUID(self, guid: str) -> Optional[list]:
|
||||
return self._guid_material_groups_map.get(guid)
|
||||
|
||||
#
|
||||
# Return a dict with all root material IDs (k) and ContainerNodes (v) that's suitable for the given setup.
|
||||
#
|
||||
|
|
@ -206,29 +223,27 @@ class MaterialManager(QObject):
|
|||
rounded_diameter = str(round(diameter))
|
||||
if rounded_diameter not in self._diameter_machine_variant_material_map:
|
||||
Logger.log("i", "Cannot find materials with diameter [%s] (rounded to [%s])", diameter, rounded_diameter)
|
||||
return {}
|
||||
return dict()
|
||||
|
||||
# If there are variant materials, get the variant material
|
||||
machine_variant_material_map = self._diameter_machine_variant_material_map[rounded_diameter]
|
||||
machine_node = machine_variant_material_map.get(machine_definition_id)
|
||||
default_machine_node = machine_variant_material_map.get(self._default_machine_definition_id)
|
||||
variant_node = None
|
||||
if machine_node is None:
|
||||
machine_node = machine_variant_material_map.get(self._default_machine_definition_id)
|
||||
if variant_name is not None and machine_node is not None:
|
||||
variant_node = machine_node.getChildNode(variant_name)
|
||||
|
||||
nodes_to_check = [variant_node, machine_node, default_machine_node]
|
||||
|
||||
# Fallback mechanism of finding materials:
|
||||
# 1. variant-specific material
|
||||
# 2. machine-specific material
|
||||
# 3. generic material (for fdmprinter)
|
||||
material_id_metadata_dict = {}
|
||||
if variant_node is not None:
|
||||
material_id_metadata_dict = {mid: node for mid, node in variant_node.material_map.items()}
|
||||
|
||||
# Fallback: machine-specific materials, including "fdmprinter"
|
||||
if not material_id_metadata_dict:
|
||||
if machine_node is not None:
|
||||
material_id_metadata_dict = {mid: node for mid, node in machine_node.material_map.items()}
|
||||
material_id_metadata_dict = dict()
|
||||
for node in nodes_to_check:
|
||||
if node is not None:
|
||||
material_id_metadata_dict = {mid: node for mid, node in variant_node.material_map.items()}
|
||||
break
|
||||
|
||||
return material_id_metadata_dict
|
||||
|
||||
|
|
|
|||
|
|
@ -764,16 +764,16 @@ class ContainerManager(QObject):
|
|||
## Create a duplicate of a material, which has the same GUID and base_file metadata
|
||||
#
|
||||
# \return \type{str} the id of the newly created container.
|
||||
@pyqtSlot(str, result = str)
|
||||
def duplicateMaterial(self, material_id: str) -> str:
|
||||
assert material_id
|
||||
@pyqtSlot("QVariant")
|
||||
def duplicateMaterial(self, material_node):
|
||||
root_material_id = material_node.metadata["base_file"]
|
||||
|
||||
from cura.CuraApplication import CuraApplication
|
||||
material_manager = CuraApplication.getInstance()._material_manager
|
||||
|
||||
material_group = material_manager.getMaterialGroup(material_id)
|
||||
material_group = material_manager.getMaterialGroup(root_material_id)
|
||||
if not material_group:
|
||||
Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", material_id)
|
||||
Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
|
||||
return ""
|
||||
|
||||
base_container = material_group.root_material_node.getContainer()
|
||||
|
|
@ -803,7 +803,7 @@ class ContainerManager(QObject):
|
|||
if container_to_copy.getMetaDataEntry("variant_name"):
|
||||
variant_name = container_to_copy.getMetaDataEntry("variant_name")
|
||||
new_id += "_" + variant_name.replace(" ", "_")
|
||||
if current_id == material_id:
|
||||
if current_id == root_material_id:
|
||||
clone_of_original = new_id
|
||||
|
||||
new_container = copy.deepcopy(container_to_copy)
|
||||
|
|
@ -814,17 +814,6 @@ class ContainerManager(QObject):
|
|||
for container_to_add in new_containers:
|
||||
container_to_add.setDirty(True)
|
||||
ContainerRegistry.getInstance().addContainer(container_to_add)
|
||||
return self._getMaterialContainerIdForActiveMachine(clone_of_original)
|
||||
|
||||
## Create a duplicate of a material or it's original entry
|
||||
#
|
||||
# \return \type{str} the id of the newly created container.
|
||||
@pyqtSlot(str, result = str)
|
||||
def duplicateOriginalMaterial(self, material_id):
|
||||
|
||||
# check if the given material has a base file (i.e. was shipped by default)
|
||||
base_file = self.getContainerMetaDataEntry(material_id, "base_file")
|
||||
return self.duplicateMaterial(base_file)
|
||||
|
||||
## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
|
||||
#
|
||||
|
|
@ -869,72 +858,43 @@ class ContainerManager(QObject):
|
|||
duplicated_container.setName(catalog.i18nc("@label", "Custom Material"))
|
||||
|
||||
self._container_registry.addContainer(duplicated_container)
|
||||
return self._getMaterialContainerIdForActiveMachine(new_id)
|
||||
|
||||
## Find the id of a material container based on the new material
|
||||
# Utilty function that is shared between duplicateMaterial and createMaterial
|
||||
#
|
||||
# \param base_file \type{str} the id of the created container.
|
||||
def _getMaterialContainerIdForActiveMachine(self, base_file):
|
||||
global_stack = Application.getInstance().getGlobalContainerStack()
|
||||
if not global_stack:
|
||||
return base_file
|
||||
|
||||
has_machine_materials = parseBool(global_stack.getMetaDataEntry("has_machine_materials", default = False))
|
||||
has_variant_materials = parseBool(global_stack.getMetaDataEntry("has_variant_materials", default = False))
|
||||
has_variants = parseBool(global_stack.getMetaDataEntry("has_variants", default = False))
|
||||
if has_machine_materials or has_variant_materials:
|
||||
if has_variants:
|
||||
materials = self._container_registry.findInstanceContainersMetadata(type = "material", base_file = base_file, definition = global_stack.getBottom().getId(), variant = self._machine_manager.activeVariantId)
|
||||
else:
|
||||
materials = self._container_registry.findInstanceContainersMetadata(type = "material", base_file = base_file, definition = global_stack.getBottom().getId())
|
||||
|
||||
if materials:
|
||||
return materials[0]["id"]
|
||||
|
||||
Logger.log("w", "Unable to find a suitable container based on %s for the current machine.", base_file)
|
||||
return "" # do not activate a new material if a container can not be found
|
||||
|
||||
return base_file
|
||||
|
||||
## Get a list of materials that have the same GUID as the reference material
|
||||
#
|
||||
# \param material_id \type{str} the id of the material for which to get the linked materials.
|
||||
# \return \type{list} a list of names of materials with the same GUID
|
||||
@pyqtSlot(str, result = "QStringList")
|
||||
def getLinkedMaterials(self, material_id: str):
|
||||
containers = self._container_registry.findInstanceContainersMetadata(id = material_id)
|
||||
if not containers:
|
||||
Logger.log("d", "Unable to find materials linked to material with id %s, because it doesn't exist.", material_id)
|
||||
return []
|
||||
@pyqtSlot("QVariant", result = "QStringList")
|
||||
def getLinkedMaterials(self, material_node):
|
||||
guid = material_node.metadata["GUID"]
|
||||
|
||||
material_container = containers[0]
|
||||
material_base_file = material_container.get("base_file", "")
|
||||
material_guid = material_container.get("GUID", "")
|
||||
if not material_guid:
|
||||
Logger.log("d", "Unable to find materials linked to material with id %s, because it doesn't have a GUID.", material_id)
|
||||
return []
|
||||
from cura.CuraApplication import CuraApplication
|
||||
material_manager = CuraApplication.getInstance()._material_manager
|
||||
|
||||
material_group_list = material_manager.getMaterialGroupListByGUID(guid)
|
||||
|
||||
containers = self._container_registry.findInstanceContainersMetadata(type = "material", GUID = material_guid)
|
||||
linked_material_names = []
|
||||
for container in containers:
|
||||
if container["id"] in [material_id, material_base_file] or container.get("base_file") != container["id"]:
|
||||
continue
|
||||
|
||||
linked_material_names.append(container["name"])
|
||||
if material_group_list:
|
||||
for material_group in material_group_list:
|
||||
linked_material_names.append(material_group.root_material_node.metadata["name"])
|
||||
return linked_material_names
|
||||
|
||||
## Unlink a material from all other materials by creating a new GUID
|
||||
# \param material_id \type{str} the id of the material to create a new GUID for.
|
||||
@pyqtSlot(str)
|
||||
def unlinkMaterial(self, material_id: str):
|
||||
containers = self._container_registry.findInstanceContainers(id=material_id)
|
||||
if not containers:
|
||||
Logger.log("d", "Unable to make the material with id %s unique, because it doesn't exist.", material_id)
|
||||
return ""
|
||||
@pyqtSlot("QVariant")
|
||||
def unlinkMaterial(self, material_node):
|
||||
# Get the material group
|
||||
from cura.CuraApplication import CuraApplication
|
||||
material_manager = CuraApplication.getInstance()._material_manager
|
||||
material_group = material_manager.getMaterialGroup(material_node.metadata["base_file"])
|
||||
|
||||
containers[0].setMetaDataEntry("GUID", str(uuid.uuid4()))
|
||||
# Generate a new GUID
|
||||
new_guid = str(uuid.uuid4())
|
||||
|
||||
# Update the GUID
|
||||
# NOTE: We only need to set the root material container because XmlMaterialProfile.setMetaDataEntry() will
|
||||
# take care of the derived containers too
|
||||
container = material_group.root_material_node.getContainer()
|
||||
container.setMetaDataEntry("GUID", new_guid)
|
||||
|
||||
## Get the singleton instance for this class.
|
||||
@classmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue