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