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