Move the MaterialX export code into the existing shader node files #18

Merged
Bogdan Nagirniak merged 14 commits from BogdanNagirniak/blender:matx-move-code into matx-export-material 2023-09-12 13:55:27 +02:00
5 changed files with 49 additions and 84 deletions
Showing only changes of commit 912b984f1c - Show all commits

View File

@ -63,6 +63,8 @@ NodeItem::Type NodeItem::type(const std::string &type_str)
std::string NodeItem::type(Type type) std::string NodeItem::type(Type type)
{ {
switch (type) { switch (type) {
case Type::Any:
return "";
case Type::String: case Type::String:
return "string"; return "string";
case Type::Filename: case Type::Filename:

View File

@ -15,18 +15,25 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node, const bNode *node,
const bNodeSocket *socket_out) const bNodeSocket *socket_out,
NodeItem::Type shader_type)
: graph_(graph), : graph_(graph),
depsgraph_(depsgraph), depsgraph_(depsgraph),
material_(material), material_(material),
node_(node), node_(node),
socket_out_(socket_out) socket_out_(socket_out),
shader_type_(shader_type)
{ {
} }
NodeItem NodeParser::compute_full() NodeItem NodeParser::compute_full()
{ {
CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d]", node_->name, node_->typeinfo->type); CLOG_INFO(LOG_MATERIALX_SHADER,
1,
"%s [%d] %s",
node_->name,
node_->typeinfo->type,
NodeItem::type(shader_type_).c_str());
NodeItem res = compute(); NodeItem res = compute();
if (res.node) { if (res.node) {
res.node->setName(node_name()); res.node->setName(node_name());
@ -36,9 +43,14 @@ NodeItem NodeParser::compute_full()
std::string NodeParser::node_name() std::string NodeParser::node_name()
{ {
return MaterialX::createValidName(node_->output_sockets().size() <= 1 ? std::string name = node_->name;
std::string(node_->name) : if (node_->output_sockets().size() > 1) {
std::string(node_->name) + "_" + socket_out_->name); name += std::string("_") + socket_out_->name;
}
if (ELEM(shader_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) {
name += "_" + NodeItem::type(shader_type_);
}
return MaterialX::createValidName(name);
} }
NodeItem NodeParser::create_node(const std::string &category, NodeItem::Type type) NodeItem NodeParser::create_node(const std::string &category, NodeItem::Type type)
@ -145,7 +157,8 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to
#define CASE_NODE_TYPE(type, T) \ #define CASE_NODE_TYPE(type, T) \
case type: \ case type: \
parser = std::make_unique<T>(graph_, depsgraph_, material_, from_node, link->fromsock); \ parser = std::make_unique<T>( \
graph_, depsgraph_, material_, from_node, link->fromsock, NodeItem::Type::Any); \
break; break;
switch (from_node->typeinfo->type) { switch (from_node->typeinfo->type) {
@ -206,51 +219,17 @@ NodeItem NodeParser::get_input_value(const bNodeSocket &socket, NodeItem::Type t
return res; return res;
} }
ShaderNodeParser::ShaderNodeParser(MaterialX::GraphElement *graph, NodeItem NodeParser::get_input_shader(const std::string &name, NodeItem::Type shader_type)
const Depsgraph *depsgraph,
const Material *material,
const bNode *node,
const bNodeSocket *socket_out,
NodeItem::Type shader_type)
: NodeParser(graph, depsgraph, material, node, socket_out), shader_type_(shader_type)
{
}
NodeItem ShaderNodeParser::compute_full()
{
CLOG_INFO(LOG_MATERIALX_SHADER,
1,
"%s [%d] - %s",
node_->name,
node_->typeinfo->type,
NodeItem::type(shader_type_).c_str());
NodeItem res = compute();
if (res.node) {
res.node->setName(node_name());
}
return res;
}
std::string ShaderNodeParser::node_name()
{
std::string name = NodeParser::node_name();
if (shader_type_ != NodeItem::Type::SurfaceShader) {
name += "_" + NodeItem::type(shader_type_);
}
return name;
}
NodeItem ShaderNodeParser::get_input_shader(const std::string &name, NodeItem::Type shader_type)
{ {
return get_input_shader(node_->input_by_identifier(name), shader_type); return get_input_shader(node_->input_by_identifier(name), shader_type);
} }
NodeItem ShaderNodeParser::get_input_shader(int index, NodeItem::Type shader_type) NodeItem NodeParser::get_input_shader(int index, NodeItem::Type shader_type)
{ {
return get_input_shader(node_->input_socket(index), shader_type); return get_input_shader(node_->input_socket(index), shader_type);
} }
NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type) NodeItem NodeParser::get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type)
{ {
NodeItem res = empty(); NodeItem res = empty();
@ -271,7 +250,7 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem:
} }
/* Creating required ShaderNodeParser object */ /* Creating required ShaderNodeParser object */
std::unique_ptr<ShaderNodeParser> parser; std::unique_ptr<NodeParser> parser;
#define CASE_SHADER_NODE_TYPE(type, T) \ #define CASE_SHADER_NODE_TYPE(type, T) \
case type: \ case type: \

View File

@ -19,13 +19,15 @@ class NodeParser {
const Material *material_; const Material *material_;
const bNode *node_; const bNode *node_;
const bNodeSocket *socket_out_; const bNodeSocket *socket_out_;
NodeItem::Type shader_type_;
public: public:
NodeParser(MaterialX::GraphElement *graph, NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node, const bNode *node,
const bNodeSocket *socket_out); const bNodeSocket *socket_out,
NodeItem::Type shader_type);
virtual ~NodeParser() = default; virtual ~NodeParser() = default;
virtual NodeItem compute() = 0; virtual NodeItem compute() = 0;
@ -40,6 +42,8 @@ class NodeParser {
NodeItem get_input_link(int index, NodeItem::Type to_type); NodeItem get_input_link(int index, NodeItem::Type to_type);
NodeItem get_input_value(const std::string &name, NodeItem::Type to_type); NodeItem get_input_value(const std::string &name, NodeItem::Type to_type);
NodeItem get_input_value(int index, NodeItem::Type to_type); NodeItem get_input_value(int index, NodeItem::Type to_type);
NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type);
NodeItem get_input_shader(int index, NodeItem::Type shader_type);
NodeItem empty() const; NodeItem empty() const;
template<class T> NodeItem val(const T &data) const; template<class T> NodeItem val(const T &data) const;
NodeItem texcoord_node(); NodeItem texcoord_node();
@ -48,27 +52,6 @@ class NodeParser {
NodeItem get_input_default(const bNodeSocket &socket, NodeItem::Type to_type); NodeItem get_input_default(const bNodeSocket &socket, NodeItem::Type to_type);
NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type to_type); NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type to_type);
NodeItem get_input_value(const bNodeSocket &socket, NodeItem::Type to_type); NodeItem get_input_value(const bNodeSocket &socket, NodeItem::Type to_type);
};
class ShaderNodeParser : public NodeParser {
protected:
NodeItem::Type shader_type_;
public:
ShaderNodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node,
const bNodeSocket *socket_out,
NodeItem::Type shader_type);
protected:
NodeItem compute_full() override;
std::string node_name() override;
NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type);
NodeItem get_input_shader(int index, NodeItem::Type shader_type);
private:
NodeItem get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type); NodeItem get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type);
}; };
@ -118,25 +101,26 @@ DECLARE_NODE_PARSER(TexNoiseNodeParser)
DECLARE_NODE_PARSER(VectorMathNodeParser) DECLARE_NODE_PARSER(VectorMathNodeParser)
DECLARE_NODE_PARSER(WavelengthNodeParser) DECLARE_NODE_PARSER(WavelengthNodeParser)
DECLARE_SHADER_NODE_PARSER(AddShaderNodeParser) DECLARE_NODE_PARSER(AddShaderNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFDiffuseNodeParser) DECLARE_NODE_PARSER(BSDFDiffuseNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFGlassNodeParser) DECLARE_NODE_PARSER(BSDFGlassNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFGlossyNodeParser) DECLARE_NODE_PARSER(BSDFGlossyNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFPrincipledNodeParser) DECLARE_NODE_PARSER(BSDFPrincipledNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFRefractionNodeParser) DECLARE_NODE_PARSER(BSDFRefractionNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFSheenNodeParser) DECLARE_NODE_PARSER(BSDFSheenNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFToonNodeParser) DECLARE_NODE_PARSER(BSDFToonNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFTranslucentNodeParser) DECLARE_NODE_PARSER(BSDFTranslucentNodeParser)
DECLARE_SHADER_NODE_PARSER(BSDFTransparentNodeParser) DECLARE_NODE_PARSER(BSDFTransparentNodeParser)
DECLARE_SHADER_NODE_PARSER(EmissionNodeParser) DECLARE_NODE_PARSER(EmissionNodeParser)
DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser) DECLARE_NODE_PARSER(MixShaderNodeParser)
DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser) DECLARE_NODE_PARSER(SubsurfaceScatteringNodeParser)
struct NodeParserData { struct NodeParserData {
MaterialX::GraphElement *graph; MaterialX::GraphElement *graph;
const Depsgraph *depsgraph; const Depsgraph *depsgraph;
const Material *material; const Material *material;
NodeItem::Type shader_type;
NodeItem result; NodeItem result;
}; };
@ -144,7 +128,7 @@ template<class T>
void node_shader_materialx(void *data, struct bNode *node, struct bNodeSocket *out) void node_shader_materialx(void *data, struct bNode *node, struct bNodeSocket *out)
{ {
materialx::NodeParserData *d = reinterpret_cast<materialx::NodeParserData *>(data); materialx::NodeParserData *d = reinterpret_cast<materialx::NodeParserData *>(data);
T parser(d->graph, d->depsgraph, d->material, node, out); T parser(d->graph, d->depsgraph, d->material, node, out, d->shader_type);
d->result = parser.compute(); d->result = parser.compute();
} }

View File

@ -9,7 +9,7 @@ OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *grap
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node)
: ShaderNodeParser(graph, depsgraph, material, node, nullptr, NodeItem::Type::Material) : NodeParser(graph, depsgraph, material, node, nullptr, NodeItem::Type::Material)
{ {
} }

View File

@ -8,7 +8,7 @@
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class OutputMaterialNodeParser : public ShaderNodeParser { class OutputMaterialNodeParser : public NodeParser {
public: public:
OutputMaterialNodeParser(MaterialX::GraphElement *graph, OutputMaterialNodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
@ -16,7 +16,7 @@ class OutputMaterialNodeParser : public ShaderNodeParser {
const bNode *node); const bNode *node);
NodeItem compute() override; NodeItem compute() override;
using ShaderNodeParser::compute_full; using NodeParser::compute_full;
NodeItem compute_default(); NodeItem compute_default();
protected: protected: