Blender V5.3
usd_attribute_utils.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#pragma once
5
7
8#include "BLI_color.hh"
12#include "BLI_span.hh"
13#include "BLI_virtual_array.hh"
14
15#include "BKE_attribute.hh"
16
17#include "IO_validate.hh"
18
19#include <pxr/base/gf/quatf.h>
20#include <pxr/base/gf/vec2f.h>
21#include <pxr/base/gf/vec3f.h>
22#include <pxr/base/vt/array.h>
23
24#include <pxr/usd/sdf/types.h>
25#include <pxr/usd/sdf/valueTypeName.h>
26#include <pxr/usd/usd/timeCode.h>
27#include <pxr/usd/usdGeom/primvar.h>
28#include <pxr/usd/usdUtils/sparseValueWriter.h>
29
30#include <cstdint>
31#include <optional>
32#include <type_traits>
33
34namespace blender {
35
36namespace usdtokens {
37inline const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal);
38}
39
40namespace io::usd {
41
42namespace detail {
43
44/* Until we can use C++20, implement our own version of std::is_layout_compatible.
45 * Types with compatible layouts can be exchanged much more efficiently than otherwise.
46 */
47template<class T, class U> struct is_layout_compatible : std::false_type {};
48
49template<> struct is_layout_compatible<float2, pxr::GfVec2f> : std::true_type {};
50template<> struct is_layout_compatible<float3, pxr::GfVec3f> : std::true_type {};
51
52template<> struct is_layout_compatible<pxr::GfVec2f, float2> : std::true_type {};
53template<> struct is_layout_compatible<pxr::GfVec3f, float3> : std::true_type {};
54
55template<typename T>
56concept IsSmall = sizeof(T) <= 16;
57
58/* Conversion utilities to convert a Blender type to an USD type. */
59template<typename From, typename To>
60 requires(IsSmall<From>)
61inline To convert_value(const From value)
62{
63 return value;
64}
65template<typename From, typename To>
66 requires(!IsSmall<From>)
67inline To convert_value(const From &value)
68{
69 return value;
70}
71
72template<> inline pxr::GfVec2f convert_value(const float2 value)
73{
74 return pxr::GfVec2f(value[0], value[1]);
75}
76template<> inline pxr::GfVec3f convert_value(const float3 value)
77{
78 return pxr::GfVec3f(value[0], value[1], value[2]);
79}
80template<> inline pxr::GfVec3f convert_value(const ColorGeometry4f value)
81{
82 return pxr::GfVec3f(value.r, value.g, value.b);
83}
84template<> inline pxr::GfVec4f convert_value(const ColorGeometry4f value)
85{
86 return pxr::GfVec4f(value.r, value.g, value.b, value.a);
87}
88template<> inline pxr::GfVec3f convert_value(const ColorGeometry4b value)
89{
90 ColorGeometry4f color4f = color::decode(value);
91 return pxr::GfVec3f(color4f.r, color4f.g, color4f.b);
92}
93template<> inline pxr::GfVec4f convert_value(const ColorGeometry4b value)
94{
95 ColorGeometry4f color4f = color::decode(value);
96 return pxr::GfVec4f(color4f.r, color4f.g, color4f.b, color4f.a);
97}
98template<> inline pxr::GfQuatf convert_value(const math::Quaternion value)
99{
100 return pxr::GfQuatf(value.w, value.x, value.y, value.z);
101}
102template<> inline pxr::GfMatrix4d convert_value(const float4x4 &value)
103{
104 pxr::GfMatrix4d result;
105 for (int i = 0; i < 4; ++i) {
106 for (int j = 0; j < 4; ++j) {
107 /* USD is row-major, Blender is column-major. */
108 result[j][i] = value[i][j];
109 }
110 }
111 return result;
112}
113
114template<> inline float2 convert_value(const pxr::GfVec2f value)
115{
116 return float2(value[0], value[1]);
117}
118template<> inline float3 convert_value(const pxr::GfVec3f value)
119{
120 return float3(value[0], value[1], value[2]);
121}
122template<> inline ColorGeometry4f convert_value(const pxr::GfVec3f value)
123{
124 return ColorGeometry4f(value[0], value[1], value[2], 1.0f);
125}
126template<> inline ColorGeometry4f convert_value(const pxr::GfVec4f value)
127{
128 return ColorGeometry4f(value[0], value[1], value[2], value[3]);
129}
130template<> inline math::Quaternion convert_value(const pxr::GfQuatf value)
131{
132 const pxr::GfVec3f &img = value.GetImaginary();
133 return math::Quaternion(value.GetReal(), img[0], img[1], img[2]);
134}
135template<> inline float4x4 convert_value(const pxr::GfMatrix4d &value)
136{
138 for (int i = 0; i < 4; ++i) {
139 for (int j = 0; j < 4; ++j) {
140 /* USD is row-major, Blender is column-major. */
141 result[j][i] = float(value[i][j]);
142 }
143 }
144 return result;
145}
146
147template<class T> struct is_vt_array : std::false_type {};
148template<class T> struct is_vt_array<pxr::VtArray<T>> : std::true_type {};
149
150} // namespace detail
151
152std::optional<pxr::SdfValueTypeName> convert_blender_type_to_usd(const bke::AttrType blender_type,
153 bool use_color3f_type = false);
154
155std::optional<bke::AttrType> convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type);
156
161template<typename USDT>
162void set_attribute(const pxr::UsdAttribute &attr,
163 const USDT value,
164 pxr::UsdTimeCode time,
165 pxr::UsdUtilsSparseValueWriter &value_writer)
166{
167 /* This overload should only be use with non-VtArray types. If it is not, then that indicates
168 * an issue on the caller side, usually because of using a const reference rather than non-const
169 * for the `value` parameter. */
170 static_assert(!detail::is_vt_array<USDT>::value, "Wrong set_attribute overload selected.");
171
172 if (!attr.HasValue()) {
173 attr.Set(value, pxr::UsdTimeCode::Default());
174 }
175
176 value_writer.SetAttribute(attr, pxr::VtValue(value), time);
177}
178
184template<typename USDT>
185void set_attribute(const pxr::UsdAttribute &attr,
186 pxr::VtArray<USDT> &value,
187 pxr::UsdTimeCode time,
188 pxr::UsdUtilsSparseValueWriter &value_writer)
189{
190 if (!attr.HasValue()) {
191 attr.Set(value, pxr::UsdTimeCode::Default());
192 }
193
194 pxr::VtValue val = pxr::VtValue::Take(value);
195 value_writer.SetAttribute(attr, &val, time);
196}
197
198/* Copy a typed Blender attribute array into a typed USD primvar attribute. */
199template<typename BlenderT, typename USDT>
201 const pxr::UsdTimeCode time,
202 const pxr::UsdGeomPrimvar &primvar,
203 pxr::UsdUtilsSparseValueWriter &value_writer)
204{
205 constexpr bool is_same = std::is_same_v<BlenderT, USDT>;
206 constexpr bool is_compatible = detail::is_layout_compatible<BlenderT, USDT>::value;
207
208 pxr::VtArray<USDT> usd_data;
209 if (const std::optional<BlenderT> value = buffer.get_if_single()) {
210 usd_data.assign(buffer.size(), detail::convert_value<BlenderT, USDT>(*value));
211 }
212 else {
213 const VArraySpan<BlenderT> data(buffer);
214 if constexpr (is_same || is_compatible) {
215 usd_data.assign(data.template cast<USDT>().begin(), data.template cast<USDT>().end());
216 }
217 else {
218 usd_data.resize(data.size());
219 for (const int i : data.index_range()) {
221 }
222 }
223 }
224
225 set_attribute(primvar, usd_data, time, value_writer);
226}
227
229 const bke::AttrType data_type,
230 const pxr::UsdTimeCode time,
231 const pxr::UsdGeomPrimvar &primvar,
232 pxr::UsdUtilsSparseValueWriter &value_writer);
233
234template<typename T>
235pxr::VtArray<T> get_primvar_array(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time)
236{
237 pxr::VtValue primvar_val;
238 if (!primvar.ComputeFlattened(&primvar_val, time)) {
239 return {};
240 }
241
242 if (!primvar_val.CanCast<pxr::VtArray<T>>()) {
243 return {};
244 }
245
246 return primvar_val.Cast<pxr::VtArray<T>>().template UncheckedGet<pxr::VtArray<T>>();
247}
248
250 const StringRef attr_name,
251 const bke::AttrDomain domain,
252 const bke::AttrType data_type,
253 const bke::AttributeInit &value)
254{
255 if (!attributes.contains(attr_name)) {
256 attributes.add(attr_name, domain, data_type, value);
257 }
258 else {
259 attributes.assign_data(attr_name, value);
260 }
261}
262
263template<typename USDT, typename BlenderT>
264void copy_primvar_to_blender_buffer(const pxr::UsdGeomPrimvar &primvar,
265 const pxr::UsdTimeCode time,
266 const bke::AttrType data_type,
267 const bke::AttrDomain domain,
268 const OffsetIndices<int> faces,
270{
271 const pxr::VtArray<USDT> usd_data = get_primvar_array<USDT>(primvar, time);
272 if (usd_data.empty()) {
273 return;
274 }
275
276 constexpr bool is_same = std::is_same_v<USDT, BlenderT>;
277 constexpr bool is_compatible = detail::is_layout_compatible<USDT, BlenderT>::value;
278 constexpr bool is_color = std::is_same_v<BlenderT, ColorGeometry4f>;
279
280 const pxr::TfToken pv_interp = primvar.GetInterpolation();
281 const pxr::TfToken pv_name = pxr::UsdGeomPrimvar::StripPrimvarsName(primvar.GetPrimvarName());
282 const StringRef attr_name = pv_name.GetText();
283
284 /* Map constant interpolation to single-value attributes. */
285 if (pv_interp == pxr::UsdGeomTokens->constant) {
286 BlenderT value = detail::convert_value<USDT, BlenderT>(usd_data[0]);
287 if constexpr (is_color) {
288 colorspace_attr_to_scene_linear(primvar.GetAttr(), value);
289 }
290 set_single_value(attributes, attr_name, domain, data_type, bke::AttributeInitValue(value));
291 return;
292 }
293
294 bke::SpanAttributeWriter<BlenderT> attribute_writer =
295 attributes.lookup_or_add_for_write_span<BlenderT>(pv_name.GetText(), domain);
296 MutableSpan<BlenderT> attribute = attribute_writer.span;
297
298 if (pv_interp == pxr::UsdGeomTokens->faceVarying) {
299 if (!faces.is_empty()) {
300 /* Reverse the index order. */
301 for (const int i : faces.index_range()) {
302 const IndexRange face = faces[i];
303 for (int j : face.index_range()) {
304 const int rev_index = face.last(j);
305 attribute[face.start() + j] = validate::index_in_range(rev_index, usd_data.size()) ?
307 usd_data[rev_index]) :
308 BlenderT();
309 }
310 }
311 }
312 else {
313 if constexpr (is_same || is_compatible) {
314 const Span<USDT> src(usd_data.data(), usd_data.size());
315 attribute.copy_from(src.template cast<BlenderT>());
316 }
317 else {
318 for (const int64_t i : attribute.index_range()) {
320 }
321 }
322 }
323 }
324 else {
325 /* Assume direct one-to-one mapping. */
326 if (usd_data.size() == attribute.size()) {
327 if constexpr (is_same || is_compatible) {
328 const Span<USDT> src(usd_data.data(), usd_data.size());
329 attribute.copy_from(src.template cast<BlenderT>());
330 }
331 else {
332 for (const int64_t i : attribute.index_range()) {
334 }
335 }
336 }
337 }
338
339 if constexpr (is_color) {
340 colorspace_attr_to_scene_linear(primvar.GetAttr(), attribute);
341 }
342
343 attribute_writer.finish();
344}
345
346void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar,
347 const pxr::UsdTimeCode time,
348 const bke::AttrType data_type,
349 const bke::AttrDomain domain,
350 const OffsetIndices<int> face_indices,
352
353} // namespace io::usd
354} // namespace blender
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t start() const
constexpr IndexRange index_range() const
std::optional< T > get_if_single() const
bool contains(StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
bool assign_data(const StringRef name, const AttributeInit &initializer)
#define attribute(slot)
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:32
#define T
BLI_INLINE ColorSceneLinear4f< Alpha > decode(const ColorSceneLinearByteEncoded4b< Alpha > &color)
Definition BLI_color.hh:77
To convert_value(const From value)
void copy_blender_attribute_to_primvar(const GVArray &attribute, const bke::AttrType data_type, const pxr::UsdTimeCode time, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
pxr::VtArray< T > get_primvar_array(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time)
void copy_primvar_to_blender_buffer(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time, const bke::AttrType data_type, const bke::AttrDomain domain, const OffsetIndices< int > faces, bke::MutableAttributeAccessor attributes)
std::optional< pxr::SdfValueTypeName > convert_blender_type_to_usd(const bke::AttrType blender_type, bool use_color3f_type)
void colorspace_attr_to_scene_linear(const pxr::UsdAttribute &attr, pxr::GfVec3f &color)
void copy_blender_buffer_to_primvar(const VArray< BlenderT > &buffer, const pxr::UsdTimeCode time, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
void set_single_value(bke::MutableAttributeAccessor attributes, const StringRef attr_name, const bke::AttrDomain domain, const bke::AttrType data_type, const bke::AttributeInit &value)
void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time, const bke::AttrType data_type, const bke::AttrDomain domain, const OffsetIndices< int > face_indices, bke::MutableAttributeAccessor attributes)
std::optional< bke::AttrType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode time, pxr::UsdUtilsSparseValueWriter &value_writer)
bool index_in_range(const int64_t index, const int64_t size)
QuaternionBase< float > Quaternion
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
BMesh const char void * data
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float