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