Create parsing system that converts supported nodes and ignores unsupported #2

Merged
Bogdan Nagirniak merged 14 commits from Vasyl-Pidhirskyi/blender:BLEN-500 into matx-export-material 2023-08-28 12:29:46 +02:00
11 changed files with 43 additions and 58 deletions
Showing only changes of commit 185950bbb8 - Show all commits

View File

@ -73,7 +73,8 @@ void MaterialData::init()
/* Create USD material. */ /* Create USD material. */
pxr::UsdShadeMaterial usd_material; pxr::UsdShadeMaterial usd_material;
if (scene_delegate_->use_materialx) { if (scene_delegate_->use_materialx) {
MaterialX::DocumentPtr doc = blender::nodes::materialx::export_to_materialx(scene_delegate_->depsgraph, (Material *)id); MaterialX::DocumentPtr doc = blender::nodes::materialx::export_to_materialx(
scene_delegate_->depsgraph, (Material *)id);
pxr::UsdMtlxRead(doc, stage); pxr::UsdMtlxRead(doc, stage);
if (pxr::UsdPrim materials = stage->GetPrimAtPath(pxr::SdfPath("/MaterialX/Materials"))) { if (pxr::UsdPrim materials = stage->GetPrimAtPath(pxr::SdfPath("/MaterialX/Materials"))) {
pxr::UsdPrimSiblingRange children = materials.GetChildren(); pxr::UsdPrimSiblingRange children = materials.GetChildren();
@ -87,7 +88,8 @@ void MaterialData::init()
} }
/* Convert USD material to Hydra material network map, adapted for render delegate. */ /* Convert USD material to Hydra material network map, adapted for render delegate. */
const pxr::HdRenderDelegate *render_delegate = scene_delegate_->GetRenderIndex().GetRenderDelegate(); const pxr::HdRenderDelegate *render_delegate =
scene_delegate_->GetRenderIndex().GetRenderDelegate();
const pxr::TfTokenVector contextVector = render_delegate->GetMaterialRenderContexts(); const pxr::TfTokenVector contextVector = render_delegate->GetMaterialRenderContexts();
pxr::TfTokenVector shaderSourceTypes = render_delegate->GetShaderSourceTypes(); pxr::TfTokenVector shaderSourceTypes = render_delegate->GetShaderSourceTypes();

View File

@ -148,16 +148,16 @@ if(WITH_MATERIALX)
list(APPEND LIB MaterialXFormat) list(APPEND LIB MaterialXFormat)
list(APPEND SRC list(APPEND SRC
materialx/material.cc materialx/material.cc
materialx/nodes/node.cc materialx/nodes/bsdf_principled.cc
materialx/nodes/material_output.cc materialx/nodes/node_parser.cc
materialx/nodes/principled_bsdf.cc materialx/nodes/output_material.cc
materialx/nodes/image.cc materialx/nodes/tex_image.cc
materialx/material.h materialx/material.h
materialx/nodes/node.h materialx/nodes/bsdf_principled.h
materialx/nodes/material_output.h materialx/nodes/node_parser.h
materialx/nodes/principled_bsdf.h materialx/nodes/output_material.h
materialx/nodes/image.h materialx/nodes/tex_image.h
) )
endif() endif()

View File

@ -3,15 +3,9 @@
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "material.h" #include "material.h"
#include "nodes/material_output.h" #include "nodes/output_material.h"
#include <MaterialXCore/Node.h> #include <MaterialXCore/Node.h>
#include <MaterialXFormat/XmlIo.h>
#include "BLI_fileops.h"
#include "BLI_path_util.h"
#include "BKE_appdir.h"
#include "NOD_shader.h" #include "NOD_shader.h"

View File

@ -2,10 +2,9 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "principled_bsdf.h" #include "bsdf_principled.h"
#include <BKE_node_runtime.hh> #include <BKE_node_runtime.hh>
#include "shader/materialx/nodes/node.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
@ -13,7 +12,7 @@ MaterialXPrincipledBSDFNode::MaterialXPrincipledBSDFNode(MaterialX::DocumentPtr
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node)
: MaterialXNode(doc, depsgraph, material, node) : NodeParser(doc, depsgraph, material, node)
{ {
matx_node = doc->addNode( matx_node = doc->addNode(
"standard_surface", MaterialX::createValidName(node->name), "surfaceshader"); "standard_surface", MaterialX::createValidName(node->name), "surfaceshader");
@ -23,18 +22,17 @@ MaterialX::NodePtr MaterialXPrincipledBSDFNode::convert()
{ {
MaterialX::Color3 default_white_color = MaterialX::Color3(1.0, 1.0, 1.0); MaterialX::Color3 default_white_color = MaterialX::Color3(1.0, 1.0, 1.0);
#pragma region get inputs #pragma region get inputs
const bNodeSocket base_color_socket = node->input_by_identifier("Base Color"); const bNodeSocket base_color_socket = node->input_by_identifier("Base Color");
const char *matx_input = "base_color"; const char *matx_input = "base_color";
if (base_color_socket.link) { if (base_color_socket.link) {
get_input_link_node(&base_color_socket, matx_node, matx_input); get_input_link_node(&base_color_socket, matx_node, matx_input);
} }
else { else {
const float *base_color = base_color_socket.default_value_typed<bNodeSocketValueRGBA>()->value; const float *base_color = base_color_socket.default_value_typed<bNodeSocketValueRGBA>()->value;
matx_node->addInput("base_color", "color3")->setValue(MaterialX::Color3(base_color[0], base_color[1], base_color[2])); matx_node->addInput("base_color", "color3")
->setValue(MaterialX::Color3(base_color[0], base_color[1], base_color[2]));
} }
const float *base_color = const float *base_color =
node->input_by_identifier("Base Color").default_value_typed<bNodeSocketValueRGBA>()->value; node->input_by_identifier("Base Color").default_value_typed<bNodeSocketValueRGBA>()->value;
const float subsurface = const float subsurface =

View File

@ -4,11 +4,11 @@
#pragma once #pragma once
#include "node.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class MaterialXPrincipledBSDFNode : public MaterialXNode { class MaterialXPrincipledBSDFNode : public NodeParser {
public: public:
MaterialXPrincipledBSDFNode(MaterialX::DocumentPtr doc, MaterialXPrincipledBSDFNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,

View File

@ -2,14 +2,16 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "node.h" #include "node_parser.h"
#include "principled_bsdf.h"
#include "image.h" #include "bsdf_principled.h"
#include "tex_image.h"
#include <BKE_node_runtime.hh> #include <BKE_node_runtime.hh>
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
MaterialXNode::MaterialXNode(MaterialX::DocumentPtr doc, NodeParser::NodeParser(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node)
@ -17,7 +19,9 @@ MaterialXNode::MaterialXNode(MaterialX::DocumentPtr doc,
{ {
} }
void MaterialXNode::get_input_link_node(const bNodeSocket *sock, MaterialX::NodePtr matx_node_to, const char *input) void NodeParser::get_input_link_node(const bNodeSocket *sock,
MaterialX::NodePtr matx_node_to,
const char *input)
{ {
const bNode *inode = sock->link->fromnode; const bNode *inode = sock->link->fromnode;
if (inode->type == SH_NODE_BSDF_PRINCIPLED) { if (inode->type == SH_NODE_BSDF_PRINCIPLED) {
@ -35,7 +39,7 @@ void MaterialXNode::get_input_link_node(const bNodeSocket *sock, MaterialX::Node
matx_node_to->addInput(input, get_mx_type(sock))->setNodeName(MaterialX::createValidName(inode->name)); matx_node_to->addInput(input, get_mx_type(sock))->setNodeName(MaterialX::createValidName(inode->name));
}; };
std::string MaterialXNode::get_mx_type(const bNodeSocket *sock) std::string NodeParser::get_mx_type(const bNodeSocket *sock)
{ {
std::string mx_sock_type; std::string mx_sock_type;
switch (sock->type) { switch (sock->type) {

View File

@ -12,7 +12,7 @@
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class MaterialXNode { class NodeParser {
public: public:
const Depsgraph *depsgraph = nullptr; const Depsgraph *depsgraph = nullptr;
const Material *material = nullptr; const Material *material = nullptr;
@ -21,11 +21,11 @@ class MaterialXNode {
MaterialX::DocumentPtr doc; MaterialX::DocumentPtr doc;
public: public:
MaterialXNode(MaterialX::DocumentPtr doc, NodeParser(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node); const bNode *node);
virtual ~MaterialXNode() = default; virtual ~NodeParser() = default;
virtual MaterialX::NodePtr convert() = 0; virtual MaterialX::NodePtr convert() = 0;

View File

@ -2,8 +2,7 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "material_output.h" #include "output_material.h"
#include "principled_bsdf.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
@ -11,25 +10,13 @@ MaterialXMaterialOutputNode::MaterialXMaterialOutputNode(MaterialX::DocumentPtr
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node)
: MaterialXNode(doc, depsgraph, material, node) : NodeParser(doc, depsgraph, material, node)
{ {
matx_node = doc->addNode("surfacematerial", MaterialX::createValidName(node->name), "material"); matx_node = doc->addNode("surfacematerial", MaterialX::createValidName(node->name), "material");
} }
MaterialX::NodePtr MaterialXMaterialOutputNode::convert() MaterialX::NodePtr MaterialXMaterialOutputNode::convert()
{ { const char *matx_socket = "surfaceshader";
//LISTBASE_FOREACH (const bNodeSocket *, sock, &node->inputs) {
// if (!sock->link) {
// continue;
// }
// if (STREQ(sock->name, "Surface")) {
// const bNode *inode = sock->link->fromnode;
// MaterialXPrincipledBSDFNode surface_node(doc, material, inode);
// surface_node.convert();
// matx_node->addInput("surfaceshader", "surfaceshader")->setNodeName(inode->name);
// }
//}
const char *matx_socket = "surfaceshader";
const bNodeSocket sock = node->input_by_identifier("Surface"); const bNodeSocket sock = node->input_by_identifier("Surface");
get_input_link_node(&sock, matx_node, matx_socket); get_input_link_node(&sock, matx_node, matx_socket);
return matx_node; return matx_node;

View File

@ -4,11 +4,11 @@
#pragma once #pragma once
#include "node.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class MaterialXMaterialOutputNode : public MaterialXNode { class MaterialXMaterialOutputNode : public NodeParser {
public: public:
MaterialXMaterialOutputNode(MaterialX::DocumentPtr doc, MaterialXMaterialOutputNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,

View File

@ -2,8 +2,8 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "node.h" #include "tex_image.h"
#include "image.h" #include "node_parser.h"
#include "hydra/image.h" #include "hydra/image.h"
@ -17,7 +17,7 @@ MaterialXTexImageNode::MaterialXTexImageNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node)
: MaterialXNode(doc, depsgraph, material, node) : NodeParser(doc, depsgraph, material, node)
{ {
matx_node = doc->addNode("image", MaterialX::createValidName(node->name), "color3"); matx_node = doc->addNode("image", MaterialX::createValidName(node->name), "color3");
} }

View File

@ -4,11 +4,11 @@
#pragma once #pragma once
#include "node.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class MaterialXTexImageNode : public MaterialXNode { class MaterialXTexImageNode : public NodeParser {
protected: protected:
/* Following Cycles color for wrong Texture nodes. */ /* Following Cycles color for wrong Texture nodes. */
static const MaterialX::Color3 texture_error_color_; static const MaterialX::Color3 texture_error_color_;