Implement type conversion for NodeItem #9

Merged
Bogdan Nagirniak merged 12 commits from BogdanNagirniak/blender:matx-nodeitem-type into matx-export-material 2023-09-05 12:03:24 +02:00
2 changed files with 38 additions and 38 deletions
Showing only changes of commit 2895a03e83 - Show all commits

View File

@ -70,8 +70,8 @@ bool NodeItem::operator==(const NodeItem &other) const
NodeItem item1 = *this; NodeItem item1 = *this;
NodeItem item2 = other; NodeItem item2 = other;
Type tp = adjust_types(item1, item2); Type to_type = adjust_types(item1, item2);
if (tp == Type::Empty) { if (to_type == Type::Empty) {
return false; return false;
} }
return item1.value->getValueString() == item2.value->getValueString(); return item1.value->getValueString() == item2.value->getValueString();
@ -238,11 +238,11 @@ NodeItem NodeItem::exp() const
NodeItem NodeItem::convert(Type to_type) const NodeItem NodeItem::convert(Type to_type) const
{ {
Type tp = type(); Type from_type = type();
BogdanNagirniak marked this conversation as resolved Outdated

tp -> from_type

tp -> from_type
if (tp == to_type) { if (from_type == to_type) {
return *this; return *this;
} }
if (!is_arithmetic(tp) || !is_arithmetic(to_type)) { if (!is_arithmetic(from_type) || !is_arithmetic(to_type)) {
return empty(); return empty();
} }
@ -251,7 +251,7 @@ NodeItem NodeItem::convert(Type to_type) const
} }
/* Converting types which requires > 1 iteration */ /* Converting types which requires > 1 iteration */
switch (tp) { switch (from_type) {
case Type::Vector2: case Type::Vector2:
switch (to_type) { switch (to_type) {
case Type::Vector4: case Type::Vector4:
@ -309,7 +309,7 @@ NodeItem NodeItem::convert(Type to_type) const
/* Converting 1 iteration types */ /* Converting 1 iteration types */
NodeItem res = empty(); NodeItem res = empty();
if (value) { if (value) {
switch (tp) { switch (from_type) {
case Type::Float: { case Type::Float: {
float v = value->asA<float>(); float v = value->asA<float>();
switch (to_type) { switch (to_type) {
@ -439,8 +439,8 @@ NodeItem NodeItem::if_else(CompareOp op,
auto item1 = if_val; auto item1 = if_val;
auto item2 = else_val; auto item2 = else_val;
Type tp = adjust_types(item1, item2); Type to_type = adjust_types(item1, item2);
if (tp == Type::Empty) { if (to_type == Type::Empty) {
return res; return res;
} }
@ -467,7 +467,7 @@ NodeItem NodeItem::if_else(CompareOp op,
res = func(value->asA<float>(), other.value->asA<float>()) ? item1 : item2; res = func(value->asA<float>(), other.value->asA<float>()) ? item1 : item2;
} }
else { else {
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, type(tp)); res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, type(to_type));
res.set_input("value1", *this); res.set_input("value1", *this);
res.set_input("value2", other); res.set_input("value2", other);
res.set_input("in1", item1); res.set_input("in1", item1);
@ -498,9 +498,9 @@ void NodeItem::set_input(const std::string &name,
const std::string &output_name) const std::string &output_name)
{ {
if (item.value) { if (item.value) {
Type tp = item.type(); Type item_type = item.type();
std::string mx_type = type(tp); std::string mx_type = type(item_type);
switch (tp) { switch (item_type) {
case Type::String: case Type::String:
set_input(name, item.value->asA<std::string>(), mx_type); set_input(name, item.value->asA<std::string>(), mx_type);
break; break;
@ -553,38 +553,38 @@ void NodeItem::add_output(const std::string &name, Type out_type)
node->addOutput(name, type(out_type)); node->addOutput(name, type(out_type));
} }
NodeItem::Type NodeItem::type(const std::string &tp) NodeItem::Type NodeItem::type(const std::string &type_str)
{ {
if (tp == "string") { if (type_str == "string") {
return Type::String; return Type::String;
} }
if (tp == "integer") { if (type_str == "integer") {
return Type::Integer; return Type::Integer;
} }
if (tp == "float") { if (type_str == "float") {
return Type::Float; return Type::Float;
} }
if (tp == "vector2") { if (type_str == "vector2") {
return Type::Vector2; return Type::Vector2;
} }
if (tp == "vector3") { if (type_str == "vector3") {
return Type::Vector3; return Type::Vector3;
} }
if (tp == "vector4") { if (type_str == "vector4") {
return Type::Vector4; return Type::Vector4;
} }
if (tp == "color3") { if (type_str == "color3") {
return Type::Color3; return Type::Color3;
} }
if (tp == "color4") { if (type_str == "color4") {
return Type::Color4; return Type::Color4;
} }
return Type::Other; return Type::Other;
} }
std::string NodeItem::type(Type tp) std::string NodeItem::type(Type type)
{ {
switch (tp) { switch (type) {
case Type::String: case Type::String:
return "string"; return "string";
case Type::Integer: case Type::Integer:
@ -607,9 +607,9 @@ std::string NodeItem::type(Type tp)
return ""; return "";
} }
bool NodeItem::is_arithmetic(Type tp) bool NodeItem::is_arithmetic(Type type)
{ {
return tp >= Type::Float; return type >= Type::Float;
BogdanNagirniak marked this conversation as resolved Outdated

Type::Float -> Type::Integer

Type::Float -> Type::Integer

Integer isn't used in arithmetic operations

Integer isn't used in arithmetic operations
} }
NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2)
@ -640,13 +640,13 @@ bool NodeItem::is_arithmetic() const
NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(float)> func) const NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(float)> func) const
{ {
NodeItem res = empty(); NodeItem res = empty();
Type tp = type(); Type type = this->type();
if (!is_arithmetic(tp)) { if (!is_arithmetic(type)) {
return res; return res;
} }
if (value) { if (value) {
switch (tp) { switch (type) {
case Type::Float: { case Type::Float: {
float v = value->asA<float>(); float v = value->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v)); res.value = MaterialX::Value::createValue<float>(func(v));
@ -687,7 +687,7 @@ NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(f
else { else {
/* TODO: Some of math functions (sin, cos, ...) doesn't work with Color types, /* TODO: Some of math functions (sin, cos, ...) doesn't work with Color types,
* we have to convert to Vector */ * we have to convert to Vector */
res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(tp)); res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, this->type(type));
res.set_input("in", *this); res.set_input("in", *this);
} }
return res; return res;
@ -700,13 +700,13 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
NodeItem res = empty(); NodeItem res = empty();
NodeItem item1 = *this; NodeItem item1 = *this;
NodeItem item2 = other; NodeItem item2 = other;
Type tp = adjust_types(item1, item2); Type to_type = adjust_types(item1, item2);
if (tp == Type::Empty) { if (to_type == Type::Empty) {
return res; return res;
} }
if (value && other.value) { if (value && other.value) {
switch (tp) { switch (to_type) {
case Type::Float: { case Type::Float: {
float v1 = item1.value->asA<float>(); float v1 = item1.value->asA<float>();
float v2 = item2.value->asA<float>(); float v2 = item2.value->asA<float>();
@ -753,7 +753,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
} }
} }
else { else {
res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(tp)); res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(to_type));
res.set_input("in1", item1); res.set_input("in1", item1);
res.set_input("in2", item2); res.set_input("in2", item2);
} }

View File

@ -12,7 +12,7 @@ class NodeItem {
public: public:
enum class Type { enum class Type {
Empty = 0, Empty = 0,
Other, Other, /* For MaterialX types like: surfaceshader, bsdf, edf, ...*/
BogdanNagirniak marked this conversation as resolved Outdated

Add comment what is Other

Add comment what is `Other`
String, String,
Integer, Integer,
Float, Float,
@ -95,9 +95,9 @@ class NodeItem {
void add_output(const std::string &in_name, Type out_type); void add_output(const std::string &in_name, Type out_type);
private: private:
static Type type(const std::string &tp); static Type type(const std::string &type_str);
static std::string type(Type tp); static std::string type(Type type);
static bool is_arithmetic(Type tp); static bool is_arithmetic(Type type);
static Type adjust_types(NodeItem &item1, NodeItem &item2); static Type adjust_types(NodeItem &item1, NodeItem &item2);
bool is_arithmetic() const; bool is_arithmetic() const;