Blender V5.3
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
7
8#include "BLI_map.hh"
10#include "BLI_sys_types.hh"
11
12#include "BKE_attribute.hh"
13
14#include <pxr/usd/sdf/valueTypeName.h>
15
16#include <optional>
17
18namespace blender::io::usd {
19
20std::optional<pxr::SdfValueTypeName> convert_blender_type_to_usd(const bke::AttrType blender_type,
21 bool use_color3f_type)
22{
23 switch (blender_type) {
25 return pxr::SdfValueTypeNames->FloatArray;
27 return pxr::SdfValueTypeNames->UCharArray;
29 return pxr::SdfValueTypeNames->IntArray;
31 return pxr::SdfValueTypeNames->Float2Array;
33 return pxr::SdfValueTypeNames->Float3Array;
35 return pxr::SdfValueTypeNames->StringArray;
37 return pxr::SdfValueTypeNames->BoolArray;
40 return use_color3f_type ? pxr::SdfValueTypeNames->Color3fArray :
41 pxr::SdfValueTypeNames->Color4fArray;
43 return pxr::SdfValueTypeNames->QuatfArray;
45 return pxr::SdfValueTypeNames->Matrix4dArray;
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 map.add_new(pxr::SdfValueTypeNames->Matrix4dArray, bke::AttrType::Float4x4);
87 return map;
88 }();
89
90 const bke::AttrType *value = type_map.lookup_ptr(usd_type);
91 if (value == nullptr) {
92 return std::nullopt;
93 }
94
95 return *value;
96}
97
98void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar,
99 const pxr::UsdTimeCode time,
100 const bke::AttrType data_type,
101 const bke::AttrDomain domain,
102 const OffsetIndices<int> face_indices,
104{
105 switch (data_type) {
108 primvar, time, data_type, domain, face_indices, attributes);
109 break;
112 primvar, time, data_type, domain, face_indices, attributes);
113 break;
116 primvar, time, data_type, domain, face_indices, attributes);
117 break;
120 primvar, time, data_type, domain, face_indices, attributes);
121 break;
124 primvar, time, data_type, domain, face_indices, attributes);
125 break;
127 const pxr::SdfValueTypeName pv_type = primvar.GetTypeName();
128 if (ELEM(pv_type,
129 pxr::SdfValueTypeNames->Color3fArray,
130 pxr::SdfValueTypeNames->Color3hArray,
131 pxr::SdfValueTypeNames->Color3dArray))
132 {
134 primvar, time, data_type, domain, face_indices, attributes);
135 }
136 else {
138 primvar, time, data_type, domain, face_indices, attributes);
139 }
140 } break;
143 primvar, time, data_type, domain, face_indices, attributes);
144 break;
147 primvar, time, data_type, domain, face_indices, attributes);
148 break;
151 primvar, time, data_type, domain, face_indices, attributes);
152 break;
153
154 default:
156 }
157}
158
160 const bke::AttrType data_type,
161 const pxr::UsdTimeCode time,
162 const pxr::UsdGeomPrimvar &primvar,
163 pxr::UsdUtilsSparseValueWriter &value_writer)
164{
165 switch (data_type) {
168 attribute.typed<float>(), time, primvar, value_writer);
169 break;
172 attribute.typed<int8_t>(), time, primvar, value_writer);
173 break;
176 attribute.typed<int>(), time, primvar, value_writer);
177 break;
180 attribute.typed<float2>(), time, primvar, value_writer);
181 break;
184 attribute.typed<float3>(), time, primvar, value_writer);
185 break;
188 attribute.typed<bool>(), time, primvar, value_writer);
189 break;
191 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
193 attribute.typed<ColorGeometry4f>(), time, primvar, value_writer);
194 }
195 else {
197 attribute.typed<ColorGeometry4f>(), time, primvar, value_writer);
198 }
199 colorspace_apply_to_prim(primvar.GetAttr().GetPrim());
200 break;
202 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
204 attribute.typed<ColorGeometry4b>(), time, primvar, value_writer);
205 }
206 else {
208 attribute.typed<ColorGeometry4b>(), time, primvar, value_writer);
209 }
210 colorspace_apply_to_prim(primvar.GetAttr().GetPrim());
211 break;
214 attribute.typed<math::Quaternion>(), time, primvar, value_writer);
215 break;
218 attribute.typed<float4x4>(), time, primvar, value_writer);
219 break;
220 default:
222 }
223}
224
225} // namespace blender::io::usd
#define BLI_assert_unreachable()
#define ELEM(...)
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
#define attribute(slot)
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)
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 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 colorspace_apply_to_prim(const pxr::UsdPrim &prim)
std::optional< bke::AttrType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
QuaternionBase< float > Quaternion
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b