Blender V4.5
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
6#include "BLI_color.hh"
10#include "BLI_span.hh"
11#include "BLI_virtual_array.hh"
12
13#include "BKE_attribute.hh"
14
16
17#include <pxr/base/gf/quatf.h>
18#include <pxr/base/gf/vec2f.h>
19#include <pxr/base/gf/vec3f.h>
20#include <pxr/base/vt/array.h>
21
22#include <pxr/usd/sdf/types.h>
23#include <pxr/usd/sdf/valueTypeName.h>
24#include <pxr/usd/usd/timeCode.h>
25#include <pxr/usd/usdGeom/primvar.h>
26#include <pxr/usd/usdUtils/sparseValueWriter.h>
27
28#include <cstdint>
29#include <optional>
30#include <type_traits>
31
32namespace usdtokens {
33inline const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal);
34}
35
36namespace blender::io::usd {
37
38namespace detail {
39
40/* Until we can use C++20, implement our own version of std::is_layout_compatible.
41 * Types with compatible layouts can be exchanged much more efficiently than otherwise.
42 */
43template<class T, class U> struct is_layout_compatible : std::false_type {};
44
45template<> struct is_layout_compatible<float2, pxr::GfVec2f> : std::true_type {};
46template<> struct is_layout_compatible<float3, pxr::GfVec3f> : std::true_type {};
47
48template<> struct is_layout_compatible<pxr::GfVec2f, float2> : std::true_type {};
49template<> struct is_layout_compatible<pxr::GfVec3f, float3> : std::true_type {};
50
51/* Conversion utilities to convert a Blender type to an USD type. */
52template<typename From, typename To> inline To convert_value(const From value)
53{
54 return value;
55}
56
57template<> inline pxr::GfVec2f convert_value(const float2 value)
58{
59 return pxr::GfVec2f(value[0], value[1]);
60}
61template<> inline pxr::GfVec3f convert_value(const float3 value)
62{
63 return pxr::GfVec3f(value[0], value[1], value[2]);
64}
65template<> inline pxr::GfVec3f convert_value(const ColorGeometry4f value)
66{
67 return pxr::GfVec3f(value.r, value.g, value.b);
68}
69template<> inline pxr::GfVec4f convert_value(const ColorGeometry4f value)
70{
71 return pxr::GfVec4f(value.r, value.g, value.b, value.a);
72}
73template<> inline pxr::GfVec3f convert_value(const ColorGeometry4b value)
74{
75 ColorGeometry4f color4f = value.decode();
76 return pxr::GfVec3f(color4f.r, color4f.g, color4f.b);
77}
78template<> inline pxr::GfVec4f convert_value(const ColorGeometry4b value)
79{
80 ColorGeometry4f color4f = value.decode();
81 return pxr::GfVec4f(color4f.r, color4f.g, color4f.b, color4f.a);
82}
83template<> inline pxr::GfQuatf convert_value(const math::Quaternion value)
84{
85 return pxr::GfQuatf(value.w, value.x, value.y, value.z);
86}
87
88template<> inline float2 convert_value(const pxr::GfVec2f value)
89{
90 return float2(value[0], value[1]);
91}
92template<> inline float3 convert_value(const pxr::GfVec3f value)
93{
94 return float3(value[0], value[1], value[2]);
95}
96template<> inline ColorGeometry4f convert_value(const pxr::GfVec3f value)
97{
98 return ColorGeometry4f(value[0], value[1], value[2], 1.0f);
99}
100template<> inline ColorGeometry4f convert_value(const pxr::GfVec4f value)
101{
102 return ColorGeometry4f(value[0], value[1], value[2], value[3]);
103}
104template<> inline math::Quaternion convert_value(const pxr::GfQuatf value)
105{
106 const pxr::GfVec3f &img = value.GetImaginary();
107 return math::Quaternion(value.GetReal(), img[0], img[1], img[2]);
108}
109
110template<class T> struct is_vt_array : std::false_type {};
111template<class T> struct is_vt_array<pxr::VtArray<T>> : std::true_type {};
112
113} // namespace detail
114
115std::optional<pxr::SdfValueTypeName> convert_blender_type_to_usd(
116 const eCustomDataType blender_type, bool use_color3f_type = false);
117
118std::optional<eCustomDataType> convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type);
119
124template<typename USDT>
125void set_attribute(const pxr::UsdAttribute &attr,
126 const USDT value,
127 pxr::UsdTimeCode timecode,
128 pxr::UsdUtilsSparseValueWriter &value_writer)
129{
130 /* This overload should only be use with non-VtArray types. If it is not, then that indicates
131 * an issue on the caller side, usually because of using a const reference rather than non-const
132 * for the `value` parameter. */
133 static_assert(!detail::is_vt_array<USDT>::value, "Wrong set_attribute overload selected.");
134
135 if (!attr.HasValue()) {
136 attr.Set(value, pxr::UsdTimeCode::Default());
137 }
138
139 value_writer.SetAttribute(attr, pxr::VtValue(value), timecode);
140}
141
146template<typename USDT>
147void set_attribute(const pxr::UsdAttribute &attr,
148 pxr::VtArray<USDT> &value,
149 pxr::UsdTimeCode timecode,
150 pxr::UsdUtilsSparseValueWriter &value_writer)
151{
152 if (!attr.HasValue()) {
153 attr.Set(value, pxr::UsdTimeCode::Default());
154 }
155
156 pxr::VtValue val = pxr::VtValue::Take(value);
157 value_writer.SetAttribute(attr, &val, timecode);
158}
159
160/* Copy a typed Blender attribute array into a typed USD primvar attribute. */
161template<typename BlenderT, typename USDT>
163 const pxr::UsdTimeCode timecode,
164 const pxr::UsdGeomPrimvar &primvar,
165 pxr::UsdUtilsSparseValueWriter &value_writer)
166{
167 constexpr bool is_same = std::is_same_v<BlenderT, USDT>;
168 constexpr bool is_compatible = detail::is_layout_compatible<BlenderT, USDT>::value;
169
170 pxr::VtArray<USDT> usd_data;
171 if (const std::optional<BlenderT> value = buffer.get_if_single()) {
172 usd_data.assign(buffer.size(), detail::convert_value<BlenderT, USDT>(*value));
173 }
174 else {
175 const VArraySpan<BlenderT> data(buffer);
176 if constexpr (is_same || is_compatible) {
177 usd_data.assign(data.template cast<USDT>().begin(), data.template cast<USDT>().end());
178 }
179 else {
180 usd_data.resize(data.size());
181 for (const int i : data.index_range()) {
183 }
184 }
185 }
186
187 set_attribute(primvar, usd_data, timecode, value_writer);
188}
189
190void copy_blender_attribute_to_primvar(const GVArray &attribute,
191 const eCustomDataType data_type,
192 const pxr::UsdTimeCode timecode,
193 const pxr::UsdGeomPrimvar &primvar,
194 pxr::UsdUtilsSparseValueWriter &value_writer);
195
196template<typename T>
197pxr::VtArray<T> get_primvar_array(const pxr::UsdGeomPrimvar &primvar,
198 const pxr::UsdTimeCode timecode)
199{
200 pxr::VtValue primvar_val;
201 if (!primvar.ComputeFlattened(&primvar_val, timecode)) {
202 return {};
203 }
204
205 if (!primvar_val.CanCast<pxr::VtArray<T>>()) {
206 return {};
207 }
208
209 return primvar_val.Cast<pxr::VtArray<T>>().template UncheckedGet<pxr::VtArray<T>>();
210}
211
212template<typename USDT, typename BlenderT>
213void copy_primvar_to_blender_buffer(const pxr::UsdGeomPrimvar &primvar,
214 const pxr::UsdTimeCode timecode,
216 MutableSpan<BlenderT> attribute)
217{
218 const pxr::VtArray<USDT> usd_data = get_primvar_array<USDT>(primvar, timecode);
219 if (usd_data.empty()) {
220 return;
221 }
222
223 constexpr bool is_same = std::is_same_v<USDT, BlenderT>;
224 constexpr bool is_compatible = detail::is_layout_compatible<USDT, BlenderT>::value;
225
226 const pxr::TfToken pv_interp = primvar.GetInterpolation();
227 if (pv_interp == pxr::UsdGeomTokens->constant) {
228 /* For situations where there's only a single item, flood fill the object. */
229 attribute.fill(detail::convert_value<USDT, BlenderT>(usd_data[0]));
230 }
231 else if (pv_interp == pxr::UsdGeomTokens->faceVarying) {
232 if (!faces.is_empty()) {
233 /* Reverse the index order. */
234 for (const int i : faces.index_range()) {
235 const IndexRange face = faces[i];
236 for (int j : face.index_range()) {
237 const int rev_index = face.last(j);
238 attribute[face.start() + j] = detail::convert_value<USDT, BlenderT>(usd_data[rev_index]);
239 }
240 }
241 }
242 else {
243 if constexpr (is_same || is_compatible) {
244 const Span<USDT> src(usd_data.data(), usd_data.size());
245 attribute.copy_from(src.template cast<BlenderT>());
246 }
247 else {
248 for (const int64_t i : attribute.index_range()) {
249 attribute[i] = detail::convert_value<USDT, BlenderT>(usd_data[i]);
250 }
251 }
252 }
253 }
254 else {
255 /* Assume direct one-to-one mapping. */
256 if (usd_data.size() == attribute.size()) {
257 if constexpr (is_same || is_compatible) {
258 const Span<USDT> src(usd_data.data(), usd_data.size());
259 attribute.copy_from(src.template cast<BlenderT>());
260 }
261 else {
262 for (const int64_t i : attribute.index_range()) {
263 attribute[i] = detail::convert_value<USDT, BlenderT>(usd_data[i]);
264 }
265 }
266 }
267 }
268}
269
270void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar,
271 const pxr::UsdTimeCode timecode,
272 const eCustomDataType data_type,
273 const bke::AttrDomain domain,
274 const OffsetIndices<int> face_indices,
276
277} // namespace blender::io::usd
BMesh const char void * data
long long int int64_t
ChannelStorageType r
Definition BLI_color.hh:93
ChannelStorageType g
Definition BLI_color.hh:93
ChannelStorageType b
Definition BLI_color.hh:93
ChannelStorageType a
Definition BLI_color.hh:93
ColorSceneLinear4f< Alpha > decode() const
Definition BLI_color.hh:229
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t start() const
constexpr IndexRange index_range() const
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:739
std::optional< T > get_if_single() const
#define cast
static char faces[256]
To convert_value(const From value)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode timecode, pxr::UsdUtilsSparseValueWriter &value_writer)
pxr::VtArray< T > get_primvar_array(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode timecode)
void copy_blender_attribute_to_primvar(const GVArray &attribute, const eCustomDataType data_type, const pxr::UsdTimeCode timecode, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
void copy_primvar_to_blender_buffer(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode timecode, const OffsetIndices< int > faces, MutableSpan< BlenderT > attribute)
std::optional< eCustomDataType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
std::optional< pxr::SdfValueTypeName > convert_blender_type_to_usd(const eCustomDataType blender_type, bool use_color3f_type)
void copy_blender_buffer_to_primvar(const VArray< BlenderT > &buffer, const pxr::UsdTimeCode timecode, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode timecode, const eCustomDataType data_type, const bke::AttrDomain domain, const OffsetIndices< int > face_indices, bke::MutableAttributeAccessor attributes)
QuaternionBase< float > Quaternion
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:342
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
Definition BLI_color.hh:343
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)
i
Definition text_draw.cc:230