Blender V5.3
usd_reader_domelight.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <pxr/usd/usdLux/domeLight.h>
10#include <pxr/usd/usdLux/domeLight_1.h>
11#include <pxr/usd/usdLux/tokens.h>
12
13namespace blender {
14
15namespace usdtokens {
16// Attribute names.
17static const pxr::TfToken color("color", pxr::TfToken::Immortal);
18static const pxr::TfToken intensity("intensity", pxr::TfToken::Immortal);
19static const pxr::TfToken texture_file("texture:file", pxr::TfToken::Immortal);
20static const pxr::TfToken pole_axis("poleAxis", pxr::TfToken::Immortal);
21} // namespace usdtokens
22
23namespace io::usd {
24
35template<typename T>
36static bool get_authored_value(const pxr::UsdAttribute &attr,
37 const pxr::UsdTimeCode time,
38 const pxr::UsdPrim &prim,
39 const pxr::TfToken fallback_attr_name,
40 T *r_value)
41{
42 if (attr && attr.HasAuthoredValue()) {
43 return attr.Get<T>(r_value, time);
44 }
45
46 if (!prim || fallback_attr_name.IsEmpty()) {
47 return false;
48 }
49
50 pxr::UsdAttribute fallback_attr = prim.GetAttribute(fallback_attr_name);
51 if (fallback_attr && fallback_attr.HasAuthoredValue()) {
52 return fallback_attr.Get<T>(r_value, time);
53 }
54
55 return false;
56}
57
58template<typename T> static float get_intensity(const T &dome_light, const pxr::UsdTimeCode time)
59{
60 float intensity = 1.0f;
62 dome_light.GetIntensityAttr(), time, dome_light.GetPrim(), usdtokens::intensity, &intensity);
63 return intensity;
64}
65
66template<typename T>
67static bool get_tex_path(const T &dome_light,
68 const pxr::UsdTimeCode time,
69 pxr::SdfAssetPath *tex_path)
70{
71 bool has_tex = get_authored_value(dome_light.GetTextureFileAttr(),
72 time,
73 dome_light.GetPrim(),
75 tex_path);
76 return has_tex;
77}
78
79template<typename T>
80static bool get_color(const T &dome_light, const pxr::UsdTimeCode time, pxr::GfVec3f *color)
81{
82 bool has_color = get_authored_value(
83 dome_light.GetColorAttr(), time, dome_light.GetPrim(), usdtokens::color, color);
84 if (has_color) {
85 colorspace_attr_to_scene_linear(dome_light.GetColorAttr(), *color);
86 }
87 return has_color;
88}
89
90static pxr::TfToken get_pole_axis(const pxr::UsdLuxDomeLight_1 &dome_light,
91 const pxr::UsdTimeCode time)
92{
93 pxr::TfToken pole_axis = pxr::UsdLuxTokens->scene;
94 get_authored_value(dome_light.GetPoleAxisAttr(), time, dome_light.GetPrim(), {}, &pole_axis);
95 return pole_axis;
96}
97
99{
100 USDImportDomeLightData dome_light_data;
101
102 /* Time varying dome lights are not currently supported. */
103 constexpr pxr::UsdTimeCode time = 0.0;
104
105 if (prim_.IsA<pxr::UsdLuxDomeLight>()) {
106 pxr::UsdLuxDomeLight dome_light = pxr::UsdLuxDomeLight(prim_);
107 dome_light_data.intensity = get_intensity(dome_light, time);
108 dome_light_data.has_tex = get_tex_path(dome_light, time, &dome_light_data.tex_path);
109 dome_light_data.has_color = get_color(dome_light, time, &dome_light_data.color);
110 dome_light_data.pole_axis = pxr::UsdLuxTokens->Y;
111 }
112 else if (prim_.IsA<pxr::UsdLuxDomeLight_1>()) {
113 pxr::UsdLuxDomeLight_1 dome_light = pxr::UsdLuxDomeLight_1(prim_);
114 dome_light_data.intensity = get_intensity(dome_light, time);
115 dome_light_data.has_tex = get_tex_path(dome_light, time, &dome_light_data.tex_path);
116 dome_light_data.has_color = get_color(dome_light, time, &dome_light_data.color);
117 dome_light_data.pole_axis = get_pole_axis(dome_light, time);
118 }
119
120 dome_light_to_world_material(import_params_, scene, bmain, dome_light_data, prim_);
121}
122
123} // namespace io::usd
124} // namespace blender
const USDImportParams & import_params_
#define T
static pxr::TfToken get_pole_axis(const pxr::UsdLuxDomeLight_1 &dome_light, const pxr::UsdTimeCode time)
void dome_light_to_world_material(const USDImportParams &params, Scene *scene, Main *bmain, const USDImportDomeLightData &dome_light_data, const pxr::UsdPrim &prim, const pxr::UsdTimeCode time)
static bool get_color(const T &dome_light, const pxr::UsdTimeCode time, pxr::GfVec3f *color)
void colorspace_attr_to_scene_linear(const pxr::UsdAttribute &attr, pxr::GfVec3f &color)
static bool get_authored_value(const pxr::UsdAttribute &attr, const pxr::UsdTimeCode time, const pxr::UsdPrim &prim, const pxr::TfToken fallback_attr_name, T *r_value)
static bool get_tex_path(const T &dome_light, const pxr::UsdTimeCode time, pxr::SdfAssetPath *tex_path)
static float get_intensity(const T &dome_light, const pxr::UsdTimeCode time)
static const pxr::TfToken texture_file("texture:file", pxr::TfToken::Immortal)
static const pxr::TfToken pole_axis("poleAxis", pxr::TfToken::Immortal)
static const pxr::TfToken intensity("intensity", pxr::TfToken::Immortal)
static const pxr::TfToken color("color", pxr::TfToken::Immortal)
BMesh Iterators.