Blender V5.3
alembic_capi.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_alembic.h"
10#include "IO_types.hh"
11
12#include <Alembic/AbcGeom/ILight.h>
13#include <Alembic/AbcGeom/INuPatch.h>
14#include <Alembic/AbcMaterial/IMaterial.h>
15
16#include "abc_keyframing.h"
17#include "abc_reader_archive.h"
18#include "abc_reader_camera.h"
19#include "abc_reader_curves.h"
20#include "abc_reader_mesh.h"
21#ifdef USE_NURBS
22# include "abc_reader_nurbs.h"
23#endif
24#include "abc_reader_points.h"
26#include "abc_util.h"
27
28#include "MEM_guardedalloc.h"
29
30#include "DNA_cachefile_types.h"
32#include "DNA_listBase.h"
33#include "DNA_object_types.h"
34#include "DNA_scene_types.h"
35
36#include "BKE_cachefile.hh"
37#include "BKE_context.hh"
38#include "BKE_global.hh"
39#include "BKE_layer.hh"
40#include "BKE_lib_id.hh"
41#include "BKE_library.hh"
42#include "BKE_object.hh"
43
44#include "DEG_depsgraph.hh"
46
47#include "ED_undo.hh"
48
50#include "BLI_listbase.hh"
51#include "BLI_math_matrix_c.hh"
52#include "BLI_path_utils.hh"
53#include "BLI_sort.hh"
54#include "BLI_span.hh"
55#include "BLI_string.hh"
56#include "BLI_timeit.hh"
57
58#include "BLT_translation.hh"
59
60#include "WM_api.hh"
61#include "WM_types.hh"
62
63#include "CLG_log.h"
64
65namespace blender {
66
67static CLG_LogRef LOG = {"io.alembic"};
68
69using Alembic::Abc::IV3fArrayProperty;
70using Alembic::Abc::ObjectHeader;
71using Alembic::Abc::PropertyHeader;
72using Alembic::Abc::V3fArraySamplePtr;
73using Alembic::AbcGeom::ICamera;
74using Alembic::AbcGeom::ICurves;
75using Alembic::AbcGeom::IFaceSet;
76using Alembic::AbcGeom::ILight;
77using Alembic::AbcGeom::INuPatch;
78using Alembic::AbcGeom::IObject;
79using Alembic::AbcGeom::IPoints;
80using Alembic::AbcGeom::IPolyMesh;
81using Alembic::AbcGeom::IPolyMeshSchema;
82using Alembic::AbcGeom::ISampleSelector;
83using Alembic::AbcGeom::ISubD;
84using Alembic::AbcGeom::IXform;
85using Alembic::AbcGeom::kWrapExisting;
86using Alembic::AbcGeom::MetaData;
87using Alembic::AbcMaterial::IMaterial;
88
89using namespace blender::io::alembic;
90
105
107{
108 return reinterpret_cast<AlembicArchiveData *>(handle);
109}
110
112{
113 return reinterpret_cast<CacheArchiveHandle *>(archive);
114}
115
116/* Add the object's path to list of object paths. No duplication is done, callers are
117 * responsible for ensuring that only unique paths are added to the list.
118 */
119static void add_object_path(ListBaseT<CacheObjectPath> *object_paths, const IObject &object)
120{
121 CacheObjectPath *abc_path = MEM_new<CacheObjectPath>("CacheObjectPath");
122 STRNCPY(abc_path->path, object.getFullName().c_str());
123 BLI_addtail(object_paths, abc_path);
124}
125
126// #define USE_NURBS
127
128/* NOTE: this function is similar to visit_objects below, need to keep them in
129 * sync. */
130static bool gather_objects_paths(const IObject &object, ListBaseT<CacheObjectPath> *object_paths)
131{
132 if (!object.valid()) {
133 return false;
134 }
135
136 size_t children_claiming_this_object = 0;
137 size_t num_children = object.getNumChildren();
138
139 for (size_t i = 0; i < num_children; i++) {
140 bool child_claims_this_object = gather_objects_paths(object.getChild(i), object_paths);
141 children_claiming_this_object += child_claims_this_object ? 1 : 0;
142 }
143
144 const MetaData &md = object.getMetaData();
145 bool get_path = false;
146 bool parent_is_part_of_this_object = false;
147
148 if (!object.getParent()) {
149 /* The root itself is not an object we should import. */
150 }
151 else if (IXform::matches(md)) {
152 if (has_property(object.getProperties(), "locator")) {
153 get_path = true;
154 }
155 else {
156 get_path = children_claiming_this_object == 0;
157 }
158
159 /* Transforms are never "data" for their parent. */
160 parent_is_part_of_this_object = false;
161 }
162 else {
163 /* These types are "data" for their parent. */
164 get_path = IPolyMesh::matches(md) || ISubD::matches(md) ||
165#ifdef USE_NURBS
166 INuPatch::matches(md) ||
167#endif
168 ICamera::matches(md) || IPoints::matches(md) || ICurves::matches(md);
169 parent_is_part_of_this_object = get_path;
170 }
171
172 if (get_path) {
173 add_object_path(object_paths, object);
174 }
175
176 return parent_is_part_of_this_object;
177}
178
180 const char *filepath,
181 const CacheFileLayer *layers,
182 ListBaseT<CacheObjectPath> *object_paths)
183{
184 std::vector<const char *> filepaths;
185 filepaths.push_back(filepath);
186
187 while (layers) {
188 if ((layers->flag & CACHEFILE_LAYER_HIDDEN) == 0) {
189 filepaths.push_back(layers->filepath);
190 }
191 layers = layers->next;
192 }
193
194 /* We need to reverse the order as overriding archives should come first. */
195 std::reverse(filepaths.begin(), filepaths.end());
196
197 ArchiveReader *archive = ArchiveReader::get(bmain, filepaths);
198
199 if (!archive || !archive->valid()) {
200 delete archive;
201 return nullptr;
202 }
203
204 if (object_paths) {
205 gather_objects_paths(archive->getTop(), object_paths);
206 }
207
208 AlembicArchiveData *archive_data = new AlembicArchiveData();
209 archive_data->archive_reader = archive;
210 archive_data->settings = new ImportSettings();
211
212 return handle_from_archive(archive_data);
213}
214
216{
217 delete archive_from_handle(handle);
218}
219
221{
222 return ALEMBIC_LIBRARY_VERSION;
223}
224
225static void find_iobject(const IObject &object, IObject &ret, const std::string &path)
226{
227 if (!object.valid()) {
228 return;
229 }
230
231 std::vector<std::string> tokens;
232 split(path, '/', tokens);
233
234 IObject tmp = object;
235
236 std::vector<std::string>::iterator iter;
237 for (iter = tokens.begin(); iter != tokens.end(); ++iter) {
238 IObject child = tmp.getChild(*iter);
239 tmp = child;
240 }
241
242 ret = tmp;
243}
244
245/* ********************** Import file ********************** */
246
267static std::pair<bool, AbcObjectReader *> visit_object(
268 const IObject &object,
270 ImportSettings &settings,
271 AbcObjectReader::ptr_vector &r_assign_as_parent)
272{
273 const std::string &full_name = object.getFullName();
274
275 if (!object.valid()) {
276 std::cerr << " - " << full_name << ": object is invalid, skipping it and all its children.\n";
277 return std::make_pair(false, static_cast<AbcObjectReader *>(nullptr));
278 }
279
280 /* The interpretation of data by the children determine the role of this
281 * object. This is especially important for Xform objects, as they can be
282 * either part of a Blender object or a Blender object (Empty) themselves.
283 */
284 size_t children_claiming_this_object = 0;
285 size_t num_children = object.getNumChildren();
286 AbcObjectReader::ptr_vector claiming_child_readers;
287 AbcObjectReader::ptr_vector nonclaiming_child_readers;
288 AbcObjectReader::ptr_vector assign_as_parent;
289 for (size_t i = 0; i < num_children; i++) {
290 const IObject ichild = object.getChild(i);
291
292 /* TODO: When we only support C++11, use std::tie() instead. */
293 std::pair<bool, AbcObjectReader *> child_result;
294 child_result = visit_object(ichild, readers, settings, assign_as_parent);
295
296 bool child_claims_this_object = child_result.first;
297 AbcObjectReader *child_reader = child_result.second;
298
299 if (child_reader == nullptr) {
300 BLI_assert(!child_claims_this_object);
301 }
302 else {
303 if (child_claims_this_object) {
304 claiming_child_readers.push_back(child_reader);
305 }
306 else {
307 nonclaiming_child_readers.push_back(child_reader);
308 }
309 }
310
311 children_claiming_this_object += child_claims_this_object ? 1 : 0;
312 }
313 BLI_assert(children_claiming_this_object == claiming_child_readers.size());
314 UNUSED_VARS_NDEBUG(children_claiming_this_object);
315
316 AbcObjectReader *reader = nullptr;
317 const MetaData &md = object.getMetaData();
318 bool parent_is_part_of_this_object = false;
319
320 const AbcReaderConstructorArgs args = create_reader_constructor_args(object, settings);
321
322 if (!object.getParent()) {
323 /* The root itself is not an object we should import. */
324 }
325 else if (IXform::matches(md)) {
326 bool create_empty;
327
328 /* An xform can either be a Blender Object (if it contains a mesh, for
329 * example), but it can also be an Empty. Its correct translation to
330 * Blender's data model depends on its children. */
331
332 /* Check whether or not this object is a Maya locator, which is
333 * similar to empties used as parent object in Blender. */
334 if (has_property(object.getProperties(), "locator")) {
335 create_empty = true;
336 }
337 else {
338 create_empty = claiming_child_readers.empty();
339 }
340
341 if (create_empty) {
342 reader = new AbcEmptyReader(args);
343 }
344 }
345 else if (IPolyMesh::matches(md)) {
346 reader = new AbcMeshReader(args);
347 parent_is_part_of_this_object = true;
348 }
349 else if (ISubD::matches(md)) {
350 reader = new AbcSubDReader(args);
351 parent_is_part_of_this_object = true;
352 }
353 else if (INuPatch::matches(md)) {
354#ifdef USE_NURBS
355 /* TODO(kevin): importing cyclic NURBS from other software crashes
356 * at the moment. This is due to the fact that NURBS in other
357 * software have duplicated points which causes buffer overflows in
358 * Blender. Need to figure out exactly how these points are
359 * duplicated, in all cases (cyclic U, cyclic V, and cyclic UV).
360 * Until this is fixed, disabling NURBS reading. */
361 reader = new AbcNurbsReader(args);
362 parent_is_part_of_this_object = true;
363#endif
364 }
365 else if (ICamera::matches(md)) {
366 reader = new AbcCameraReader(args);
367 parent_is_part_of_this_object = true;
368 }
369 else if (IPoints::matches(md)) {
370 reader = new AbcPointsReader(args);
371 parent_is_part_of_this_object = true;
372 }
373 else if (IMaterial::matches(md)) {
374 /* Pass for now. */
375 }
376 else if (ILight::matches(md)) {
377 /* Pass for now. */
378 }
379 else if (IFaceSet::matches(md)) {
380 /* Pass, those are handled in the mesh reader. */
381 }
382 else if (ICurves::matches(md)) {
383 reader = new AbcCurveReader(args);
384 parent_is_part_of_this_object = true;
385 }
386 else {
387 std::cerr << "Alembic object " << full_name << " is of unsupported schema type '"
388 << object.getMetaData().get("schemaObjTitle") << "'" << std::endl;
389 }
390
391 if (reader) {
392 /* We have created a reader, which should imply that this object is
393 * not claimed as part of any child Alembic object. */
394 BLI_assert(claiming_child_readers.empty());
395
396 readers.push_back(reader);
397 reader->incref();
398
399 add_object_path(&settings.cache_file->object_paths, object);
400
401 /* We can now assign this reader as parent for our children. */
402 if (nonclaiming_child_readers.size() + assign_as_parent.size() > 0) {
403 for (AbcObjectReader *child_reader : nonclaiming_child_readers) {
404 child_reader->parent_reader = reader;
405 }
406 for (AbcObjectReader *child_reader : assign_as_parent) {
407 child_reader->parent_reader = reader;
408 }
409 }
410 }
411 else if (object.getParent()) {
412 if (!claiming_child_readers.empty()) {
413 /* The first claiming child will serve just fine as parent to
414 * our non-claiming children. Since all claiming children share
415 * the same XForm, it doesn't really matter which one we pick. */
416 AbcObjectReader *claiming_child = claiming_child_readers[0];
417 for (AbcObjectReader *child_reader : nonclaiming_child_readers) {
418 child_reader->parent_reader = claiming_child;
419 }
420 for (AbcObjectReader *child_reader : assign_as_parent) {
421 child_reader->parent_reader = claiming_child;
422 }
423 /* Claiming children should have our parent set as their parent. */
424 for (AbcObjectReader *child_reader : claiming_child_readers) {
425 r_assign_as_parent.push_back(child_reader);
426 }
427 }
428 else {
429 /* This object isn't claimed by any child, and didn't produce
430 * a reader. Odd situation, could be the top Alembic object, or
431 * an unsupported Alembic schema. Delegate to our parent. */
432 for (AbcObjectReader *child_reader : claiming_child_readers) {
433 r_assign_as_parent.push_back(child_reader);
434 }
435 for (AbcObjectReader *child_reader : nonclaiming_child_readers) {
436 r_assign_as_parent.push_back(child_reader);
437 }
438 for (AbcObjectReader *child_reader : assign_as_parent) {
439 r_assign_as_parent.push_back(child_reader);
440 }
441 }
442 }
443
444 return std::make_pair(parent_is_part_of_this_object, reader);
445}
446
447enum {
450};
451
481
483{
484 timeit::Nanoseconds duration = timeit::Clock::now() - data->start_time;
485 std::cout << "Alembic import took ";
486 timeit::print_duration(duration);
487 std::cout << '\n';
488}
489
491{
493 readers.begin(), readers.end(), [](const AbcObjectReader *a, const AbcObjectReader *b) {
494 const char *na = a->name().c_str();
495 const char *nb = b->name().c_str();
496 return BLI_strcasecmp(na, nb) < 0;
497 });
498}
499
500static void import_file(ImportJobData *data, const char *filepath, float progress_factor)
501{
502 timeit::TimePoint start_time = timeit::Clock::now();
503
504 ArchiveReader *archive = ArchiveReader::get(data->bmain, {filepath});
505
506 if (!archive || !archive->valid()) {
507 data->error_code = ABC_ARCHIVE_FAIL;
508 delete archive;
509 return;
510 }
511
512 CacheFile *cache_file = static_cast<CacheFile *>(
513 BKE_cachefile_add(data->bmain, BLI_path_basename(filepath)));
514
515 /* Decrement the ID ref-count because it is going to be incremented for each
516 * modifier and constraint that it will be attached to, so since currently
517 * it is not used by anyone, its use count will be off by one. */
518 id_us_min(&cache_file->id);
519
520 cache_file->is_sequence = data->settings.is_sequence;
521 cache_file->scale = data->settings.scale;
522 STRNCPY(cache_file->filepath, filepath);
523
524 data->archives.append(archive);
525 data->settings.cache_file = cache_file;
526 data->settings.blender_archive_version_prior_44 = archive->is_blender_archive_version_prior_44();
527
528 *data->do_update = true;
529 *data->progress += 0.05f * progress_factor;
530
531 /* Parse Alembic Archive. */
532 AbcObjectReader::ptr_vector assign_as_parent;
533 std::vector<AbcObjectReader *> readers{};
534 visit_object(archive->getTop(), readers, data->settings, assign_as_parent);
535
536 /* There shouldn't be any orphans. */
537 BLI_assert(assign_as_parent.empty());
538
539 if (G.is_break) {
540 data->was_cancelled = true;
541 data->readers.extend(readers);
542 return;
543 }
544
545 *data->do_update = true;
546 *data->progress += 0.05f * progress_factor;
547
548 /* Create objects and set scene frame range. */
549
550 /* Sort readers by name: when creating a lot of objects in Blender,
551 * it is much faster if the order is sorted by name. */
552 sort_readers(readers);
553 data->readers.extend(readers);
554
555 const float size = float(readers.size());
556
557 ISampleSelector sample_sel(0.0);
558 std::vector<AbcObjectReader *>::iterator iter;
559 const float read_object_progress_step = (0.6f / size) * progress_factor;
560 for (iter = readers.begin(); iter != readers.end(); ++iter) {
561 AbcObjectReader *reader = *iter;
562
563 if (reader->valid()) {
564 reader->readObjectData(data->bmain, sample_sel);
565 reader->readVisibility();
566 }
567 else {
568 std::cerr << "Object " << reader->name() << " in Alembic file " << filepath
569 << " is invalid.\n";
570 }
571 *data->progress += read_object_progress_step;
572 *data->do_update = true;
573
574 if (G.is_break) {
575 data->was_cancelled = true;
576 return;
577 }
578 }
579
580 const TimeInfo time_info = archive->getTimeInfo();
581 if (time_info.is_valid()) {
582 data->min_time = std::min(data->min_time, time_info.min_time);
583 data->max_time = std::max(data->max_time, time_info.max_time);
584
586
587 for (iter = readers.begin(); iter != readers.end(); ++iter) {
588 AbcObjectReader *reader = *iter;
589
590 if (reader->valid()) {
591 reader->getKeyFramingHelpers(keyframing_helpers);
592 }
593 }
594
595 create_keyframes(data->bmain, data->scene, keyframing_helpers, time_info);
596 }
597
598 /* Setup parenthood. */
599 for (iter = readers.begin(); iter != readers.end(); ++iter) {
600 const AbcObjectReader *reader = *iter;
601 const AbcObjectReader *parent_reader = reader->parent_reader;
602 Object *ob = reader->object();
603
604 if (parent_reader == nullptr || !reader->inherits_xform()) {
605 ob->parent = nullptr;
606 }
607 else {
608 ob->parent = parent_reader->object();
609 }
610 }
611
612 /* Setup transformations and constraints. */
613 const float setup_object_transform_progress_step = (0.3f / size) * progress_factor;
614 for (iter = readers.begin(); iter != readers.end(); ++iter) {
615 AbcObjectReader *reader = *iter;
616 reader->setupObjectTransform(0.0);
617
618 *data->progress += setup_object_transform_progress_step;
619 *data->do_update = true;
620
621 if (G.is_break) {
622 data->was_cancelled = true;
623 return;
624 }
625 }
626 timeit::Nanoseconds duration = timeit::Clock::now() - start_time;
627 std::cout << "Alembic import " << filepath << " took ";
628 timeit::print_duration(duration);
629 std::cout << '\n';
630}
631
633{
634 if (!data->settings.set_frame_range) {
635 return;
636 }
637 Scene *scene = data->scene;
638 if (data->settings.is_sequence) {
639 scene->r.sfra = data->settings.sequence_min_frame;
640 scene->r.efra = data->settings.sequence_max_frame;
641 scene->r.cfra = scene->r.sfra;
642 }
643 else if (data->min_time < data->max_time) {
644 scene->r.sfra = int(round(data->min_time * scene->frames_per_second()));
645 scene->r.efra = int(round(data->max_time * scene->frames_per_second()));
646 scene->r.cfra = scene->r.sfra;
647 }
648}
649
650static void import_startjob(void *user_data, wmJobWorkerStatus *worker_status)
651{
652 ImportJobData *data = static_cast<ImportJobData *>(user_data);
653 data->stop = &worker_status->stop;
654 data->do_update = &worker_status->do_update;
655 data->progress = &worker_status->progress;
656 data->start_time = timeit::Clock::now();
657
658 WM_locked_interface_set(data->wm, true);
659 float file_progress_factor = 1.0f / float(data->paths.size());
660 for (int idx : data->paths.index_range()) {
661 import_file(data, data->paths[idx].c_str(), file_progress_factor);
662
663 if (G.is_break || data->was_cancelled) {
664 data->was_cancelled = true;
665 return;
666 }
667
668 worker_status->progress = float(idx + 1) * file_progress_factor;
669 }
671}
672
673static void import_endjob(void *user_data)
674{
675 ImportJobData *data = static_cast<ImportJobData *>(user_data);
676
677 /* Delete objects on cancellation. */
678 if (data->was_cancelled) {
679 for (AbcObjectReader *reader : data->readers) {
680 Object *ob = reader->object();
681
682 /* It's possible that cancellation occurred between the creation of
683 * the reader and the creation of the Blender object. */
684 if (ob == nullptr) {
685 continue;
686 }
687
688 BKE_id_free_us(data->bmain, ob);
689 }
690 }
691 else {
692 const Main *bmain = data->bmain;
693 const Scene *scene = data->scene;
694 ViewLayer *view_layer = data->view_layer;
695
696 BKE_view_layer_base_deselect_all(*bmain, scene, view_layer);
697
699 if (!ID_IS_EDITABLE(lc->collection)) {
701 "Could not find an editable collection in current scene, imported data "
702 "will not be instantiated");
703 }
704
705 for (AbcObjectReader *reader : data->readers) {
706 Object *ob = reader->object();
708 }
709 /* Sync and do the view layer operations. */
710 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
711 bool has_instantiated_object = false;
712 bool has_uninstantiated_object = false;
713 for (AbcObjectReader *reader : data->readers) {
714 Object *ob = reader->object();
715 Base *base = BKE_view_layer_base_find(view_layer, ob);
716 if (!base) {
717 /* Object not instantiated in current viewlayer. */
718 has_uninstantiated_object = true;
719 continue;
720 }
721 has_instantiated_object = true;
722 /* TODO: is setting active needed? */
724
725 /* If the object is hidden, we set the base as hidden instead so that hide/unhide shortcuts
726 * work and the outliner shows the right value. We also unset the flag on the object as users
727 * are more likely to interact with viewport visibility from the outliner or shortcuts than
728 * in the object visibility panel.
729 * We don't do this if keyframes are added for the visibility, otherwise the objects won't
730 * show up in the viewport and we cannot transfer the keyframes to the base. */
731 if ((ob->visibility_flag & OB_HIDE_VIEWPORT) != 0 && !reader->has_visibility_keyframes()) {
732 base->flag |= BASE_HIDDEN;
734 /* Needed for the shortcut (ALT+H) to work. */
736 }
737
740 &ob->id,
743 }
744
745 if (has_instantiated_object && has_uninstantiated_object) {
746 CLOG_ERROR(&LOG, "Some imported objects were not instantiated, while others were");
747 }
748
751
752 if (data->is_background_job) {
753 /* Blender already returned from the import operator, so we need to store our own extra undo
754 * step. */
755 ED_undo_push(data->C, "Alembic Import Finished");
756 }
757 }
758
759 for (AbcObjectReader *reader : data->readers) {
760 reader->decref();
761
762 if (reader->refcount() == 0) {
763 delete reader;
764 }
765 }
766
767 WM_locked_interface_set(data->wm, false);
768
769 switch (data->error_code) {
770 default:
771 case ABC_NO_ERROR:
772 data->import_ok = !data->was_cancelled;
773 break;
774 case ABC_ARCHIVE_FAIL:
776 "Could not open Alembic archive for reading, see console for detail");
777 break;
778 }
779
782}
783
784static void import_freejob(void *user_data)
785{
786 ImportJobData *data = static_cast<ImportJobData *>(user_data);
787 for (ArchiveReader *archive : data->archives) {
788 delete archive;
789 }
790 delete data;
791}
792
793bool ABC_import(bContext *C, const AlembicImportParams *params, bool as_background_job)
794{
795 /* Using new here since MEM_* functions do not call constructor to properly initialize data. */
796 ImportJobData *job = new ImportJobData();
797 job->C = C;
798 job->bmain = CTX_data_main(C);
799 job->scene = CTX_data_scene(C);
801 job->wm = CTX_wm_manager(C);
802 job->import_ok = false;
803 job->paths = params->paths;
804
805 job->settings.scale = params->global_scale;
806 job->settings.is_sequence = params->is_sequence;
813 job->was_cancelled = false;
814 job->is_background_job = as_background_job;
815
816 G.is_break = false;
817
818 bool import_ok = false;
819 if (as_background_job) {
822 job->scene,
823 "Importing Alembic...",
826
827 /* setup job */
830 WM_jobs_callbacks(wm_job, import_startjob, nullptr, nullptr, import_endjob);
831
833 }
834 else {
835 wmJobWorkerStatus worker_status = {};
836 import_startjob(job, &worker_status);
837 import_endjob(job);
838 import_ok = job->import_ok;
839
840 import_freejob(job);
841 }
842
843 return import_ok;
844}
845
846/* ************************************************************************** */
847
848void ABC_get_transform(CacheReader *reader, float r_mat_world[4][4], double time, float scale)
849{
850 if (!reader) {
851 return;
852 }
853
854 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
855
856 bool is_constant = false;
857
858 /* Convert from the local matrix we obtain from Alembic to world coordinates
859 * for Blender. This conversion is done here rather than by Blender due to
860 * work around the non-standard interpretation of CONSTRAINT_SPACE_LOCAL in
861 * BKE_constraint_mat_convertspace(). */
862 Object *object = abc_reader->object();
863 if (object->parent == nullptr) {
864 /* No parent, so local space is the same as world space. */
865 abc_reader->read_matrix(r_mat_world, time, scale, is_constant);
866 return;
867 }
868
869 float mat_parent[4][4];
870 BKE_object_get_parent_matrix(object, object->parent, mat_parent);
871
872 float mat_local[4][4];
873 abc_reader->read_matrix(mat_local, time, scale, is_constant);
874 mul_m4_m4m4(r_mat_world, mat_parent, object->parentinv);
875 mul_m4_m4m4(r_mat_world, r_mat_world, mat_local);
876}
877
878/* ************************************************************************** */
879
880static AbcObjectReader *get_abc_reader(CacheReader *reader, Object *ob, const char **r_err_str)
881{
882 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
883 IObject iobject = abc_reader->iobject();
884
885 if (!iobject.valid()) {
886 *r_err_str = RPT_("Invalid object: verify object path");
887 return nullptr;
888 }
889
890 const ObjectHeader &header = iobject.getHeader();
891 if (!abc_reader->accepts_object_type(header, ob, r_err_str)) {
892 /* r_err_str is set by acceptsObjectType() */
893 return nullptr;
894 }
895
896 return abc_reader;
897}
898
899static ISampleSelector sample_selector_for_time(chrono_t time)
900{
901 /* kFloorIndex is used to be compatible with non-interpolating
902 * properties; they use the floor. */
903 return ISampleSelector(time, ISampleSelector::kFloorIndex);
904}
905
907 Object *ob,
908 bke::GeometrySet &geometry_set,
909 const ABCReadParams *params,
910 const char **r_err_str)
911{
912 AbcObjectReader *abc_reader = get_abc_reader(reader, ob, r_err_str);
913 if (abc_reader == nullptr) {
914 return;
915 }
916
917 ISampleSelector sample_sel = sample_selector_for_time(params->time);
918 AbcReadGeometryParams read_params;
919 read_params.read_flag = params->read_flags;
920 read_params.velocity_name = params->velocity_name ? params->velocity_name : "";
921 read_params.velocity_scale = params->velocity_scale;
922 abc_reader->read_geometry(geometry_set, sample_sel, read_params, r_err_str);
923}
924
926 Object *ob,
927 const Mesh *existing_mesh,
928 const double time,
929 const char **r_err_str)
930{
931 AbcObjectReader *abc_reader = get_abc_reader(reader, ob, r_err_str);
932 if (abc_reader == nullptr) {
933 return false;
934 }
935
936 ISampleSelector sample_sel = sample_selector_for_time(time);
937 return abc_reader->topology_changed(existing_mesh, sample_sel);
938}
939
940/* ************************************************************************** */
941
943{
944 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
945 abc_reader->decref();
946
947 if (abc_reader->refcount() == 0) {
948 delete abc_reader;
949 }
950}
951
953 CacheReader *reader,
954 Object *object,
955 const char *object_path,
956 const bool is_sequence)
957{
958 if (object_path[0] == '\0') {
959 return reader;
960 }
961
962 AlembicArchiveData *archive_data = archive_from_handle(handle);
963 if (!archive_data) {
964 return reader;
965 }
966
967 ArchiveReader *archive = archive_data->archive_reader;
968 if (!archive || !archive->valid()) {
969 return reader;
970 }
971
972 IObject iobject;
973 find_iobject(archive->getTop(), iobject, object_path);
974
975 if (reader) {
976 ABC_CacheReader_free(reader);
977 }
978
979 archive_data->settings->is_sequence = is_sequence;
982
984 *archive_data->settings);
985
986 AbcObjectReader *abc_reader = create_reader(args);
987 if (abc_reader == nullptr) {
988 /* This object is not supported */
989 return nullptr;
990 }
991 abc_reader->object(object);
992 abc_reader->incref();
993
994 return reinterpret_cast<CacheReader *>(abc_reader);
995}
996
997} // namespace blender
General operations, lookup, etc. for blender objects.
#define RPT_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Object groups, one object can be in many groups at once.
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_ID
Definition WM_types.hh:408
#define NC_SCENE
Definition WM_types.hh:391
#define NA_ADDED
Definition WM_types.hh:636
#define ND_FRAME
Definition WM_types.hh:453
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
int getParent(int link_num) const
constexpr T * end() const
Definition BLI_span.hh:554
constexpr T * begin() const
Definition BLI_span.hh:550
AbcCameraReader(const AbcReaderConstructorArgs &args)
AbcCurveReader(const AbcReaderConstructorArgs &args)
AbcEmptyReader(const AbcReaderConstructorArgs &args)
AbcMeshReader(const AbcReaderConstructorArgs &args)
AbcNurbsReader(const AbcReaderConstructorArgs &args)
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
virtual bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **r_err_str) const =0
virtual bool topology_changed(const Mesh *existing_mesh, const Alembic::Abc::ISampleSelector &sample_sel)
virtual void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)=0
std::vector< AbcObjectReader * > ptr_vector
AbcPointsReader(const AbcReaderConstructorArgs &args)
AbcSubDReader(const AbcReaderConstructorArgs &args)
static ArchiveReader * get(const struct Main *bmain, const std::vector< const char * > &filenames)
#define round
float3x3 scale(float3x3 mat, float2 scale)
void create_keyframes(Main *bmain, Scene *scene, Span< std::unique_ptr< FCurveCreationHelper > > helpers, const TimeInfo time_info)
AbcObjectReader * create_reader(const AbcReaderConstructorArgs &args)
Definition abc_util.cc:162
AbcReaderConstructorArgs create_reader_constructor_args(const IObject &object, ImportSettings &settings)
bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
Definition abc_util.cc:82
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
void print_duration(Nanoseconds duration)
Definition timeit.cc:45
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static bool gather_objects_paths(const IObject &object, ListBaseT< CacheObjectPath > *object_paths)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * BKE_cachefile_add(Main *bmain, const char *name)
Definition cachefile.cc:310
void ABC_get_transform(struct CacheReader *reader, float r_mat_world[4][4], double time, float scale)
char * STRNCPY(char(&dst)[N], const char *src)
LayerCollection * BKE_layer_collection_get_active_editable(ViewLayer *view_layer)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
@ RPT_WARNING
Definition BKE_report.hh:40
static void find_iobject(const IObject &object, IObject &ret, const std::string &path)
static void import_startjob(void *user_data, wmJobWorkerStatus *worker_status)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
void BKE_id_free_us(Main *bmain, void *idv) ATTR_NONNULL()
void BKE_object_get_parent_matrix(const Object *ob, Object *par, float r_parentmat[4][4])
Definition object.cc:3419
@ WM_JOB_TYPE_ALEMBIC_IMPORT
Definition WM_api.hh:2036
struct CacheArchiveHandle * ABC_create_handle(const struct Main *bmain, const char *filepath, const struct CacheFileLayer *layers, ListBaseT< CacheObjectPath > *object_paths)
void ABC_free_handle(struct CacheArchiveHandle *handle)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
static void import_endjob(void *user_data)
wmWindow * CTX_wm_window(const bContext *C)
void ABC_CacheReader_free(struct CacheReader *reader)
BLI_INLINE AlembicArchiveData * archive_from_handle(CacheArchiveHandle *handle)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
BLI_INLINE CacheArchiveHandle * handle_from_archive(AlembicArchiveData *archive)
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
static AbcObjectReader * get_abc_reader(CacheReader *reader, Object *ob, const char **r_err_str)
static void import_file(ImportJobData *data, const char *filepath, float progress_factor)
static void add_object_path(ListBaseT< CacheObjectPath > *object_paths, const IObject &object)
ViewLayer * CTX_data_view_layer(const bContext *C)
void ABC_read_geometry(CacheReader *reader, Object *ob, bke::GeometrySet &geometry_set, const ABCReadParams *params, const char **r_err_str)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
Global G
Definition blender.cc:57
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition BLI_sort.hh:37
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
static std::pair< bool, AbcObjectReader * > visit_object(const IObject &object, AbcObjectReader::ptr_vector &readers, ImportSettings &settings, AbcObjectReader::ptr_vector &r_assign_as_parent)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
static void import_freejob(void *user_data)
static void set_frame_range(ImportJobData *data)
static void sort_readers(MutableSpan< AbcObjectReader * > readers)
static ISampleSelector sample_selector_for_time(chrono_t time)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void WM_global_report(eReportType type, const char *message)
bool ABC_mesh_topology_changed(struct CacheReader *reader, struct Object *ob, const struct Mesh *existing_mesh, double time, const char **r_err_str)
int ABC_get_version()
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool ABC_import(struct bContext *C, const struct AlembicImportParams *params, bool as_background_job)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
void BKE_base_eval_flags(Base *base)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1104
struct CacheReader * CacheReader_open_alembic_object(struct CacheArchiveHandle *handle, struct CacheReader *reader, struct Object *object, const char *object_path, bool is_sequence)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
static CLG_LogRef LOG
Definition logging.cpp:12
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
void id_us_min(ID *id)
Definition lib_id.cc:441
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
static void report_job_duration(const ImportJobData *data)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const char * velocity_name
ArchiveReader * archive_reader
AlembicArchiveData & operator==(const AlembicArchiveData &)=delete
AlembicArchiveData(const AlembicArchiveData &)=delete
Vector< std::string > paths
Definition ABC_alembic.h:72
eCacheFileLayer_Flag flag
struct CacheFileLayer * next
ListBaseT< CacheObjectPath > object_paths
wmWindowManager * wm
Vector< AbcObjectReader * > readers
timeit::TimePoint start_time
Vector< std::string > paths
Vector< ArchiveReader * > archives
ImportSettings settings
struct Collection * collection
struct Object * parent
eObject_VisibilityFlag visibility_flag
struct RenderData r
Alembic::Abc::chrono_t max_time
Alembic::Abc::chrono_t min_time