Blender V5.0
usd_attribute_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "usd_hash_types.hh"
7
8#include "BLI_map.hh"
10#include "BLI_sys_types.h"
11
12#include "BKE_attribute.hh"
13
15
16#include <pxr/usd/sdf/valueTypeName.h>
17
18#include <optional>
19
20namespace blender::io::usd {
21
22std::optional<pxr::SdfValueTypeName> convert_blender_type_to_usd(const bke::AttrType blender_type,
23 bool use_color3f_type)
24{
25 switch (blender_type) {
27 return pxr::SdfValueTypeNames->FloatArray;
29 return pxr::SdfValueTypeNames->UCharArray;
31 return pxr::SdfValueTypeNames->IntArray;
33 return pxr::SdfValueTypeNames->Float2Array;
35 return pxr::SdfValueTypeNames->Float3Array;
37 return pxr::SdfValueTypeNames->StringArray;
39 return pxr::SdfValueTypeNames->BoolArray;
42 return use_color3f_type ? pxr::SdfValueTypeNames->Color3fArray :
43 pxr::SdfValueTypeNames->Color4fArray;
45 return pxr::SdfValueTypeNames->QuatfArray;
46 default:
47 return std::nullopt;
48 }
49}
50
51std::optional<bke::AttrType> convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
52{
53 static const Map<pxr::SdfValueTypeName, bke::AttrType> type_map = []() {
55 map.add_new(pxr::SdfValueTypeNames->FloatArray, bke::AttrType::Float);
56 map.add_new(pxr::SdfValueTypeNames->Double, bke::AttrType::Float);
57 map.add_new(pxr::SdfValueTypeNames->UCharArray, bke::AttrType::Int8);
58 map.add_new(pxr::SdfValueTypeNames->IntArray, bke::AttrType::Int32);
59 map.add_new(pxr::SdfValueTypeNames->Float2Array, bke::AttrType::Float2);
60 map.add_new(pxr::SdfValueTypeNames->TexCoord2dArray, bke::AttrType::Float2);
61 map.add_new(pxr::SdfValueTypeNames->TexCoord2fArray, bke::AttrType::Float2);
62 map.add_new(pxr::SdfValueTypeNames->TexCoord2hArray, bke::AttrType::Float2);
63 map.add_new(pxr::SdfValueTypeNames->TexCoord3dArray, bke::AttrType::Float2);
64 map.add_new(pxr::SdfValueTypeNames->TexCoord3fArray, bke::AttrType::Float2);
65 map.add_new(pxr::SdfValueTypeNames->TexCoord3hArray, bke::AttrType::Float2);
66 map.add_new(pxr::SdfValueTypeNames->Float3Array, bke::AttrType::Float3);
67 map.add_new(pxr::SdfValueTypeNames->Point3fArray, bke::AttrType::Float3);
68 map.add_new(pxr::SdfValueTypeNames->Point3dArray, bke::AttrType::Float3);
69 map.add_new(pxr::SdfValueTypeNames->Point3hArray, bke::AttrType::Float3);
70 map.add_new(pxr::SdfValueTypeNames->Normal3fArray, bke::AttrType::Float3);
71 map.add_new(pxr::SdfValueTypeNames->Normal3dArray, bke::AttrType::Float3);
72 map.add_new(pxr::SdfValueTypeNames->Normal3hArray, bke::AttrType::Float3);
73 map.add_new(pxr::SdfValueTypeNames->Vector3fArray, bke::AttrType::Float3);
74 map.add_new(pxr::SdfValueTypeNames->Vector3hArray, bke::AttrType::Float3);
75 map.add_new(pxr::SdfValueTypeNames->Vector3dArray, bke::AttrType::Float3);
76 map.add_new(pxr::SdfValueTypeNames->Color3fArray, bke::AttrType::ColorFloat);
77 map.add_new(pxr::SdfValueTypeNames->Color3hArray, bke::AttrType::ColorFloat);
78 map.add_new(pxr::SdfValueTypeNames->Color3dArray, bke::AttrType::ColorFloat);
79 map.add_new(pxr::SdfValueTypeNames->Color4fArray, bke::AttrType::ColorFloat);
80 map.add_new(pxr::SdfValueTypeNames->Color4hArray, bke::AttrType::ColorFloat);
81 map.add_new(pxr::SdfValueTypeNames->Color4dArray, bke::AttrType::ColorFloat);
82 map.add_new(pxr::SdfValueTypeNames->BoolArray, bke::AttrType::Bool);
83 map.add_new(pxr::SdfValueTypeNames->QuatfArray, bke::AttrType::Quaternion);
84 map.add_new(pxr::SdfValueTypeNames->QuatdArray, bke::AttrType::Quaternion);
85 map.add_new(pxr::SdfValueTypeNames->QuathArray, bke::AttrType::Quaternion);
86 return map;
87 }();
88
89 const bke::AttrType *value = type_map.lookup_ptr(usd_type);
90 if (value == nullptr) {
91 return std::nullopt;
92 }
93
94 return *value;
95}
96
97void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar,
98 const pxr::UsdTimeCode time,
99 const bke::AttrType data_type,
100 const bke::AttrDomain domain,
101 const OffsetIndices<int> face_indices,
103{
104 const pxr::TfToken pv_name = pxr::UsdGeomPrimvar::StripPrimvarsName(primvar.GetPrimvarName());
105
107 pv_name.GetText(), domain, data_type);
108
109 switch (data_type) {
112 primvar, time, face_indices, attribute.span.typed<float>());
113 break;
116 primvar, time, face_indices, attribute.span.typed<int8_t>());
117 break;
120 primvar, time, face_indices, attribute.span.typed<int>());
121 break;
124 primvar, time, face_indices, attribute.span.typed<float2>());
125 break;
128 primvar, time, face_indices, attribute.span.typed<float3>());
129 break;
131 const pxr::SdfValueTypeName pv_type = primvar.GetTypeName();
132 if (ELEM(pv_type,
133 pxr::SdfValueTypeNames->Color3fArray,
134 pxr::SdfValueTypeNames->Color3hArray,
135 pxr::SdfValueTypeNames->Color3dArray))
136 {
138 primvar, time, face_indices, attribute.span.typed<ColorGeometry4f>());
139 }
140 else {
142 primvar, time, face_indices, attribute.span.typed<ColorGeometry4f>());
143 }
144 } break;
147 primvar, time, face_indices, attribute.span.typed<bool>());
148 break;
151 primvar, time, face_indices, attribute.span.typed<math::Quaternion>());
152 break;
153
154 default:
156 }
157
158 attribute.finish();
159}
160
162 const bke::AttrType data_type,
163 const pxr::UsdTimeCode time,
164 const pxr::UsdGeomPrimvar &primvar,
165 pxr::UsdUtilsSparseValueWriter &value_writer)
166{
167 switch (data_type) {
170 attribute.typed<float>(), time, primvar, value_writer);
171 break;
174 attribute.typed<int8_t>(), time, primvar, value_writer);
175 break;
178 attribute.typed<int>(), time, primvar, value_writer);
179 break;
182 attribute.typed<float2>(), time, primvar, value_writer);
183 break;
186 attribute.typed<float3>(), time, primvar, value_writer);
187 break;
190 attribute.typed<bool>(), time, primvar, value_writer);
191 break;
193 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
195 attribute.typed<ColorGeometry4f>(), time, primvar, value_writer);
196 }
197 else {
199 attribute.typed<ColorGeometry4f>(), time, primvar, value_writer);
200 }
201 break;
203 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
205 attribute.typed<ColorGeometry4b>(), time, primvar, value_writer);
206 }
207 else {
209 attribute.typed<ColorGeometry4b>(), time, primvar, value_writer);
210 }
211 break;
214 attribute.typed<math::Quaternion>(), time, primvar, value_writer);
215 break;
216 default:
218 }
219}
220
221} // namespace blender::io::usd
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define ELEM(...)
MutableSpan< T > typed() const
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef attribute_id, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
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)
std::optional< pxr::SdfValueTypeName > convert_blender_type_to_usd(const bke::AttrType blender_type, bool use_color3f_type)
void copy_blender_buffer_to_primvar(const VArray< BlenderT > &buffer, const pxr::UsdTimeCode time, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
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)
void copy_primvar_to_blender_buffer(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time, const OffsetIndices< int > faces, MutableSpan< BlenderT > attribute)
std::optional< bke::AttrType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
QuaternionBase< float > Quaternion
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b