Blender V5.3
usd_reader_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 NVIDIA Corporation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "usd_reader_utils.hh"
6
7#include "BLI_string_ref.hh"
8
9#include "BKE_idprop.hh"
10
11#include <pxr/usd/usd/attribute.h>
12#include <pxr/usd/usdUI/accessibilityAPI.h>
13
14#include "CLG_log.h"
15
16namespace blender {
17
18static CLG_LogRef LOG = {"io.usd"};
19
20namespace {
21
22template<typename VECT>
23void set_array_prop(IDProperty *idgroup,
24 const StringRefNull prop_name,
25 const pxr::UsdAttribute &attr,
26 const pxr::UsdTimeCode time)
27{
28 if (!idgroup || !attr) {
29 return;
30 }
31
32 VECT vec;
33 if (!attr.Get<VECT>(&vec, time)) {
34 return;
35 }
36
37 IDPropertyTemplate val = {0};
38 val.array.len = int(vec.dimension);
39
40 if (val.array.len <= 0) {
41 CLOG_WARN(&LOG, "Invalid array length for prop %s", prop_name.c_str());
42 return;
43 }
44
45 if (std::is_same<float, typename VECT::ScalarType>()) {
46 val.array.type = IDP_FLOAT;
47 }
48 else if (std::is_same<pxr::GfHalf, typename VECT::ScalarType>()) {
49 val.array.type = IDP_FLOAT;
50 }
51 else if (std::is_same<double, typename VECT::ScalarType>()) {
52 val.array.type = IDP_DOUBLE;
53 }
54 else if (std::is_same<int, typename VECT::ScalarType>()) {
55 val.array.type = IDP_INT;
56 }
57 else {
58 CLOG_WARN(&LOG, "Couldn't determine array type for prop %s", prop_name.c_str());
59 return;
60 }
61
62 IDProperty *prop = IDP_New(IDP_ARRAY, &val, prop_name);
63
64 if (!prop) {
65 CLOG_WARN(&LOG, "Couldn't create array prop %s", prop_name.c_str());
66 return;
67 }
68
69 if (std::is_same<pxr::GfHalf, typename VECT::ScalarType>()) {
70 float *prop_data = static_cast<float *>(prop->data.pointer);
71 for (int i = 0; i < val.array.len; ++i) {
72 prop_data[i] = vec[i];
73 }
74 }
75 else {
76 std::memcpy(prop->data.pointer, vec.data(), prop->len * sizeof(typename VECT::ScalarType));
77 }
78
79 IDP_AddToGroup(idgroup, prop);
80}
81
82bool equivalent(const pxr::SdfValueTypeName &type_name1, const pxr::SdfValueTypeName &type_name2)
83{
84 return type_name1.GetType().IsA(type_name2.GetType());
85}
86
87} // anonymous namespace
88
89namespace io::usd {
90
91static void set_string_prop(IDProperty *idgroup,
92 const StringRefNull prop_name,
93 const StringRefNull str_val)
94{
95 if (!idgroup) {
96 return;
97 }
98
99 IDPropertyTemplate val = {0};
100 val.string.str = str_val.data();
101 /* Note length includes null terminator. */
102 val.string.len = str_val.size() + 1;
103 val.string.subtype = IDP_STRING_SUB_UTF8;
104
105 IDProperty *prop = IDP_New(IDP_STRING, &val, prop_name);
106
107 IDP_AddToGroup(idgroup, prop);
108}
109
110static void set_int_prop(IDProperty *idgroup, const StringRefNull prop_name, const int ival)
111{
112 if (!idgroup) {
113 return;
114 }
115
116 IDPropertyTemplate val = {0};
117 val.i = ival;
118 IDProperty *prop = IDP_New(IDP_INT, &val, prop_name);
119
120 IDP_AddToGroup(idgroup, prop);
121}
122
123static void set_bool_prop(IDProperty *idgroup, const StringRefNull prop_name, const bool bval)
124{
125 if (!idgroup) {
126 return;
127 }
128
129 IDPropertyTemplate val = {0};
130 val.i = bval;
131 IDProperty *prop = IDP_New(IDP_BOOLEAN, &val, prop_name);
132
133 IDP_AddToGroup(idgroup, prop);
134}
135
136static void set_float_prop(IDProperty *idgroup, const StringRefNull prop_name, const float fval)
137{
138 if (!idgroup) {
139 return;
140 }
141
142 IDPropertyTemplate val = {0};
143 val.f = fval;
144 IDProperty *prop = IDP_New(IDP_FLOAT, &val, prop_name);
145
146 IDP_AddToGroup(idgroup, prop);
147}
148
149static void set_double_prop(IDProperty *idgroup, const StringRefNull prop_name, const double dval)
150{
151 if (!idgroup) {
152 return;
153 }
154
155 IDPropertyTemplate val = {0};
156 val.d = dval;
157 IDProperty *prop = IDP_New(IDP_DOUBLE, &val, prop_name);
158
159 IDP_AddToGroup(idgroup, prop);
160}
161
162static void set_accessibility_property(const pxr::UsdAttribute &attr,
163 IDProperty *idgroup,
164 const pxr::UsdTimeCode time_code)
165{
166 /* Only set the property if the attribute has an authored value. */
167 if (!attr.IsAuthored()) {
168 return;
169 }
170
171 /* Since STRING properties do not support keyframes, if there is already a value written
172 * for this property, don't try to write it again. */
173 std::string property_name = attr.GetName().GetString();
174 if (IDP_GetPropertyFromGroup(idgroup, property_name)) {
175 return;
176 }
177
178 pxr::SdfValueTypeName type_name = attr.GetTypeName();
179 if (type_name == pxr::SdfValueTypeNames->String) {
180 std::string value;
181 if (attr.Get<std::string>(&value, time_code)) {
182 set_string_prop(idgroup, property_name, value);
183 }
184 }
185 else if (type_name == pxr::SdfValueTypeNames->Token) {
186 pxr::TfToken value;
187 if (attr.Get<pxr::TfToken>(&value, time_code)) {
188 set_string_prop(idgroup, property_name, value.GetString());
189 }
190 }
191}
192
194 const pxr::UsdPrim &prim,
195 const PropertyImportMode property_import_mode,
196 const pxr::UsdTimeCode time_code)
197{
198 for (const auto &api : pxr::UsdUIAccessibilityAPI::GetAll(prim)) {
199 IDProperty *idgroup = IDP_EnsureProperties(id);
200 set_accessibility_property(api.GetLabelAttr(), idgroup, time_code);
201 set_accessibility_property(api.GetDescriptionAttr(), idgroup, time_code);
202 set_accessibility_property(api.GetPriorityAttr(), idgroup, time_code);
203 }
204
205 pxr::UsdAttributeVector attribs = prim.GetAuthoredAttributes();
206 if (attribs.empty()) {
207 return;
208 }
209
210 bool all_custom_attrs = (property_import_mode == PropertyImportMode::All);
211
212 for (const pxr::UsdAttribute &attr : attribs) {
213 if (!attr.IsCustom()) {
214 continue;
215 }
216
217 std::vector<std::string> attr_names = attr.SplitName();
218
219 const bool is_user_prop = attr_names[0] == "userProperties";
220
221 if (attr_names.size() > 2 && is_user_prop && attr_names[1] == "blender") {
222 continue;
223 }
224
225 if (!all_custom_attrs && !is_user_prop) {
226 continue;
227 }
228
229 IDProperty *idgroup = IDP_EnsureProperties(id);
230
231 /* When importing user properties, strip the namespace. */
232 pxr::TfToken attr_name;
233 if (is_user_prop) {
234 /* We strip the userProperties namespace, but leave others in case
235 * someone's custom attribute namespace is important in their pipeline. */
236 const StringRefNull token = "userProperties:";
237 const StringRefNull name = attr.GetName().GetString();
238 attr_name = pxr::TfToken(name.substr(token.size()));
239 }
240 else {
241 attr_name = attr.GetName();
242 }
243
244 pxr::SdfValueTypeName type_name = attr.GetTypeName();
245
246 if (type_name == pxr::SdfValueTypeNames->Int) {
247 int ival = 0;
248 if (attr.Get<int>(&ival, time_code)) {
249 set_int_prop(idgroup, attr_name.GetString(), ival);
250 }
251 }
252 else if (type_name == pxr::SdfValueTypeNames->Float) {
253 float fval = 0.0f;
254 if (attr.Get<float>(&fval, time_code)) {
255 set_float_prop(idgroup, attr_name.GetString(), fval);
256 }
257 }
258 else if (type_name == pxr::SdfValueTypeNames->Double) {
259 double dval = 0.0;
260 if (attr.Get<double>(&dval, time_code)) {
261 set_double_prop(idgroup, attr_name.GetString(), dval);
262 }
263 }
264 else if (type_name == pxr::SdfValueTypeNames->Half) {
265 pxr::GfHalf hval = 0.0f;
266 if (attr.Get<pxr::GfHalf>(&hval, time_code)) {
267 set_float_prop(idgroup, attr_name.GetString(), hval);
268 }
269 }
270 else if (type_name == pxr::SdfValueTypeNames->String) {
271 std::string sval;
272 if (attr.Get<std::string>(&sval, time_code)) {
273 set_string_prop(idgroup, attr_name.GetString(), sval);
274 }
275 }
276 else if (type_name == pxr::SdfValueTypeNames->Token) {
277 pxr::TfToken tval;
278 if (attr.Get<pxr::TfToken>(&tval, time_code)) {
279 set_string_prop(idgroup, attr_name.GetString(), tval.GetString());
280 }
281 }
282 else if (type_name == pxr::SdfValueTypeNames->Asset) {
283 pxr::SdfAssetPath aval;
284 if (attr.Get<pxr::SdfAssetPath>(&aval, time_code)) {
285 set_string_prop(idgroup, attr_name.GetString(), aval.GetAssetPath());
286 }
287 }
288 else if (type_name == pxr::SdfValueTypeNames->Bool) {
289 bool bval = false;
290 if (attr.Get<bool>(&bval, time_code)) {
291 set_bool_prop(idgroup, attr_name.GetString(), bval);
292 }
293 }
294 else if (equivalent(type_name, pxr::SdfValueTypeNames->Float2)) {
295 set_array_prop<pxr::GfVec2f>(idgroup, attr_name.GetString(), attr, time_code);
296 }
297 else if (equivalent(type_name, pxr::SdfValueTypeNames->Float3)) {
298 set_array_prop<pxr::GfVec3f>(idgroup, attr_name.GetString(), attr, time_code);
299 }
300 else if (equivalent(type_name, pxr::SdfValueTypeNames->Float4)) {
301 set_array_prop<pxr::GfVec4f>(idgroup, attr_name.GetString(), attr, time_code);
302 }
303 else if (equivalent(type_name, pxr::SdfValueTypeNames->Double2)) {
304 set_array_prop<pxr::GfVec2d>(idgroup, attr_name.GetString(), attr, time_code);
305 }
306 else if (equivalent(type_name, pxr::SdfValueTypeNames->Double3)) {
307 set_array_prop<pxr::GfVec3d>(idgroup, attr_name.GetString(), attr, time_code);
308 }
309 else if (equivalent(type_name, pxr::SdfValueTypeNames->Double4)) {
310 set_array_prop<pxr::GfVec4d>(idgroup, attr_name.GetString(), attr, time_code);
311 }
312 else if (equivalent(type_name, pxr::SdfValueTypeNames->Int2)) {
313 set_array_prop<pxr::GfVec2i>(idgroup, attr_name.GetString(), attr, time_code);
314 }
315 else if (equivalent(type_name, pxr::SdfValueTypeNames->Int3)) {
316 set_array_prop<pxr::GfVec3i>(idgroup, attr_name.GetString(), attr, time_code);
317 }
318 else if (equivalent(type_name, pxr::SdfValueTypeNames->Int4)) {
319 set_array_prop<pxr::GfVec4i>(idgroup, attr_name.GetString(), attr, time_code);
320 }
321 else if (equivalent(type_name, pxr::SdfValueTypeNames->Half2)) {
322 set_array_prop<pxr::GfVec2h>(idgroup, attr_name.GetString(), attr, time_code);
323 }
324 else if (equivalent(type_name, pxr::SdfValueTypeNames->Half3)) {
325 set_array_prop<pxr::GfVec3h>(idgroup, attr_name.GetString(), attr, time_code);
326 }
327 else if (equivalent(type_name, pxr::SdfValueTypeNames->Half4)) {
328 set_array_prop<pxr::GfVec4h>(idgroup, attr_name.GetString(), attr, time_code);
329 }
330 }
331}
332
333} // namespace io::usd
334} // namespace blender
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
constexpr int64_t size() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
#define LOG(level)
Definition log.h:97
void set_id_props_from_prim(ID *id, const pxr::UsdPrim &prim, const PropertyImportMode property_import_mode, const pxr::UsdTimeCode time_code)
static void set_double_prop(IDProperty *idgroup, const StringRefNull prop_name, const double dval)
static void set_string_prop(IDProperty *idgroup, const StringRefNull prop_name, const StringRefNull str_val)
static void set_float_prop(IDProperty *idgroup, const StringRefNull prop_name, const float fval)
static void set_accessibility_property(const pxr::UsdAttribute &attr, IDProperty *idgroup, const pxr::UsdTimeCode time_code)
static void set_bool_prop(IDProperty *idgroup, const StringRefNull prop_name, const bool bval)
static void set_int_prop(IDProperty *idgroup, const StringRefNull prop_name, const int ival)
BMesh Iterators.
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
@ IDP_STRING_SUB_UTF8
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1005
static CLG_LogRef LOG
Definition logging.cpp:12
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1132
@ String
Definition types.hh:154
struct blender::IDPropertyTemplate::@100215251242210352227251277016155305041022127377 array
struct blender::IDPropertyTemplate::@116303142222164257205355242364302075230051143265 string