Blender V5.3
abc_reader_object.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_reader_object.h"
10#include "abc_axis_conversion.h"
11#include "abc_keyframing.h"
12#include "abc_util.h"
13
14#include "DNA_cachefile_types.h"
16#include "DNA_modifier_types.h"
17#include "DNA_object_types.h"
18
19#include "ANIM_fcurve.hh"
20
21#include "BKE_constraint.h"
22#include "BKE_lib_id.hh"
23#include "BKE_modifier.hh"
24#include "BKE_object.hh"
25#include "BKE_object_types.hh"
26
27#include "BLI_listbase.hh"
28#include "BLI_math_matrix_c.hh"
30#include "BLI_string.hh"
31
32#include "Alembic/AbcGeom/Visibility.h"
33
34#include "CLG_log.h"
35
36namespace blender {
37
38using Alembic::AbcGeom::IObject;
39using Alembic::AbcGeom::ISampleSelector;
40using Alembic::AbcGeom::IVisibilityProperty;
41using Alembic::AbcGeom::IXform;
42using Alembic::AbcGeom::IXformSchema;
43using Alembic::AbcGeom::ObjectVisibility;
44
45namespace io::alembic {
46
47static CLG_LogRef LOG = {"io.alembic"};
48
50 ImportSettings &settings)
51{
52 return AbcReaderConstructorArgs{.object = object, .settings = settings};
53}
54
57 m_iobject(args.object),
58 m_settings(&args.settings),
59 m_is_reading_a_file_sequence(args.settings.is_sequence),
60 m_refcount(0),
62{
63 m_name = m_iobject.getFullName();
64 std::vector<std::string> parts;
65 split(m_name, '/', parts);
66
67 if (parts.size() >= 2) {
68 m_object_name = parts[parts.size() - 2];
69 m_data_name = parts[parts.size() - 1];
70 }
71 else {
72 m_object_name = m_data_name = parts[parts.size() - 1];
73 }
74
76}
77
79{
80 m_inherits_xform = false;
81
82 IXform ixform = xform();
83 if (!ixform) {
84 return;
85 }
86
87 const IXformSchema &schema(ixform.getSchema());
88 if (!schema.valid()) {
89 std::cerr << "Alembic object " << ixform.getFullName() << " has an invalid schema."
90 << std::endl;
91 return;
92 }
93
94 m_inherits_xform = schema.getInheritsXforms();
95
96 IObject ixform_parent = ixform.getParent();
97 if (!ixform_parent.getParent()) {
98 /* The archive top object certainly is not a transform itself, so handle
99 * it as "no parent". */
100 m_inherits_xform = false;
101 }
102 else {
103 m_inherits_xform = ixform_parent && m_inherits_xform;
104 }
105}
106
107const IObject &AbcObjectReader::iobject() const
108{
109 return m_iobject;
110}
111
113{
114 return m_object;
115}
116
118{
119 m_object = ob;
120}
121
122static Imath::M44d blend_matrices(const Imath::M44d &m0,
123 const Imath::M44d &m1,
124 const double weight)
125{
126 float mat0[4][4], mat1[4][4], ret[4][4];
127
128 /* Cannot use Imath::M44d::getValue() since this returns a pointer to
129 * doubles and interp_m4_m4m4 expects pointers to floats. So need to convert
130 * the matrices manually.
131 */
132
133 convert_matrix_datatype(m0, mat0);
134 convert_matrix_datatype(m1, mat1);
135 interp_m4_m4m4(ret, mat0, mat1, float(weight));
137}
138
139Imath::M44d get_matrix(const IXformSchema &schema, const chrono_t time)
140{
141 Alembic::AbcGeom::ISampleSelector selector(time);
142
143 const std::optional<SampleInterpolationSettings> interpolation_settings =
145 selector, schema.getTimeSampling(), schema.getNumSamples());
146
147 if (!interpolation_settings.has_value()) {
148 /* No interpolation, just read the current time. */
149 Alembic::AbcGeom::XformSample s0;
150 schema.get(s0, selector);
151 return s0.getMatrix();
152 }
153
154 Alembic::AbcGeom::XformSample s0, s1;
155 schema.get(s0, Alembic::AbcGeom::ISampleSelector(interpolation_settings->index));
156 schema.get(s1, Alembic::AbcGeom::ISampleSelector(interpolation_settings->ceil_index));
157 return blend_matrices(s0.getMatrix(), s1.getMatrix(), interpolation_settings->weight);
158}
159
161 const Alembic::Abc::ISampleSelector & /*sample_sel*/,
162 const AbcReadGeometryParams & /*read_params*/,
163 const char ** /*r_err_str*/)
164{
165}
166
167bool AbcObjectReader::topology_changed(const Mesh * /*existing_mesh*/,
168 const Alembic::Abc::ISampleSelector & /*sample_sel*/)
169{
170 /* The default implementation of read_mesh() just returns the original mesh, so never changes the
171 * topology. */
172 return false;
173}
174
176 IObject vis_object_{};
177 IVisibilityProperty vis_prop_{};
178
179 FCurve *viewport_fcurve = nullptr;
180 FCurve *render_fcurve = nullptr;
181
182 public:
184 const IObject &vis_object,
185 const IVisibilityProperty &vis_prop)
186 : FCurveCreationHelper(&object->id), vis_object_(vis_object), vis_prop_(vis_prop)
187 {
188 }
189
190 void create_fcurves(const int sample_count) override
191 {
192 viewport_fcurve = create_fcurve({"hide_viewport", 0}, sample_count);
193 render_fcurve = create_fcurve({"hide_render", 0}, sample_count);
194 }
195
196 void set_fcurves_sample(const FrameSampleInfo &sample_info) override
197 {
198 ObjectVisibility vis = ObjectVisibility(vis_prop_.getValue(sample_info.selector));
199
200 if (vis == Alembic::AbcGeom::kVisibilityDeferred) {
201 IObject parent = vis_object_.getParent();
202
203 while (parent) {
204 const IVisibilityProperty &parent_vis_prop(
205 Alembic::AbcGeom::GetVisibilityProperty(parent));
206 if (parent_vis_prop) {
207 vis = ObjectVisibility(parent_vis_prop.getValue(sample_info.selector));
208 if (vis != Alembic::AbcGeom::kVisibilityDeferred) {
209 break;
210 }
211 }
212
213 parent = parent.getParent();
214 }
215 }
216
217 const float hidden = (vis == ObjectVisibility::kVisibilityHidden) ? 1.0f : 0.0f;
218 set_fcurve_sample(viewport_fcurve, sample_info.sample_index, sample_info.frame, hidden);
219 set_fcurve_sample(render_fcurve, sample_info.sample_index, sample_info.frame, hidden);
220 }
221};
222
224 Vector<std::unique_ptr<FCurveCreationHelper>> &keyframing_helpers)
225{
226 /* Check if we have animated visibility. */
227 IObject vis_object = m_iobject;
228 ObjectVisibility vis = Alembic::AbcGeom::kVisibilityDeferred;
229 while (vis_object) {
230 IVisibilityProperty vis_prop = Alembic::AbcGeom::GetVisibilityProperty(vis_object);
231 if (vis_prop) {
232 if (!vis_prop.isConstant()) {
233 std::unique_ptr<FCurveCreationHelper> helper =
234 std::make_unique<VisibilityFCurveCreationHelper>(m_object, vis_object, vis_prop);
235 keyframing_helpers.append(std::move(helper));
237 break;
238 }
239
240 vis = ObjectVisibility(vis_prop.getValue(ISampleSelector()));
241 if (vis != Alembic::AbcGeom::kVisibilityDeferred) {
242 break;
243 }
244 }
245
246 vis_object = vis_object.getParent();
247 }
248
249 /* Helper for the object data. */
250 std::unique_ptr<FCurveCreationHelper> specific_helper = getKeyFramingHelper();
251 if (specific_helper) {
252 keyframing_helpers.append(std::move(specific_helper));
253 }
254}
255
256std::unique_ptr<FCurveCreationHelper> AbcObjectReader::getKeyFramingHelper()
257{
258 return nullptr;
259}
260
262{
263 bool is_constant = false;
264 float transform_from_alembic[4][4];
265
266 /* If the parent is a camera, apply the inverse rotation to make up for the from-Maya rotation.
267 * This assumes that the parent object also was imported from Alembic. */
268 if (m_object->parent != nullptr && m_object->parent->type == OB_CAMERA) {
270 }
271
272 this->read_matrix(transform_from_alembic, time, m_settings->scale, is_constant);
273
274 /* Apply the matrix to the object. */
275 BKE_object_apply_mat4(m_object, transform_from_alembic, true, false);
277
278 if (!is_constant || m_settings->always_add_cache_reader) {
282 STRNCPY(data->object_path, m_iobject.getFullName().c_str());
283
284 data->cache_file = m_settings->cache_file;
285 id_us_plus(&data->cache_file->id);
286 }
287}
288
289Alembic::AbcGeom::IXform AbcObjectReader::xform()
290{
291 /* Check that we have an empty object (locator, bone head/tail...). */
292 if (IXform::matches(m_iobject.getMetaData())) {
293 try {
294 return IXform(m_iobject, Alembic::AbcGeom::kWrapExisting);
295 }
296 catch (Alembic::Util::Exception &ex) {
297 CLOG_WARN(&LOG,
298 "Error reading object transform for '%s': %s",
299 m_iobject.getFullName().c_str(),
300 ex.what());
301 return IXform();
302 }
303 }
304
305 /* Check that we have an object with actual data, in which case the
306 * parent Alembic object should contain the transform. */
307 IObject abc_parent = m_iobject.getParent();
308
309 /* The archive's top object can be recognized by not having a parent. */
310 if (abc_parent.getParent() && IXform::matches(abc_parent.getMetaData())) {
311 try {
312 return IXform(abc_parent, Alembic::AbcGeom::kWrapExisting);
313 }
314 catch (Alembic::Util::Exception &ex) {
315 CLOG_WARN(&LOG,
316 "Error reading object transform for '%s': %s",
317 abc_parent.getFullName().c_str(),
318 ex.what());
319 return IXform();
320 }
321 }
322
323 /* This can happen in certain cases. For example, MeshLab exports
324 * point clouds without parent XForm. */
325 return IXform();
326}
327
328void AbcObjectReader::read_matrix(float r_mat[4][4] /* local matrix */,
329 const chrono_t time,
330 const float scale,
331 bool &r_is_constant)
332{
333 IXform ixform = xform();
334 if (!ixform) {
335 unit_m4(r_mat);
336 r_is_constant = true;
337 return;
338 }
339
340 const IXformSchema &schema(ixform.getSchema());
341 if (!schema.valid()) {
342 std::cerr << "Alembic object " << ixform.getFullName() << " has an invalid schema."
343 << std::endl;
344 return;
345 }
346
347 const Imath::M44d matrix = get_matrix(schema, time);
348 convert_matrix_datatype(matrix, r_mat);
350
351 /* Convert from Maya to Blender camera orientation. Children of this camera
352 * will have the opposite transform as their Parent Inverse matrix.
353 * See AbcObjectReader::setupObjectTransform(). */
354 if (m_object->type == OB_CAMERA) {
355 float camera_rotation[4][4];
356 axis_angle_to_mat4_single(camera_rotation, 'X', M_PI_2);
357 mul_m4_m4m4(r_mat, r_mat, camera_rotation);
358 }
359
360 if (!m_inherits_xform) {
361 /* Only apply scaling to root objects, parenting will propagate it. */
362 float scale_mat[4][4];
363 scale_m4_fl(scale_mat, scale);
364 mul_m4_m4m4(r_mat, scale_mat, r_mat);
365 }
366
367 r_is_constant = schema.isConstant();
368}
369
383
385{
386 IObject vis_object = m_iobject;
387 ObjectVisibility vis = Alembic::AbcGeom::kVisibilityDeferred;
388 while (vis_object) {
389 IVisibilityProperty vis_prop = Alembic::AbcGeom::GetVisibilityProperty(vis_object);
390 if (vis_prop) {
391 if (!vis_prop.isConstant()) {
392 return;
393 }
394 vis = ObjectVisibility(vis_prop.getValue(ISampleSelector()));
395 if (vis != Alembic::AbcGeom::kVisibilityDeferred) {
396 break;
397 }
398 }
399
400 vis_object = vis_object.getParent();
401 }
402
403 if (vis == Alembic::AbcGeom::kVisibilityHidden) {
405 }
406}
407
409{
410 return m_refcount;
411}
412
414{
415 m_refcount++;
416}
417
419{
420 m_refcount--;
422}
423
424} // namespace io::alembic
425} // namespace blender
Functions to modify FCurves.
General operations, lookup, etc. for blender objects.
#define M_PI_2
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
virtual void read_geometry(bke::GeometrySet &geometry_set, const Alembic::Abc::ISampleSelector &sample_sel, const AbcReadGeometryParams &read_params, const char **r_err_str)
void getKeyFramingHelpers(Vector< std::unique_ptr< FCurveCreationHelper > > &keyframing_helpers)
void read_matrix(float r_mat[4][4], chrono_t time, float scale, bool &is_constant)
const Alembic::Abc::IObject & iobject() const
AbcObjectReader(const AbcReaderConstructorArgs &args)
virtual bool topology_changed(const Mesh *existing_mesh, const Alembic::Abc::ISampleSelector &sample_sel)
virtual Alembic::AbcGeom::IXform xform()
virtual std::unique_ptr< FCurveCreationHelper > getKeyFramingHelper()
FCurve * create_fcurve(const animrig::FCurveDescriptor &fcurve_descriptor, const int sample_count)
VisibilityFCurveCreationHelper(Object *object, const IObject &vis_object, const IVisibilityProperty &vis_prop)
void create_fcurves(const int sample_count) override
void set_fcurves_sample(const FrameSampleInfo &sample_info) override
float3x3 scale(float3x3 mat, float2 scale)
Imath::M44d get_matrix(const IXformSchema &schema, const chrono_t time)
void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode)
static Imath::M44d blend_matrices(const Imath::M44d &m0, const Imath::M44d &m1, const double weight)
AbcReaderConstructorArgs create_reader_constructor_args(const IObject &object, ImportSettings &settings)
Imath::M44d convert_matrix_datatype(const float mat[4][4])
Definition abc_util.cc:46
static CLG_LogRef LOG
void set_fcurve_sample(FCurve *fcu, int64_t sample_index, const float frame, const float value)
std::optional< SampleInterpolationSettings > get_sample_interpolation_settings(const Alembic::AbcGeom::ISampleSelector &selector, const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling, size_t samples_number)
Definition abc_util.cc:129
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
@ CONSTRAINT_TYPE_TRANSFORM_CACHE
void id_us_plus(ID *id)
Definition lib_id.cc:433
@ eModifierType_MeshSequenceCache
void interp_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4], float t)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
struct bConstraint * BKE_constraint_add_for_object(struct Object *ob, const char *name, eBConstraint_Types type)
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
void BKE_object_to_mat4(const Object *ob, float r_mat[4][4])
Definition object.cc:3125
BMesh const char void * data
ModifierData * BKE_modifier_new(int type)
void unit_m4(float m[4][4])
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static CLG_LogRef LOG
void scale_m4_fl(float R[4][4], float scale)
const c_style_mat & ptr() const
struct Object * parent
ListBaseT< ModifierData > modifiers
float parentinv[4][4]
bke::ObjectRuntime * runtime
eObject_VisibilityFlag visibility_flag
Alembic::Abc::ISampleSelector selector