Blender V5.0
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 "WM_types.hh"
9
10#include "BLI_map.hh"
12#include "BLI_string_ref.hh"
13#include "BLI_vector.hh"
14
15#include <pxr/usd/usdShade/material.h>
16
17#include <string>
18
19struct Main;
20struct Material;
21struct bNode;
22struct bNodeTree;
23struct ReportList;
24
25namespace blender::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
65 std::string get_key(const pxr::UsdShadeShader &usd_shader, const blender::StringRef tag) const;
66
67 /* Returns the Blender node previously cached for the given USD shader. Returns null if no cached
68 * shader was found. */
69 bNode *get_cached_node(const pxr::UsdShadeShader &usd_shader,
70 const blender::StringRef tag = {}) const;
71
72 /* Cache the Blender node translated from the given USD shader. */
73 void cache_node(const pxr::UsdShadeShader &usd_shader,
74 bNode *node,
75 const blender::StringRef tag = {});
76};
77
78/* Helper struct which carries an assortment of optional
79 * information that is sometimes required when linking
80 * nodes together. */
82 bool is_color_corrected = false;
83
84 float opacity_threshold = 0.0f;
85};
86
87/* Converts USD materials to Blender representation. */
88
113 private:
114 const USDImportParams &params_;
115 Main &bmain_;
116
117 public:
119
120 Material *add_material(const pxr::UsdShadeMaterial &usd_material,
121 bool read_usd_preview = true) const;
122
123 void import_usd_preview(Material *mtl, const pxr::UsdShadeMaterial &usd_material) const;
124
127 {
128 return params_.worker_status ? params_.worker_status->reports : nullptr;
129 }
130
131 protected:
134 const pxr::UsdShadeMaterial &usd_material,
135 const pxr::UsdShadeShader &usd_shader) const;
136
137 void set_principled_node_inputs(bNode *principled_node,
138 bNodeTree *ntree,
139 const pxr::UsdShadeShader &usd_shader) const;
140
142 bNode *output,
143 const pxr::UsdShadeShader &usd_shader) const;
144
146 bool set_node_input(const pxr::UsdShadeInput &usd_input,
147 bNode *dest_node,
148 const StringRefNull dest_socket_name,
149 bNodeTree *ntree,
150 int column,
152 const ExtraLinkInfo &extra = {}) const;
153
158 bool follow_connection(const pxr::UsdShadeInput &usd_input,
159 bNode *dest_node,
160 const StringRefNull dest_socket_name,
161 bNodeTree *ntree,
162 int column,
163 NodePlacementContext &ctx,
164 const ExtraLinkInfo &extra = {}) const;
165
166 void convert_usd_uv_texture(const pxr::UsdShadeShader &usd_shader,
167 const pxr::TfToken &usd_source_name,
168 bNode *dest_node,
169 const StringRefNull dest_socket_name,
170 bNodeTree *ntree,
171 int column,
172 NodePlacementContext &ctx,
173 const ExtraLinkInfo &extra = {}) const;
174
175 void convert_usd_transform_2d(const pxr::UsdShadeShader &usd_shader,
176 bNode *dest_node,
177 const StringRefNull dest_socket_name,
178 bNodeTree *ntree,
179 int column,
180 NodePlacementContext &ctx) const;
181
186 void load_tex_image(const pxr::UsdShadeShader &usd_shader,
187 bNode *tex_image,
188 const ExtraLinkInfo &extra = {}) const;
189
194 void convert_usd_primvar_reader_float2(const pxr::UsdShadeShader &usd_shader,
195 const pxr::TfToken &usd_source_name,
196 bNode *dest_node,
197 const StringRefNull dest_socket_name,
198 bNodeTree *ntree,
199 int column,
200 NodePlacementContext &ctx) const;
201 void convert_usd_primvar_reader_generic(const pxr::UsdShadeShader &usd_shader,
202 StringRef output_type,
203 bNode *dest_node,
204 const StringRefNull dest_socket_name,
205 bNodeTree *ntree,
206 int column,
207 NodePlacementContext &ctx) const;
208};
209
210/* Utility functions. */
211
218void build_material_map(const Main *bmain, blender::Map<std::string, Material *> &r_mat_map);
219
233Material *find_existing_material(const pxr::SdfPath &usd_mat_path,
234 const USDImportParams &params,
235 const blender::Map<std::string, Material *> &mat_map,
236 const blender::Map<pxr::SdfPath, Material *> &usd_path_to_mat);
237
238} // namespace blender::io::usd
struct Material Material
struct bNode bNode
struct bNodeTree bNodeTree
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
#define output
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
Map< std::string, bNode * > ShaderToNodeMap
void build_material_map(const Main *bmain, blender::Map< std::string, Material * > &r_mat_map)
Material * find_existing_material(const pxr::SdfPath &usd_mat_path, const USDImportParams &params, const blender::Map< std::string, Material * > &mat_map, const blender::Map< pxr::SdfPath, Material * > &usd_path_to_mat)
VecBase< float, 2 > float2
bNode * get_cached_node(const pxr::UsdShadeShader &usd_shader, const blender::StringRef tag={}) const
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 blender::StringRef tag={})
std::string get_key(const pxr::UsdShadeShader &usd_shader, const blender::StringRef tag) const