Cleanup: rename BKE_libblock_free_us to BKE_id_free_us.
[blender.git] / source / blender / alembic / intern / alembic_capi.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "../ABC_alembic.h"
24 #include <boost/foreach.hpp>
25
26 #include <Alembic/AbcMaterial/IMaterial.h>
27
28 #include "abc_archive.h"
29 #include "abc_camera.h"
30 #include "abc_curves.h"
31 #include "abc_hair.h"
32 #include "abc_mesh.h"
33 #include "abc_nurbs.h"
34 #include "abc_points.h"
35 #include "abc_transform.h"
36 #include "abc_util.h"
37
38 extern "C" {
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_cachefile_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "BKE_cachefile.h"
48 #include "BKE_cdderivedmesh.h"
49 #include "BKE_context.h"
50 #include "BKE_curve.h"
51 #include "BKE_global.h"
52 #include "BKE_layer.h"
53 #include "BKE_library.h"
54 #include "BKE_scene.h"
55
56 #include "DEG_depsgraph.h"
57 #include "DEG_depsgraph_build.h"
58
59 /* SpaceType struct has a member called 'new' which obviously conflicts with C++
60  * so temporarily redefining the new keyword to make it compile. */
61 #define new extern_new
62 #include "BKE_screen.h"
63 #undef new
64
65 #include "BLI_fileops.h"
66 #include "BLI_ghash.h"
67 #include "BLI_listbase.h"
68 #include "BLI_math.h"
69 #include "BLI_path_util.h"
70 #include "BLI_string.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74 }
75
76 using Alembic::Abc::Int32ArraySamplePtr;
77 using Alembic::Abc::ObjectHeader;
78
79 using Alembic::AbcGeom::MetaData;
80 using Alembic::AbcGeom::P3fArraySamplePtr;
81 using Alembic::AbcGeom::kWrapExisting;
82
83 using Alembic::AbcGeom::ICamera;
84 using Alembic::AbcGeom::ICurves;
85 using Alembic::AbcGeom::ICurvesSchema;
86 using Alembic::AbcGeom::IFaceSet;
87 using Alembic::AbcGeom::ILight;
88 using Alembic::AbcGeom::INuPatch;
89 using Alembic::AbcGeom::IObject;
90 using Alembic::AbcGeom::IPoints;
91 using Alembic::AbcGeom::IPointsSchema;
92 using Alembic::AbcGeom::IPolyMesh;
93 using Alembic::AbcGeom::IPolyMeshSchema;
94 using Alembic::AbcGeom::ISampleSelector;
95 using Alembic::AbcGeom::ISubD;
96 using Alembic::AbcGeom::IV2fGeomParam;
97 using Alembic::AbcGeom::IXform;
98 using Alembic::AbcGeom::IXformSchema;
99 using Alembic::AbcGeom::N3fArraySamplePtr;
100 using Alembic::AbcGeom::XformSample;
101 using Alembic::AbcGeom::ICompoundProperty;
102 using Alembic::AbcGeom::IN3fArrayProperty;
103 using Alembic::AbcGeom::IN3fGeomParam;
104 using Alembic::AbcGeom::V3fArraySamplePtr;
105
106 using Alembic::AbcMaterial::IMaterial;
107
108 struct AbcArchiveHandle {
109         int unused;
110 };
111
112 ABC_INLINE ArchiveReader *archive_from_handle(AbcArchiveHandle *handle)
113 {
114         return reinterpret_cast<ArchiveReader *>(handle);
115 }
116
117 ABC_INLINE AbcArchiveHandle *handle_from_archive(ArchiveReader *archive)
118 {
119         return reinterpret_cast<AbcArchiveHandle *>(archive);
120 }
121
122 //#define USE_NURBS
123
124 /* NOTE: this function is similar to visit_objects below, need to keep them in
125  * sync. */
126 static bool gather_objects_paths(const IObject &object, ListBase *object_paths)
127 {
128         if (!object.valid()) {
129                 return false;
130         }
131
132
133         size_t children_claiming_this_object = 0;
134         size_t num_children = object.getNumChildren();
135
136         for (size_t i = 0; i < num_children; ++i) {
137                 bool child_claims_this_object = gather_objects_paths(object.getChild(i), object_paths);
138                 children_claiming_this_object += child_claims_this_object ? 1 : 0;
139         }
140
141         const MetaData &md = object.getMetaData();
142         bool get_path = false;
143         bool parent_is_part_of_this_object = false;
144
145         if (!object.getParent()) {
146                 /* The root itself is not an object we should import. */
147         }
148         else if (IXform::matches(md)) {
149                 if (has_property(object.getProperties(), "locator")) {
150                         get_path = true;
151                 }
152                 else {
153                         get_path = children_claiming_this_object == 0;
154                 }
155
156                 /* Transforms are never "data" for their parent. */
157                 parent_is_part_of_this_object = false;
158         }
159         else {
160                 /* These types are "data" for their parent. */
161                 get_path =
162                         IPolyMesh::matches(md) ||
163                         ISubD::matches(md) ||
164 #ifdef USE_NURBS
165                         INuPatch::matches(md) ||
166 #endif
167                         ICamera::matches(md) ||
168                         IPoints::matches(md) ||
169                         ICurves::matches(md);
170                 parent_is_part_of_this_object = get_path;
171         }
172
173         if (get_path) {
174                 void *abc_path_void = MEM_callocN(sizeof(AlembicObjectPath), "AlembicObjectPath");
175                 AlembicObjectPath *abc_path = static_cast<AlembicObjectPath *>(abc_path_void);
176
177                 BLI_strncpy(abc_path->path, object.getFullName().c_str(), sizeof(abc_path->path));
178                 BLI_addtail(object_paths, abc_path);
179         }
180
181         return parent_is_part_of_this_object;
182 }
183
184 AbcArchiveHandle *ABC_create_handle(const char *filename, ListBase *object_paths)
185 {
186         ArchiveReader *archive = new ArchiveReader(filename);
187
188         if (!archive->valid()) {
189                 delete archive;
190                 return NULL;
191         }
192
193         if (object_paths) {
194                 gather_objects_paths(archive->getTop(), object_paths);
195         }
196
197         return handle_from_archive(archive);
198 }
199
200 void ABC_free_handle(AbcArchiveHandle *handle)
201 {
202         delete archive_from_handle(handle);
203 }
204
205 int ABC_get_version()
206 {
207         return ALEMBIC_LIBRARY_VERSION;
208 }
209
210 static void find_iobject(const IObject &object, IObject &ret,
211                          const std::string &path)
212 {
213         if (!object.valid()) {
214                 return;
215         }
216
217         std::vector<std::string> tokens;
218         split(path, '/', tokens);
219
220         IObject tmp = object;
221
222         std::vector<std::string>::iterator iter;
223         for (iter = tokens.begin(); iter != tokens.end(); ++iter) {
224                 IObject child = tmp.getChild(*iter);
225                 tmp = child;
226         }
227
228         ret = tmp;
229 }
230
231 struct ExportJobData {
232         ViewLayer *view_layer;
233         Main *bmain;
234
235         char filename[1024];
236         ExportSettings settings;
237
238         short *stop;
239         short *do_update;
240         float *progress;
241
242         bool was_canceled;
243         bool export_ok;
244 };
245
246 static void export_startjob(void *customdata, short *stop, short *do_update, float *progress)
247 {
248         ExportJobData *data = static_cast<ExportJobData *>(customdata);
249
250         data->stop = stop;
251         data->do_update = do_update;
252         data->progress = progress;
253
254         /* XXX annoying hack: needed to prevent data corruption when changing
255          * scene frame in separate threads
256          */
257         G.is_rendering = true;
258         BKE_spacedata_draw_locks(true);
259
260         G.is_break = false;
261
262         DEG_graph_build_from_view_layer(data->settings.depsgraph,
263                                         data->bmain,
264                                         data->settings.scene,
265                                         data->view_layer);
266         BKE_scene_graph_update_tagged(data->settings.depsgraph, data->bmain);
267
268         try {
269                 AbcExporter exporter(data->bmain, data->filename, data->settings);
270
271                 Scene *scene = data->settings.scene; /* for the CFRA macro */
272                 const int orig_frame = CFRA;
273
274                 data->was_canceled = false;
275                 exporter(*data->progress, data->was_canceled);
276
277                 if (CFRA != orig_frame) {
278                         CFRA = orig_frame;
279
280                         BKE_scene_graph_update_for_newframe(data->settings.depsgraph, data->bmain);
281                 }
282
283                 data->export_ok = !data->was_canceled;
284         }
285         catch (const std::exception &e) {
286                 ABC_LOG(data->settings.logger) << "Abc Export error: " << e.what() << '\n';
287         }
288         catch (...) {
289                 ABC_LOG(data->settings.logger) << "Abc Export: unknown error...\n";
290         }
291 }
292
293 static void export_endjob(void *customdata)
294 {
295         ExportJobData *data = static_cast<ExportJobData *>(customdata);
296
297         if (data->was_canceled && BLI_exists(data->filename)) {
298                 BLI_delete(data->filename, false, false);
299         }
300
301         if (!data->settings.logger.empty()) {
302                 std::cerr << data->settings.logger;
303                 WM_report(RPT_ERROR, "Errors occurred during the export, look in the console to know more...");
304         }
305
306         G.is_rendering = false;
307         BKE_spacedata_draw_locks(false);
308 }
309
310 bool ABC_export(
311         Scene *scene,
312         bContext *C,
313         const char *filepath,
314         const struct AlembicExportParams *params,
315         bool as_background_job)
316 {
317         ExportJobData *job = static_cast<ExportJobData *>(MEM_mallocN(sizeof(ExportJobData), "ExportJobData"));
318
319         job->view_layer = CTX_data_view_layer(C);
320         job->bmain = CTX_data_main(C);
321         job->export_ok = false;
322         BLI_strncpy(job->filename, filepath, 1024);
323
324         /* Alright, alright, alright....
325          *
326          * ExportJobData contains an ExportSettings containing a SimpleLogger.
327          *
328          * Since ExportJobData is a C-style struct dynamically allocated with
329          * MEM_mallocN (see above), its constructor is never called, therefore the
330          * ExportSettings constructor is not called which implies that the
331          * SimpleLogger one is not called either. SimpleLogger in turn does not call
332          * the constructor of its data members which ultimately means that its
333          * std::ostringstream member has a NULL pointer. To be able to properly use
334          * the stream's operator<<, the pointer needs to be set, therefore we have
335          * to properly construct everything. And this is done using the placement
336          * new operator as here below. It seems hackish, but I'm too lazy to
337          * do bigger refactor and maybe there is a better way which does not involve
338          * hardcore refactoring. */
339         new (&job->settings) ExportSettings();
340         job->settings.scene = scene;
341         job->settings.depsgraph = DEG_graph_new(scene, job->view_layer, DAG_EVAL_RENDER);
342
343         /* Sybren: for now we only export the active scene layer.
344          * Later in the 2.8 development process this may be replaced by using
345          * a specific collection for Alembic I/O, which can then be toggled
346          * between "real" objects and cached Alembic files. */
347         job->settings.view_layer = job->view_layer;
348
349         job->settings.frame_start = params->frame_start;
350         job->settings.frame_end = params->frame_end;
351         job->settings.frame_samples_xform = params->frame_samples_xform;
352         job->settings.frame_samples_shape = params->frame_samples_shape;
353         job->settings.shutter_open = params->shutter_open;
354         job->settings.shutter_close = params->shutter_close;
355
356         /* Sybren: For now this is ignored, until we can get selection
357          * detection working through Base pointers (instead of ob->flags). */
358         job->settings.selected_only = params->selected_only;
359
360         job->settings.export_face_sets = params->face_sets;
361         job->settings.export_normals = params->normals;
362         job->settings.export_uvs = params->uvs;
363         job->settings.export_vcols = params->vcolors;
364         job->settings.export_hair = params->export_hair;
365         job->settings.export_particles = params->export_particles;
366         job->settings.apply_subdiv = params->apply_subdiv;
367         job->settings.flatten_hierarchy = params->flatten_hierarchy;
368
369         /* Sybren: visible_layer & renderable only is ignored for now,
370          * to be replaced with collections later in the 2.8 dev process
371          * (also see note above). */
372         job->settings.visible_layers_only = params->visible_layers_only;
373         job->settings.renderable_only = params->renderable_only;
374
375         job->settings.use_subdiv_schema = params->use_subdiv_schema;
376         job->settings.export_ogawa = (params->compression_type == ABC_ARCHIVE_OGAWA);
377         job->settings.pack_uv = params->packuv;
378         job->settings.global_scale = params->global_scale;
379         job->settings.triangulate = params->triangulate;
380         job->settings.quad_method = params->quad_method;
381         job->settings.ngon_method = params->ngon_method;
382
383         if (job->settings.frame_start > job->settings.frame_end) {
384                 std::swap(job->settings.frame_start, job->settings.frame_end);
385         }
386
387         bool export_ok = false;
388         if (as_background_job) {
389                 wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
390                                             CTX_wm_window(C),
391                                             job->settings.scene,
392                                             "Alembic Export",
393                                             WM_JOB_PROGRESS,
394                                             WM_JOB_TYPE_ALEMBIC);
395
396                 /* setup job */
397                 WM_jobs_customdata_set(wm_job, job, MEM_freeN);
398                 WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
399                 WM_jobs_callbacks(wm_job, export_startjob, NULL, NULL, export_endjob);
400
401                 WM_jobs_start(CTX_wm_manager(C), wm_job);
402         }
403         else {
404                 /* Fake a job context, so that we don't need NULL pointer checks while exporting. */
405                 short stop = 0, do_update = 0;
406                 float progress = 0.f;
407
408                 export_startjob(job, &stop, &do_update, &progress);
409                 export_endjob(job);
410                 export_ok = job->export_ok;
411
412                 MEM_freeN(job);
413         }
414
415         return export_ok;
416 }
417
418 /* ********************** Import file ********************** */
419
420 /**
421  * Generates an AbcObjectReader for this Alembic object and its children.
422  *
423  * \param object: The Alembic IObject to visit.
424  * \param readers: The created AbcObjectReader * will be appended to this vector.
425  * \param settings: Import settings, not used directly but passed to the
426  *                 AbcObjectReader subclass constructors.
427  * \param r_assign_as_parent: Return parameter, contains a list of reader
428  *                 pointers, whose parent pointer should still be set.
429  *                 This is filled when this call to visit_object() didn't create
430  *                 a reader that should be the parent.
431  * \return A pair of boolean and reader pointer. The boolean indicates whether
432  *         this IObject claims its parent as part of the same object
433  *         (for example an IPolyMesh object would claim its parent, as the mesh
434  *         is interpreted as the object's data, and the parent IXform as its
435  *         Blender object). The pointer is the AbcObjectReader that represents
436  *         the IObject parameter.
437  *
438  * NOTE: this function is similar to gather_object_paths above, need to keep
439  * them in sync. */
440 static std::pair<bool, AbcObjectReader *> visit_object(
441         const IObject &object,
442         AbcObjectReader::ptr_vector &readers,
443         ImportSettings &settings,
444         AbcObjectReader::ptr_vector &r_assign_as_parent)
445 {
446         const std::string & full_name = object.getFullName();
447
448         if (!object.valid()) {
449                 std::cerr << "  - "
450                           << full_name
451                           << ": object is invalid, skipping it and all its children.\n";
452                 return std::make_pair(false, static_cast<AbcObjectReader *>(NULL));
453         }
454
455         /* The interpretation of data by the children determine the role of this
456          * object. This is especially important for Xform objects, as they can be
457          * either part of a Blender object or a Blender object (Empty) themselves.
458          */
459         size_t children_claiming_this_object = 0;
460         size_t num_children = object.getNumChildren();
461         AbcObjectReader::ptr_vector claiming_child_readers;
462         AbcObjectReader::ptr_vector nonclaiming_child_readers;
463         AbcObjectReader::ptr_vector assign_as_parent;
464         for (size_t i = 0; i < num_children; ++i) {
465                 const IObject ichild = object.getChild(i);
466
467                 /* TODO: When we only support C++11, use std::tie() instead. */
468                 std::pair<bool, AbcObjectReader *> child_result;
469                 child_result = visit_object(ichild, readers, settings, assign_as_parent);
470
471                 bool child_claims_this_object = child_result.first;
472                 AbcObjectReader *child_reader = child_result.second;
473
474                 if (child_reader == NULL) {
475                         BLI_assert(!child_claims_this_object);
476                 }
477                 else {
478                         if (child_claims_this_object) {
479                                 claiming_child_readers.push_back(child_reader);
480                         }
481                         else {
482                                 nonclaiming_child_readers.push_back(child_reader);
483                         }
484                 }
485
486                 children_claiming_this_object += child_claims_this_object ? 1 : 0;
487         }
488         BLI_assert(children_claiming_this_object == claiming_child_readers.size());
489
490         AbcObjectReader *reader = NULL;
491         const MetaData &md = object.getMetaData();
492         bool parent_is_part_of_this_object = false;
493
494         if (!object.getParent()) {
495                 /* The root itself is not an object we should import. */
496         }
497         else if (IXform::matches(md)) {
498                 bool create_empty;
499
500                 /* An xform can either be a Blender Object (if it contains a mesh, for
501                  * example), but it can also be an Empty. Its correct translation to
502                  * Blender's data model depends on its children. */
503
504                 /* Check whether or not this object is a Maya locator, which is
505                  * similar to empties used as parent object in Blender. */
506                 if (has_property(object.getProperties(), "locator")) {
507                         create_empty = true;
508                 }
509                 else {
510                         create_empty = claiming_child_readers.empty();
511                 }
512
513                 if (create_empty) {
514                         reader = new AbcEmptyReader(object, settings);
515                 }
516         }
517         else if (IPolyMesh::matches(md)) {
518                 reader = new AbcMeshReader(object, settings);
519                 parent_is_part_of_this_object = true;
520         }
521         else if (ISubD::matches(md)) {
522                 reader = new AbcSubDReader(object, settings);
523                 parent_is_part_of_this_object = true;
524         }
525         else if (INuPatch::matches(md)) {
526 #ifdef USE_NURBS
527                 /* TODO(kevin): importing cyclic NURBS from other software crashes
528                  * at the moment. This is due to the fact that NURBS in other
529                  * software have duplicated points which causes buffer overflows in
530                  * Blender. Need to figure out exactly how these points are
531                  * duplicated, in all cases (cyclic U, cyclic V, and cyclic UV).
532                  * Until this is fixed, disabling NURBS reading. */
533                 reader = new AbcNurbsReader(object, settings);
534                 parent_is_part_of_this_object = true;
535 #endif
536         }
537         else if (ICamera::matches(md)) {
538                 reader = new AbcCameraReader(object, settings);
539                 parent_is_part_of_this_object = true;
540         }
541         else if (IPoints::matches(md)) {
542                 reader = new AbcPointsReader(object, settings);
543                 parent_is_part_of_this_object = true;
544         }
545         else if (IMaterial::matches(md)) {
546                 /* Pass for now. */
547         }
548         else if (ILight::matches(md)) {
549                 /* Pass for now. */
550         }
551         else if (IFaceSet::matches(md)) {
552                 /* Pass, those are handled in the mesh reader. */
553         }
554         else if (ICurves::matches(md)) {
555                 reader = new AbcCurveReader(object, settings);
556                 parent_is_part_of_this_object = true;
557         }
558         else {
559                 std::cerr << "Alembic object " << full_name
560                           << " is of unsupported schema type '"
561                           << object.getMetaData().get("schemaObjTitle") << "'"
562                           << std::endl;
563         }
564
565         if (reader) {
566                 /* We have created a reader, which should imply that this object is
567                  * not claimed as part of any child Alembic object. */
568                 BLI_assert(claiming_child_readers.empty());
569
570                 readers.push_back(reader);
571                 reader->incref();
572
573                 AlembicObjectPath *abc_path = static_cast<AlembicObjectPath *>(
574                                                   MEM_callocN(sizeof(AlembicObjectPath), "AlembicObjectPath"));
575                 BLI_strncpy(abc_path->path, full_name.c_str(), sizeof(abc_path->path));
576                 BLI_addtail(&settings.cache_file->object_paths, abc_path);
577
578                 /* We can now assign this reader as parent for our children. */
579                 if (nonclaiming_child_readers.size() + assign_as_parent.size() > 0) {
580                         /* TODO: When we only support C++11, use for (a: b) instead. */
581                         BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
582                                 child_reader->parent_reader = reader;
583                         }
584                         BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
585                                 child_reader->parent_reader = reader;
586                         }
587                 }
588         }
589         else if (object.getParent()) {
590                 if (claiming_child_readers.size() > 0) {
591                         /* The first claiming child will serve just fine as parent to
592                          * our non-claiming children. Since all claiming children share
593                          * the same XForm, it doesn't really matter which one we pick. */
594                         AbcObjectReader *claiming_child = claiming_child_readers[0];
595                         BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
596                                 child_reader->parent_reader = claiming_child;
597                         }
598                         BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
599                                 child_reader->parent_reader = claiming_child;
600                         }
601                         /* Claiming children should have our parent set as their parent. */
602                         BOOST_FOREACH(AbcObjectReader *child_reader, claiming_child_readers) {
603                                 r_assign_as_parent.push_back(child_reader);
604                         }
605                 }
606                 else {
607                         /* This object isn't claimed by any child, and didn't produce
608                          * a reader. Odd situation, could be the top Alembic object, or
609                          * an unsupported Alembic schema. Delegate to our parent. */
610                         BOOST_FOREACH(AbcObjectReader *child_reader, claiming_child_readers) {
611                                 r_assign_as_parent.push_back(child_reader);
612                         }
613                         BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
614                                 r_assign_as_parent.push_back(child_reader);
615                         }
616                         BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
617                                 r_assign_as_parent.push_back(child_reader);
618                         }
619                 }
620         }
621
622         return std::make_pair(parent_is_part_of_this_object, reader);
623 }
624
625 enum {
626         ABC_NO_ERROR = 0,
627         ABC_ARCHIVE_FAIL,
628         ABC_UNSUPPORTED_HDF5,
629 };
630
631 struct ImportJobData {
632         Main *bmain;
633         Scene *scene;
634         ViewLayer *view_layer;
635         wmWindowManager *wm;
636
637         char filename[1024];
638         ImportSettings settings;
639
640         std::vector<AbcObjectReader *> readers;
641
642         short *stop;
643         short *do_update;
644         float *progress;
645
646         char error_code;
647         bool was_cancelled;
648         bool import_ok;
649 };
650
651 static void import_startjob(void *user_data, short *stop, short *do_update, float *progress)
652 {
653         SCOPE_TIMER("Alembic import, objects reading and creation");
654
655         ImportJobData *data = static_cast<ImportJobData *>(user_data);
656
657         data->stop = stop;
658         data->do_update = do_update;
659         data->progress = progress;
660
661         WM_set_locked_interface(data->wm, true);
662
663         ArchiveReader *archive = new ArchiveReader(data->filename);
664
665         if (!archive->valid()) {
666 #ifndef WITH_ALEMBIC_HDF5
667                 data->error_code = archive->is_hdf5() ? ABC_UNSUPPORTED_HDF5 : ABC_ARCHIVE_FAIL;
668 #else
669                 data->error_code = ABC_ARCHIVE_FAIL;
670 #endif
671                 delete archive;
672                 return;
673         }
674
675         CacheFile *cache_file = static_cast<CacheFile *>(BKE_cachefile_add(data->bmain, BLI_path_basename(data->filename)));
676
677         /* Decrement the ID ref-count because it is going to be incremented for each
678          * modifier and constraint that it will be attached to, so since currently
679          * it is not used by anyone, its use count will off by one. */
680         id_us_min(&cache_file->id);
681
682         cache_file->is_sequence = data->settings.is_sequence;
683         cache_file->scale = data->settings.scale;
684         cache_file->handle = handle_from_archive(archive);
685         BLI_strncpy(cache_file->filepath, data->filename, 1024);
686
687         data->settings.cache_file = cache_file;
688
689         *data->do_update = true;
690         *data->progress = 0.05f;
691
692         /* Parse Alembic Archive. */
693         AbcObjectReader::ptr_vector assign_as_parent;
694         visit_object(archive->getTop(), data->readers, data->settings, assign_as_parent);
695
696         /* There shouldn't be any orphans. */
697         BLI_assert(assign_as_parent.size() == 0);
698
699         if (G.is_break) {
700                 data->was_cancelled = true;
701                 return;
702         }
703
704         *data->do_update = true;
705         *data->progress = 0.1f;
706
707         /* Create objects and set scene frame range. */
708
709         const float size = static_cast<float>(data->readers.size());
710         size_t i = 0;
711
712         chrono_t min_time = std::numeric_limits<chrono_t>::max();
713         chrono_t max_time = std::numeric_limits<chrono_t>::min();
714
715         ISampleSelector sample_sel(0.0f);
716         std::vector<AbcObjectReader *>::iterator iter;
717         for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
718                 AbcObjectReader *reader = *iter;
719
720                 if (reader->valid()) {
721                         reader->readObjectData(data->bmain, sample_sel);
722
723                         min_time = std::min(min_time, reader->minTime());
724                         max_time = std::max(max_time, reader->maxTime());
725                 }
726                 else {
727                         std::cerr << "Object " << reader->name() << " in Alembic file "
728                                   << data->filename << " is invalid.\n";
729                 }
730
731                 *data->progress = 0.1f + 0.3f * (++i / size);
732                 *data->do_update = true;
733
734                 if (G.is_break) {
735                         data->was_cancelled = true;
736                         return;
737                 }
738         }
739
740         if (data->settings.set_frame_range) {
741                 Scene *scene = data->scene;
742
743                 if (data->settings.is_sequence) {
744                         SFRA = data->settings.sequence_offset;
745                         EFRA = SFRA + (data->settings.sequence_len - 1);
746                         CFRA = SFRA;
747                 }
748                 else if (min_time < max_time) {
749                         SFRA = static_cast<int>(round(min_time * FPS));
750                         EFRA = static_cast<int>(round(max_time * FPS));
751                         CFRA = SFRA;
752                 }
753         }
754
755         /* Setup parenthood. */
756         for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
757                 const AbcObjectReader *reader = *iter;
758                 const AbcObjectReader *parent_reader = reader->parent_reader;
759                 Object *ob = reader->object();
760
761                 if (parent_reader == NULL || !reader->inherits_xform()) {
762                         ob->parent = NULL;
763                 }
764                 else {
765                         ob->parent = parent_reader->object();
766                 }
767         }
768
769         /* Setup transformations and constraints. */
770         i = 0;
771         for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
772                 AbcObjectReader *reader = *iter;
773                 reader->setupObjectTransform(0.0f);
774
775                 *data->progress = 0.7f + 0.3f * (++i / size);
776                 *data->do_update = true;
777
778                 if (G.is_break) {
779                         data->was_cancelled = true;
780                         return;
781                 }
782         }
783 }
784
785 static void import_endjob(void *user_data)
786 {
787         SCOPE_TIMER("Alembic import, cleanup");
788
789         ImportJobData *data = static_cast<ImportJobData *>(user_data);
790
791         std::vector<AbcObjectReader *>::iterator iter;
792
793         /* Delete objects on cancelation. */
794         if (data->was_cancelled) {
795                 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
796                         Object *ob = (*iter)->object();
797
798                         /* It's possible that cancellation occurred between the creation of
799                          * the reader and the creation of the Blender object. */
800                         if (ob == NULL) continue;
801
802                         BKE_id_free_us(data->bmain, ob);
803                 }
804         }
805         else {
806                 /* Add object to scene. */
807                 Base *base;
808                 LayerCollection *lc;
809                 ViewLayer *view_layer = data->view_layer;
810
811                 BKE_view_layer_base_deselect_all(view_layer);
812
813                 lc = BKE_layer_collection_get_active(view_layer);
814
815                 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
816                         Object *ob = (*iter)->object();
817
818                         BKE_collection_object_add(data->bmain, lc->collection, ob);
819
820                         base = BKE_view_layer_base_find(view_layer, ob);
821                         /* TODO: is setting active needed? */
822                         BKE_view_layer_base_select_and_set_active(view_layer, base);
823
824                         DEG_id_tag_update(&lc->collection->id, ID_RECALC_COPY_ON_WRITE);
825                         DEG_id_tag_update_ex(data->bmain, &ob->id,
826                                              ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION | ID_RECALC_BASE_FLAGS);
827                 }
828
829                 DEG_id_tag_update(&data->scene->id, ID_RECALC_BASE_FLAGS);
830                 DEG_relations_tag_update(data->bmain);
831         }
832
833         for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
834                 AbcObjectReader *reader = *iter;
835                 reader->decref();
836
837                 if (reader->refcount() == 0) {
838                         delete reader;
839                 }
840         }
841
842         WM_set_locked_interface(data->wm, false);
843
844         switch (data->error_code) {
845                 default:
846                 case ABC_NO_ERROR:
847                         data->import_ok = !data->was_cancelled;
848                         break;
849                 case ABC_ARCHIVE_FAIL:
850                         WM_report(RPT_ERROR, "Could not open Alembic archive for reading! See console for detail.");
851                         break;
852                 case ABC_UNSUPPORTED_HDF5:
853                         WM_report(RPT_ERROR, "Alembic archive in obsolete HDF5 format is not supported.");
854                         break;
855         }
856
857         WM_main_add_notifier(NC_SCENE | ND_FRAME, data->scene);
858 }
859
860 static void import_freejob(void *user_data)
861 {
862         ImportJobData *data = static_cast<ImportJobData *>(user_data);
863         delete data;
864 }
865
866 bool ABC_import(bContext *C, const char *filepath, float scale, bool is_sequence,
867                 bool set_frame_range, int sequence_len, int offset,
868                 bool validate_meshes, bool as_background_job)
869 {
870         /* Using new here since MEM_* funcs do not call ctor to properly initialize
871          * data. */
872         ImportJobData *job = new ImportJobData();
873         job->bmain = CTX_data_main(C);
874         job->scene = CTX_data_scene(C);
875         job->view_layer = CTX_data_view_layer(C);
876         job->wm = CTX_wm_manager(C);
877         job->import_ok = false;
878         BLI_strncpy(job->filename, filepath, 1024);
879
880         job->settings.scale = scale;
881         job->settings.is_sequence = is_sequence;
882         job->settings.set_frame_range = set_frame_range;
883         job->settings.sequence_len = sequence_len;
884         job->settings.sequence_offset = offset;
885         job->settings.validate_meshes = validate_meshes;
886         job->error_code = ABC_NO_ERROR;
887         job->was_cancelled = false;
888
889         G.is_break = false;
890
891         bool import_ok = false;
892         if (as_background_job) {
893                 wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
894                                             CTX_wm_window(C),
895                                             job->scene,
896                                             "Alembic Import",
897                                             WM_JOB_PROGRESS,
898                                             WM_JOB_TYPE_ALEMBIC);
899
900                 /* setup job */
901                 WM_jobs_customdata_set(wm_job, job, import_freejob);
902                 WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
903                 WM_jobs_callbacks(wm_job, import_startjob, NULL, NULL, import_endjob);
904
905                 WM_jobs_start(CTX_wm_manager(C), wm_job);
906         }
907         else {
908                 /* Fake a job context, so that we don't need NULL pointer checks while importing. */
909                 short stop = 0, do_update = 0;
910                 float progress = 0.f;
911
912                 import_startjob(job, &stop, &do_update, &progress);
913                 import_endjob(job);
914                 import_ok = job->import_ok;
915
916                 import_freejob(job);
917         }
918
919         return import_ok;
920 }
921
922 /* ************************************************************************** */
923
924 void ABC_get_transform(CacheReader *reader, float r_mat[4][4], float time, float scale)
925 {
926         if (!reader) {
927                 return;
928         }
929
930         AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
931
932         bool is_constant = false;
933         abc_reader->read_matrix(r_mat, time, scale, is_constant);
934 }
935
936 /* ************************************************************************** */
937
938 Mesh *ABC_read_mesh(CacheReader *reader,
939                     Object *ob,
940                     Mesh *existing_mesh,
941                     const float time,
942                     const char **err_str,
943                     int read_flag)
944 {
945         AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
946         IObject iobject = abc_reader->iobject();
947
948         if (!iobject.valid()) {
949                 *err_str = "Invalid object: verify object path";
950                 return NULL;
951         }
952
953         const ObjectHeader &header = iobject.getHeader();
954         if (!abc_reader->accepts_object_type(header, ob, err_str)) {
955                 /* err_str is set by acceptsObjectType() */
956                 return NULL;
957         }
958
959         /* kFloorIndex is used to be compatible with non-interpolating
960          * properties; they use the floor. */
961         ISampleSelector sample_sel(time, ISampleSelector::kFloorIndex);
962         return abc_reader->read_mesh(existing_mesh, sample_sel, read_flag, err_str);
963 }
964
965 /* ************************************************************************** */
966
967 void CacheReader_free(CacheReader *reader)
968 {
969         AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
970         abc_reader->decref();
971
972         if (abc_reader->refcount() == 0) {
973                 delete abc_reader;
974         }
975 }
976
977 void CacheReader_incref(CacheReader *reader)
978 {
979         AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
980         abc_reader->incref();
981 }
982
983 CacheReader *CacheReader_open_alembic_object(AbcArchiveHandle *handle, CacheReader *reader, Object *object, const char *object_path)
984 {
985         if (object_path[0] == '\0') {
986                 return reader;
987         }
988
989         ArchiveReader *archive = archive_from_handle(handle);
990
991         if (!archive || !archive->valid()) {
992                 return reader;
993         }
994
995         IObject iobject;
996         find_iobject(archive->getTop(), iobject, object_path);
997
998         if (reader) {
999                 CacheReader_free(reader);
1000         }
1001
1002         ImportSettings settings;
1003         AbcObjectReader *abc_reader = create_reader(iobject, settings);
1004         if (abc_reader == NULL) {
1005                 /* This object is not supported */
1006                 return NULL;
1007         }
1008         abc_reader->object(object);
1009         abc_reader->incref();
1010
1011         return reinterpret_cast<CacheReader *>(abc_reader);
1012 }