Blender V4.5
nodes/shader/materialx/material.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <MaterialXFormat/XmlIo.h>
6
7#include "node_parser.h"
8
9#include "DEG_depsgraph.hh"
10
11#include "DNA_material_types.h"
12
13#include "NOD_shader.h"
14
15#include "material.h"
16
18
20 public:
22
23 NodeItem compute() override
24 {
25 const Material *material = graph_.material;
26 NodeItem surface = create_node(
27 "open_pbr_surface",
29 {{"base_weight", val(1.0f)},
30 {"base_color", val(MaterialX::Color3(material->r, material->g, material->b))},
31 {"base_diffuse_roughness", val(material->roughness)},
32 {"specular_weight", val(material->spec)},
33 {"base_metalness", val(material->metallic)}});
34
36 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
37 return res;
38 }
39
41 {
42 NodeItem surface = create_node("open_pbr_surface",
44 {{"base_color", val(MaterialX::Color3(1.0f, 0.0f, 1.0f))}});
46 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
47 return res;
48 }
49};
50
51MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
52 Material *material,
53 const ExportParams &export_params)
54{
55 CLOG_INFO(LOG_MATERIALX_SHADER, 0, "Material: %s", material->id.name);
56
57 MaterialX::DocumentPtr doc = MaterialX::createDocument();
58 NodeItem output_item;
59
60 NodeGraph graph(depsgraph, material, export_params, doc);
61
62 if (material->use_nodes) {
63 material->nodetree->ensure_topology_cache();
64 bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
65 if (output_node && output_node->typeinfo->materialx_fn) {
66 NodeParserData data = {graph, NodeItem::Type::Material, nullptr, graph.empty_node()};
67 output_node->typeinfo->materialx_fn(&data, output_node, nullptr);
68 output_item = data.result;
69 }
70 else {
71 output_item = DefaultMaterialNodeParser(
72 graph, nullptr, nullptr, NodeItem::Type::Material, nullptr)
74 }
75 }
76 else {
77 output_item = DefaultMaterialNodeParser(
78 graph, nullptr, nullptr, NodeItem::Type::Material, nullptr)
79 .compute();
80 }
81
82 /* This node is expected to have a specific name to link up to USD. */
83 graph.set_output_node_name(output_item);
84
86 1,
87 "Material: %s\n%s",
88 material->id.name,
89 MaterialX::writeToXmlString(doc).c_str());
90 return doc;
91}
92
93} // namespace blender::nodes::materialx
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
@ SHD_OUTPUT_ALL
struct bNode * ntreeShaderOutputNode(struct bNodeTree *ntree, int target)
BMesh const char void * data
BPy_StructRNA * depsgraph
NodeParser(NodeGraph &graph, const bNode *node, const bNodeSocket *socket_out, NodeItem::Type to_type, GroupNodeParser *group_parser)
NodeParser(NodeGraph &graph, const bNode *node, const bNodeSocket *socket_out, NodeItem::Type to_type, GroupNodeParser *group_parser)
NodeItem create_node(const std::string &category, NodeItem::Type type)
NodeItem val(const T &data) const
Definition node_parser.h:73
struct CLG_LogRef * LOG_MATERIALX_SHADER
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material, const ExportParams &export_params)
char name[66]
Definition DNA_ID.h:415
struct bNodeTree * nodetree
bNodeTypeHandle * typeinfo
void set_output_node_name(const NodeItem &item) const