Implement type conversion for NodeItem #10

Closed
Bogdan Nagirniak wants to merge 9 commits from BogdanNagirniak/blender:matx-nodeitem-type into matx-export-material

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
2 changed files with 40 additions and 40 deletions
Showing only changes of commit 266f05721b - Show all commits

View File

@ -650,11 +650,6 @@ NodeItem NodeItem::to_color3() const
return res; return res;
} }
bool NodeItem::is_numeric() const
{
return type() >= Type::Float;
}
NodeItem::Type NodeItem::type() const NodeItem::Type NodeItem::type() const
{ {
if (value) { if (value) {
@ -679,43 +674,49 @@ bool NodeItem::is_arithmetic() const
NodeItem NodeItem::arithmetic(const std::string &mx_category, NodeItem NodeItem::arithmetic(const std::string &mx_category,
std::function<float(float)> func) const std::function<float(float)> func) const
{ {
if (!is_numeric()) { NodeItem res = empty();
return empty(); Type t = type();
if (!is_arithmetic(t)) {
return res;
} }
std::string t = value ? value->getTypeString() : node->getType();
NodeItem res(graph_);
if (value) { if (value) {
if (t == "float") { switch (t) {
float v = value->asA<float>(); case Type::Float: {
res.value = MaterialX::Value::createValue<float>(func(v)); float v = value->asA<float>();
} res.value = MaterialX::Value::createValue<float>(func(v));
else if (t == "color3") { break;
auto v = value->asA<MaterialX::Color3>(); }
res.value = MaterialX::Value::createValue<MaterialX::Color3>( case Type::Color3: {
{func(v[0]), func(v[1]), func(v[2])}); auto v = value->asA<MaterialX::Color3>();
} res.value = MaterialX::Value::createValue<MaterialX::Color3>(
else if (t == "color4") { {func(v[0]), func(v[1]), func(v[2])});
auto v = value->asA<MaterialX::Color4>(); break;
res.value = MaterialX::Value::createValue<MaterialX::Color4>( }
{func(v[0]), func(v[1]), func(v[2]), func(v[3])}); case Type::Color4: {
} auto v = value->asA<MaterialX::Color4>();
else if (t == "vector2") { res.value = MaterialX::Value::createValue<MaterialX::Color4>(
auto v = value->asA<MaterialX::Vector2>(); {func(v[0]), func(v[1]), func(v[2]), func(v[3])});
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])}); break;
} }
else if (t == "vector3") { case Type::Vector2: {
auto v = value->asA<MaterialX::Vector3>(); auto v = value->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector3>( res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
{func(v[0]), func(v[1]), func(v[2])}); }
} case Type::Vector3: {
else if (t == "vector4") { auto v = value->asA<MaterialX::Vector3>();
auto v = value->asA<MaterialX::Vector4>(); res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
res.value = MaterialX::Value::createValue<MaterialX::Vector4>( {func(v[0]), func(v[1]), func(v[2])});
{func(v[0]), func(v[1]), func(v[2]), func(v[3])}); break;
} }
else { case Type::Vector4: {
BLI_assert_unreachable(); auto v = value->asA<MaterialX::Vector4>();
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
{func(v[0]), func(v[1]), func(v[2]), func(v[3])});
break;
}
default:
BLI_assert_unreachable();
} }
} }
else { else {

View File

@ -81,7 +81,6 @@ class NodeItem {
NodeItem convert(Type to_type) const; NodeItem convert(Type to_type) const;
NodeItem to_color3() const; NodeItem to_color3() const;
bool is_numeric() const;
Type type() const; Type type() const;
private: private: