Blender V5.3
usd_reader_material.cc
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
6#include "usd_asset_utils.hh"
8#include "usd_reader_utils.hh"
9
10#include "BKE_image.hh"
11#include "BKE_lib_id.hh"
12#include "BKE_library.hh"
13#include "BKE_main.hh"
14#include "BKE_material.hh"
15#include "BKE_node.hh"
17#include "BKE_node_runtime.hh"
19#include "BKE_report.hh"
20
21#include "BLI_fileops.hh"
23#include "BLI_map.hh"
24#include "BLI_math_vector_c.hh"
26#include "BLI_string.hh"
27#include "BLI_string_ref.hh"
28#include "BLI_vector.hh"
29
30#include "DNA_listBase.h"
31#include "DNA_material_types.h"
32#include "DNA_node_types.h"
33
35
36#include "WM_types.hh"
37
38#include <pxr/base/gf/vec3f.h>
39#include <pxr/usd/ar/packageUtils.h>
40#include <pxr/usd/usdShade/material.h>
41#include <pxr/usd/usdShade/shader.h>
42
43#include "CLG_log.h"
44
45namespace blender {
46
47static CLG_LogRef LOG = {"io.usd"};
48
49namespace usdtokens {
50
51/* Parameter names. */
52static const pxr::TfToken a("a", pxr::TfToken::Immortal);
53static const pxr::TfToken b("b", pxr::TfToken::Immortal);
54static const pxr::TfToken bias("bias", pxr::TfToken::Immortal);
55static const pxr::TfToken clearcoat("clearcoat", pxr::TfToken::Immortal);
56static const pxr::TfToken clearcoatRoughness("clearcoatRoughness", pxr::TfToken::Immortal);
57static const pxr::TfToken diffuseColor("diffuseColor", pxr::TfToken::Immortal);
58static const pxr::TfToken displacement("displacement", pxr::TfToken::Immortal);
59static const pxr::TfToken emissiveColor("emissiveColor", pxr::TfToken::Immortal);
60static const pxr::TfToken file("file", pxr::TfToken::Immortal);
61static const pxr::TfToken g("g", pxr::TfToken::Immortal);
62static const pxr::TfToken ior("ior", pxr::TfToken::Immortal);
63static const pxr::TfToken in("in", pxr::TfToken::Immortal);
64static const pxr::TfToken metallic("metallic", pxr::TfToken::Immortal);
65static const pxr::TfToken normal("normal", pxr::TfToken::Immortal);
66static const pxr::TfToken occlusion("occlusion", pxr::TfToken::Immortal);
67static const pxr::TfToken opacity("opacity", pxr::TfToken::Immortal);
68static const pxr::TfToken opacityThreshold("opacityThreshold", pxr::TfToken::Immortal);
69static const pxr::TfToken r("r", pxr::TfToken::Immortal);
70static const pxr::TfToken rgb("rgb", pxr::TfToken::Immortal);
71static const pxr::TfToken rgba("rgba", pxr::TfToken::Immortal);
72static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal);
73static const pxr::TfToken scale("scale", pxr::TfToken::Immortal);
74static const pxr::TfToken specularColor("specularColor", pxr::TfToken::Immortal);
75static const pxr::TfToken st("st", pxr::TfToken::Immortal);
76static const pxr::TfToken varname("varname", pxr::TfToken::Immortal);
77
78/* Wrap mode names. */
79static const pxr::TfToken black("black", pxr::TfToken::Immortal);
80static const pxr::TfToken clamp("clamp", pxr::TfToken::Immortal);
81static const pxr::TfToken repeat("repeat", pxr::TfToken::Immortal);
82static const pxr::TfToken mirror("mirror", pxr::TfToken::Immortal);
83static const pxr::TfToken wrapS("wrapS", pxr::TfToken::Immortal);
84static const pxr::TfToken wrapT("wrapT", pxr::TfToken::Immortal);
85
86/* Transform 2d names. */
87static const pxr::TfToken rotation("rotation", pxr::TfToken::Immortal);
88static const pxr::TfToken translation("translation", pxr::TfToken::Immortal);
89
90/* USD shader names. */
91static const pxr::TfToken UsdPreviewSurface("UsdPreviewSurface", pxr::TfToken::Immortal);
92static const pxr::TfToken UsdPrimvarReader_float2("UsdPrimvarReader_float2",
93 pxr::TfToken::Immortal);
94static const pxr::TfToken UsdUVTexture("UsdUVTexture", pxr::TfToken::Immortal);
95static const pxr::TfToken UsdTransform2d("UsdTransform2d", pxr::TfToken::Immortal);
96} // namespace usdtokens
97
99
100/* Add a node of the given type at the given location coordinates. */
101static bNode *add_node(bNodeTree *ntree, const int type, const float2 loc)
102{
103 bNode *new_node = bke::node_add_static_node(nullptr, *ntree, type);
104 new_node->location[0] = loc.x;
105 new_node->location[1] = loc.y;
106
107 return new_node;
108}
109
110/* Connect the output socket of node 'source' to the input socket of node 'dest'. */
111static void link_nodes(bNodeTree *ntree,
112 bNode *source,
113 const StringRefNull sock_out,
114 bNode *dest,
115 const StringRefNull sock_in)
116{
117 bNodeSocket *source_socket = bke::node_find_socket(*source, SOCK_OUT, UString(sock_out));
118 if (!source_socket) {
119 CLOG_ERROR(&LOG, "Couldn't find output socket %s", sock_out.c_str());
120 return;
121 }
122
123 bNodeSocket *dest_socket = bke::node_find_socket(*dest, SOCK_IN, UString(sock_in));
124 if (!dest_socket) {
125 CLOG_ERROR(&LOG, "Couldn't find input socket %s", sock_in.c_str());
126 return;
127 }
128
129 /* Only add the link if this is the first one to be connected. */
130 if (bke::node_count_socket_links(*ntree, *dest_socket) == 0) {
131 bke::node_add_link(*ntree, *source, *source_socket, *dest, *dest_socket);
132 }
133}
134
135/* Returns a layer handle retrieved from the given attribute's property specs.
136 * Note that the returned handle may be invalid if no layer could be found. */
137static pxr::SdfLayerHandle get_layer_handle(const pxr::UsdAttribute &attribute)
138{
139 for (const auto &PropertySpec : attribute.GetPropertyStack(pxr::UsdTimeCode::EarliestTime())) {
140 if (PropertySpec->HasDefaultValue() ||
141 PropertySpec->GetLayer()->GetNumTimeSamplesForPath(PropertySpec->GetPath()) > 0)
142 {
143 return PropertySpec->GetLayer();
144 }
145 }
146
147 return pxr::SdfLayerHandle();
148}
149
150/* For the given UDIM path (assumed to contain the UDIM token), returns an array
151 * containing valid tile indices. */
152static Vector<int> get_udim_tiles(const std::string &file_path)
153{
154 char base_udim_path[FILE_MAX];
155 STRNCPY(base_udim_path, file_path.c_str());
156
157 Vector<int> udim_tiles;
158
159 /* Extract the tile numbers from all files on disk. */
160 ListBaseT<LinkData> tiles = {nullptr, nullptr};
161 int tile_start, tile_range;
162 bool result = BKE_image_get_tile_info(base_udim_path, &tiles, &tile_start, &tile_range);
163 if (result) {
164 for (LinkData &tile : tiles) {
165 int tile_number = POINTER_AS_INT(tile.data);
166 udim_tiles.append(tile_number);
167 }
168 }
169
170 tiles.free_no_destruct();
171
172 return udim_tiles;
173}
174
175/* Add tiles with the given indices to the given image. */
176static void add_udim_tiles(Image *image, const Vector<int> &indices)
177{
178 image->source = IMA_SRC_TILED;
179
180 /* All images are created with a default, 1001, first tile. If this tile does not end up being
181 * used, it should be removed. */
182 ImageTile *first_tile = BKE_image_get_tile(image, 0);
183 bool remove_first = true;
184
185 for (int tile_number : indices) {
186 BKE_image_add_tile(image, tile_number, nullptr);
187 if (tile_number == first_tile->tile_number) {
188 remove_first = false;
189 }
190 }
191
192 if (remove_first) {
193 BKE_image_remove_tile(image, first_tile);
194 }
195}
196
197/* Returns true if the given shader may have opacity < 1.0, based
198 * on heuristics. */
199static bool needs_blend(const pxr::UsdShadeShader &usd_shader)
200{
201 if (!usd_shader) {
202 return false;
203 }
204
205 bool needs_blend = false;
206
207 if (pxr::UsdShadeInput opacity_input = usd_shader.GetInput(usdtokens::opacity)) {
208
209 if (opacity_input.HasConnectedSource()) {
210 needs_blend = true;
211 }
212 else {
213 pxr::VtValue val;
214 if (opacity_input.GetAttr().HasAuthoredValue() && opacity_input.GetAttr().Get(&val)) {
215 float opacity = val.Get<float>();
216 needs_blend = opacity < 1.0f;
217 }
218 }
219 }
220
221 return needs_blend;
222}
223
224/* Returns the given shader's opacityThreshold input value, if this input has an
225 * authored value. Otherwise, returns the given default value. */
226static float get_opacity_threshold(const pxr::UsdShadeShader &usd_shader,
227 float default_value = 0.0f)
228{
229 if (!usd_shader) {
230 return default_value;
231 }
232
233 pxr::UsdShadeInput opacity_threshold_input = usd_shader.GetInput(usdtokens::opacityThreshold);
234
235 if (!opacity_threshold_input) {
236 return default_value;
237 }
238
239 pxr::VtValue val;
240 if (opacity_threshold_input.GetAttr().HasAuthoredValue() &&
241 opacity_threshold_input.GetAttr().Get(&val))
242 {
243 return val.Get<float>();
244 }
245
246 return default_value;
247}
248
249static int get_image_extension(const pxr::UsdShadeShader &usd_shader, const int default_value)
250{
251 pxr::UsdShadeInput wrap_input = usd_shader.GetInput(usdtokens::wrapS);
252
253 if (!wrap_input) {
254 wrap_input = usd_shader.GetInput(usdtokens::wrapT);
255 }
256
257 if (!wrap_input) {
258 return default_value;
259 }
260
261 pxr::VtValue wrap_input_val;
262 if (!(wrap_input.Get(&wrap_input_val) && wrap_input_val.IsHolding<pxr::TfToken>())) {
263 return default_value;
264 }
265
266 pxr::TfToken wrap_val = wrap_input_val.UncheckedGet<pxr::TfToken>();
267
268 if (wrap_val == usdtokens::repeat) {
270 }
271
272 if (wrap_val == usdtokens::clamp) {
274 }
275
276 if (wrap_val == usdtokens::black) {
278 }
279
280 if (wrap_val == usdtokens::mirror) {
282 }
283
284 return default_value;
285}
286
287/* Attempts to return in r_preview_surface the UsdPreviewSurface shader source
288 * of the given material. Returns true if a UsdPreviewSurface source was found
289 * and returns false otherwise. */
290static bool get_usd_preview_surface(const pxr::UsdShadeMaterial &usd_material,
291 pxr::UsdShadeShader &r_preview_surface)
292{
293 if (!usd_material) {
294 return false;
295 }
296
297 if (pxr::UsdShadeShader surf_shader = usd_material.ComputeSurfaceSource()) {
298 /* Check if we have a UsdPreviewSurface shader. */
299 pxr::TfToken shader_id;
300 if (surf_shader.GetShaderId(&shader_id) && shader_id == usdtokens::UsdPreviewSurface) {
301 r_preview_surface = surf_shader;
302 return true;
303 }
304 }
305
306 return false;
307}
308
309/* Set the Blender material's viewport display color, metallic and roughness
310 * properties from the given USD preview surface shader's inputs. */
311static void set_viewport_material_props(Material *mtl, const pxr::UsdShadeShader &usd_preview)
312{
313 if (!(mtl && usd_preview)) {
314 return;
315 }
316
317 if (pxr::UsdShadeInput diffuse_color_input = usd_preview.GetInput(usdtokens::diffuseColor)) {
318 const pxr::UsdShadeAttributeVector attrs = diffuse_color_input.GetValueProducingAttributes();
319 if (!attrs.empty()) {
320 pxr::VtValue val;
321 if (attrs[0].Get(&val) && val.IsHolding<pxr::GfVec3f>()) {
322 pxr::GfVec3f color = val.UncheckedGet<pxr::GfVec3f>();
324 /* Note: The material is expected to be rendered by the Workbench render engine (Viewport
325 * Display), so no need to define a material node tree. */
326 mtl->r = color[0];
327 mtl->g = color[1];
328 mtl->b = color[2];
329 }
330 }
331 }
332
333 if (pxr::UsdShadeInput metallic_input = usd_preview.GetInput(usdtokens::metallic)) {
334 const pxr::UsdShadeAttributeVector attrs = metallic_input.GetValueProducingAttributes();
335 if (!attrs.empty()) {
336 pxr::VtValue val;
337 if (attrs[0].Get(&val) && val.IsHolding<float>()) {
338 mtl->metallic = val.UncheckedGet<float>();
339 }
340 }
341 }
342
343 if (pxr::UsdShadeInput roughness_input = usd_preview.GetInput(usdtokens::roughness)) {
344 const pxr::UsdShadeAttributeVector attrs = roughness_input.GetValueProducingAttributes();
345 if (!attrs.empty()) {
346 pxr::VtValue val;
347 if (attrs[0].Get(&val) && val.IsHolding<float>()) {
348 mtl->roughness = val.UncheckedGet<float>();
349 }
350 }
351 }
352}
353
354static pxr::UsdShadeInput get_input(const pxr::UsdShadeShader &usd_shader,
355 const pxr::TfToken &input_name)
356{
357 pxr::UsdShadeInput input = usd_shader.GetInput(input_name);
358
359 /* Check if the shader's input is connected to another source,
360 * and use that instead if so. */
361 if (input) {
362 for (const pxr::UsdShadeConnectionSourceInfo &source_info : input.GetConnectedSources()) {
363 pxr::UsdShadeShader shader = pxr::UsdShadeShader(source_info.source.GetPrim());
364 pxr::UsdShadeInput secondary_input = shader.GetInput(source_info.sourceName);
365 if (secondary_input) {
366 input = secondary_input;
367 break;
368 }
369 }
370 }
371
372 return input;
373}
374
376 const StringRefNull identifier,
377 ReportList *reports)
378{
379 bNodeSocket *sock = bke::node_find_socket(*node, SOCK_IN, UString(identifier));
380 if (!sock) {
381 BKE_reportf(reports,
382 RPT_ERROR,
383 "%s: Error: Couldn't get input socket %s for node %s",
384 __func__,
385 identifier.c_str(),
386 node->idname);
387 }
388
389 return sock;
390}
391
392namespace io::usd {
393
395{
396 if (column >= column_offsets_.size()) {
397 /* UsdPreviewSurface graphs are all very tiny due to their constrained nature. It is unlikely
398 * we need to grow at all but, if we do, do so by small chunks at a time. */
399 column_offsets_.resize(column + 4);
400 }
401
402 float2 loc;
403 loc.x = origx_ - column * horizontal_step_;
404 loc.y = origy_ - column_offsets_[column];
405
406 /* Record the y-offset of the occupied region in
407 * the column, including padding. */
408 column_offsets_[column] += vertical_step_ + 10.0f;
409
410 return loc;
411}
412
413std::string NodePlacementContext::get_key(const pxr::UsdShadeShader &usd_shader,
414 const StringRef tag) const
415{
416 std::string key = usd_shader.GetPath().GetAsString();
417 if (!tag.is_empty()) {
418 key += ":";
419 key += tag;
420 }
421 return key;
422}
423
424bNode *NodePlacementContext::get_cached_node(const pxr::UsdShadeShader &usd_shader,
425 const StringRef tag) const
426{
427 return node_cache_.lookup_default(get_key(usd_shader, tag), nullptr);
428}
429
430void NodePlacementContext::cache_node(const pxr::UsdShadeShader &usd_shader,
431 bNode *node,
432 const StringRef tag)
433{
434 node_cache_.add_new(get_key(usd_shader, tag), node);
435}
436
438 : params_(params), bmain_(bmain)
439{
440}
441
443{
444 return params_.worker_status ? params_.worker_status->reports : nullptr;
445}
446
447Material *USDMaterialReader::add_material(const pxr::UsdShadeMaterial &usd_material,
448 const bool read_usd_preview) const
449{
450 if (!usd_material) {
451 return nullptr;
452 }
453
454 std::string mtl_name = usd_material.GetPrim().GetName().GetString();
455
456 /* Create the material. */
457 Material *mtl = BKE_material_add(&bmain_, mtl_name.c_str());
458 id_us_min(&mtl->id);
459
460 if (read_usd_preview) {
461 import_usd_preview(mtl, usd_material);
462 }
463
464 /* Load custom properties directly from the Material's prim. */
465 set_id_props_from_prim(&mtl->id, usd_material.GetPrim());
466
467 return mtl;
468}
469
471 const pxr::UsdShadeMaterial &usd_material) const
472{
473 /* Get the UsdPreviewSurface shader source for the material,
474 * if there is one. */
475 pxr::UsdShadeShader usd_preview;
476 if (get_usd_preview_surface(usd_material, usd_preview)) {
477
478 set_viewport_material_props(mtl, usd_preview);
479
480 /* Optionally, create shader nodes to represent a UsdPreviewSurface. */
481 if (params_.import_usd_preview) {
482 import_usd_preview_nodes(mtl, usd_material, usd_preview);
483 }
484 }
485}
486
488 const pxr::UsdShadeMaterial &usd_material,
489 const pxr::UsdShadeShader &usd_shader) const
490{
491 if (!(mtl && usd_shader)) {
492 return;
493 }
494
495 /* Create the Material's node tree containing the principled BSDF
496 * and output shaders. */
497 bNodeTree *ntree = mtl->nodetree;
498 BLI_assert(ntree != nullptr);
499
500 /* Create the Principled BSDF shader node. */
501 bNode *principled = add_node(ntree, SH_NODE_BSDF_PRINCIPLED, {0.0f, 300.0f});
502
503 /* Create the material output node. */
504 bNode *output = add_node(ntree, SH_NODE_OUTPUT_MATERIAL, {300.0f, 300.0f});
505
506 /* Connect the Principled BSDF node to the output node. */
507 link_nodes(ntree, principled, "BSDF", output, "Surface");
508
509 /* Recursively create the principled shader input networks. */
510 set_principled_node_inputs(principled, ntree, usd_shader);
511
512 /* Process displacement if we have a valid displacement source. */
513 if (pxr::UsdShadeShader disp_shader = usd_material.ComputeDisplacementSource()) {
514 if (set_displacement_node_inputs(ntree, output, disp_shader)) {
515 mtl->displacement_method = MA_DISPLACEMENT_BOTH;
516 }
517 }
518
520
522
523 /* Optionally, set the material blend mode. */
524 if (params_.set_material_blend) {
525 if (needs_blend(usd_shader)) {
526 mtl->surface_render_method = MA_SURFACE_METHOD_FORWARD;
527 }
528 }
529}
530
532 bNodeTree *ntree,
533 const pxr::UsdShadeShader &usd_shader) const
534{
535 /* The context struct keeps track of the locations for adding
536 * input nodes. */
537 NodePlacementContext context(0.0f, 300.0);
538
539 /* The column index (from right to left relative to the principled
540 * node) where we're adding the nodes. */
541 int column = 0;
542
543 /* Recursively set the principled shader inputs. */
544
545 if (pxr::UsdShadeInput diffuse_input = usd_shader.GetInput(usdtokens::diffuseColor)) {
546 ExtraLinkInfo extra;
547 extra.is_color_corrected = true;
548 set_node_input(diffuse_input, principled, "Base Color", ntree, column, context, extra);
549 }
550
551 float emission_strength = 0.0f;
552 if (pxr::UsdShadeInput emissive_input = usd_shader.GetInput(usdtokens::emissiveColor)) {
553 ExtraLinkInfo extra;
554 extra.is_color_corrected = true;
555 if (set_node_input(
556 emissive_input, principled, "Emission Color", ntree, column, context, extra))
557 {
558 emission_strength = 1.0f;
559 }
560 }
561
562 bNodeSocket *emission_strength_sock = bke::node_find_socket(
563 *principled, SOCK_IN, "Emission Strength"_ustr);
564 emission_strength_sock->default_value_typed<bNodeSocketValueFloat>()->value = emission_strength;
565
566 if (pxr::UsdShadeInput specular_input = usd_shader.GetInput(usdtokens::specularColor)) {
567 set_node_input(specular_input, principled, "Specular Tint", ntree, column, context);
568 }
569
570 if (pxr::UsdShadeInput metallic_input = usd_shader.GetInput(usdtokens::metallic)) {
571 set_node_input(metallic_input, principled, "Metallic", ntree, column, context);
572 }
573
574 if (pxr::UsdShadeInput roughness_input = usd_shader.GetInput(usdtokens::roughness)) {
575 set_node_input(roughness_input, principled, "Roughness", ntree, column, context);
576 }
577
578 if (pxr::UsdShadeInput coat_input = usd_shader.GetInput(usdtokens::clearcoat)) {
579 set_node_input(coat_input, principled, "Coat Weight", ntree, column, context);
580 }
581
582 if (pxr::UsdShadeInput coat_roughness_input = usd_shader.GetInput(usdtokens::clearcoatRoughness))
583 {
584 set_node_input(coat_roughness_input, principled, "Coat Roughness", ntree, column, context);
585 }
586
587 if (pxr::UsdShadeInput opacity_input = usd_shader.GetInput(usdtokens::opacity)) {
588 ExtraLinkInfo extra;
589
590 bool is_alpha_exception = false;
591 if (opacity_input.HasConnectedSource()) {
592 for (const pxr::UsdShadeConnectionSourceInfo &source_info :
593 opacity_input.GetConnectedSources())
594 {
595 if (source_info.sourceName == usdtokens::a) {
596 is_alpha_exception = true;
597 break;
598 }
599 }
600 }
601
602 const float opacity_threshold = get_opacity_threshold(usd_shader, 0.0f);
603 if (opacity_threshold == 0.0f && !is_alpha_exception) {
604 extra.is_inverted = true;
606 opacity_input, principled, "Transmission Weight", ntree, column, context, extra);
607 }
608 else {
609 extra.opacity_threshold = opacity_threshold;
610 set_node_input(opacity_input, principled, "Alpha", ntree, column, context, extra);
611 }
612 }
613
614 if (pxr::UsdShadeInput ior_input = usd_shader.GetInput(usdtokens::ior)) {
615 set_node_input(ior_input, principled, "IOR", ntree, column, context);
616 }
617
618 if (pxr::UsdShadeInput normal_input = usd_shader.GetInput(usdtokens::normal)) {
619 set_node_input(normal_input, principled, "Normal", ntree, column, context);
620 }
621}
622
624 bNode *output,
625 const pxr::UsdShadeShader &usd_shader) const
626{
627 /* Only continue if this UsdPreviewSurface has displacement to process. */
628 pxr::UsdShadeInput displacement_input = usd_shader.GetInput(usdtokens::displacement);
629 if (!displacement_input) {
630 return false;
631 }
632
633 bNode *displacement_node = add_node(ntree, SH_NODE_DISPLACEMENT, {0.0f, -100.0f});
634
635 /* The context struct keeps track of the locations for adding
636 * input nodes. */
637 NodePlacementContext context(0.0f, -100.0f);
638
639 /* The column index, from right to left relative to the output node. */
640 int column = 0;
641
642 const StringRefNull height = "Height";
643 ExtraLinkInfo extra;
644 extra.is_color_corrected = false;
645 set_node_input(displacement_input, displacement_node, height, ntree, column, context, extra);
646
647 /* If the displacement input is not connected, then this is "constant" displacement which is
648 * a lossy conversion from the UsdPreviewSurface. We adjust the Height input assuming a
649 * Midlevel of 0.5 and Scale of 1 as that closely matches the scene in `usdview`. */
650 if (!displacement_input.HasConnectedSource()) {
651 bNodeSocket *sock_height = bke::node_find_socket(*displacement_node, SOCK_IN, UString(height));
652 bNodeSocket *sock_mid = bke::node_find_socket(*displacement_node, SOCK_IN, "Midlevel"_ustr);
653 bNodeSocket *sock_scale = bke::node_find_socket(*displacement_node, SOCK_IN, "Scale"_ustr);
654
655 sock_height->default_value_typed<bNodeSocketValueFloat>()->value += 0.5f;
656 sock_mid->default_value_typed<bNodeSocketValueFloat>()->value = 0.5f;
657 sock_scale->default_value_typed<bNodeSocketValueFloat>()->value = 1.0f;
658 }
659
660 /* Connect the Displacement node to the output node. */
661 link_nodes(ntree, displacement_node, "Displacement", output, "Displacement");
662 return true;
663}
664
665bool USDMaterialReader::set_node_input(const pxr::UsdShadeInput &usd_input,
666 bNode *dest_node,
667 const StringRefNull dest_socket_name,
668 bNodeTree *ntree,
669 const int column,
671 const ExtraLinkInfo &extra) const
672{
673 if (!(usd_input && dest_node)) {
674 return false;
675 }
676
677 const pxr::UsdShadeSourceInfoVector sources = usd_input.GetConnectedSources();
678 const bool needs_follow = !sources.empty() &&
679 sources[0].sourceType == pxr::UsdShadeAttributeType::Output;
680
681 if (needs_follow) {
682 /* The USD shader input has a connected source shader. Follow the connection
683 * and attempt to convert the connected USD shader to a Blender node. */
684 return follow_connection(usd_input, dest_node, dest_socket_name, ntree, column, ctx, extra);
685 }
686
687 /* Set the destination node socket value from the USD shader input value. */
688 bNodeSocket *sock = bke::node_find_socket(*dest_node, SOCK_IN, UString(dest_socket_name));
689 if (!sock) {
690 CLOG_ERROR(&LOG, "Couldn't get destination node socket %s", dest_socket_name.c_str());
691 return false;
692 }
693
694 pxr::VtValue val;
695 const pxr::UsdShadeAttributeVector attrs = usd_input.GetValueProducingAttributes();
696 if (attrs.empty() || !attrs[0].Get(&val)) {
698 "Couldn't get value for usd shader input %s",
699 usd_input.GetPrim().GetPath().GetAsString().c_str());
700 return false;
701 }
702
703 switch (sock->type) {
704 case SOCK_FLOAT:
705 if (val.IsHolding<float>()) {
706 bNodeSocketValueFloat *sock_val = sock->default_value_typed<bNodeSocketValueFloat>();
707 sock_val->value = val.UncheckedGet<float>();
708 if (extra.is_inverted) {
709 sock_val->value = 1.0f - sock_val->value;
710 }
711 return true;
712 }
713 else if (val.IsHolding<pxr::GfVec3f>()) {
714 pxr::GfVec3f v3f = val.UncheckedGet<pxr::GfVec3f>();
715 float average = (v3f[0] + v3f[1] + v3f[2]) / 3.0f;
716 sock->default_value_typed<bNodeSocketValueFloat>()->value = average;
717 return true;
718 }
719 break;
720 case SOCK_RGBA:
721 if (val.IsHolding<pxr::GfVec3f>()) {
722 pxr::GfVec3f v3f = val.UncheckedGet<pxr::GfVec3f>();
723 colorspace_attr_to_scene_linear(attrs[0], v3f);
724 copy_v3_v3(sock->default_value_typed<bNodeSocketValueRGBA>()->value, v3f.data());
725 return true;
726 }
727 break;
728 case SOCK_VECTOR:
729 if (val.IsHolding<pxr::GfVec3f>()) {
730 pxr::GfVec3f v3f = val.UncheckedGet<pxr::GfVec3f>();
731 copy_v3_v3(sock->default_value_typed<bNodeSocketValueVector>()->value, v3f.data());
732 return true;
733 }
734 else if (val.IsHolding<pxr::GfVec2f>()) {
735 pxr::GfVec2f v2f = val.UncheckedGet<pxr::GfVec2f>();
736 copy_v2_v2(sock->default_value_typed<bNodeSocketValueVector>()->value, v2f.data());
737 return true;
738 }
739 break;
740 default:
741 CLOG_WARN(&LOG,
742 "Unexpected type %s for destination node socket %s",
743 sock->idname,
744 dest_socket_name.c_str());
745 break;
746 }
747
748 return false;
749}
750
756
758{
759 const float2 loc = ctx.compute_node_loc(column);
760
761 /* Currently, the Normal Map node has Tangent Space as the default,
762 * which is what we need, so we don't need to explicitly set it. */
763 IntermediateNode normal_map{};
764 normal_map.node = add_node(ntree, SH_NODE_NORMAL_MAP, loc);
765 normal_map.sock_input_name = "Color";
766 normal_map.sock_output_name = "Normal";
767
768 return normal_map;
769}
770
771static IntermediateNode add_scale_bias(const pxr::UsdShadeShader &usd_shader,
772 bNodeTree *ntree,
773 int column,
774 bool feeds_normal_map,
776{
777 /* Handle the scale-bias inputs if present. */
778 pxr::UsdShadeInput scale_input = usd_shader.GetInput(usdtokens::scale);
779 pxr::UsdShadeInput bias_input = usd_shader.GetInput(usdtokens::bias);
780 pxr::GfVec4f scale(1.0f, 1.0f, 1.0f, 1.0f);
781 pxr::GfVec4f bias(0.0f, 0.0f, 0.0f, 0.0f);
782
783 pxr::VtValue val;
784 if (scale_input.Get(&val) && val.CanCast<pxr::GfVec4f>()) {
785 scale = pxr::VtValue::Cast<pxr::GfVec4f>(val).UncheckedGet<pxr::GfVec4f>();
786 }
787 if (bias_input.Get(&val) && val.CanCast<pxr::GfVec4f>()) {
788 bias = pxr::VtValue::Cast<pxr::GfVec4f>(val).UncheckedGet<pxr::GfVec4f>();
789 }
790
791 /* Nothing to be done if the values match their defaults. */
792 if (scale == pxr::GfVec4f{1.0f, 1.0f, 1.0f, 1.0f} &&
793 bias == pxr::GfVec4f{0.0f, 0.0f, 0.0f, 0.0f})
794 {
795 return {};
796 }
797
798 /* Nothing to be done if this feeds a Normal Map and the values match those defaults. */
799 if (feeds_normal_map && (scale[0] == 2.0f && scale[1] == 2.0f && scale[2] == 2.0f) &&
800 (bias[0] == -1.0f && bias[1] == -1.0f && bias[2] == -1.0f))
801 {
802 return {};
803 }
804
805 /* If we know a Normal Map node will be involved, leave room for the another
806 * adjustment node which will be added later. */
807 const float2 loc = ctx.compute_node_loc(feeds_normal_map ? column + 1 : column);
808
809 IntermediateNode scale_bias{};
810
811 const StringRefNull tag = "scale_bias";
812 bNode *node = ctx.get_cached_node(usd_shader, tag);
813
814 if (!node) {
815 node = add_node(ntree, SH_NODE_VECTOR_MATH, loc);
816 ctx.cache_node(usd_shader, node, tag);
817 }
818
819 scale_bias.node = node;
821 scale_bias.sock_input_name = "Vector";
822 scale_bias.sock_output_name = "Vector";
823
824 bNodeSocket *sock_scale = bke::node_find_socket(*scale_bias.node, SOCK_IN, "Vector_001"_ustr);
825 bNodeSocket *sock_bias = bke::node_find_socket(*scale_bias.node, SOCK_IN, "Vector_002"_ustr);
826 copy_v3_v3(sock_scale->default_value_typed<bNodeSocketValueVector>()->value, scale.data());
827 copy_v3_v3(sock_bias->default_value_typed<bNodeSocketValueVector>()->value, bias.data());
828
829 return scale_bias;
830}
831
833 int column,
835{
836 const float2 loc = ctx.compute_node_loc(column);
837
838 IntermediateNode adjust{};
839 adjust.node = add_node(ntree, SH_NODE_VECTOR_MATH, loc);
841 adjust.sock_input_name = "Vector";
842 adjust.sock_output_name = "Vector";
843
844 bNodeSocket *sock_scale = bke::node_find_socket(*adjust.node, SOCK_IN, "Vector_001"_ustr);
845 bNodeSocket *sock_bias = bke::node_find_socket(*adjust.node, SOCK_IN, "Vector_002"_ustr);
846 copy_v3_fl3(sock_scale->default_value_typed<bNodeSocketValueVector>()->value, 0.5f, 0.5f, 0.5f);
847 copy_v3_fl3(sock_bias->default_value_typed<bNodeSocketValueVector>()->value, 0.5f, 0.5f, 0.5f);
848
849 return adjust;
850}
851
852static IntermediateNode add_separate_color(const pxr::UsdShadeShader &usd_shader,
853 const pxr::TfToken &usd_source_name,
854 bNodeTree *ntree,
855 int column,
857{
858 IntermediateNode separate_color{};
859
860 if (usd_source_name == usdtokens::r || usd_source_name == usdtokens::g ||
861 usd_source_name == usdtokens::b)
862 {
863 const StringRefNull tag = "separate_color";
864 bNode *node = ctx.get_cached_node(usd_shader, tag);
865
866 if (!node) {
867 const float2 loc = ctx.compute_node_loc(column);
868
869 node = add_node(ntree, SH_NODE_SEPARATE_COLOR, loc);
870 ctx.cache_node(usd_shader, node, tag);
871 }
872
873 separate_color.node = node;
874 separate_color.sock_input_name = "Color";
875
876 if (usd_source_name == usdtokens::r) {
877 separate_color.sock_output_name = "Red";
878 }
879 if (usd_source_name == usdtokens::g) {
880 separate_color.sock_output_name = "Green";
881 }
882 if (usd_source_name == usdtokens::b) {
883 separate_color.sock_output_name = "Blue";
884 }
885 }
886
887 return separate_color;
888}
889
891 float threshold,
892 int column,
894{
895 const float2 loc = ctx.compute_node_loc(column);
896
897 IntermediateNode lessthan{};
898 lessthan.node = add_node(ntree, SH_NODE_MATH, loc);
899 lessthan.node->custom1 = NODE_MATH_LESS_THAN;
900 lessthan.sock_input_name = "Value";
901 lessthan.sock_output_name = "Value";
902
903 bNodeSocket *thresh_sock = bke::node_find_socket(*lessthan.node, SOCK_IN, "Value_001"_ustr);
904 thresh_sock->default_value_typed<bNodeSocketValueFloat>()->value = threshold;
905
906 return lessthan;
907}
908
910{
911 const float2 loc = ctx.compute_node_loc(column);
912
913 /* An "invert" node : 1.0f - Value_001 */
914 IntermediateNode oneminus{};
915 oneminus.node = add_node(ntree, SH_NODE_MATH, loc);
916 oneminus.node->custom1 = NODE_MATH_SUBTRACT;
917 oneminus.sock_input_name = "Value_001";
918 oneminus.sock_output_name = "Value";
919
920 bNodeSocket *val_sock = blender::bke::node_find_socket(*oneminus.node, SOCK_IN, "Value"_ustr);
921 val_sock->default_value_typed<bNodeSocketValueFloat>()->value = 1.0f;
922
923 return oneminus;
924}
925
926static void configure_displacement(const pxr::UsdShadeShader &usd_shader, bNode *displacement_node)
927{
928 /* Transform the scale-bias values into something that the Displacement node
929 * can understand. */
930 pxr::UsdShadeInput scale_input = usd_shader.GetInput(usdtokens::scale);
931 pxr::UsdShadeInput bias_input = usd_shader.GetInput(usdtokens::bias);
932 pxr::GfVec4f scale(1.0f, 1.0f, 1.0f, 1.0f);
933 pxr::GfVec4f bias(0.0f, 0.0f, 0.0f, 0.0f);
934
935 pxr::VtValue val;
936 if (scale_input.Get(&val) && val.CanCast<pxr::GfVec4f>()) {
937 scale = pxr::VtValue::Cast<pxr::GfVec4f>(val).UncheckedGet<pxr::GfVec4f>();
938 }
939 if (bias_input.Get(&val) && val.CanCast<pxr::GfVec4f>()) {
940 bias = pxr::VtValue::Cast<pxr::GfVec4f>(val).UncheckedGet<pxr::GfVec4f>();
941 }
942
943 const float scale_avg = (scale[0] + scale[1] + scale[2]) / 3.0f;
944 const float bias_avg = (bias[0] + bias[1] + bias[2]) / 3.0f;
945
946 bNodeSocket *sock_mid = bke::node_find_socket(*displacement_node, SOCK_IN, "Midlevel"_ustr);
947 bNodeSocket *sock_scale = bke::node_find_socket(*displacement_node, SOCK_IN, "Scale"_ustr);
948 sock_mid->default_value_typed<bNodeSocketValueFloat>()->value = -1.0f * (bias_avg / scale_avg);
949 sock_scale->default_value_typed<bNodeSocketValueFloat>()->value = scale_avg;
950}
951
952static pxr::UsdShadeShader node_graph_output_source(const pxr::UsdShadeNodeGraph &node_graph,
953 const pxr::TfToken &output_name)
954{
955 // Check that we have a legit output
956 pxr::UsdShadeOutput output = node_graph.GetOutput(output_name);
957 if (!output) {
958 return pxr::UsdShadeShader();
959 }
960
961 pxr::UsdShadeAttributeVector attrs = pxr::UsdShadeUtils::GetValueProducingAttributes(output);
962 if (attrs.empty()) {
963 return pxr::UsdShadeShader();
964 }
965
966 pxr::UsdAttribute attr = attrs[0];
967
968 std::pair<pxr::TfToken, pxr::UsdShadeAttributeType> name_and_type =
969 pxr::UsdShadeUtils::GetBaseNameAndType(attr.GetName());
970
971 pxr::UsdShadeShader shader(attr.GetPrim());
972 if (name_and_type.second != pxr::UsdShadeAttributeType::Output || !shader) {
973 return pxr::UsdShadeShader();
974 }
975
976 return shader;
977}
978
979bool USDMaterialReader::follow_connection(const pxr::UsdShadeInput &usd_input,
980 bNode *dest_node,
981 const StringRefNull dest_socket_name,
982 bNodeTree *ntree,
983 int column,
985 const ExtraLinkInfo &extra) const
986{
987 if (!(usd_input && dest_node && !dest_socket_name.is_empty() && ntree)) {
988 return false;
989 }
990
991 pxr::UsdShadeConnectableAPI source;
992 pxr::TfToken source_name;
993 pxr::UsdShadeAttributeType source_type;
994
995 usd_input.GetConnectedSource(&source, &source_name, &source_type);
996
997 if (!source) {
998 return false;
999 }
1000
1001 const pxr::UsdPrim source_prim = source.GetPrim();
1002 pxr::UsdShadeShader source_shader;
1003 if (source_prim.IsA<pxr::UsdShadeShader>()) {
1004 source_shader = pxr::UsdShadeShader(source_prim);
1005 }
1006 else if (source_prim.IsA<pxr::UsdShadeNodeGraph>()) {
1007 pxr::UsdShadeNodeGraph node_graph(source_prim);
1008 source_shader = node_graph_output_source(node_graph, source_name);
1009 }
1010
1011 if (!source_shader) {
1012 return false;
1013 }
1014
1015 pxr::TfToken shader_id;
1016 if (!source_shader.GetShaderId(&shader_id)) {
1017 CLOG_WARN(&LOG,
1018 "Couldn't get shader id for source shader %s",
1019 source_shader.GetPath().GetAsString().c_str());
1020 return false;
1021 }
1022
1023 /* For now, only convert UsdUVTexture, UsdTransform2d and UsdPrimvarReader_float2 inputs. */
1024 if (shader_id == usdtokens::UsdUVTexture) {
1025 int shift = 1;
1026
1027 /* Create a Normal Map node if the source is flowing into a 'Normal' socket. */
1028 IntermediateNode normal_map{};
1029 const bool is_normal_map = dest_socket_name == "Normal";
1030 if (is_normal_map) {
1031 normal_map = add_normal_map(ntree, column + shift, ctx);
1032 shift++;
1033 }
1034
1035 /* Create a Separate Color node if necessary. */
1036 IntermediateNode separate_color = add_separate_color(
1037 source_shader, source_name, ntree, column + shift, ctx);
1038 if (separate_color.node) {
1039 shift++;
1040 }
1041
1042 /* Create a Scale-Bias adjustment node or fill in Displacement settings if necessary. */
1043 IntermediateNode scale_bias{};
1044 if (dest_socket_name == "Height") {
1045 configure_displacement(source_shader, dest_node);
1046 }
1047 else {
1048 scale_bias = add_scale_bias(source_shader, ntree, column + shift, is_normal_map, ctx);
1049 }
1050
1051 /* Wire up any intermediate nodes that are present. Keep track of the
1052 * final "target" destination for the Image link. */
1053 bNode *target_node = dest_node;
1054 StringRefNull target_sock_name = dest_socket_name;
1055
1056 /* Handle opacity inversion if necessary. */
1057 if (extra.is_inverted) {
1058 IntermediateNode invert = add_oneminus(ntree, column + 1, ctx);
1059 link_nodes(ntree, invert.node, invert.sock_output_name, target_node, target_sock_name);
1060 target_node = invert.node;
1061 target_sock_name = invert.sock_input_name;
1062 }
1063
1064 if (normal_map.node) {
1065 /* If a scale-bias node is required, we need to re-adjust the output
1066 * so it can be passed into the NormalMap node properly. */
1067 if (scale_bias.node) {
1068 IntermediateNode re_adjust = add_scale_bias_adjust(ntree, column + shift, ctx);
1069 link_nodes(ntree,
1070 scale_bias.node,
1071 scale_bias.sock_output_name,
1072 re_adjust.node,
1073 re_adjust.sock_input_name);
1074 link_nodes(ntree,
1075 re_adjust.node,
1076 re_adjust.sock_output_name,
1077 normal_map.node,
1078 normal_map.sock_input_name);
1079
1080 target_node = scale_bias.node;
1081 target_sock_name = scale_bias.sock_input_name;
1082 shift += 2;
1083 }
1084 else {
1085 target_node = normal_map.node;
1086 target_sock_name = normal_map.sock_input_name;
1087 }
1088
1089 link_nodes(ntree, normal_map.node, normal_map.sock_output_name, dest_node, dest_socket_name);
1090 }
1091 else if (scale_bias.node) {
1092 if (separate_color.node) {
1093 link_nodes(ntree,
1094 separate_color.node,
1095 separate_color.sock_output_name,
1096 target_node,
1097 target_sock_name);
1098 link_nodes(ntree,
1099 scale_bias.node,
1100 scale_bias.sock_output_name,
1101 separate_color.node,
1102 separate_color.sock_input_name);
1103 }
1104 else {
1105 link_nodes(
1106 ntree, scale_bias.node, scale_bias.sock_output_name, dest_node, dest_socket_name);
1107 }
1108 target_node = scale_bias.node;
1109 target_sock_name = scale_bias.sock_input_name;
1110 shift++;
1111 }
1112 else if (separate_color.node) {
1113 if (extra.opacity_threshold == 0.0f || dest_socket_name != "Alpha") {
1114 link_nodes(ntree,
1115 separate_color.node,
1116 separate_color.sock_output_name,
1117 target_node,
1118 target_sock_name);
1119 }
1120 target_node = separate_color.node;
1121 target_sock_name = separate_color.sock_input_name;
1122 }
1123
1124 /* Handle opacity threshold if necessary. */
1125 if (extra.opacity_threshold > 0.0f) {
1126 /* USD defines the threshold as >= but Blender does not have that operation. Use < instead
1127 * and then invert it. */
1128 IntermediateNode lessthan = add_lessthan(ntree, extra.opacity_threshold, column + 1, ctx);
1129 IntermediateNode invert = add_oneminus(ntree, column + 1, ctx);
1130 link_nodes(
1131 ntree, lessthan.node, lessthan.sock_output_name, invert.node, invert.sock_input_name);
1132 link_nodes(ntree, invert.node, invert.sock_output_name, dest_node, dest_socket_name);
1133 if (separate_color.node) {
1134 link_nodes(ntree,
1135 separate_color.node,
1136 separate_color.sock_output_name,
1137 lessthan.node,
1138 lessthan.sock_input_name);
1139 }
1140 else {
1141 target_node = lessthan.node;
1142 target_sock_name = lessthan.sock_input_name;
1143 }
1144 }
1145
1146 convert_usd_uv_texture(source_shader,
1147 source_name,
1148 target_node,
1149 target_sock_name,
1150 ntree,
1151 column + shift,
1152 ctx,
1153 extra);
1154 }
1155 else if (shader_id == usdtokens::UsdPrimvarReader_float2) {
1157 source_shader, source_name, dest_node, dest_socket_name, ntree, column + 1, ctx);
1158 }
1159 else if (shader_id == usdtokens::UsdTransform2d) {
1160 convert_usd_transform_2d(source_shader, dest_node, dest_socket_name, ntree, column + 1, ctx);
1161 }
1162 else {
1163 /* Handle any remaining "generic" primvar readers. */
1164 StringRef shader_id_name(shader_id.GetString());
1165 if (shader_id_name.startswith("UsdPrimvarReader_")) {
1166 int64_t type_offset = shader_id_name.rfind('_');
1167 if (type_offset >= 0) {
1168 StringRef output_type = shader_id_name.drop_prefix(type_offset + 1);
1170 source_shader, output_type, dest_node, dest_socket_name, ntree, column + 1, ctx);
1171 }
1172 }
1173 }
1174
1175 return true;
1176}
1177
1178void USDMaterialReader::convert_usd_uv_texture(const pxr::UsdShadeShader &usd_shader,
1179 const pxr::TfToken &usd_source_name,
1180 bNode *dest_node,
1181 const StringRefNull dest_socket_name,
1182 bNodeTree *ntree,
1183 const int column,
1185 const ExtraLinkInfo &extra) const
1186{
1187 if (!usd_shader || !dest_node || !ntree || dest_socket_name.is_empty()) {
1188 return;
1189 }
1190
1191 bNode *tex_image = ctx.get_cached_node(usd_shader);
1192
1193 if (tex_image == nullptr) {
1194 const float2 loc = ctx.compute_node_loc(column);
1195
1196 /* Create the Texture Image node. */
1197 tex_image = add_node(ntree, SH_NODE_TEX_IMAGE, loc);
1198
1199 /* Cache newly created node. */
1200 ctx.cache_node(usd_shader, tex_image);
1201
1202 /* Load the texture image. */
1203 load_tex_image(usd_shader, tex_image, extra);
1204 }
1205
1206 /* Connect to destination node input. */
1207
1208 /* Get the source socket name. */
1209 const StringRefNull source_socket_name = usd_source_name == usdtokens::a ? "Alpha" : "Color";
1210
1211 link_nodes(ntree, tex_image, source_socket_name, dest_node, dest_socket_name);
1212
1213 /* Connect the texture image node "Vector" input. */
1214 if (pxr::UsdShadeInput st_input = usd_shader.GetInput(usdtokens::st)) {
1215 set_node_input(st_input, tex_image, "Vector", ntree, column, ctx);
1216 }
1217}
1218
1219void USDMaterialReader::convert_usd_transform_2d(const pxr::UsdShadeShader &usd_shader,
1220 bNode *dest_node,
1221 const StringRefNull dest_socket_name,
1222 bNodeTree *ntree,
1223 int column,
1224 NodePlacementContext &ctx) const
1225{
1226 if (!usd_shader || !dest_node || !ntree || dest_socket_name.is_empty()) {
1227 return;
1228 }
1229
1230 bNode *mapping = ctx.get_cached_node(usd_shader);
1231
1232 if (mapping == nullptr) {
1233 const float2 loc = ctx.compute_node_loc(column);
1234
1235 /* Create the MAPPING node. */
1236 mapping = add_node(ntree, SH_NODE_MAPPING, loc);
1237
1238 /* Cache newly created node. */
1239 ctx.cache_node(usd_shader, mapping);
1240
1241 mapping->custom1 = TEXMAP_TYPE_POINT;
1242
1243 if (bNodeSocket *scale_socket = get_input_socket(mapping, "Scale", reports())) {
1244 if (pxr::UsdShadeInput scale_input = get_input(usd_shader, usdtokens::scale)) {
1245 pxr::VtValue val;
1246 if (scale_input.Get(&val) && val.CanCast<pxr::GfVec2f>()) {
1247 pxr::GfVec2f scale_val = val.Cast<pxr::GfVec2f>().UncheckedGet<pxr::GfVec2f>();
1248 float scale[3] = {scale_val[0], scale_val[1], 1.0f};
1249 copy_v3_v3(scale_socket->default_value_typed<bNodeSocketValueVector>()->value, scale);
1250 }
1251 }
1252 }
1253
1254 if (bNodeSocket *loc_socket = get_input_socket(mapping, "Location", reports())) {
1255 if (pxr::UsdShadeInput trans_input = get_input(usd_shader, usdtokens::translation)) {
1256 pxr::VtValue val;
1257 if (trans_input.Get(&val) && val.CanCast<pxr::GfVec2f>()) {
1258 pxr::GfVec2f trans_val = val.Cast<pxr::GfVec2f>().UncheckedGet<pxr::GfVec2f>();
1259 float location[3] = {trans_val[0], trans_val[1], 0.0f};
1260 copy_v3_v3(loc_socket->default_value_typed<bNodeSocketValueVector>()->value, location);
1261 }
1262 }
1263 }
1264
1265 if (bNodeSocket *rot_socket = get_input_socket(mapping, "Rotation", reports())) {
1266 if (pxr::UsdShadeInput rot_input = get_input(usd_shader, usdtokens::rotation)) {
1267 pxr::VtValue val;
1268 if (rot_input.Get(&val) && val.CanCast<float>()) {
1269 float rot_val = val.Cast<float>().UncheckedGet<float>() * M_PI / 180.0f;
1270 float rot[3] = {0.0f, 0.0f, rot_val};
1271 copy_v3_v3(rot_socket->default_value_typed<bNodeSocketValueVector>()->value, rot);
1272 }
1273 }
1274 }
1275 }
1276
1277 /* Connect to destination node input. */
1278 link_nodes(ntree, mapping, "Vector", dest_node, dest_socket_name);
1279
1280 /* Connect the mapping node "Vector" input. */
1281 if (pxr::UsdShadeInput in_input = usd_shader.GetInput(usdtokens::in)) {
1282 set_node_input(in_input, mapping, "Vector", ntree, column, ctx);
1283 }
1284}
1285
1286void USDMaterialReader::load_tex_image(const pxr::UsdShadeShader &usd_shader,
1287 bNode *tex_image,
1288 const ExtraLinkInfo &extra) const
1289{
1290 if (!(usd_shader && tex_image && tex_image->type_legacy == SH_NODE_TEX_IMAGE)) {
1291 return;
1292 }
1293
1294 /* Try to load the texture image. */
1295 pxr::UsdShadeInput file_input = usd_shader.GetInput(usdtokens::file);
1296
1297 if (!file_input) {
1298 CLOG_WARN(&LOG,
1299 "Couldn't get file input property for USD shader %s",
1300 usd_shader.GetPath().GetAsString().c_str());
1301 return;
1302 }
1303
1304 /* File input may have a connected source, e.g., if it's been overridden by
1305 * an input on the material. */
1306 if (file_input.HasConnectedSource()) {
1307 pxr::UsdShadeConnectableAPI source;
1308 pxr::TfToken source_name;
1309 pxr::UsdShadeAttributeType source_type;
1310
1311 if (file_input.GetConnectedSource(&source, &source_name, &source_type)) {
1312 file_input = source.GetInput(source_name);
1313 }
1314 else {
1315 CLOG_WARN(&LOG,
1316 "Couldn't get connected source for file input %s (%s)\n",
1317 file_input.GetPrim().GetPath().GetText(),
1318 file_input.GetFullName().GetText());
1319 }
1320 }
1321
1322 pxr::VtValue file_val;
1323 if (!file_input.Get(&file_val) || !file_val.IsHolding<pxr::SdfAssetPath>()) {
1324 CLOG_WARN(&LOG,
1325 "Couldn't get file input value for USD shader %s",
1326 usd_shader.GetPath().GetAsString().c_str());
1327 return;
1328 }
1329
1330 const pxr::SdfAssetPath &asset_path = file_val.UncheckedGet<pxr::SdfAssetPath>();
1331 std::string file_path = asset_path.GetResolvedPath();
1332
1333 if (file_path.empty()) {
1334 /* No resolved path, so use the asset path (usually necessary for UDIM paths). */
1335 file_path = asset_path.GetAssetPath();
1336
1337 if (!file_path.empty() && is_udim_path(file_path)) {
1338 /* Texture paths are frequently relative to the USD, so get the absolute path. */
1339 if (pxr::SdfLayerHandle layer_handle = get_layer_handle(file_input.GetAttr())) {
1340 file_path = layer_handle->ComputeAbsolutePath(file_path);
1341 }
1342 }
1343 }
1344
1345 if (file_path.empty()) {
1346 CLOG_WARN(&LOG,
1347 "Couldn't resolve image asset '%s' for Texture Image node",
1348 asset_path.GetAssetPath().c_str());
1349 return;
1350 }
1351
1352 /* Optionally copy the asset if it's inside a USDZ package. */
1353 const bool is_relative = pxr::ArIsPackageRelativePath(file_path);
1354 const bool import_textures = params_.import_textures_mode != TexImportMode::None && is_relative;
1355
1356 std::string imported_file_source_path;
1357
1358 if (import_textures) {
1359 imported_file_source_path = file_path;
1360
1361 /* If we are packing the imported textures, we first write them
1362 * to a temporary directory. */
1363 const char *textures_dir = params_.import_textures_mode == TexImportMode::Pack ?
1365 params_.import_textures_dir;
1366
1367 const TexNameCollisionMode name_collision_mode = params_.import_textures_mode ==
1370 params_.tex_name_collision_mode;
1371
1372 file_path = import_asset(file_path, textures_dir, name_collision_mode, reports());
1373 }
1374
1375 /* If this is a UDIM texture, this will store the
1376 * UDIM tile indices. */
1377 Vector<int> udim_tiles;
1378 bool check_open = true;
1379
1380 if (is_udim_path(file_path)) {
1381 udim_tiles = get_udim_tiles(file_path);
1382 /* We can skip the additional check for file existence if we've successfully found tiles. */
1383 if (!udim_tiles.is_empty()) {
1384 check_open = false;
1385 }
1386 }
1387
1388 const char *im_file = file_path.c_str();
1389 Image *image = BKE_image_load_exists(&bmain_, im_file, check_open);
1390 if (!image) {
1391 CLOG_WARN(&LOG, "Couldn't open image file '%s' for Texture Image node", im_file);
1392 return;
1393 }
1394
1395 if (!udim_tiles.is_empty()) {
1396 add_udim_tiles(image, udim_tiles);
1397 }
1398
1399 tex_image->id = &image->id;
1400
1401 colorspace_to_image_texture(usd_shader, file_input, !extra.is_color_corrected, image);
1402
1403 NodeTexImage *storage = static_cast<NodeTexImage *>(tex_image->storage);
1404 storage->extension = get_image_extension(usd_shader, storage->extension);
1405
1406 if (import_textures && imported_file_source_path != file_path) {
1407 ensure_usd_source_path_prop(imported_file_source_path, &image->id);
1408 }
1409
1410 if (import_textures && params_.import_textures_mode == TexImportMode::Pack &&
1412 {
1413 BKE_image_packfiles(nullptr, image, ID_BLEND_PATH(&bmain_, &image->id));
1415 BLI_delete(temp_textures_dir(), true, true);
1416 }
1417 }
1418}
1419
1420void USDMaterialReader::convert_usd_primvar_reader_float2(const pxr::UsdShadeShader &usd_shader,
1421 const pxr::TfToken & /*usd_source_name*/,
1422 bNode *dest_node,
1423 const StringRefNull dest_socket_name,
1424 bNodeTree *ntree,
1425 const int column,
1426 NodePlacementContext &ctx) const
1427{
1428 if (!usd_shader || !dest_node || !ntree || dest_socket_name.is_empty()) {
1429 return;
1430 }
1431
1432 bNode *uv_map = ctx.get_cached_node(usd_shader);
1433
1434 if (uv_map == nullptr) {
1435 const float2 loc = ctx.compute_node_loc(column);
1436
1437 /* Create the UV Map node. */
1438 uv_map = add_node(ntree, SH_NODE_UVMAP, loc);
1439
1440 /* Cache newly created node. */
1441 ctx.cache_node(usd_shader, uv_map);
1442
1443 /* Set the texmap name. */
1444 pxr::UsdShadeInput varname_input = usd_shader.GetInput(usdtokens::varname);
1445
1446 /* First check if the shader's "varname" input is connected to another source,
1447 * and use that instead if so. */
1448 if (varname_input) {
1449 for (const pxr::UsdShadeConnectionSourceInfo &source_info :
1450 varname_input.GetConnectedSources())
1451 {
1452 pxr::UsdShadeShader shader = pxr::UsdShadeShader(source_info.source.GetPrim());
1453 pxr::UsdShadeInput secondary_varname_input = shader.GetInput(source_info.sourceName);
1454 if (secondary_varname_input) {
1455 varname_input = secondary_varname_input;
1456 break;
1457 }
1458 }
1459 }
1460
1461 if (varname_input) {
1462 pxr::VtValue varname_val;
1463 /* The varname input may be a string or TfToken, so just cast it to a string.
1464 * The Cast function is defined to provide an empty result if it fails. */
1465 if (varname_input.Get(&varname_val) && varname_val.CanCastToTypeid(typeid(std::string))) {
1466 std::string varname = varname_val.Cast<std::string>().Get<std::string>();
1467 if (!varname.empty()) {
1468 NodeShaderUVMap *storage = static_cast<NodeShaderUVMap *>(uv_map->storage);
1469 STRNCPY(storage->uv_map, varname.c_str());
1470 }
1471 }
1472 }
1473 }
1474
1475 /* Connect to destination node input. */
1476 link_nodes(ntree, uv_map, "UV", dest_node, dest_socket_name);
1477}
1478
1479void USDMaterialReader::convert_usd_primvar_reader_generic(const pxr::UsdShadeShader &usd_shader,
1480 const StringRef output_type,
1481 bNode *dest_node,
1482 const StringRefNull dest_socket_name,
1483 bNodeTree *ntree,
1484 const int column,
1485 NodePlacementContext &ctx) const
1486{
1487 if (!usd_shader || !dest_node || !ntree) {
1488 return;
1489 }
1490
1491 bNode *attribute = ctx.get_cached_node(usd_shader);
1492
1493 if (attribute == nullptr) {
1494 const float2 loc = ctx.compute_node_loc(column);
1495
1496 /* Create the attribute node. */
1497 attribute = add_node(ntree, SH_NODE_ATTRIBUTE, loc);
1498
1499 /* Cache newly created node. */
1500 ctx.cache_node(usd_shader, attribute);
1501
1502 /* Set the attribute name. */
1503 pxr::UsdShadeInput varname_input = usd_shader.GetInput(usdtokens::varname);
1504
1505 /* First check if the shader's "varname" input is connected to another source,
1506 * and use that instead if so. */
1507 if (varname_input) {
1508 for (const pxr::UsdShadeConnectionSourceInfo &source_info :
1509 varname_input.GetConnectedSources())
1510 {
1511 pxr::UsdShadeShader shader = pxr::UsdShadeShader(source_info.source.GetPrim());
1512 pxr::UsdShadeInput secondary_varname_input = shader.GetInput(source_info.sourceName);
1513 if (secondary_varname_input) {
1514 varname_input = secondary_varname_input;
1515 break;
1516 }
1517 }
1518 }
1519
1520 if (varname_input) {
1521 pxr::VtValue varname_val;
1522 /* The varname input may be a string or TfToken, so just cast it to a string.
1523 * The Cast function is defined to provide an empty result if it fails. */
1524 if (varname_input.Get(&varname_val) && varname_val.CanCastToTypeid(typeid(std::string))) {
1525 std::string varname = varname_val.Cast<std::string>().Get<std::string>();
1526 if (!varname.empty()) {
1527 NodeShaderAttribute *storage = static_cast<NodeShaderAttribute *>(attribute->storage);
1528 STRNCPY(storage->name, varname.c_str());
1529 }
1530 }
1531 }
1532 }
1533
1534 /* Connect to destination node input. */
1535 if (ELEM(output_type, "float", "int")) {
1536 link_nodes(ntree, attribute, "Fac", dest_node, dest_socket_name);
1537 }
1538 else if (ELEM(output_type, "float3", "float4")) {
1539 link_nodes(ntree, attribute, "Color", dest_node, dest_socket_name);
1540 }
1541 else if (ELEM(output_type, "vector", "normal", "point")) {
1542 link_nodes(ntree, attribute, "Vector", dest_node, dest_socket_name);
1543 }
1544}
1545
1547{
1548 BLI_assert_msg(r_mat_map.is_empty(), "The incoming material map should be empty");
1549
1550 for (Material &material : bmain->materials) {
1551 r_mat_map.add_new(material.id.name + 2, &material);
1552 }
1553}
1554
1555Material *find_existing_material(const pxr::SdfPath &usd_mat_path,
1556 const USDImportParams &params,
1557 const Map<std::string, Material *> &mat_map,
1558 const Map<pxr::SdfPath, Material *> &usd_path_to_mat)
1559{
1561 /* Check if we've already created the Blender material with a modified name. */
1562 return usd_path_to_mat.lookup_default(usd_mat_path, nullptr);
1563 }
1564
1565 return mat_map.lookup_default(usd_mat_path.GetName(), nullptr);
1566}
1567
1568} // namespace io::usd
1569} // namespace blender
General operations, lookup, etc. for materials.
#define SH_NODE_UVMAP
#define SH_NODE_TEX_IMAGE
#define SH_NODE_BSDF_PRINCIPLED
#define SH_NODE_SEPARATE_COLOR
#define SH_NODE_VECTOR_MATH
#define SH_NODE_NORMAL_MAP
#define SH_NODE_MATH
#define SH_NODE_OUTPUT_MATERIAL
#define SH_NODE_MAPPING
#define SH_NODE_DISPLACEMENT
#define SH_NODE_ATTRIBUTE
File and directory operations.
#define M_PI
#define FILE_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
These structs are the foundation for all linked lists in the library system.
#define POINTER_AS_INT(i)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define output
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool is_empty() const
Definition BLI_map.hh:986
constexpr const char * c_str() const
constexpr StringRef drop_prefix(int64_t n) const
UString()=default
void append(const T &value)
bool is_empty() const
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
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 rot(x, k)
#define storage(slot, qualifiers)
#define attribute(slot)
float2x2 invert(float2x2 mat)
float3x3 scale(float3x3 mat, float2 scale)
float average(float2 a)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
const ccl_global KernelWorkTile * tile
int node_count_socket_links(const bNodeTree &ntree, const bNodeSocket &sock)
Definition node.cc:5434
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
const char * temp_textures_dir()
static IntermediateNode add_separate_color(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &usd_source_name, bNodeTree *ntree, int column, NodePlacementContext &ctx)
Map< std::string, bNode * > ShaderToNodeMap
bool is_udim_path(const std::string &path)
static IntermediateNode add_scale_bias_adjust(bNodeTree *ntree, int column, NodePlacementContext &ctx)
void colorspace_attr_to_scene_linear(const pxr::UsdAttribute &attr, pxr::GfVec3f &color)
static IntermediateNode add_scale_bias(const pxr::UsdShadeShader &usd_shader, bNodeTree *ntree, int column, bool feeds_normal_map, NodePlacementContext &ctx)
void set_id_props_from_prim(ID *id, const pxr::UsdPrim &prim, const PropertyImportMode property_import_mode, const pxr::UsdTimeCode time_code)
static pxr::UsdShadeShader node_graph_output_source(const pxr::UsdShadeNodeGraph &node_graph, const pxr::TfToken &output_name)
static IntermediateNode add_oneminus(bNodeTree *ntree, int column, NodePlacementContext &ctx)
void colorspace_to_image_texture(const pxr::UsdShadeShader &usd_shader, const pxr::UsdShadeInput &file_input, const bool is_data, Image *image)
std::string import_asset(const std::string &src, const char *import_dir, TexNameCollisionMode name_collision_mode, ReportList *reports)
static IntermediateNode add_normal_map(bNodeTree *ntree, int column, NodePlacementContext &ctx)
void ensure_usd_source_path_prop(const std::string &path, ID *id)
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)
static void configure_displacement(const pxr::UsdShadeShader &usd_shader, bNode *displacement_node)
void build_material_map(const Main *bmain, Map< std::string, Material * > &r_mat_map)
static IntermediateNode add_lessthan(bNodeTree *ntree, float threshold, int column, NodePlacementContext &ctx)
static const pxr::TfToken rgba("rgba", pxr::TfToken::Immortal)
static const pxr::TfToken UsdPrimvarReader_float2("UsdPrimvarReader_float2", pxr::TfToken::Immortal)
static const pxr::TfToken varname("varname", pxr::TfToken::Immortal)
static const pxr::TfToken rotation("rotation", pxr::TfToken::Immortal)
static const pxr::TfToken mirror("mirror", pxr::TfToken::Immortal)
static const pxr::TfToken opacity("opacity", pxr::TfToken::Immortal)
static const pxr::TfToken rgb("rgb", pxr::TfToken::Immortal)
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
static const pxr::TfToken wrapS("wrapS", pxr::TfToken::Immortal)
static const pxr::TfToken UsdUVTexture("UsdUVTexture", pxr::TfToken::Immortal)
static const pxr::TfToken clearcoatRoughness("clearcoatRoughness", pxr::TfToken::Immortal)
static const pxr::TfToken opacityThreshold("opacityThreshold", pxr::TfToken::Immortal)
static const pxr::TfToken clamp("clamp", pxr::TfToken::Immortal)
static const pxr::TfToken g("g", pxr::TfToken::Immortal)
static const pxr::TfToken clearcoat("clearcoat", pxr::TfToken::Immortal)
static const pxr::TfToken metallic("metallic", pxr::TfToken::Immortal)
static const pxr::TfToken bias("bias", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static const pxr::TfToken black("black", pxr::TfToken::Immortal)
static const pxr::TfToken diffuseColor("diffuseColor", pxr::TfToken::Immortal)
static const pxr::TfToken a("a", pxr::TfToken::Immortal)
static const pxr::TfToken file("file", pxr::TfToken::Immortal)
static const pxr::TfToken in("in", pxr::TfToken::Immortal)
static const pxr::TfToken scale("scale", pxr::TfToken::Immortal)
static const pxr::TfToken emissiveColor("emissiveColor", pxr::TfToken::Immortal)
static const pxr::TfToken occlusion("occlusion", pxr::TfToken::Immortal)
static const pxr::TfToken r("r", pxr::TfToken::Immortal)
static const pxr::TfToken UsdTransform2d("UsdTransform2d", pxr::TfToken::Immortal)
static const pxr::TfToken specularColor("specularColor", pxr::TfToken::Immortal)
static const pxr::TfToken ior("ior", pxr::TfToken::Immortal)
static const pxr::TfToken displacement("displacement", pxr::TfToken::Immortal)
static const pxr::TfToken st("st", pxr::TfToken::Immortal)
static const pxr::TfToken translation("translation", pxr::TfToken::Immortal)
static const pxr::TfToken normal("normal", pxr::TfToken::Immortal)
static const pxr::TfToken wrapT("wrapT", pxr::TfToken::Immortal)
static const pxr::TfToken repeat("repeat", pxr::TfToken::Immortal)
static const pxr::TfToken UsdPreviewSurface("UsdPreviewSurface", pxr::TfToken::Immortal)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static Vector< int > get_udim_tiles(const std::string &file_path)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ SHD_IMAGE_EXTENSION_EXTEND
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_REPEAT
static bNodeSocket * get_input_socket(bNode *node, const StringRefNull identifier, ReportList *reports)
static void add_udim_tiles(Image *image, const Vector< int > &indices)
MINLINE void copy_v2_v2(float r[2], const float a[2])
ImageTile * BKE_image_get_tile(Image *ima, int tile_number)
@ NODE_MATH_LESS_THAN
ImageTile * BKE_image_add_tile(Image *ima, int tile_number, const char *label)
static void link_nodes(bNodeTree *ntree, bNode *source, const StringRefNull sock_out, bNode *dest, const StringRefNull sock_in)
bool BKE_image_remove_tile(Image *ima, ImageTile *tile)
VecBase< float, 2 > float2
Image * BKE_image_load_exists(Main *bmain, const char *filepath, bool check_open=true, bool *r_exists=nullptr)
static pxr::SdfLayerHandle get_layer_handle(const pxr::UsdAttribute &attribute)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
static bool get_usd_preview_surface(const pxr::UsdShadeMaterial &usd_material, pxr::UsdShadeShader &r_preview_surface)
static bool needs_blend(const pxr::UsdShadeShader &usd_shader)
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
static float get_opacity_threshold(const pxr::UsdShadeShader &usd_shader, float default_value=0.0f)
bool BKE_image_get_tile_info(char *filepath, ListBaseT< LinkData > *tiles, int *r_tile_start, int *r_tile_range)
static void set_viewport_material_props(Material *mtl, const pxr::UsdShadeShader &usd_preview)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
static bNode * add_node(bNodeTree *ntree, const int type, const float2 loc)
static CLG_LogRef LOG
Definition logging.cpp:12
@ NODE_VECTOR_MATH_MULTIPLY_ADD
static int get_image_extension(const pxr::UsdShadeShader &usd_shader, const int default_value)
Material * BKE_material_add(Main *bmain, const char *name)
bool BKE_image_has_packedfile(const Image *image)
void id_us_min(ID *id)
Definition lib_id.cc:441
static pxr::UsdShadeInput get_input(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &input_name)
ListBaseT< Material > materials
Definition BKE_main.hh:392
eNodeSocketDatatype type
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
MtlNameCollisionMode mtl_name_collision_mode
Definition usd.hh:230