diff --git a/cura/CuraPackageManager.py b/cura/CuraPackageManager.py index 813ce40763..0cb18ccc92 100644 --- a/cura/CuraPackageManager.py +++ b/cura/CuraPackageManager.py @@ -10,6 +10,8 @@ from cura.Settings.GlobalStack import GlobalStack from UM.PackageManager import PackageManager # The class we're extending. from UM.Resources import Resources # To find storage paths for some resource types. from UM.i18n import i18nCatalog +from plugins.XmlMaterialProfile.XmlMaterialProfile import XmlMaterialProfile + catalog = i18nCatalog("cura") if TYPE_CHECKING: @@ -59,13 +61,14 @@ class CuraPackageManager(PackageManager): for root, _, file_names in os.walk(material_package.path): if file_name not in file_names: - #File with the name we are looking for is not in this directory + # File with the name we are looking for is not in this directory continue with open(root + "/" + file_name, encoding="utf-8") as f: # Make sure the file we found has the same guid as our material # Parsing this xml would be better but the namespace is needed to search it. - if guid in f.read(): + parsed_guid = XmlMaterialProfile.getMetadataFromSerialized(f.read(), "GUID") + if guid == parsed_guid: return package_id continue diff --git a/plugins/UFPWriter/UFPWriter.py b/plugins/UFPWriter/UFPWriter.py index 59067b1932..c14b5fbfe9 100644 --- a/plugins/UFPWriter/UFPWriter.py +++ b/plugins/UFPWriter/UFPWriter.py @@ -201,10 +201,14 @@ class UFPWriter(MeshWriter): package_manager = cast(CuraPackageManager, CuraApplication.getInstance().getPackageManager()) for extruder in CuraApplication.getInstance().getExtruderManager().getActiveExtruderStacks(): + if not extruder.isEnabled: + # Don't export materials not in use + continue + package_id = package_manager.getMaterialFilePackageId(extruder.material.getFileName(), extruder.material.getMetaDataEntry("GUID")) package_data = package_manager.getInstalledPackageInfo(package_id) - if package_data.get("is_bundled"): + if not package_data or package_data.get("is_bundled"): continue material_metadata = {"id": package_id, diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index d1da10399a..05ae9a79c9 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -480,6 +480,15 @@ class XmlMaterialProfile(InstanceContainer): return version * 1000000 + setting_version + @classmethod + def getMetadataFromSerialized(cls, serialized: str, property_name: str) -> str: + data = ET.fromstring(serialized) + metadata = data.find("./um:metadata", cls.__namespaces) + property = metadata.find("./um:" + property_name, cls.__namespaces) + + # This is a necessary property != None check, xml library overrides __bool__ to return False in cases when Element is not None. + return property.text if property != None else "" + def deserialize(self, serialized, file_name = None): """Overridden from InstanceContainer"""