Blender V4.3
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 NodeItem surface = create_node(
26 "standard_surface",
28 {{"base", val(1.0f)},
29 {"base_color", val(MaterialX::Color3(material_->r, material_->g, material_->b))},
30 {"diffuse_roughness", val(material_->roughness)},
31 {"specular", val(material_->spec)},
32 {"metalness", val(material_->metallic)}});
33
35 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
36 res.node->setName("Material_Default");
37 return res;
38 }
39
41 {
42 NodeItem surface = create_node("standard_surface",
44 {{"base_color", val(MaterialX::Color3(1.0f, 0.0f, 1.0f))}});
46 "surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}});
47 res.node->setName("Material_Error");
48 return res;
49 }
50};
51
52MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
53 Material *material,
54 const std::string &material_name,
55 const ExportParams &export_params)
56{
57 CLOG_INFO(LOG_MATERIALX_SHADER, 0, "Material: %s", material->id.name);
58
59 MaterialX::DocumentPtr doc = MaterialX::createDocument();
60 NodeItem output_item;
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 = {doc.get(),
70 nullptr,
71 NodeItem(doc.get()),
72 export_params};
73 output_node->typeinfo->materialx_fn(&data, output_node, nullptr);
74 output_item = data.result;
75 }
76 else {
77 output_item = DefaultMaterialNodeParser(doc.get(),
79 material,
80 nullptr,
81 nullptr,
83 nullptr,
84 export_params)
86 }
87 }
88 else {
89 output_item = DefaultMaterialNodeParser(doc.get(),
91 material,
92 nullptr,
93 nullptr,
95 nullptr,
96 export_params)
97 .compute();
98 }
99
100 if (output_item.node) {
101 output_item.node->setName(material_name);
102 }
103
105 1,
106 "Material: %s\n%s",
107 material->id.name,
108 MaterialX::writeToXmlString(doc).c_str());
109 return doc;
110}
111
112} // 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)
NodeParser(MaterialX::GraphElement *graph, const Depsgraph *depsgraph, const Material *material, const bNode *node, const bNodeSocket *socket_out, NodeItem::Type to_type, GroupNodeParser *group_parser, const ExportParams &export_params)
NodeItem create_node(const std::string &category, NodeItem::Type type)
NodeItem val(const T &data) const
Definition node_parser.h:80
Material material
const Depsgraph * depsgraph
struct CLG_LogRef * LOG_MATERIALX_SHADER
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material, const std::string &material_name, const ExportParams &export_params)
bNodeTypeHandle * typeinfo