App: refactor MaterialPy to avoid code duplication

This commit is contained in:
wmayer
2024-04-05 13:48:54 +02:00
parent 03a2fe40fa
commit bb10ff72b5
3 changed files with 15 additions and 85 deletions

View File

@@ -63,5 +63,8 @@ Satin, Metalized, Neon GNC, Chrome, Aluminium, Obsidian, Neon PHC, Jade, Ruby or
<Parameter Name="Transparency" Type="Float"/>
</Attribute>
<CustomAttributes />
<ClassDeclarations>public:
static App::Color toColor(PyObject* value);
</ClassDeclarations>
</PythonExport>
</GenerateModel>

View File

@@ -32,11 +32,11 @@
using namespace App;
Color parseColor(PyObject* value)
Color MaterialPy::toColor(PyObject* value)
{
Color cCol;
if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) {
PyObject* item;
PyObject* item {};
item = PyTuple_GetItem(value, 0);
if (PyFloat_Check(item)) {
cCol.r = (float)PyFloat_AsDouble(item);
@@ -65,17 +65,17 @@ Color parseColor(PyObject* value)
}
}
else if (PyLong_Check(item)) {
cCol.r = PyLong_AsLong(item) / 255.0;
cCol.r = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
item = PyTuple_GetItem(value, 1);
if (PyLong_Check(item)) {
cCol.g = PyLong_AsLong(item) / 255.0;
cCol.g = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
}
item = PyTuple_GetItem(value, 2);
if (PyLong_Check(item)) {
cCol.b = PyLong_AsLong(item) / 255.0;
cCol.b = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
@@ -83,7 +83,7 @@ Color parseColor(PyObject* value)
if (PyTuple_Size(value) == 4) {
item = PyTuple_GetItem(value, 3);
if (PyLong_Check(item)) {
cCol.a = PyLong_AsLong(item) / 255.0;
cCol.a = static_cast<float>(PyLong_AsLong(item)) / 255.0F;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
@@ -178,7 +178,7 @@ std::string MaterialPy::representation() const
PyObject* MaterialPy::set(PyObject* args)
{
char* pstr;
char* pstr {};
if (!PyArg_ParseTuple(args, "s", &pstr)) {
return nullptr;
}
@@ -200,7 +200,7 @@ Py::Object MaterialPy::getAmbientColor() const
void MaterialPy::setAmbientColor(Py::Object arg)
{
getMaterialPtr()->ambientColor = parseColor(*arg);
getMaterialPtr()->ambientColor = toColor(*arg);
}
Py::Object MaterialPy::getDiffuseColor() const
@@ -215,7 +215,7 @@ Py::Object MaterialPy::getDiffuseColor() const
void MaterialPy::setDiffuseColor(Py::Object arg)
{
getMaterialPtr()->diffuseColor = parseColor(*arg);
getMaterialPtr()->diffuseColor = toColor(*arg);
}
Py::Object MaterialPy::getEmissiveColor() const
@@ -230,7 +230,7 @@ Py::Object MaterialPy::getEmissiveColor() const
void MaterialPy::setEmissiveColor(Py::Object arg)
{
getMaterialPtr()->emissiveColor = parseColor(*arg);
getMaterialPtr()->emissiveColor = toColor(*arg);
}
Py::Object MaterialPy::getSpecularColor() const
@@ -245,7 +245,7 @@ Py::Object MaterialPy::getSpecularColor() const
void MaterialPy::setSpecularColor(Py::Object arg)
{
getMaterialPtr()->specularColor = parseColor(*arg);
getMaterialPtr()->specularColor = toColor(*arg);
}
Py::Float MaterialPy::getShininess() const

View File

@@ -2560,83 +2560,11 @@ PyObject* PropertyMaterial::getPyObject()
void PropertyMaterial::setPyObject(PyObject* value)
{
App::Color cCol;
if (PyObject_TypeCheck(value, &(MaterialPy::Type))) {
setValue(*static_cast<MaterialPy*>(value)->getMaterialPtr());
}
else if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) {
PyObject* item;
item = PyTuple_GetItem(value, 0);
if (PyFloat_Check(item)) {
cCol.r = (float)PyFloat_AsDouble(item);
item = PyTuple_GetItem(value, 1);
if (PyFloat_Check(item)) {
cCol.g = (float)PyFloat_AsDouble(item);
}
else {
throw Base::TypeError("Type in tuple must be consistent (float)");
}
item = PyTuple_GetItem(value, 2);
if (PyFloat_Check(item)) {
cCol.b = (float)PyFloat_AsDouble(item);
}
else {
throw Base::TypeError("Type in tuple must be consistent (float)");
}
if (PyTuple_Size(value) == 4) {
item = PyTuple_GetItem(value, 3);
if (PyFloat_Check(item)) {
cCol.a = (float)PyFloat_AsDouble(item);
}
else {
throw Base::TypeError("Type in tuple must be consistent (float)");
}
}
setValue(cCol);
}
else if (PyLong_Check(item)) {
cCol.r = PyLong_AsLong(item) / 255.0;
item = PyTuple_GetItem(value, 1);
if (PyLong_Check(item)) {
cCol.g = PyLong_AsLong(item) / 255.0;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
}
item = PyTuple_GetItem(value, 2);
if (PyLong_Check(item)) {
cCol.b = PyLong_AsLong(item) / 255.0;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
}
if (PyTuple_Size(value) == 4) {
item = PyTuple_GetItem(value, 3);
if (PyLong_Check(item)) {
cCol.a = PyLong_AsLong(item) / 255.0;
}
else {
throw Base::TypeError("Type in tuple must be consistent (integer)");
}
}
setValue(cCol);
}
else {
throw Base::TypeError("Type in tuple must be float or integer");
}
}
else if (PyLong_Check(value)) {
cCol.setPackedValue(PyLong_AsUnsignedLong(value));
setValue(cCol);
}
else {
std::string error = std::string(
"type must be 'Material', integer, tuple of float, or tuple of integer, not ");
error += value->ob_type->tp_name;
throw Base::TypeError(error);
setValue(MaterialPy::toColor(value));
}
}
@@ -3269,7 +3197,6 @@ void PropertyMaterialList::RestoreDocFileV1(Base::Reader& reader)
std::vector<Material> values(count);
uint32_t value; // must be 32 bit long
float valueF;
char valueS[37]; // UUID length is 36 including '-'s
for (auto& it : values) {
str >> value;
it.ambientColor.setPackedValue(value);