Blender V5.3
abc_writer_attribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "DNA_meshdata_types.h" // For MStringProperty
8
9#include "BLI_color.hh"
12
13#include "BKE_attribute.hh"
14#include "BKE_attribute_math.hh"
15
17#include "intern/abc_util.h"
18
19#include "CLG_log.h"
20
21using namespace Alembic::AbcGeom;
22
23static CLG_LogRef LOG = {"io.alembic"};
24
25namespace blender::io::alembic {
26
27template<typename BlenderDataType> struct TypeTraitConverter;
28
29template<> struct TypeTraitConverter<bool> {
30 using AbcTraitType = Alembic::Abc::BooleanTPTraits;
31
32 static Alembic::Abc::bool_t convert(bool v)
33 {
34 return v;
35 }
36};
37
38template<> struct TypeTraitConverter<int8_t> {
39 using AbcTraitType = Alembic::Abc::Int8TPTraits;
40
42 {
43 return static_cast<int8_t>(v);
44 }
45};
46
47template<> struct TypeTraitConverter<short2> {
48 using AbcTraitType = Alembic::Abc::V2sTPTraits;
49
50 static Imath::V2s convert(short2 v)
51 {
52 return {v.x, v.y};
53 }
54};
55
56template<> struct TypeTraitConverter<int> {
57 using AbcTraitType = Alembic::Abc::Int32TPTraits;
58
59 static int convert(int v)
60 {
61 return v;
62 }
63};
64
65template<> struct TypeTraitConverter<int2> {
66 using AbcTraitType = Alembic::Abc::V2iTPTraits;
67
68 static Imath::V2i convert(int2 v)
69 {
70 return {v.x, v.y};
71 }
72};
73
74template<> struct TypeTraitConverter<float> {
75 using AbcTraitType = Alembic::Abc::Float32TPTraits;
76
77 static float convert(float v)
78 {
79 return v;
80 }
81};
82
83template<> struct TypeTraitConverter<float2> {
84 using AbcTraitType = Alembic::Abc::V2fTPTraits;
85
86 static Imath::V2f convert(float2 v)
87 {
88 return {v.x, v.y};
89 }
90};
91
92template<> struct TypeTraitConverter<float3> {
93 using AbcTraitType = Alembic::Abc::V3fTPTraits;
94
95 static Imath::V3f convert(float3 v)
96 {
98 return {v.x, v.y, v.z};
99 }
100};
101
102template<> struct TypeTraitConverter<float4x4> {
103 /* Although matrices are stored as floats in Blender, we convert to double as this is what most
104 * other software use. */
105 using AbcTraitType = Alembic::Abc::M44dTPTraits;
106
107 static Imath::M44d convert(float4x4 v)
108 {
109 copy_m44_axis_swap(v.ptr(), v.ptr(), ABC_YUP_FROM_ZUP);
110 return convert_matrix_datatype(v.ptr());
111 }
112};
113
115 using AbcTraitType = Alembic::Abc::C4fTPTraits;
116
117 static Imath::C4f convert(ColorGeometry4f v)
118 {
119 return {v.r, v.g, v.b, v.a};
120 }
121};
122
124 /* TODO(kevindietrich) : the importer only cares about float colors. */
125 using AbcTraitType = Alembic::Abc::C4fTPTraits;
126
127 static Imath::C4f convert(ColorGeometry4b v)
128 {
129 static constexpr float float_scale = 1.0f / 255.0f;
130 return {float(v.r) * float_scale,
131 float(v.g) * float_scale,
132 float(v.b) * float_scale,
133 float(v.a) * float_scale};
134 }
135};
136
137template<> struct TypeTraitConverter<math::Quaternion> {
138 using AbcTraitType = Alembic::Abc::QuatfTPTraits;
139
140 static Imath::Quatf convert(math::Quaternion quat)
141 {
142 float3 v = quat.imaginary_part();
144 return {quat.w, v.x, v.y, v.z};
145 }
146};
147
149 using AbcTraitType = Alembic::Abc::StringTPTraits;
150
151 static std::string convert(const MStringProperty &v)
152 {
153 return std::string(v.s, size_t(v.s_len));
154 }
155};
156
158{
159 switch (domain) {
163 /* Not supported */
164 break;
165 }
167 return kVertexScope;
168 }
171 return kUniformScope;
172 }
174 return kFacevaryingScope;
175 }
176 }
177
178 return kUnknownScope;
179}
180
181template<typename GeomParamType>
182static void write_empty_samples(GeomParamType &param, const size_t num_geom_samples)
183{
184 /* If we were to write the empty sample as a default constructed TypedArray, Alembic would
185 * duplicate the last known sample. This could pose problem if, for example, the number of points
186 * changes but the attribute is missing on the same frame (it would get a number of points which
187 * might be bigger or smaller). Therefore, we need to write an array of size 0, but which still
188 * has a valid (non-null) address, as a zero-size array should be understood by everyone as "the
189 * attribute is missing". */
190 for (size_t i = param.getNumSamples(); i < num_geom_samples; i++) {
191 typename GeomParamType::Sample sample;
192 static typename GeomParamType::value_type address_for_array{};
193 sample.setVals({&address_for_array, 0ul});
194 param.set(sample);
195 }
196}
197
198void AttributeParamMaps::write_empty_samples(const size_t num_geom_samples)
199{
200#define WRITE_EMPTY_SAMPLE(abc_type, map_name) \
201 for (auto &iter : this->map_name) { \
202 io::alembic::write_empty_samples(iter.second, num_geom_samples); \
203 }
205#undef WRITE_EMPTY_SAMPLE
206}
207
208template<typename BlenderDataType>
209static void create_geom_param_for_attribute(const Alembic::Abc::OCompoundProperty &prop,
210 AttributeParamMaps &param_maps,
211 const VArray<BlenderDataType> &buffer,
212 const StringRefNull name,
213 const StringRefNull valid_name,
214 const GeometryScope scope,
215 const int timesample_index,
216 const OffsetIndices<int> faces,
217 const StringRefNull object_name,
218 const size_t num_geom_samples)
219{
220 using TypeConverter = TypeTraitConverter<BlenderDataType>;
221 using AlembicTraitType = typename TypeConverter::AbcTraitType;
222 using AlembicType = typename AlembicTraitType::value_type;
223
224 std::vector<AlembicType> values;
225
226 GeometryScope corrected_scope = scope;
227
228 if (buffer.is_single()) {
229 corrected_scope = GeometryScope::kConstantScope;
230 values.push_back(TypeConverter::convert(buffer[0]));
231 }
232 else {
233 values.resize(static_cast<size_t>(buffer.size()));
234 AlembicType *values_ptr = values.data();
235 if (scope == kFacevaryingScope) {
236 /* NOTE: data needs to be written in the reverse order. */
237 for (const int i : faces.index_range()) {
238 const IndexRange face = faces[i];
239 for (int j = face.size() - 1; j >= 0; j--) {
240 const int blender_index = face[j];
241 *values_ptr++ = TypeConverter::convert(buffer[blender_index]);
242 }
243 }
244 }
245 else {
246 for (int i = 0; i < buffer.size(); i++) {
247 *values_ptr++ = TypeConverter::convert(buffer[i]);
248 }
249 }
250 }
251
252 using ParamType = OTypedGeomParam<AlembicTraitType>;
253 using SampleType = typename ParamType::Sample;
254
255 try {
256 const Alembic::Abc::PropertyHeader *prop_header = prop.getPropertyHeader(valid_name.c_str());
257 if (prop_header && !ParamType::matches(*prop_header)) {
258 CLOG_WARN(&LOG,
259 "On object '%s', cannot write attribute '%s' as the type has changed",
260 object_name.c_str(),
261 name.c_str());
262 return;
263 }
264
265 ParamType param;
266 param_maps.ensure_param(prop, param, valid_name, corrected_scope);
267 param.setTimeSampling(timesample_index);
268
269 write_empty_samples(param, num_geom_samples);
270
271 SampleType sample(values, corrected_scope);
272 param.set(sample);
273 }
274 catch (const Alembic::Util::Exception &ex) {
275 CLOG_WARN(&LOG,
276 "On object '%s', error writing attribute '%s' : %s",
277 object_name.c_str(),
278 name.c_str(),
279 ex.what());
280 }
281}
282
283void create_geom_param_for_attribute(const Alembic::Abc::OCompoundProperty &prop,
284 AttributeParamMaps &param_maps,
285 const bke::AttributeIter &attr,
286 const int timesample_index,
287 const OffsetIndices<int> faces,
288 const StringRefNull object_name,
289 const size_t num_geom_samples)
290{
291 const GeometryScope scope = get_scope_for_attribute_domain(attr.domain);
292 if (scope == kUnknownScope) {
293 CLOG_WARN(&LOG,
294 "On object '%s', attribute '%s' (Blender domain %d, type %d) cannot be converted "
295 "to Alembic",
296 object_name.c_str(),
297 attr.name.c_str(),
298 int8_t(attr.domain),
299 int(attr.data_type));
300 return;
301 }
302
303 /* Only exclude attributes whose name clash with reserved attributes if the latter are exported.
304 */
305 const std::string valid_name = get_valid_abc_name(attr.name.c_str());
306 if (is_reserved_attribute_name(valid_name) && prop.getPropertyHeader(valid_name) != nullptr) {
307 CLOG_WARN(&LOG,
308 "On object '%s', attribute '%s' (Blender domain %d, type %d) cannot be exported "
309 "to Alembic as it is using a reserved name",
310 object_name.c_str(),
311 attr.name.c_str(),
312 int8_t(attr.domain),
313 int(attr.data_type));
314 return;
315 }
316
317 const GVArray attribute = *attr.get();
318
319 /* #bke::attribute_math::to_static_type does not yet support strings. */
320 if (attr.data_type == bke::AttrType::String) {
322 param_maps,
323 attribute.typed<MStringProperty>(),
324 attr.name,
325 valid_name,
326 scope,
327 timesample_index,
328 faces,
329 object_name,
330 num_geom_samples);
331 return;
332 }
333
334 bke::attribute_math::to_static_type(attr.data_type, [&]<typename BlenderT>() {
335 if constexpr (!std::is_same_v<BlenderT, float4>) {
336 create_geom_param_for_attribute(prop,
337 param_maps,
338 attribute.typed<BlenderT>(),
339 attr.name,
340 valid_name,
341 scope,
342 timesample_index,
343 faces,
344 object_name,
345 num_geom_samples);
346 }
347 else {
348 /* Alembic does not have an equivalent to float4. If needed we could either flatten it to a
349 * float, export it as a float2 with an array extent of 2, or even export as quaternion or
350 * box2. */
351 CLOG_WARN(&LOG,
352 "On object '%s', attribute '%s' of type %d is not supported by Alembic",
353 object_name.c_str(),
354 attr.name.c_str(),
355 int(attr.data_type));
356 }
357 });
358}
359
360} // namespace blender::io::alembic
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define WRITE_EMPTY_SAMPLE(abc_type, map_name)
#define ENUMERATE_EXPORTED_TYPES(X)
constexpr int64_t size() const
constexpr const char * c_str() const
GAttributeReader get() const
void write_empty_samples(const size_t num_geom_samples)
#define attribute(slot)
#define LOG(level)
Definition log.h:97
void to_static_type(const CPPType &cpp_type, Fn &&fn)
static void create_geom_param_for_attribute(const Alembic::Abc::OCompoundProperty &prop, AttributeParamMaps &param_maps, const VArray< BlenderDataType > &buffer, const StringRefNull name, const StringRefNull valid_name, const GeometryScope scope, const int timesample_index, const OffsetIndices< int > faces, const StringRefNull object_name, const size_t num_geom_samples)
BLI_INLINE void copy_yup_from_zup(float yup[3], const float zup[3])
std::string get_valid_abc_name(const char *name)
Definition abc_util.cc:36
void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode)
static void write_empty_samples(GeomParamType &param, const size_t num_geom_samples)
bool is_reserved_attribute_name(const StringRefNull name)
Definition abc_util.cc:31
Imath::M44d convert_matrix_datatype(const float mat[4][4])
Definition abc_util.cc:46
static GeometryScope get_scope_for_attribute_domain(bke::AttrDomain domain)
QuaternionBase< float > Quaternion
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int16_t, 2 > short2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const VecBase< T, 3 > & imaginary_part() const