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