Blender V5.0
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
7
8#include <pxr/usd/usdLux/domeLight.h>
9#include <pxr/usd/usdLux/domeLight_1.h>
10#include <pxr/usd/usdLux/tokens.h>
11
12namespace usdtokens {
13// Attribute names.
14static const pxr::TfToken color("color", pxr::TfToken::Immortal);
15static const pxr::TfToken intensity("intensity", pxr::TfToken::Immortal);
16static const pxr::TfToken texture_file("texture:file", pxr::TfToken::Immortal);
17static const pxr::TfToken pole_axis("poleAxis", pxr::TfToken::Immortal);
18} // namespace usdtokens
19
20namespace blender::io::usd {
21
32template<typename T>
33static bool get_authored_value(const pxr::UsdAttribute &attr,
34 const pxr::UsdTimeCode time,
35 const pxr::UsdPrim &prim,
36 const pxr::TfToken fallback_attr_name,
37 T *r_value)
38{
39 if (attr && attr.HasAuthoredValue()) {
40 return attr.Get<T>(r_value, time);
41 }
42
43 if (!prim || fallback_attr_name.IsEmpty()) {
44 return false;
45 }
46
47 pxr::UsdAttribute fallback_attr = prim.GetAttribute(fallback_attr_name);
48 if (fallback_attr && fallback_attr.HasAuthoredValue()) {
49 return fallback_attr.Get<T>(r_value, time);
50 }
51
52 return false;
53}
54
55template<typename T> static float get_intensity(const T &dome_light, const pxr::UsdTimeCode time)
56{
57 float intensity = 1.0f;
59 dome_light.GetIntensityAttr(), time, dome_light.GetPrim(), usdtokens::intensity, &intensity);
60 return intensity;
61}
62
63template<typename T>
64static bool get_tex_path(const T &dome_light,
65 const pxr::UsdTimeCode time,
66 pxr::SdfAssetPath *tex_path)
67{
68 bool has_tex = get_authored_value(dome_light.GetTextureFileAttr(),
69 time,
70 dome_light.GetPrim(),
72 tex_path);
73 return has_tex;
74}
75
76template<typename T>
77static bool get_color(const T &dome_light, const pxr::UsdTimeCode time, pxr::GfVec3f *color)
78{
79 bool has_color = get_authored_value(
80 dome_light.GetColorAttr(), time, dome_light.GetPrim(), usdtokens::color, color);
81 return has_color;
82}
83
84static pxr::TfToken get_pole_axis(const pxr::UsdLuxDomeLight_1 &dome_light,
85 const pxr::UsdTimeCode time)
86{
87 pxr::TfToken pole_axis = pxr::UsdLuxTokens->scene;
88 get_authored_value(dome_light.GetPoleAxisAttr(), time, dome_light.GetPrim(), {}, &pole_axis);
89 return pole_axis;
90}
91
93{
94 USDImportDomeLightData dome_light_data;
95
96 /* Time varying dome lights are not currently supported. */
97 constexpr pxr::UsdTimeCode time = 0.0;
98
99 if (prim_.IsA<pxr::UsdLuxDomeLight>()) {
100 pxr::UsdLuxDomeLight dome_light = pxr::UsdLuxDomeLight(prim_);
101 dome_light_data.intensity = get_intensity(dome_light, time);
102 dome_light_data.has_tex = get_tex_path(dome_light, time, &dome_light_data.tex_path);
103 dome_light_data.has_color = get_color(dome_light, time, &dome_light_data.color);
104 dome_light_data.pole_axis = pxr::UsdLuxTokens->Y;
105 }
106 else if (prim_.IsA<pxr::UsdLuxDomeLight_1>()) {
107 pxr::UsdLuxDomeLight_1 dome_light = pxr::UsdLuxDomeLight_1(prim_);
108 dome_light_data.intensity = get_intensity(dome_light, time);
109 dome_light_data.has_tex = get_tex_path(dome_light, time, &dome_light_data.tex_path);
110 dome_light_data.has_color = get_color(dome_light, time, &dome_light_data.color);
111 dome_light_data.pole_axis = get_pole_axis(dome_light, time);
112 }
113
114 dome_light_to_world_material(import_params_, scene, bmain, dome_light_data, prim_);
115}
116
117} // namespace blender::io::usd
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)
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 intensity("intensity", pxr::TfToken::Immortal)
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 color("color", pxr::TfToken::Immortal)