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>
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>
33inline const pxr::TfToken
displayColor(
"displayColor", pxr::TfToken::Immortal);
52template<
typename From,
typename To>
inline To
convert_value(
const From value)
59 return pxr::GfVec2f(value[0], value[1]);
63 return pxr::GfVec3f(value[0], value[1], value[2]);
67 return pxr::GfVec3f(value.
r, value.
g, value.
b);
71 return pxr::GfVec4f(value.
r, value.
g, value.
b, value.
a);
76 return pxr::GfVec3f(color4f.
r, color4f.
g, color4f.
b);
81 return pxr::GfVec4f(color4f.
r, color4f.
g, color4f.
b, color4f.
a);
85 return pxr::GfQuatf(value.
w, value.
x, value.
y, value.
z);
90 return float2(value[0], value[1]);
94 return float3(value[0], value[1], value[2]);
106 const pxr::GfVec3f &img = value.GetImaginary();
111template<
class T>
struct is_vt_array<pxr::VtArray<T>> : std::true_type {};
124template<
typename USDT>
127 pxr::UsdTimeCode timecode,
128 pxr::UsdUtilsSparseValueWriter &value_writer)
135 if (!attr.HasValue()) {
136 attr.Set(value, pxr::UsdTimeCode::Default());
139 value_writer.SetAttribute(attr, pxr::VtValue(value), timecode);
146template<
typename USDT>
148 pxr::VtArray<USDT> &value,
149 pxr::UsdTimeCode timecode,
150 pxr::UsdUtilsSparseValueWriter &value_writer)
152 if (!attr.HasValue()) {
153 attr.Set(value, pxr::UsdTimeCode::Default());
156 pxr::VtValue val = pxr::VtValue::Take(value);
157 value_writer.SetAttribute(attr, &val, timecode);
161template<
typename BlenderT,
typename USDT>
163 const pxr::UsdTimeCode timecode,
164 const pxr::UsdGeomPrimvar &primvar,
165 pxr::UsdUtilsSparseValueWriter &value_writer)
167 constexpr bool is_same = std::is_same_v<BlenderT, USDT>;
170 pxr::VtArray<USDT> usd_data;
171 if (
const std::optional<BlenderT> value = buffer.
get_if_single()) {
176 if constexpr (is_same || is_compatible) {
180 usd_data.resize(
data.size());
181 for (
const int i :
data.index_range()) {
192 const pxr::UsdTimeCode timecode,
193 const pxr::UsdGeomPrimvar &primvar,
194 pxr::UsdUtilsSparseValueWriter &value_writer);
198 const pxr::UsdTimeCode timecode)
200 pxr::VtValue primvar_val;
201 if (!primvar.ComputeFlattened(&primvar_val, timecode)) {
205 if (!primvar_val.CanCast<pxr::VtArray<T>>()) {
209 return primvar_val.Cast<pxr::VtArray<T>>().
template UncheckedGet<pxr::VtArray<T>>();
212template<
typename USDT,
typename BlenderT>
214 const pxr::UsdTimeCode timecode,
219 if (usd_data.empty()) {
223 constexpr bool is_same = std::is_same_v<USDT, BlenderT>;
226 const pxr::TfToken pv_interp = primvar.GetInterpolation();
227 if (pv_interp == pxr::UsdGeomTokens->constant) {
231 else if (pv_interp == pxr::UsdGeomTokens->faceVarying) {
232 if (!
faces.is_empty()) {
234 for (
const int i :
faces.index_range()) {
237 const int rev_index = face.
last(j);
243 if constexpr (is_same || is_compatible) {
244 const Span<USDT> src(usd_data.data(), usd_data.size());
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());
271 const pxr::UsdTimeCode timecode,
BMesh const char void * data
ColorSceneLinear4f< Alpha > decode() const
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
constexpr void fill(const T &value) const
constexpr IndexRange index_range() const
constexpr void copy_from(Span< T > values) const
std::optional< T > get_if_single() const
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
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)