diff --git a/src/Mod/Material/App/MaterialManager.cpp b/src/Mod/Material/App/MaterialManager.cpp index 8d3496c210..25627c46e3 100644 --- a/src/Mod/Material/App/MaterialManager.cpp +++ b/src/Mod/Material/App/MaterialManager.cpp @@ -138,10 +138,8 @@ bool MaterialManager::isMaterial(const QFileInfo& file) const return false; } -std::shared_ptr MaterialManager::defaultMaterial() +std::shared_ptr MaterialManager::defaultAppearance() { - MaterialManager manager; - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"); @@ -149,8 +147,9 @@ std::shared_ptr MaterialManager::defaultMaterial() uint32_t packed = color.getPackedRGB(); packed = hGrp->GetUnsigned(parameter, packed); color.setPackedRGB(packed); + color.a = 1.0; // The default color sets fully transparent, not opaque }; - auto intRandom = [] (int min, int max) -> int { + auto intRandom = [](int min, int max) -> int { static std::mt19937 generator; std::uniform_int_distribution distribution(min, max); return distribution(generator); @@ -163,7 +162,7 @@ std::shared_ptr MaterialManager::defaultMaterial() float red = static_cast(intRandom(0, 255)) / 255.0F; float green = static_cast(intRandom(0, 255)) / 255.0F; float blue = static_cast(intRandom(0, 255)) / 255.0F; - mat.diffuseColor = App::Color(red, green, blue); + mat.diffuseColor = App::Color(red, green, blue, 1.0); } else { getColor("DefaultShapeColor", mat.diffuseColor); @@ -175,24 +174,34 @@ std::shared_ptr MaterialManager::defaultMaterial() long initialTransparency = hGrp->GetInt("DefaultShapeTransparency", 0); long initialShininess = hGrp->GetInt("DefaultShapeShininess", 90); + mat.shininess = ((float)initialShininess / 100.0F); + mat.transparency = ((float)initialTransparency / 100.0F); + return std::make_shared(mat); +} + +std::shared_ptr MaterialManager::defaultMaterial() +{ + MaterialManager manager; + + auto mat = defaultAppearance(); auto material = manager.getMaterial(defaultMaterialUUID()); if (!material) { material = manager.getMaterial(QLatin1String("7f9fd73b-50c9-41d8-b7b2-575a030c1eeb")); } if (material->hasAppearanceModel(ModelUUIDs::ModelUUID_Rendering_Basic)) { material->getAppearanceProperty(QString::fromLatin1("DiffuseColor")) - ->setColor(mat.diffuseColor); + ->setColor(mat->diffuseColor); material->getAppearanceProperty(QString::fromLatin1("AmbientColor")) - ->setColor(mat.ambientColor); + ->setColor(mat->ambientColor); material->getAppearanceProperty(QString::fromLatin1("EmissiveColor")) - ->setColor(mat.emissiveColor); + ->setColor(mat->emissiveColor); material->getAppearanceProperty(QString::fromLatin1("SpecularColor")) - ->setColor(mat.specularColor); + ->setColor(mat->specularColor); material->getAppearanceProperty(QString::fromLatin1("Transparency")) - ->setFloat((float)initialTransparency / 100.0F); + ->setFloat(mat->transparency); material->getAppearanceProperty(QString::fromLatin1("Shininess")) - ->setFloat((float)initialShininess / 100.0F); + ->setFloat(mat->shininess); } return material; diff --git a/src/Mod/Material/App/MaterialManager.h b/src/Mod/Material/App/MaterialManager.h index dafd663b76..c263e5d8c4 100644 --- a/src/Mod/Material/App/MaterialManager.h +++ b/src/Mod/Material/App/MaterialManager.h @@ -56,6 +56,7 @@ public: static void cleanup(); static void refresh(); + static std::shared_ptr defaultAppearance(); static std::shared_ptr defaultMaterial(); static QString defaultMaterialUUID(); diff --git a/src/Mod/Material/Gui/MaterialTreeWidgetPyImpl.cpp b/src/Mod/Material/Gui/MaterialTreeWidgetPyImpl.cpp index c107880518..8862c5677a 100644 --- a/src/Mod/Material/Gui/MaterialTreeWidgetPyImpl.cpp +++ b/src/Mod/Material/Gui/MaterialTreeWidgetPyImpl.cpp @@ -169,15 +169,12 @@ PyObject* MaterialTreeWidgetPy::setFilter(PyObject* args) } if (PyObject_TypeCheck(obj, &(Materials::MaterialFilterPy::Type))) { auto filter = static_cast(obj)->getMaterialFilterPtr(); - Base::Console().Log("Filter '%s'\n", filter->name().toStdString().c_str()); auto filterPtr = std::make_shared(*filter); getMaterialTreeWidgetPtr()->setFilter(filterPtr); } else if (PyList_Check(obj)) { // The argument is a list of filters - Base::Console().Log("Filter List\n"); Py_ssize_t n = PyList_Size(obj); - Base::Console().Log("n = %d\n", n); if (n < 0) { Py_Return; } @@ -188,13 +185,8 @@ PyObject* MaterialTreeWidgetPy::setFilter(PyObject* args) if (PyObject_TypeCheck(item, &(Materials::MaterialFilterPy::Type))) { auto filter = static_cast(item)->getMaterialFilterPtr(); - Base::Console().Log("\tFilter '%s'\n", - filter->name().toStdString().c_str()); auto filterPtr = - std::make_shared(*filter); + auto filterPtr = std::make_shared(*filter); filterList->push_back(filterPtr); - // getMaterialTreeWidgetPtr()->setFilter( - // - // *static_cast(obj)->getMaterialFilterPtr()); } else { PyErr_Format(PyExc_TypeError, @@ -222,8 +214,6 @@ PyObject* MaterialTreeWidgetPy::selectFilter(PyObject* args) return nullptr; } - Base::Console().Log("selectFilter(%s)\n", name); - Py_Return; } diff --git a/src/Mod/Material/Gui/MaterialsEditor.cpp b/src/Mod/Material/Gui/MaterialsEditor.cpp index fc7687f685..582600b6db 100644 --- a/src/Mod/Material/Gui/MaterialsEditor.cpp +++ b/src/Mod/Material/Gui/MaterialsEditor.cpp @@ -47,6 +47,7 @@ #include #include +#include #include "MaterialDelegate.h" #include "MaterialSave.h" @@ -425,6 +426,13 @@ void MaterialsEditor::onAppearanceAdd(bool checked) if (dialog.exec() == QDialog::Accepted) { QString selected = dialog.selectedModel(); _material->addAppearance(selected); + auto model = getModelManager().getModel(selected); + if (selected == Materials::ModelUUIDs::ModelUUID_Rendering_Basic + || model->inherits(Materials::ModelUUIDs::ModelUUID_Rendering_Basic)) { + // Add default appearance properties + *_material = *(getMaterialManager().defaultAppearance()); + } + updateMaterial(); } else { @@ -1008,6 +1016,7 @@ void MaterialsEditor::updateMaterialAppearance() auto valueItem = new QStandardItem(_material->getAppearanceValueString(key)); valueItem->setToolTip(itp->second.getDescription()); QVariant variant; + // variant.setValue(_material->getAppearanceValueString(key)); variant.setValue(_material); valueItem->setData(variant); items.append(valueItem); diff --git a/src/Mod/Material/Gui/MaterialsEditor.ui b/src/Mod/Material/Gui/MaterialsEditor.ui index 5825062271..1f9704ba17 100644 --- a/src/Mod/Material/Gui/MaterialsEditor.ui +++ b/src/Mod/Material/Gui/MaterialsEditor.ui @@ -209,7 +209,7 @@ - Properties + Physical diff --git a/src/Mod/Part/parttests/ColorTransparencyTest.py b/src/Mod/Part/parttests/ColorTransparencyTest.py index 0d22cdd8df..706a8c4c4e 100644 --- a/src/Mod/Part/parttests/ColorTransparencyTest.py +++ b/src/Mod/Part/parttests/ColorTransparencyTest.py @@ -41,14 +41,18 @@ class ColorTransparencyTest(unittest.TestCase): """ This test isn't currently valid as it draws from the hard coded default material. + + The preference editor doesn't allow for setting transparencies. The default value + of 0 corresponds to a fully transparent color, which is not desireable. It changes + the transparency when loading to 1.0 """ self._pg.SetUnsigned('DefaultShapeColor', 0xff000000) # red obj = self._doc.addObject('Part::Box') - self.assertEqual(obj.ViewObject.ShapeAppearance[0].DiffuseColor, (1.0, 0.0, 0.0, 0.0), + self.assertEqual(obj.ViewObject.ShapeAppearance[0].DiffuseColor, (1.0, 0.0, 0.0, 1.0), 'default shape color was not set correctly') - self.assertEqual(obj.ViewObject.ShapeMaterial.DiffuseColor, (1.0, 0.0, 0.0, 0.0), + self.assertEqual(obj.ViewObject.ShapeMaterial.DiffuseColor, (1.0, 0.0, 0.0, 1.0), 'default material color was not set correctly') diff --git a/tests/src/Mod/Material/App/TestMaterialFilter.py b/tests/src/Mod/Material/App/TestMaterialFilter.py deleted file mode 100644 index 7b5fa85675..0000000000 --- a/tests/src/Mod/Material/App/TestMaterialFilter.py +++ /dev/null @@ -1,104 +0,0 @@ -# ************************************************************************** -# Copyright (c) 2023 David Carter * -# * -# This file is part of the FreeCAD CAx development system. * -# * -# This program is free software; you can redistribute it and/or modify * -# it under the terms of the GNU Lesser General Public License (LGPL) * -# as published by the Free Software Foundation; either version 2 of * -# the License, or (at your option) any later version. * -# for detail see the LICENCE text file. * -# * -# FreeCAD is distributed in the hope that it will be useful, * -# but WITHOUT ANY WARRANTY; without even the implied warranty of * -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -# GNU Library General Public License for more details. * -# * -# You should have received a copy of the GNU Library General Public * -# License along with FreeCAD; if not, write to the Free Software * -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * -# USA * -# ************************************************************************** - -""" -Test module for FreeCAD material cards and APIs -""" -import os - -import unittest -import FreeCAD -import Materials - -parseQuantity = FreeCAD.Units.parseQuantity - -UUIDAcrylicLegacy = "" # This can't be known until it is loaded -UUIDAluminumAppearance = "3c6d0407-66b3-48ea-a2e8-ee843edf0311" -UUIDAluminumMixed = "5f546608-fcbb-40db-98d7-d8e104eb33ce" -UUIDAluminumPhysical = "a8e60089-550d-4370-8e7e-1734db12a3a9" -UUIDBrassAppearance = "fff3d5c8-98c3-4ee2-8fe5-7e17403c48fcc" - - -class MaterialFilterTestCases(unittest.TestCase): - """ - Test class for FreeCAD material cards and APIs - """ - - def setUp(self): - """Setup function to initialize test data""" - self.ModelManager = Materials.ModelManager() - self.MaterialManager = Materials.MaterialManager() - self.uuids = Materials.UUIDs() - - # Use our test files as a custom directory - param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources") - self.customDir = param.GetString("CustomMaterialsDir", "") - self.useBuiltInDir = param.GetBool("UseBuiltInMaterials", False) - self.useWorkbenchDir = param.GetBool("UseMaterialsFromWorkbenches", False) - self.useUserDir = param.GetBool("UseMaterialsFromConfigDir", False) - self.useCustomDir = param.GetBool("UseMaterialsFromCustomDir", False) - - filePath = os.path.dirname(__file__) + os.sep - testPath = filePath + "Materials" - param.SetString("CustomMaterialsDir", testPath) - param.SetBool("UseBuiltInMaterials", False) - param.SetBool("UseMaterialsFromWorkbenches", False) - param.SetBool("UseMaterialsFromConfigDir", False) - param.SetBool("UseMaterialsFromCustomDir", True) - - self.MaterialManager.refresh() - - def tearDown(self): - param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources") - - # Restore preferences - param.SetString("CustomMaterialsDir", self.customDir) - param.SetBool("UseBuiltInMaterials", self.useBuiltInDir) - param.SetBool("UseMaterialsFromWorkbenches", self.useWorkbenchDir) - param.SetBool("UseMaterialsFromConfigDir", self.useUserDir) - param.SetBool("UseMaterialsFromCustomDir", self.useCustomDir) - - self.MaterialManager.refresh() - - def testFilter(self): - """Test that our filter returns the correct materials""" - - # First check that our materials are loading - material = self.MaterialManager.getMaterial(UUIDAluminumAppearance) - self.assertIsNotNone(material) - self.assertEqual(material.Name, "TestAluminumAppearance") - self.assertEqual(material.UUID, UUIDAluminumAppearance) - - material = self.MaterialManager.getMaterial(UUIDAluminumMixed) - self.assertIsNotNone(material) - self.assertEqual(material.Name, "TestAluminumMixed") - self.assertEqual(material.UUID, UUIDAluminumMixed) - - material = self.MaterialManager.getMaterial(UUIDAluminumPhysical) - self.assertIsNotNone(material) - self.assertEqual(material.Name, "TestAluminumPhysical") - self.assertEqual(material.UUID, UUIDAluminumPhysical) - - material = self.MaterialManager.getMaterial(UUIDBrassAppearance) - self.assertIsNotNone(material) - self.assertEqual(material.Name, "TestBrassAppearance") - self.assertEqual(material.UUID, UUIDBrassAppearance)