Blender V5.3
abc_writer_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Kévin Dietrich. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <functional>
10#include <memory>
11
12#include "abc_writer_curves.h"
14#include "intern/abc_util.h"
15
16#include "BLI_array_utils.hh"
17#include "BLI_offset_indices.hh"
18
19#include "DNA_curve_types.h"
20#include "DNA_object_types.h"
21
24#include "BKE_curve_to_mesh.hh"
25#include "BKE_curves.hh"
26#include "BKE_lib_id.hh"
27#include "BKE_mesh.hh"
28#include "BKE_object.hh"
29
30#include "CLG_log.h"
31
32namespace blender {
33
34static CLG_LogRef LOG = {"io.alembic"};
35
36using Alembic::AbcGeom::kConstantScope;
37using Alembic::AbcGeom::kVertexScope;
38using Alembic::AbcGeom::OCompoundProperty;
39using Alembic::AbcGeom::OCurves;
40using Alembic::AbcGeom::OCurvesSchema;
41using Alembic::AbcGeom::OInt16Property;
42using Alembic::AbcGeom::ON3fGeomParam;
43using Alembic::AbcGeom::OV2fGeomParam;
44
45namespace io::alembic {
46
47static inline Imath::V3f to_yup_V3f(float3 v)
48{
49 Imath::V3f p;
50 copy_yup_from_zup(p.getValue(), v);
51 return p;
52}
53
54/* Excluded attributes are those which are handled through native Alembic concepts
55 * and should not be exported as generic attributes. */
57{
58 static const Set<StringRefNull> excluded_attrs = {
59 "position",
60 "radius",
61 "resolution",
62 "id",
63 "cyclic",
64 "curve_type",
65 "normal_mode",
66 "handle_left",
67 "handle_right",
68 "handle_type_left",
69 "handle_type_right",
70 "knots_mode",
71 "nurbs_order",
72 "nurbs_weight",
73 "velocity",
74 };
75
76 return excluded_attrs.contains(name);
77}
78
80
82{
83 CLOG_DEBUG(&LOG, "exporting %s", args_.abc_path.c_str());
84 abc_curve_ = OCurves(args_.abc_parent, args_.abc_name, timesample_index_);
85 abc_curve_schema_ = abc_curve_.getSchema();
86}
87
88Alembic::Abc::OObject ABCCurveWriter::get_alembic_object() const
89{
90 return abc_curve_;
91}
92
93Alembic::Abc::OCompoundProperty ABCCurveWriter::abc_prop_for_custom_props()
94{
95 return abc_schema_prop_for_custom_props(abc_curve_schema_);
96}
97
99{
100 const Curves *curves_id;
101 std::unique_ptr<Curves, std::function<void(Curves *)>> converted_curves;
102
103 switch (context.object->type) {
104 case OB_CURVES_LEGACY: {
105 const Curve *legacy_curve = id_cast<Curve *>(context.object->data);
106 converted_curves = std::unique_ptr<Curves, std::function<void(Curves *)>>(
107 bke::curve_legacy_to_curves(*legacy_curve), [](Curves *c) { BKE_id_free(nullptr, c); });
108 curves_id = converted_curves.get();
109 break;
110 }
111 case OB_CURVES:
112 curves_id = id_cast<Curves *>(context.object->data);
113 break;
114 default:
116 return;
117 }
118
119 const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
120
121 /* Alembic only supports 1 curve type / periodicity combination per object. Enforce this here.
122 * See: Alembic source code for OCurves.h as no documentation explicitly exists for this. */
123 const std::array<int, CURVE_TYPES_NUM> &curve_type_counts = curves.curve_type_counts();
124 const int number_of_curve_types = std::count_if(curve_type_counts.begin(),
125 curve_type_counts.end(),
126 [](const int count) { return count > 0; });
127 if (number_of_curve_types > 1) {
128 CLOG_WARN(&LOG, "Cannot export mixed curve types in the same Curves object");
129 return;
130 }
131
133 CLOG_WARN(&LOG, "Cannot export mixed cyclic and non-cyclic curves in the same Curves object");
134 return;
135 }
136
137 if (!curves.is_empty()) {
138 /* TODO: Blender supports per-curve resolutions but we're only using the first curve's data
139 * here. Investigate using OInt16ArrayProperty to write out all the data but do so efficiently.
140 * e.g. Write just a single value if all curves share the same resolution etc. */
141 OCompoundProperty user_props = abc_curve_schema_.getUserProperties();
142 const Alembic::Abc::PropertyHeader *prop_header = user_props.getPropertyHeader(
143 ABC_CURVE_RESOLUTION_U_PROPNAME);
144 if (!prop_header) {
145 const int resolution_u = curves.resolution().first();
146 OInt16Property user_prop_resolu(user_props, ABC_CURVE_RESOLUTION_U_PROPNAME);
147 user_prop_resolu.set(resolution_u);
148 }
149 }
150
151 const bool is_cyclic = !curves.is_empty() && curves.cyclic().first();
152 Alembic::AbcGeom::BasisType curve_basis = Alembic::AbcGeom::kNoBasis;
153 Alembic::AbcGeom::CurveType curve_type = Alembic::AbcGeom::kLinear;
154 Alembic::AbcGeom::CurvePeriodicity periodicity = is_cyclic ? Alembic::AbcGeom::kPeriodic :
155 Alembic::AbcGeom::kNonPeriodic;
156 const CurveType blender_curve_type = curves.is_empty() ? CURVE_TYPE_POLY :
157 CurveType(curves.curve_types().first());
158 switch (blender_curve_type) {
159 case CURVE_TYPE_POLY:
160 curve_basis = Alembic::AbcGeom::kNoBasis;
161 curve_type = Alembic::AbcGeom::kLinear;
162 break;
164 curve_basis = Alembic::AbcGeom::kCatmullromBasis;
165 curve_type = Alembic::AbcGeom::kLinear;
166 break;
168 curve_basis = Alembic::AbcGeom::kBezierBasis;
169 curve_type = Alembic::AbcGeom::kCubic;
170 break;
171 case CURVE_TYPE_NURBS:
172 curve_basis = Alembic::AbcGeom::kBsplineBasis;
173 curve_type = Alembic::AbcGeom::kVariableOrder;
174 break;
175 }
176
177 std::vector<Imath::V3f> verts;
178 std::vector<int32_t> vert_counts;
179 std::vector<float> widths;
180 std::vector<float> weights;
181 std::vector<float> knots;
182 std::vector<uint8_t> orders;
183
184 const Span<float3> positions = curves.positions();
185 const std::optional<Span<float>> nurbs_weights = curves.nurbs_weights();
186 const VArray<int8_t> nurbs_orders = curves.nurbs_orders();
187
188 const VArray<float> radii = curves.radius();
189 Alembic::AbcGeom::GeometryScope width_scope = kVertexScope;
190 if (radii.is_single() && !radii.is_empty()) {
191 width_scope = kConstantScope;
192 widths.push_back(radii[0] * 2.0f);
193 }
194
195 vert_counts.resize(curves.curves_num());
196 const OffsetIndices points_by_curve = curves.points_by_curve();
197 const std::optional<Span<float3>> handles_l = curves.handle_positions_left();
198 const std::optional<Span<float3>> handles_r = curves.handle_positions_right();
199 if (blender_curve_type == CURVE_TYPE_BEZIER && handles_l && handles_r) {
200
201 for (const int i_curve : curves.curves_range()) {
202 const IndexRange points = points_by_curve[i_curve];
203 const size_t current_vert_count = verts.size();
204
205 const int start_point_index = points.first();
206 const int last_point_index = points.last();
207
208 /* Vert order in the bezier curve representation is:
209 * [
210 * control point 0(+ width), right handle 0, left handle 1,
211 * control point 1(+ width), right handle 1, left handle 2,
212 * control point 2(+ width), ...
213 * ] */
214 for (const int i_point : points.drop_back(1)) {
215 verts.push_back(to_yup_V3f(positions[i_point]));
216 if (width_scope != kConstantScope) {
217 widths.push_back(radii[i_point] * 2.0f);
218 }
219
220 verts.push_back(to_yup_V3f((*handles_r)[i_point]));
221 verts.push_back(to_yup_V3f((*handles_l)[i_point + 1]));
222 }
223
224 /* The last vert in the array doesn't need a right handle because the curve stops
225 * at that point. */
226 verts.push_back(to_yup_V3f(positions[last_point_index]));
227 if (width_scope != kConstantScope) {
228 widths.push_back(radii[last_point_index] * 2.0f);
229 }
230
231 /* If the curve is cyclic, include the right handle of the last point and the
232 * left handle of the first point. */
233 if (is_cyclic) {
234 verts.push_back(to_yup_V3f((*handles_r)[last_point_index]));
235 verts.push_back(to_yup_V3f((*handles_l)[start_point_index]));
236 }
237
238 vert_counts[i_curve] = verts.size() - current_vert_count;
239 }
240 }
241 else {
242 verts.resize(curves.points_num());
243 if (width_scope != kConstantScope) {
244 widths.resize(curves.points_num());
245 }
246 for (const int i_point : curves.points_range()) {
247 verts[i_point] = to_yup_V3f(positions[i_point]);
248 if (width_scope != kConstantScope) {
249 widths[i_point] = radii[i_point] * 2.0f;
250 }
251 }
252
253 if (blender_curve_type == CURVE_TYPE_NURBS) {
254 if (nurbs_weights) {
255 weights.resize(curves.points_num());
256 std::copy_n(nurbs_weights->data(), weights.size(), weights.data());
257 }
258
259 orders.resize(curves.curves_num());
260 for (const int i_curve : curves.curves_range()) {
261 orders[i_curve] = nurbs_orders[i_curve];
262 }
263 }
264
265 offset_indices::copy_group_sizes(points_by_curve, points_by_curve.index_range(), vert_counts);
266 }
267
268 Alembic::AbcGeom::OFloatGeomParam::Sample width_sample;
269 width_sample.setVals(widths);
270 width_sample.setScope(width_scope);
271
272 OCurvesSchema::Sample sample(verts,
273 vert_counts,
274 curve_type,
275 periodicity,
276 width_sample,
277 OV2fGeomParam::Sample(), /* UVs */
278 ON3fGeomParam::Sample(), /* normals */
279 curve_basis,
280 weights,
281 orders,
282 knots);
283
284 std::vector<Imath::V3f> velocities;
285 if (get_velocities(curves.attributes(), velocities)) {
286 sample.setVelocities(velocities);
287 }
288
289 update_bounding_box(context.object);
290 sample.setSelfBounds(bounding_box_);
291 abc_curve_schema_.set(sample);
292
293 write_arb_geo_params(curves, *context.object, abc_curve_schema_.getNumSamples());
294}
295
296void ABCCurveWriter::write_arb_geo_params(const bke::CurvesGeometry &curves,
297 const Object &object,
298 const size_t num_geom_samples)
299{
300 Alembic::Abc::OCompoundProperty arb_geom_params = abc_curve_schema_.getArbGeomParams();
301
302 const bke::AttributeAccessor attributes = curves.attributes();
303
304 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
305 /* Skip "internal" Blender properties and attributes dealt with elsewhere. */
306 if (iter.name[0] == '.' || bke::attribute_name_is_anonymous(iter.name) ||
308 {
309 return;
310 }
311
312 AttributeParamMaps &param_maps = get_attribute_param_maps();
313 /* Pass num_geom_samples - 1 so we write empty samples up until this frame. */
314 BLI_assert(num_geom_samples >= 1);
315 create_geom_param_for_attribute(arb_geom_params,
316 param_maps,
317 iter,
319 {},
320 BKE_id_name(object.id),
321 num_geom_samples - 1);
322 });
323
324 if (attribute_maps_) {
325 /* If an attribute was missing this frame, write empty samples for it.
326 * This is mostly to ensure that attributes have the same number of samples as the geometry
327 * data if some disappear midway in the animation and never come back. */
328 attribute_maps_->write_empty_samples(num_geom_samples);
329 }
330}
331
332AttributeParamMaps &ABCCurveWriter::get_attribute_param_maps()
333{
334 if (!attribute_maps_) {
335 attribute_maps_ = std::make_unique<AttributeParamMaps>();
336 }
337 return *attribute_maps_.get();
338}
339
344
345Mesh *ABCCurveMeshWriter::get_export_mesh(Object *object_eval, bool &r_needsfree)
346{
347 switch (object_eval->type) {
348 case OB_CURVES_LEGACY: {
349 Mesh *mesh_eval = BKE_object_get_evaluated_mesh(object_eval);
350 if (mesh_eval != nullptr) {
351 /* Mesh_eval only exists when generative modifiers are in use. */
352 r_needsfree = false;
353 return mesh_eval;
354 }
355
356 r_needsfree = true;
357 return BKE_mesh_new_nomain_from_curve(object_eval);
358 }
359
360 case OB_CURVES: {
361 Curves *curves = id_cast<Curves *>(object_eval->data);
362 r_needsfree = true;
363 return bke::curve_to_wire_mesh(curves->geometry.wrap());
364 }
365 default:
366 break;
367 }
368
369 return nullptr;
370}
371
372} // namespace io::alembic
373} // namespace blender
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
bool contains(const Key &key) const
Definition BLI_set.hh:310
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
ABCAbstractWriter(const ABCWriterConstructorArgs &args)
Alembic::Abc::OCompoundProperty abc_schema_prop_for_custom_props(T abc_schema)
const ABCWriterConstructorArgs args_
virtual void update_bounding_box(Object *object)
Mesh * get_export_mesh(Object *object_eval, bool &r_needsfree) override
ABCCurveMeshWriter(const ABCWriterConstructorArgs &args)
Alembic::Abc::OObject get_alembic_object() const override
ABCCurveWriter(const ABCWriterConstructorArgs &args)
void create_alembic_objects(const HierarchyContext *context) override
void do_write(HierarchyContext &context) override
Alembic::Abc::OCompoundProperty abc_prop_for_custom_props() override
ABCGenericMeshWriter(const ABCWriterConstructorArgs &args)
BooleanMix booleans_mix_calc(const VArray< bool > &varray, IndexRange range_to_check)
bool attribute_name_is_anonymous(const StringRef name)
Curves * curve_legacy_to_curves(const Curve &curve_legacy)
Mesh * curve_to_wire_mesh(const CurvesGeometry &curve, const bke::AttributeFilter &attribute_filter={})
void create_geom_param_for_attribute(const Alembic::Abc::OCompoundProperty &prop, AttributeParamMaps &param_maps, const bke::AttributeIter &attr, 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])
static Imath::V3f to_yup_V3f(float3 v)
bool get_velocities(const bke::AttributeAccessor &attributes, std::vector< Imath::V3f > &velocities)
static bool is_excluded_attr(StringRefNull name)
void copy_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ATTR_WARN_UNUSED_RESULT const BMVert * v
const char * BKE_id_name(const ID &id)
Mesh * BKE_mesh_new_nomain_from_curve(const Object *ob)
void BKE_id_free(Main *bmain, void *idv)
@ CURVE_TYPE_CATMULL_ROM
static CLG_LogRef LOG
Definition logging.cpp:12
VecBase< float, 3 > float3
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436