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