From 2a60613257bbe968943e3894ddcf1da76df4deba Mon Sep 17 00:00:00 2001 From: David Carter Date: Sun, 4 Feb 2024 10:55:56 -0500 Subject: [PATCH] Material: Material editor enhancements Improves compatibility when using the legacy editor. Corrects a number of issues that prevented saved files from being displayed when using the legacy editor. Reduces the verbosity of console messages. --- src/Mod/Material/App/MaterialLoader.cpp | 8 ++-- src/Mod/Material/App/MaterialManager.cpp | 34 +++++++++++++++- src/Mod/Material/App/MaterialManager.h | 2 + src/Mod/Material/App/Materials.cpp | 43 +++++++++++++-------- src/Mod/Material/App/Materials.h | 6 +++ src/Mod/Material/MaterialEditor.py | 8 ++++ src/Mod/Material/materialtools/cardutils.py | 5 +-- 7 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/Mod/Material/App/MaterialLoader.cpp b/src/Mod/Material/App/MaterialLoader.cpp index fc0e6f14c7..7008555db0 100644 --- a/src/Mod/Material/App/MaterialLoader.cpp +++ b/src/Mod/Material/App/MaterialLoader.cpp @@ -242,8 +242,8 @@ void MaterialYamlEntry::addToTree( QString propertyValue = QString::fromStdString((itp->second).as()); if (type == MaterialValue::Image) { - propertyValue = - propertyValue.remove(QRegularExpression(QString::fromStdString("[\r\n]"))); + propertyValue = propertyValue.remove( + QRegularExpression(QString::fromStdString("[\r\n]"))); } finalModel->setPhysicalValue(QString::fromStdString(propertyName), propertyValue); @@ -309,8 +309,8 @@ void MaterialYamlEntry::addToTree( QString propertyValue = QString::fromStdString((itp->second).as()); if (type == MaterialValue::Image) { - propertyValue = - propertyValue.remove(QRegularExpression(QString::fromStdString("[\r\n]"))); + propertyValue = propertyValue.remove( + QRegularExpression(QString::fromStdString("[\r\n]"))); } finalModel->setAppearanceValue(QString::fromStdString(propertyName), propertyValue); diff --git a/src/Mod/Material/App/MaterialManager.cpp b/src/Mod/Material/App/MaterialManager.cpp index 72c801dbd8..b1094cba60 100644 --- a/src/Mod/Material/App/MaterialManager.cpp +++ b/src/Mod/Material/App/MaterialManager.cpp @@ -29,6 +29,7 @@ #include #include "Exceptions.h" +#include "MaterialConfigLoader.h" #include "MaterialLoader.h" #include "MaterialManager.h" #include "ModelManager.h" @@ -123,7 +124,23 @@ std::shared_ptr MaterialManager::getMaterialByPath(const QString& path for (auto& library : *_libraryList) { if (cleanPath.startsWith(library->getDirectory())) { - return library->getMaterialByPath(cleanPath); + try { + return library->getMaterialByPath(cleanPath); + } catch (const MaterialNotFound&) {} + + // See if it's a new file saved by the old editor + { + QMutexLocker locker(&_mutex); + + if (MaterialConfigLoader::isConfigStyle(path)) { + auto material = MaterialConfigLoader::getMaterialFromPath(library, path); + if (material) { + (*_materialMap)[material->getUUID()] = library->addMaterial(material, path); + } + + return material; + } + } } } @@ -244,6 +261,21 @@ MaterialManager::materialsWithModelComplete(const QString& uuid) const return dict; } +void MaterialManager::dereference() const +{ + // First clear the inheritences + for (auto& it : *_materialMap) { + auto material = it.second; + material->clearDereferenced(); + material->clearInherited(); + } + + // Run the dereference again + for (auto& it : *_materialMap) { + dereference(it.second); + } +} + void MaterialManager::dereference(std::shared_ptr material) const { MaterialLoader::dereference(_materialMap, material); diff --git a/src/Mod/Material/App/MaterialManager.h b/src/Mod/Material/App/MaterialManager.h index 34136038a7..400941be6f 100644 --- a/src/Mod/Material/App/MaterialManager.h +++ b/src/Mod/Material/App/MaterialManager.h @@ -85,6 +85,7 @@ public: void deleteRecursive(const std::shared_ptr& library, const QString& path) const { library->deleteRecursive(path); + dereference(); } void remove(const QString& uuid) const { @@ -106,6 +107,7 @@ public: std::shared_ptr>> materialsWithModelComplete(const QString& uuid) const; void dereference(std::shared_ptr material) const; + void dereference() const; private: static std::shared_ptr>> _libraryList; diff --git a/src/Mod/Material/App/Materials.cpp b/src/Mod/Material/App/Materials.cpp index 106bf43bdf..0cb7090d6e 100644 --- a/src/Mod/Material/App/Materials.cpp +++ b/src/Mod/Material/App/Materials.cpp @@ -491,6 +491,19 @@ void Material::clearModels() _appearance.clear(); } +void Material::clearInherited() +{ + _allUuids.clear(); + + // Rebuild the UUID lists without the inherited UUIDs + for (auto& uuid : _physicalUuids) { + _allUuids << uuid; + } + for (auto& uuid : _appearanceUuids) { + _allUuids << uuid; + } +} + void Material::setName(const QString& name) { _name = name; @@ -599,14 +612,7 @@ void Material::removePhysical(const QString& uuid) } // If it's an inherited model, do nothing - bool inherited = true; - for (const auto& it : qAsConst(_physicalUuids)) { - if (it == uuid) { - inherited = false; - break; - } - } - if (inherited) { + if (isInherited(uuid)) { return; } @@ -677,14 +683,7 @@ void Material::removeAppearance(const QString& uuid) } // If it's an inherited model, do nothing - bool inherited = true; - for (const auto& it : qAsConst(_appearanceUuids)) { - if (it == uuid) { - inherited = false; - break; - } - } - if (inherited) { + if (isInherited(uuid)) { return; } @@ -988,6 +987,18 @@ bool Material::hasAppearanceProperty(const QString& name) const return true; } +bool Material::isInherited(const QString& uuid) const +{ + if (_physicalUuids.contains(uuid)) { + return false; + } + if (_appearanceUuids.contains(uuid)) { + return false; + } + + return _allUuids.contains(uuid); +} + bool Material::hasModel(const QString& uuid) const { return _allUuids.contains(uuid); diff --git a/src/Mod/Material/App/Materials.h b/src/Mod/Material/App/Materials.h index b5db7743bd..ffc71cac1c 100644 --- a/src/Mod/Material/App/Materials.h +++ b/src/Mod/Material/App/Materials.h @@ -266,6 +266,7 @@ public: void addAppearance(const QString& uuid); void removeAppearance(const QString& uuid); void clearModels(); + void clearInherited(); void newUuid(); void setPhysicalValue(const QString& name, const QString& value); @@ -298,6 +299,7 @@ public: bool hasModel(const QString& uuid) const; bool hasPhysicalModel(const QString& uuid) const; bool hasAppearanceModel(const QString& uuid) const; + bool isInherited(const QString& uuid) const; bool isModelComplete(const QString& uuid) const { return isPhysicalModelComplete(uuid) || isAppearanceModelComplete(uuid); @@ -324,6 +326,10 @@ public: { _dereferenced = true; } + void clearDereferenced() + { + _dereferenced = false; + } bool isOldFormat() const { return _oldFormat; diff --git a/src/Mod/Material/MaterialEditor.py b/src/Mod/Material/MaterialEditor.py index 697693f519..2757a90076 100644 --- a/src/Mod/Material/MaterialEditor.py +++ b/src/Mod/Material/MaterialEditor.py @@ -298,6 +298,7 @@ class MaterialEditor: card_name_list.append([a_name, a_path, self.icons[a_path]]) card_name_list.insert(0, [None, "", ""]) + self.widget.ComboMaterial.clear() for mat in card_name_list: self.widget.ComboMaterial.addItem(QtGui.QIcon(mat[2]), mat[0], mat[1]) @@ -684,9 +685,16 @@ class MaterialEditor: from importFCMat import write write(filename, d) + import Material + # Load the material + manager = Material.MaterialManager() + manager.getMaterialByPath(filename) self.edited = False self.updateCardsInCombo() + # Ensure our card is selected + self.widget.ComboMaterial.setCurrentText(path.stem) + def show(self): return self.widget.show() diff --git a/src/Mod/Material/materialtools/cardutils.py b/src/Mod/Material/materialtools/cardutils.py index 42a6ee8aff..54280d86d8 100644 --- a/src/Mod/Material/materialtools/cardutils.py +++ b/src/Mod/Material/materialtools/cardutils.py @@ -271,14 +271,11 @@ def import_materials(category='Solid', template=False): fluid = ('1ae66d8c-1ba1-4211-ad12-b9917573b202' in physicalModels) if not fluid: path = mat.LibraryRoot + "/" + mat.Directory - print(path) + materials[path] = mat.Properties cards[path] = mat.Name icons[path] = mat.LibraryIcon - print(path) - print(mat.Properties) - return (materials, cards, icons) def add_cards_from_a_dir(materials, cards, icons, mat_dir, icon, template=False):