Blender V5.0
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 "BKE_lib_id.hh"
10
11#include "DEG_depsgraph.hh"
12
13#include "DNA_material_types.h"
14
15#include "NOD_shader.h"
17
18#include "material.h"
19
21
23 public:
25
26 NodeItem compute() override
27 {
28 const Material *material = graph_.material;
29 NodeItem surface = create_node(
30 "open_pbr_surface",
32 {{"base_weight", val(1.0f)},
33 {"base_color", val(MaterialX::Color3(material->r, material->g, material->b))},
34 {"base_diffuse_roughness", val(material->roughness)},
35 {"specular_weight", val(material->spec)},
36 {"base_metalness", val(material->metallic)}});
37
39 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
40 return res;
41 }
42
44 {
45 NodeItem surface = create_node("open_pbr_surface",
47 {{"base_color", val(MaterialX::Color3(1.0f, 0.0f, 1.0f))}});
49 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
50 return res;
51 }
52};
53
54MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
55 Material *material,
56 const ExportParams &export_params)
57{
58 CLOG_DEBUG(LOG_IO_MATERIALX, "Material: %s", material->id.name);
59
60 MaterialX::DocumentPtr doc = MaterialX::createDocument();
61 NodeItem output_item;
62
63 NodeGraph graph(depsgraph, material, export_params, doc);
64
66 nullptr, "Inlined Tree", material->nodetree->idname);
67 BLI_SCOPED_DEFER([&]() { BKE_id_free(nullptr, &local_tree->id); });
68
70 inline_shader_node_tree(*material->nodetree, *local_tree, params);
71
72 local_tree->ensure_topology_cache();
73 bNode *output_node = ntreeShaderOutputNode(local_tree, SHD_OUTPUT_ALL);
74 if (output_node && output_node->typeinfo->materialx_fn) {
75 NodeParserData data = {graph, NodeItem::Type::Material, nullptr, graph.empty_node()};
76 output_node->typeinfo->materialx_fn(&data, output_node, nullptr);
77 output_item = data.result;
78 }
79 else {
80 output_item = DefaultMaterialNodeParser(
81 graph, nullptr, nullptr, NodeItem::Type::Material, nullptr)
83 }
84
85 /* This node is expected to have a specific name to link up to USD. */
86 graph.set_output_node_name(output_item);
87
89 "Material: %s\n%s",
90 material->id.name,
91 MaterialX::writeToXmlString(doc).c_str());
92 return doc;
93}
94
95} // namespace blender::nodes::materialx
void BKE_id_free(Main *bmain, void *idv)
#define BLI_SCOPED_DEFER(function_to_defer)
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
@ 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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4085
struct CLG_LogRef * LOG_IO_MATERIALX
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material, const ExportParams &export_params)
bool inline_shader_node_tree(const bNodeTree &src_tree, bNodeTree &dst_tree, InlineShaderNodeTreeParams &params)
char name[258]
Definition DNA_ID.h:432
struct bNodeTree * nodetree
char idname[64]
bNodeTypeHandle * typeinfo
void set_output_node_name(const NodeItem &item) const