Blender V5.3
usd_reader_material.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 NVIDIA Corporation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#pragma once
5
6#include "usd.hh"
7
8#include "BLI_map.hh"
10#include "BLI_string_ref.hh"
11#include "BLI_vector.hh"
12
13#include <pxr/usd/usdShade/material.h>
14
15#include <string>
16
17namespace blender {
18
19struct Main;
20struct Material;
21struct bNode;
22struct bNodeTree;
23struct ReportList;
24
25namespace io::usd {
26
28
29/* Helper struct used when arranging nodes in columns, keeping track the
30 * occupancy information for a given column. I.e., for column n,
31 * column_offsets[n] is the y-offset (from top to bottom) of the occupied
32 * region in that column. */
34 const float origx_;
35 const float origy_;
36 const float horizontal_step_;
37 const float vertical_step_;
39
40 /* Map a USD shader prim path to the Blender node converted
41 * from that shader. This map is updated during shader
42 * conversion and is used to avoid creating duplicate nodes
43 * for a given shader. */
45
47 float origy,
48 float horizontal_step = 300.0f,
49 float vertical_step = 300.0f)
50 : origx_(origx),
51 origy_(origy),
52 horizontal_step_(horizontal_step),
53 vertical_step_(vertical_step)
54 {
55 }
56
57 /* Compute the x- and y-coordinates for placing a new node in an unoccupied region of
58 * the column with the given index. */
59 float2 compute_node_loc(int column);
60
66 std::string get_key(const pxr::UsdShadeShader &usd_shader, const StringRef tag) const;
67
68 /* Returns the Blender node previously cached for the given USD shader. Returns null if no cached
69 * shader was found. */
70 bNode *get_cached_node(const pxr::UsdShadeShader &usd_shader, const StringRef tag = {}) const;
71
72 /* Cache the Blender node translated from the given USD shader. */
73 void cache_node(const pxr::UsdShadeShader &usd_shader, bNode *node, const StringRef tag = {});
74};
75
76/* Helper struct which carries an assortment of optional
77 * information that is sometimes required when linking
78 * nodes together. */
80 bool is_color_corrected = false;
81
82 /* Is the value inverted with respect to Blender.
83 * For example: Opacity 0.85 <-> Transmission Weight 0.15 */
84 bool is_inverted = false;
85
86 float opacity_threshold = 0.0f;
87};
88
89/* Converts USD materials to Blender representation. */
90
115 private:
116 const USDImportParams &params_;
117 Main &bmain_;
118
119 public:
120 USDMaterialReader(const USDImportParams &params, Main &bmain);
121
122 Material *add_material(const pxr::UsdShadeMaterial &usd_material,
123 bool read_usd_preview = true) const;
124
125 void import_usd_preview(Material *mtl, const pxr::UsdShadeMaterial &usd_material) const;
126
128 ReportList *reports() const;
129
130 protected:
133 const pxr::UsdShadeMaterial &usd_material,
134 const pxr::UsdShadeShader &usd_shader) const;
135
136 void set_principled_node_inputs(bNode *principled_node,
137 bNodeTree *ntree,
138 const pxr::UsdShadeShader &usd_shader) const;
139
141 bNode *output,
142 const pxr::UsdShadeShader &usd_shader) const;
143
145 bool set_node_input(const pxr::UsdShadeInput &usd_input,
146 bNode *dest_node,
147 const StringRefNull dest_socket_name,
148 bNodeTree *ntree,
149 int column,
151 const ExtraLinkInfo &extra = {}) const;
152
157 bool follow_connection(const pxr::UsdShadeInput &usd_input,
158 bNode *dest_node,
159 const StringRefNull dest_socket_name,
160 bNodeTree *ntree,
161 int column,
163 const ExtraLinkInfo &extra = {}) const;
164
165 void convert_usd_uv_texture(const pxr::UsdShadeShader &usd_shader,
166 const pxr::TfToken &usd_source_name,
167 bNode *dest_node,
168 const StringRefNull dest_socket_name,
169 bNodeTree *ntree,
170 int column,
172 const ExtraLinkInfo &extra = {}) const;
173
174 void convert_usd_transform_2d(const pxr::UsdShadeShader &usd_shader,
175 bNode *dest_node,
176 const StringRefNull dest_socket_name,
177 bNodeTree *ntree,
178 int column,
179 NodePlacementContext &ctx) const;
180
185 void load_tex_image(const pxr::UsdShadeShader &usd_shader,
186 bNode *tex_image,
187 const ExtraLinkInfo &extra = {}) const;
188
193 void convert_usd_primvar_reader_float2(const pxr::UsdShadeShader &usd_shader,
194 const pxr::TfToken &usd_source_name,
195 bNode *dest_node,
196 const StringRefNull dest_socket_name,
197 bNodeTree *ntree,
198 int column,
199 NodePlacementContext &ctx) const;
200 void convert_usd_primvar_reader_generic(const pxr::UsdShadeShader &usd_shader,
201 StringRef output_type,
202 bNode *dest_node,
203 const StringRefNull dest_socket_name,
204 bNodeTree *ntree,
205 int column,
206 NodePlacementContext &ctx) const;
207};
208
209/* Utility functions. */
210
217void build_material_map(const Main *bmain, Map<std::string, Material *> &r_mat_map);
218
232Material *find_existing_material(const pxr::SdfPath &usd_mat_path,
233 const USDImportParams &params,
234 const Map<std::string, Material *> &mat_map,
235 const Map<pxr::SdfPath, Material *> &usd_path_to_mat);
236
237} // namespace io::usd
238} // namespace blender
#define output
bool follow_connection(const pxr::UsdShadeInput &usd_input, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx, const ExtraLinkInfo &extra={}) const
void convert_usd_transform_2d(const pxr::UsdShadeShader &usd_shader, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx) const
Material * add_material(const pxr::UsdShadeMaterial &usd_material, bool read_usd_preview=true) const
void import_usd_preview_nodes(Material *mtl, const pxr::UsdShadeMaterial &usd_material, const pxr::UsdShadeShader &usd_shader) const
USDMaterialReader(const USDImportParams &params, Main &bmain)
bool set_displacement_node_inputs(bNodeTree *ntree, bNode *output, const pxr::UsdShadeShader &usd_shader) const
bool set_node_input(const pxr::UsdShadeInput &usd_input, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx, const ExtraLinkInfo &extra={}) const
void convert_usd_primvar_reader_generic(const pxr::UsdShadeShader &usd_shader, StringRef output_type, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx) const
void convert_usd_primvar_reader_float2(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &usd_source_name, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx) const
void convert_usd_uv_texture(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &usd_source_name, bNode *dest_node, const StringRefNull dest_socket_name, bNodeTree *ntree, int column, NodePlacementContext &ctx, const ExtraLinkInfo &extra={}) const
void set_principled_node_inputs(bNode *principled_node, bNodeTree *ntree, const pxr::UsdShadeShader &usd_shader) const
void import_usd_preview(Material *mtl, const pxr::UsdShadeMaterial &usd_material) const
void load_tex_image(const pxr::UsdShadeShader &usd_shader, bNode *tex_image, const ExtraLinkInfo &extra={}) const
Map< std::string, bNode * > ShaderToNodeMap
Material * find_existing_material(const pxr::SdfPath &usd_mat_path, const USDImportParams &params, const Map< std::string, Material * > &mat_map, const Map< pxr::SdfPath, Material * > &usd_path_to_mat)
void build_material_map(const Main *bmain, Map< std::string, Material * > &r_mat_map)
BMesh Iterators.
VecBase< float, 2 > float2
NodePlacementContext(float origx, float origy, float horizontal_step=300.0f, float vertical_step=300.0f)
void cache_node(const pxr::UsdShadeShader &usd_shader, bNode *node, const StringRef tag={})
std::string get_key(const pxr::UsdShadeShader &usd_shader, const StringRef tag) const
bNode * get_cached_node(const pxr::UsdShadeShader &usd_shader, const StringRef tag={}) const