Blender V4.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
6#include "usd_hash_types.hh"
7
8#include "BLI_generic_span.hh"
9#include "BLI_map.hh"
10#include "BLI_offset_indices.hh"
11#include "BLI_sys_types.h"
12
13#include "BKE_attribute.hh"
14
16
17#include <pxr/usd/sdf/valueTypeName.h>
18
19#include <optional>
20
21namespace blender::io::usd {
22
23std::optional<pxr::SdfValueTypeName> convert_blender_type_to_usd(
24 const eCustomDataType blender_type, bool use_color3f_type)
25{
26 switch (blender_type) {
27 case CD_PROP_FLOAT:
28 return pxr::SdfValueTypeNames->FloatArray;
29 case CD_PROP_INT8:
30 return pxr::SdfValueTypeNames->UCharArray;
31 case CD_PROP_INT32:
32 return pxr::SdfValueTypeNames->IntArray;
33 case CD_PROP_FLOAT2:
34 return pxr::SdfValueTypeNames->Float2Array;
35 case CD_PROP_FLOAT3:
36 return pxr::SdfValueTypeNames->Float3Array;
37 case CD_PROP_STRING:
38 return pxr::SdfValueTypeNames->StringArray;
39 case CD_PROP_BOOL:
40 return pxr::SdfValueTypeNames->BoolArray;
41 case CD_PROP_COLOR:
43 return use_color3f_type ? pxr::SdfValueTypeNames->Color3fArray :
44 pxr::SdfValueTypeNames->Color4fArray;
46 return pxr::SdfValueTypeNames->QuatfArray;
47 default:
48 return std::nullopt;
49 }
50}
51
52std::optional<eCustomDataType> convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
53{
54 static const Map<pxr::SdfValueTypeName, eCustomDataType> type_map = []() {
56 map.add_new(pxr::SdfValueTypeNames->FloatArray, CD_PROP_FLOAT);
57 map.add_new(pxr::SdfValueTypeNames->Double, CD_PROP_FLOAT);
58 map.add_new(pxr::SdfValueTypeNames->UCharArray, CD_PROP_INT8);
59 map.add_new(pxr::SdfValueTypeNames->IntArray, CD_PROP_INT32);
60 map.add_new(pxr::SdfValueTypeNames->Float2Array, CD_PROP_FLOAT2);
61 map.add_new(pxr::SdfValueTypeNames->TexCoord2dArray, CD_PROP_FLOAT2);
62 map.add_new(pxr::SdfValueTypeNames->TexCoord2fArray, CD_PROP_FLOAT2);
63 map.add_new(pxr::SdfValueTypeNames->TexCoord2hArray, CD_PROP_FLOAT2);
64 map.add_new(pxr::SdfValueTypeNames->TexCoord3dArray, CD_PROP_FLOAT2);
65 map.add_new(pxr::SdfValueTypeNames->TexCoord3fArray, CD_PROP_FLOAT2);
66 map.add_new(pxr::SdfValueTypeNames->TexCoord3hArray, CD_PROP_FLOAT2);
67 map.add_new(pxr::SdfValueTypeNames->Float3Array, CD_PROP_FLOAT3);
68 map.add_new(pxr::SdfValueTypeNames->Point3fArray, CD_PROP_FLOAT3);
69 map.add_new(pxr::SdfValueTypeNames->Point3dArray, CD_PROP_FLOAT3);
70 map.add_new(pxr::SdfValueTypeNames->Point3hArray, CD_PROP_FLOAT3);
71 map.add_new(pxr::SdfValueTypeNames->Normal3fArray, CD_PROP_FLOAT3);
72 map.add_new(pxr::SdfValueTypeNames->Normal3dArray, CD_PROP_FLOAT3);
73 map.add_new(pxr::SdfValueTypeNames->Normal3hArray, CD_PROP_FLOAT3);
74 map.add_new(pxr::SdfValueTypeNames->Vector3fArray, CD_PROP_FLOAT3);
75 map.add_new(pxr::SdfValueTypeNames->Vector3hArray, CD_PROP_FLOAT3);
76 map.add_new(pxr::SdfValueTypeNames->Vector3dArray, CD_PROP_FLOAT3);
77 map.add_new(pxr::SdfValueTypeNames->Color3fArray, CD_PROP_COLOR);
78 map.add_new(pxr::SdfValueTypeNames->Color3hArray, CD_PROP_COLOR);
79 map.add_new(pxr::SdfValueTypeNames->Color3dArray, CD_PROP_COLOR);
80 map.add_new(pxr::SdfValueTypeNames->Color4fArray, CD_PROP_COLOR);
81 map.add_new(pxr::SdfValueTypeNames->Color4hArray, CD_PROP_COLOR);
82 map.add_new(pxr::SdfValueTypeNames->Color4dArray, CD_PROP_COLOR);
83 map.add_new(pxr::SdfValueTypeNames->StringArray, CD_PROP_STRING);
84 map.add_new(pxr::SdfValueTypeNames->BoolArray, CD_PROP_BOOL);
85 map.add_new(pxr::SdfValueTypeNames->QuatfArray, CD_PROP_QUATERNION);
86 map.add_new(pxr::SdfValueTypeNames->QuatdArray, CD_PROP_QUATERNION);
87 map.add_new(pxr::SdfValueTypeNames->QuathArray, CD_PROP_QUATERNION);
88 return map;
89 }();
90
91 const eCustomDataType *value = type_map.lookup_ptr(usd_type);
92 if (value == nullptr) {
93 return std::nullopt;
94 }
95
96 return *value;
97}
98
99void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar,
100 const pxr::UsdTimeCode timecode,
101 const eCustomDataType data_type,
102 const bke::AttrDomain domain,
103 const OffsetIndices<int> face_indices,
105{
106 const pxr::TfToken pv_name = pxr::UsdGeomPrimvar::StripPrimvarsName(primvar.GetPrimvarName());
107
109 pv_name.GetText(), domain, data_type);
110
111 switch (data_type) {
112 case CD_PROP_FLOAT:
114 primvar, timecode, face_indices, attribute.span.typed<float>());
115 break;
116 case CD_PROP_INT8:
118 primvar, timecode, face_indices, attribute.span.typed<int8_t>());
119 break;
120 case CD_PROP_INT32:
122 primvar, timecode, face_indices, attribute.span.typed<int>());
123 break;
124 case CD_PROP_FLOAT2:
126 primvar, timecode, face_indices, attribute.span.typed<float2>());
127 break;
128 case CD_PROP_FLOAT3:
130 primvar, timecode, face_indices, attribute.span.typed<float3>());
131 break;
132 case CD_PROP_COLOR: {
133 const pxr::SdfValueTypeName pv_type = primvar.GetTypeName();
134 if (ELEM(pv_type,
135 pxr::SdfValueTypeNames->Color3fArray,
136 pxr::SdfValueTypeNames->Color3hArray,
137 pxr::SdfValueTypeNames->Color3dArray))
138 {
140 primvar, timecode, face_indices, attribute.span.typed<ColorGeometry4f>());
141 }
142 else {
144 primvar, timecode, face_indices, attribute.span.typed<ColorGeometry4f>());
145 }
146 } break;
147 case CD_PROP_BOOL:
149 primvar, timecode, face_indices, attribute.span.typed<bool>());
150 break;
153 primvar, timecode, face_indices, attribute.span.typed<math::Quaternion>());
154 break;
155
156 default:
158 }
159
160 attribute.finish();
161}
162
164 const eCustomDataType data_type,
165 const pxr::UsdTimeCode timecode,
166 const pxr::UsdGeomPrimvar &primvar,
167 pxr::UsdUtilsSparseValueWriter &value_writer)
168{
169 switch (data_type) {
170 case CD_PROP_FLOAT:
172 attribute.typed<float>(), timecode, primvar, value_writer);
173 break;
174 case CD_PROP_INT8:
176 attribute.typed<int8_t>(), timecode, primvar, value_writer);
177 break;
178 case CD_PROP_INT32:
180 attribute.typed<int>(), timecode, primvar, value_writer);
181 break;
182 case CD_PROP_FLOAT2:
184 attribute.typed<float2>(), timecode, primvar, value_writer);
185 break;
186 case CD_PROP_FLOAT3:
188 attribute.typed<float3>(), timecode, primvar, value_writer);
189 break;
190 case CD_PROP_BOOL:
192 attribute.typed<bool>(), timecode, primvar, value_writer);
193 break;
194 case CD_PROP_COLOR:
195 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
197 attribute.typed<ColorGeometry4f>(), timecode, primvar, value_writer);
198 }
199 else {
201 attribute.typed<ColorGeometry4f>(), timecode, primvar, value_writer);
202 }
203 break;
205 if (primvar.GetTypeName() == pxr::SdfValueTypeNames->Color3fArray) {
207 attribute.typed<ColorGeometry4b>(), timecode, primvar, value_writer);
208 }
209 else {
211 attribute.typed<ColorGeometry4b>(), timecode, primvar, value_writer);
212 }
213 break;
216 attribute.typed<math::Quaternion>(), timecode, primvar, value_writer);
217 break;
218 default:
220 }
221}
222
223} // namespace blender::io::usd
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#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
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color attribute
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:484
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:241
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:337
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
Definition BLI_color.hh:338
signed char int8_t
Definition stdint.h:75