Blender V4.5
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(
23 const eCustomDataType blender_type, bool use_color3f_type)
24{
25 switch (blender_type) {
26 case CD_PROP_FLOAT:
27 return pxr::SdfValueTypeNames->FloatArray;
28 case CD_PROP_INT8:
29 return pxr::SdfValueTypeNames->UCharArray;
30 case CD_PROP_INT32:
31 return pxr::SdfValueTypeNames->IntArray;
32 case CD_PROP_FLOAT2:
33 return pxr::SdfValueTypeNames->Float2Array;
34 case CD_PROP_FLOAT3:
35 return pxr::SdfValueTypeNames->Float3Array;
36 case CD_PROP_STRING:
37 return pxr::SdfValueTypeNames->StringArray;
38 case CD_PROP_BOOL:
39 return pxr::SdfValueTypeNames->BoolArray;
40 case CD_PROP_COLOR:
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<eCustomDataType> convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
52{
53 static const Map<pxr::SdfValueTypeName, eCustomDataType> type_map = []() {
55 map.add_new(pxr::SdfValueTypeNames->FloatArray, CD_PROP_FLOAT);
56 map.add_new(pxr::SdfValueTypeNames->Double, CD_PROP_FLOAT);
57 map.add_new(pxr::SdfValueTypeNames->UCharArray, CD_PROP_INT8);
58 map.add_new(pxr::SdfValueTypeNames->IntArray, CD_PROP_INT32);
59 map.add_new(pxr::SdfValueTypeNames->Float2Array, CD_PROP_FLOAT2);
60 map.add_new(pxr::SdfValueTypeNames->TexCoord2dArray, CD_PROP_FLOAT2);
61 map.add_new(pxr::SdfValueTypeNames->TexCoord2fArray, CD_PROP_FLOAT2);
62 map.add_new(pxr::SdfValueTypeNames->TexCoord2hArray, CD_PROP_FLOAT2);
63 map.add_new(pxr::SdfValueTypeNames->TexCoord3dArray, CD_PROP_FLOAT2);
64 map.add_new(pxr::SdfValueTypeNames->TexCoord3fArray, CD_PROP_FLOAT2);
65 map.add_new(pxr::SdfValueTypeNames->TexCoord3hArray, CD_PROP_FLOAT2);
66 map.add_new(pxr::SdfValueTypeNames->Float3Array, CD_PROP_FLOAT3);
67 map.add_new(pxr::SdfValueTypeNames->Point3fArray, CD_PROP_FLOAT3);
68 map.add_new(pxr::SdfValueTypeNames->Point3dArray, CD_PROP_FLOAT3);
69 map.add_new(pxr::SdfValueTypeNames->Point3hArray, CD_PROP_FLOAT3);
70 map.add_new(pxr::SdfValueTypeNames->Normal3fArray, CD_PROP_FLOAT3);
71 map.add_new(pxr::SdfValueTypeNames->Normal3dArray, CD_PROP_FLOAT3);
72 map.add_new(pxr::SdfValueTypeNames->Normal3hArray, CD_PROP_FLOAT3);
73 map.add_new(pxr::SdfValueTypeNames->Vector3fArray, CD_PROP_FLOAT3);
74 map.add_new(pxr::SdfValueTypeNames->Vector3hArray, CD_PROP_FLOAT3);
75 map.add_new(pxr::SdfValueTypeNames->Vector3dArray, CD_PROP_FLOAT3);
76 map.add_new(pxr::SdfValueTypeNames->Color3fArray, CD_PROP_COLOR);
77 map.add_new(pxr::SdfValueTypeNames->Color3hArray, CD_PROP_COLOR);
78 map.add_new(pxr::SdfValueTypeNames->Color3dArray, CD_PROP_COLOR);
79 map.add_new(pxr::SdfValueTypeNames->Color4fArray, CD_PROP_COLOR);
80 map.add_new(pxr::SdfValueTypeNames->Color4hArray, CD_PROP_COLOR);
81 map.add_new(pxr::SdfValueTypeNames->Color4dArray, CD_PROP_COLOR);
82 map.add_new(pxr::SdfValueTypeNames->BoolArray, CD_PROP_BOOL);
83 map.add_new(pxr::SdfValueTypeNames->QuatfArray, CD_PROP_QUATERNION);
84 map.add_new(pxr::SdfValueTypeNames->QuatdArray, CD_PROP_QUATERNION);
85 map.add_new(pxr::SdfValueTypeNames->QuathArray, CD_PROP_QUATERNION);
86 return map;
87 }();
88
89 const eCustomDataType *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 timecode,
99 const eCustomDataType 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) {
110 case CD_PROP_FLOAT:
112 primvar, timecode, face_indices, attribute.span.typed<float>());
113 break;
114 case CD_PROP_INT8:
116 primvar, timecode, face_indices, attribute.span.typed<int8_t>());
117 break;
118 case CD_PROP_INT32:
120 primvar, timecode, face_indices, attribute.span.typed<int>());
121 break;
122 case CD_PROP_FLOAT2:
124 primvar, timecode, face_indices, attribute.span.typed<float2>());
125 break;
126 case CD_PROP_FLOAT3:
128 primvar, timecode, face_indices, attribute.span.typed<float3>());
129 break;
130 case CD_PROP_COLOR: {
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, timecode, face_indices, attribute.span.typed<ColorGeometry4f>());
139 }
140 else {
142 primvar, timecode, face_indices, attribute.span.typed<ColorGeometry4f>());
143 }
144 } break;
145 case CD_PROP_BOOL:
147 primvar, timecode, face_indices, attribute.span.typed<bool>());
148 break;
151 primvar, timecode, face_indices, attribute.span.typed<math::Quaternion>());
152 break;
153
154 default:
156 }
157
158 attribute.finish();
159}
160
162 const eCustomDataType data_type,
163 const pxr::UsdTimeCode timecode,
164 const pxr::UsdGeomPrimvar &primvar,
165 pxr::UsdUtilsSparseValueWriter &value_writer)
166{
167 switch (data_type) {
168 case CD_PROP_FLOAT:
170 attribute.typed<float>(), timecode, primvar, value_writer);
171 break;
172 case CD_PROP_INT8:
174 attribute.typed<int8_t>(), timecode, primvar, value_writer);
175 break;
176 case CD_PROP_INT32:
178 attribute.typed<int>(), timecode, primvar, value_writer);
179 break;
180 case CD_PROP_FLOAT2:
182 attribute.typed<float2>(), timecode, primvar, value_writer);
183 break;
184 case CD_PROP_FLOAT3:
186 attribute.typed<float3>(), timecode, primvar, value_writer);
187 break;
188 case CD_PROP_BOOL:
190 attribute.typed<bool>(), timecode, primvar, value_writer);
191 break;
192 case CD_PROP_COLOR:
193 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
195 attribute.typed<ColorGeometry4f>(), timecode, primvar, value_writer);
196 }
197 else {
199 attribute.typed<ColorGeometry4f>(), timecode, primvar, value_writer);
200 }
201 break;
203 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
205 attribute.typed<ColorGeometry4b>(), timecode, primvar, value_writer);
206 }
207 else {
209 attribute.typed<ColorGeometry4b>(), timecode, primvar, value_writer);
210 }
211 break;
214 attribute.typed<math::Quaternion>(), timecode, 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(...)
@ CD_PROP_BYTE_COLOR
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ CD_PROP_COLOR
@ CD_PROP_QUATERNION
@ CD_PROP_INT32
@ CD_PROP_FLOAT2
@ CD_PROP_STRING
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, eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
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