Blender V5.3
abc_custom_props.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_custom_props.h"
10
11#include "abc_writer_abstract.h"
13
14#include <string>
15
16#include <Alembic/Abc/OTypedArrayProperty.h>
17
18#include "BLI_listbase.hh"
19
20#include "BKE_idprop.hh"
21#include "DNA_ID.h"
22
23namespace blender {
24
25using Alembic::Abc::ArraySample;
26using Alembic::Abc::OArrayProperty;
27using Alembic::Abc::OBoolArrayProperty;
28using Alembic::Abc::OCompoundProperty;
29using Alembic::Abc::ODoubleArrayProperty;
30using Alembic::Abc::OFloatArrayProperty;
31using Alembic::Abc::OInt32ArrayProperty;
32using Alembic::Abc::OStringArrayProperty;
33
34namespace io::alembic {
35
37
39{
40 if (group == nullptr) {
41 return;
42 }
43 BLI_assert(group->type == IDP_GROUP);
44
45 /* Loop over the properties, just like IDP_foreach_property() does, but without the recursion. */
46 for (IDProperty &id_property : group->data.group) {
47 write(&id_property);
48 }
49}
50
51void CustomPropertiesExporter::write(const IDProperty *id_property)
52{
53 BLI_assert(id_property->name[0] != '\0');
54
55 switch (id_property->type) {
56 case IDP_STRING: {
57 /* The Alembic library doesn't accept null-terminated character arrays. */
58 const std::string prop_value(IDP_string_get(id_property), id_property->len - 1);
59 set_scalar_property<OStringArrayProperty, std::string>(id_property->name, prop_value);
60 break;
61 }
62 case IDP_INT:
63 static_assert(sizeof(int) == sizeof(int32_t), "Expecting 'int' to be 32-bit");
64 set_scalar_property<OInt32ArrayProperty, int32_t>(id_property->name,
65 IDP_int_get(id_property));
66 break;
67 case IDP_FLOAT:
68 set_scalar_property<OFloatArrayProperty, float>(id_property->name,
69 IDP_float_get(id_property));
70 break;
71 case IDP_DOUBLE:
72 set_scalar_property<ODoubleArrayProperty, double>(id_property->name,
73 IDP_double_get(id_property));
74 break;
75 case IDP_BOOLEAN:
76 set_scalar_property<OBoolArrayProperty, bool>(id_property->name, IDP_bool_get(id_property));
77 break;
78 case IDP_ARRAY:
79 write_array(id_property);
80 break;
81 case IDP_IDPARRAY:
82 write_idparray(id_property);
83 break;
84 case IDP_GROUP:
85 case IDP_ID:
86 /* Not supported. */
87 break;
88 }
89}
90
91void CustomPropertiesExporter::write_array(const IDProperty *id_property)
92{
93 BLI_assert(id_property->type == IDP_ARRAY);
94
95 switch (id_property->subtype) {
96 case IDP_INT: {
97 const int *array = IDP_array_int_get(id_property);
98 static_assert(sizeof(int) == sizeof(int32_t), "Expecting 'int' to be 32-bit");
99 set_array_property<OInt32ArrayProperty, int32_t>(id_property->name, array, id_property->len);
100 break;
101 }
102 case IDP_FLOAT: {
103 const float *array = IDP_array_float_get(id_property);
104 set_array_property<OFloatArrayProperty, float>(id_property->name, array, id_property->len);
105 break;
106 }
107 case IDP_DOUBLE: {
108 const double *array = IDP_array_double_get(id_property);
109 set_array_property<ODoubleArrayProperty, double>(id_property->name, array, id_property->len);
110 break;
111 }
112 case IDP_BOOLEAN: {
113 const int8_t *array = IDP_array_bool_get(id_property);
114 set_array_property<OBoolArrayProperty, int8_t>(id_property->name, array, id_property->len);
115 break;
116 }
117 }
118}
119
120void CustomPropertiesExporter::write_idparray(const IDProperty *idp_array)
121{
122 BLI_assert(idp_array->type == IDP_IDPARRAY);
123
124 if (idp_array->len == 0) {
125 /* Don't bother writing dataless arrays. */
126 return;
127 }
128
129 IDProperty *idp_elements = IDP_property_array_get(idp_array);
130
131#ifndef NDEBUG
132 /* Sanity check that all elements of the array have the same type.
133 * Blender should already enforce this, hence it's only used in debug mode. */
134 for (int i = 1; i < idp_array->len; i++) {
135 if (idp_elements[i].type == idp_elements[0].type) {
136 continue;
137 }
138 std::cerr << "Custom property " << idp_array->name << " has elements of varying type";
139 BLI_assert_msg(0, "Mixed type IDP_ARRAY custom property found");
140 }
141#endif
142
143 switch (idp_elements[0].type) {
144 case IDP_STRING:
145 write_idparray_of_strings(idp_array);
146 break;
147 case IDP_ARRAY:
148 write_idparray_of_numbers(idp_array);
149 break;
150 default:
151 /* Other types not supported. */
152 break;
153 }
154}
155
156void CustomPropertiesExporter::write_idparray_of_strings(const IDProperty *idp_array)
157{
158 BLI_assert(idp_array->type == IDP_IDPARRAY);
159 BLI_assert(idp_array->len > 0);
160
161 /* Convert to an array of std::strings, because Alembic doesn't like zero-delimited strings. */
162 IDProperty *idp_elements = IDP_property_array_get(idp_array);
163 std::vector<std::string> strings(idp_array->len);
164 for (int i = 0; i < idp_array->len; i++) {
165 BLI_assert(idp_elements[i].type == IDP_STRING);
166 strings[i] = IDP_string_get(&idp_elements[i]);
167 }
168
169 /* Alembic needs a pointer to the first value of the array. */
170 const std::string *array_of_strings = strings.data();
171 set_array_property<OStringArrayProperty, std::string>(
172 idp_array->name, array_of_strings, strings.size());
173}
174
175void CustomPropertiesExporter::write_idparray_of_numbers(const IDProperty *idp_array)
176{
177 BLI_assert(idp_array->type == IDP_IDPARRAY);
178 BLI_assert(idp_array->len > 0);
179
180 /* This must be an array of arrays. */
181 IDProperty *idp_rows = IDP_property_array_get(idp_array);
182 BLI_assert(idp_rows[0].type == IDP_ARRAY);
183
184 const int subtype = idp_rows[0].subtype;
185 if (!ELEM(subtype, IDP_INT, IDP_FLOAT, IDP_DOUBLE, IDP_BOOLEAN)) {
186 /* Non-numerical types are not supported. */
187 return;
188 }
189
190 switch (subtype) {
191 case IDP_INT:
192 static_assert(sizeof(int) == sizeof(int32_t), "Expecting 'int' to be 32-bit");
193 write_idparray_flattened_typed<OInt32ArrayProperty, int32_t>(idp_array);
194 break;
195 case IDP_FLOAT:
196 write_idparray_flattened_typed<OFloatArrayProperty, float>(idp_array);
197 break;
198 case IDP_DOUBLE:
199 write_idparray_flattened_typed<ODoubleArrayProperty, double>(idp_array);
200 break;
201 case IDP_BOOLEAN:
202 write_idparray_flattened_typed<OBoolArrayProperty, int8_t>(idp_array);
203 break;
204 }
205}
206
207template<typename ABCPropertyType, typename BlenderValueType>
208void CustomPropertiesExporter::write_idparray_flattened_typed(const IDProperty *idp_array)
209{
210 BLI_assert(idp_array->type == IDP_IDPARRAY);
211 BLI_assert(idp_array->len > 0);
212
213 const IDProperty *idp_rows = IDP_property_array_get(idp_array);
214 BLI_assert(idp_rows[0].type == IDP_ARRAY);
215 BLI_assert(ELEM(idp_rows[0].subtype, IDP_INT, IDP_FLOAT, IDP_DOUBLE, IDP_BOOLEAN));
216
217 const uint64_t num_rows = idp_array->len;
218 std::vector<BlenderValueType> matrix_values;
219 for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {
220 const BlenderValueType *row = static_cast<BlenderValueType *> IDP_array_voidp_get(
221 &idp_rows[row_idx]);
222 for (size_t col_idx = 0; col_idx < idp_rows[row_idx].len; col_idx++) {
223 matrix_values.push_back(row[col_idx]);
224 }
225 }
226
227 set_array_property<ABCPropertyType, BlenderValueType>(
228 idp_array->name, matrix_values.data(), matrix_values.size());
229}
230
231template<typename ABCPropertyType, typename BlenderValueType>
232void CustomPropertiesExporter::set_scalar_property(const StringRef property_name,
233 const BlenderValueType property_value)
234{
235 set_array_property<ABCPropertyType, BlenderValueType>(property_name, &property_value, 1);
236}
237
238template<typename ABCPropertyType, typename BlenderValueType>
239void CustomPropertiesExporter::set_array_property(const StringRef property_name,
240 const BlenderValueType *array_values,
241 const size_t num_array_items)
242{
243 auto create_callback = [this, property_name]() -> OArrayProperty {
244 return create_abc_property<ABCPropertyType>(property_name);
245 };
246
247 OArrayProperty array_prop = abc_properties_.lookup_or_add_cb(property_name, create_callback);
248 Alembic::Util::Dimensions array_dimensions(num_array_items);
249 ArraySample sample(array_values, array_prop.getDataType(), array_dimensions);
250 array_prop.set(sample);
251}
252
253template<typename ABCPropertyType>
254OArrayProperty CustomPropertiesExporter::create_abc_property(const StringRef property_name)
255{
256 /* Get the necessary info from our owner. */
257 OCompoundProperty abc_prop_for_custom_props = owner_->abc_prop_for_custom_props();
258 const uint32_t timesample_index = owner_->timesample_index();
259
260 /* Construct the Alembic property. */
261 ABCPropertyType abc_property(abc_prop_for_custom_props, property_name);
262 abc_property.setTimeSampling(timesample_index);
263 return abc_property;
264}
265
266void get_positions(const Span<float3> positions, std::vector<Imath::V3f> &points)
267{
268 points.clear();
269 points.resize(positions.size());
270
271 for (const int i : positions.index_range()) {
272 copy_yup_from_zup(points[i].getValue(), positions[i]);
273 }
274}
275
276bool get_velocities(const bke::AttributeAccessor &attributes, std::vector<Imath::V3f> &velocities)
277{
278 /* Export velocity attribute output by fluid sim, sequence cache modifier
279 * and geometry nodes. */
280 const VArraySpan attr = *attributes.lookup<float3>("velocity", bke::AttrDomain::Point);
281 if (attr.is_empty()) {
282 return false;
283 }
284
285 velocities.clear();
286 velocities.resize(attr.size());
287
288 for (const int i : attr.index_range()) {
289 copy_yup_from_zup(velocities[i].getValue(), attr[i]);
290 }
291
292 return true;
293}
294
295} // namespace io::alembic
296} // namespace blender
#define IDP_float_get(prop)
#define IDP_array_voidp_get(prop)
#define IDP_int_get(prop)
#define IDP_array_double_get(prop)
#define IDP_string_get(prop)
#define IDP_double_get(prop)
#define IDP_array_bool_get(prop)
#define IDP_array_int_get(prop)
#define IDP_property_array_get(prop)
#define IDP_array_float_get(prop)
#define IDP_bool_get(prop)
ID and Library types, which are fundamental for SDNA.
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
constexpr IndexRange index_range() const
constexpr int64_t size() const
BLI_INLINE void copy_yup_from_zup(float yup[3], const float zup[3])
bool get_velocities(const bke::AttributeAccessor &attributes, std::vector< Imath::V3f > &velocities)
void get_positions(const Span< float3 > positions, std::vector< Imath::V3f > &points)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint32_t
Definition BKE_text.h:81
signed int int32_t
Definition BKE_text.h:78
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyType type
Definition DNA_ID.h:145
IDPropertyData data
Definition DNA_ID.h:157