2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
20 * ***** END GPL LICENSE BLOCK *****
23 #include "../ABC_alembic.h"
24 #include <boost/foreach.hpp>
26 #include <Alembic/AbcMaterial/IMaterial.h>
28 #include "abc_archive.h"
29 #include "abc_camera.h"
30 #include "abc_curves.h"
33 #include "abc_nurbs.h"
34 #include "abc_points.h"
35 #include "abc_transform.h"
39 #include "MEM_guardedalloc.h"
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"
47 #include "BKE_cachefile.h"
48 #include "BKE_cdderivedmesh.h"
49 #include "BKE_context.h"
50 #include "BKE_curve.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_global.h"
53 #include "BKE_library.h"
55 #include "BKE_scene.h"
57 /* SpaceType struct has a member called 'new' which obviously conflicts with C++
58 * so temporarily redefining the new keyword to make it compile. */
59 #define new extern_new
60 #include "BKE_screen.h"
63 #include "BLI_fileops.h"
64 #include "BLI_ghash.h"
65 #include "BLI_listbase.h"
67 #include "BLI_path_util.h"
68 #include "BLI_string.h"
74 using Alembic::Abc::Int32ArraySamplePtr;
75 using Alembic::Abc::ObjectHeader;
77 using Alembic::AbcGeom::MetaData;
78 using Alembic::AbcGeom::P3fArraySamplePtr;
79 using Alembic::AbcGeom::kWrapExisting;
81 using Alembic::AbcGeom::ICamera;
82 using Alembic::AbcGeom::ICurves;
83 using Alembic::AbcGeom::ICurvesSchema;
84 using Alembic::AbcGeom::IFaceSet;
85 using Alembic::AbcGeom::ILight;
86 using Alembic::AbcGeom::INuPatch;
87 using Alembic::AbcGeom::IObject;
88 using Alembic::AbcGeom::IPoints;
89 using Alembic::AbcGeom::IPointsSchema;
90 using Alembic::AbcGeom::IPolyMesh;
91 using Alembic::AbcGeom::IPolyMeshSchema;
92 using Alembic::AbcGeom::ISampleSelector;
93 using Alembic::AbcGeom::ISubD;
94 using Alembic::AbcGeom::IV2fGeomParam;
95 using Alembic::AbcGeom::IXform;
96 using Alembic::AbcGeom::IXformSchema;
97 using Alembic::AbcGeom::N3fArraySamplePtr;
98 using Alembic::AbcGeom::XformSample;
99 using Alembic::AbcGeom::ICompoundProperty;
100 using Alembic::AbcGeom::IN3fArrayProperty;
101 using Alembic::AbcGeom::IN3fGeomParam;
102 using Alembic::AbcGeom::V3fArraySamplePtr;
104 using Alembic::AbcMaterial::IMaterial;
106 struct AbcArchiveHandle {
110 ABC_INLINE ArchiveReader *archive_from_handle(AbcArchiveHandle *handle)
112 return reinterpret_cast<ArchiveReader *>(handle);
115 ABC_INLINE AbcArchiveHandle *handle_from_archive(ArchiveReader *archive)
117 return reinterpret_cast<AbcArchiveHandle *>(archive);
122 /* NOTE: this function is similar to visit_objects below, need to keep them in
124 static bool gather_objects_paths(const IObject &object, ListBase *object_paths)
126 if (!object.valid()) {
131 size_t children_claiming_this_object = 0;
132 size_t num_children = object.getNumChildren();
134 for (size_t i = 0; i < num_children; ++i) {
135 bool child_claims_this_object = gather_objects_paths(object.getChild(i), object_paths);
136 children_claiming_this_object += child_claims_this_object ? 1 : 0;
139 const MetaData &md = object.getMetaData();
140 bool get_path = false;
141 bool parent_is_part_of_this_object = false;
143 if (!object.getParent()) {
144 /* The root itself is not an object we should import. */
146 else if (IXform::matches(md)) {
147 if (has_property(object.getProperties(), "locator")) {
151 get_path = children_claiming_this_object == 0;
154 /* Transforms are never "data" for their parent. */
155 parent_is_part_of_this_object = false;
158 /* These types are "data" for their parent. */
160 IPolyMesh::matches(md) ||
161 ISubD::matches(md) ||
163 INuPatch::matches(md) ||
165 ICamera::matches(md) ||
166 IPoints::matches(md) ||
167 ICurves::matches(md);
168 parent_is_part_of_this_object = get_path;
172 void *abc_path_void = MEM_callocN(sizeof(AlembicObjectPath), "AlembicObjectPath");
173 AlembicObjectPath *abc_path = static_cast<AlembicObjectPath *>(abc_path_void);
175 BLI_strncpy(abc_path->path, object.getFullName().c_str(), PATH_MAX);
176 BLI_addtail(object_paths, abc_path);
179 return parent_is_part_of_this_object;
182 AbcArchiveHandle *ABC_create_handle(const char *filename, ListBase *object_paths)
184 ArchiveReader *archive = new ArchiveReader(filename);
186 if (!archive->valid()) {
192 gather_objects_paths(archive->getTop(), object_paths);
195 return handle_from_archive(archive);
198 void ABC_free_handle(AbcArchiveHandle *handle)
200 delete archive_from_handle(handle);
203 int ABC_get_version()
205 return ALEMBIC_LIBRARY_VERSION;
208 static void find_iobject(const IObject &object, IObject &ret,
209 const std::string &path)
211 if (!object.valid()) {
215 std::vector<std::string> tokens;
216 split(path, '/', tokens);
218 IObject tmp = object;
220 std::vector<std::string>::iterator iter;
221 for (iter = tokens.begin(); iter != tokens.end(); ++iter) {
222 IObject child = tmp.getChild(*iter);
229 struct ExportJobData {
234 ExportSettings settings;
244 static void export_startjob(void *customdata, short *stop, short *do_update, float *progress)
246 ExportJobData *data = static_cast<ExportJobData *>(customdata);
249 data->do_update = do_update;
250 data->progress = progress;
252 /* XXX annoying hack: needed to prevent data corruption when changing
253 * scene frame in separate threads
255 G.is_rendering = true;
256 BKE_spacedata_draw_locks(true);
261 Scene *scene = data->scene;
262 AbcExporter exporter(scene, data->filename, data->settings);
264 const int orig_frame = CFRA;
266 data->was_canceled = false;
267 exporter(data->bmain, *data->progress, data->was_canceled);
269 if (CFRA != orig_frame) {
272 BKE_scene_update_for_newframe(data->bmain->eval_ctx, data->bmain,
276 data->export_ok = !data->was_canceled;
278 catch (const std::exception &e) {
279 ABC_LOG(data->settings.logger) << "Abc Export error: " << e.what() << '\n';
282 ABC_LOG(data->settings.logger) << "Abc Export: unknown error...\n";
286 static void export_endjob(void *customdata)
288 ExportJobData *data = static_cast<ExportJobData *>(customdata);
290 if (data->was_canceled && BLI_exists(data->filename)) {
291 BLI_delete(data->filename, false, false);
294 if (!data->settings.logger.empty()) {
295 std::cerr << data->settings.logger;
296 WM_report(RPT_ERROR, "Errors occured during the export, look in the console to know more...");
299 G.is_rendering = false;
300 BKE_spacedata_draw_locks(false);
306 const char *filepath,
307 const struct AlembicExportParams *params,
308 bool as_background_job)
310 ExportJobData *job = static_cast<ExportJobData *>(MEM_mallocN(sizeof(ExportJobData), "ExportJobData"));
312 job->bmain = CTX_data_main(C);
313 job->export_ok = false;
314 BLI_strncpy(job->filename, filepath, 1024);
316 /* Alright, alright, alright....
318 * ExportJobData contains an ExportSettings containing a SimpleLogger.
320 * Since ExportJobData is a C-style struct dynamically allocated with
321 * MEM_mallocN (see above), its construtor is never called, therefore the
322 * ExportSettings constructor is not called which implies that the
323 * SimpleLogger one is not called either. SimpleLogger in turn does not call
324 * the constructor of its data members which ultimately means that its
325 * std::ostringstream member has a NULL pointer. To be able to properly use
326 * the stream's operator<<, the pointer needs to be set, therefore we have
327 * to properly construct everything. And this is done using the placement
328 * new operator as here below. It seems hackish, but I'm too lazy to
329 * do bigger refactor and maybe there is a better way which does not involve
330 * hardcore refactoring. */
331 new (&job->settings) ExportSettings();
332 job->settings.scene = job->scene;
333 job->settings.frame_start = params->frame_start;
334 job->settings.frame_end = params->frame_end;
335 job->settings.frame_step_xform = params->frame_step_xform;
336 job->settings.frame_step_shape = params->frame_step_shape;
337 job->settings.shutter_open = params->shutter_open;
338 job->settings.shutter_close = params->shutter_close;
339 job->settings.selected_only = params->selected_only;
340 job->settings.export_face_sets = params->face_sets;
341 job->settings.export_normals = params->normals;
342 job->settings.export_uvs = params->uvs;
343 job->settings.export_vcols = params->vcolors;
344 job->settings.export_hair = params->export_hair;
345 job->settings.export_particles = params->export_particles;
346 job->settings.apply_subdiv = params->apply_subdiv;
347 job->settings.flatten_hierarchy = params->flatten_hierarchy;
348 job->settings.visible_layers_only = params->visible_layers_only;
349 job->settings.renderable_only = params->renderable_only;
350 job->settings.use_subdiv_schema = params->use_subdiv_schema;
351 job->settings.export_ogawa = (params->compression_type == ABC_ARCHIVE_OGAWA);
352 job->settings.pack_uv = params->packuv;
353 job->settings.global_scale = params->global_scale;
354 job->settings.triangulate = params->triangulate;
355 job->settings.quad_method = params->quad_method;
356 job->settings.ngon_method = params->ngon_method;
358 if (job->settings.frame_start > job->settings.frame_end) {
359 std::swap(job->settings.frame_start, job->settings.frame_end);
362 if (as_background_job) {
363 wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
368 WM_JOB_TYPE_ALEMBIC);
371 WM_jobs_customdata_set(wm_job, job, MEM_freeN);
372 WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
373 WM_jobs_callbacks(wm_job, export_startjob, NULL, NULL, export_endjob);
375 WM_jobs_start(CTX_wm_manager(C), wm_job);
378 /* Fake a job context, so that we don't need NULL pointer checks while exporting. */
379 short stop = 0, do_update = 0;
380 float progress = 0.f;
382 export_startjob(job, &stop, &do_update, &progress);
386 return job->export_ok;
389 /* ********************** Import file ********************** */
392 * Generates an AbcObjectReader for this Alembic object and its children.
394 * \param object The Alembic IObject to visit.
395 * \param readers The created AbcObjectReader * will be appended to this vector.
396 * \param settings Import settings, not used directly but passed to the
397 * AbcObjectReader subclass constructors.
398 * \param r_assign_as_parent Return parameter, contains a list of reader
399 * pointers, whose parent pointer should still be set.
400 * This is filled when this call to visit_object() didn't create
401 * a reader that should be the parent.
402 * \return A pair of boolean and reader pointer. The boolean indicates whether
403 * this IObject claims its parent as part of the same object
404 * (for example an IPolyMesh object would claim its parent, as the mesh
405 * is interpreted as the object's data, and the parent IXform as its
406 * Blender object). The pointer is the AbcObjectReader that represents
407 * the IObject parameter.
409 * NOTE: this function is similar to gather_object_paths above, need to keep
411 static std::pair<bool, AbcObjectReader *> visit_object(
412 const IObject &object,
413 AbcObjectReader::ptr_vector &readers,
414 ImportSettings &settings,
415 AbcObjectReader::ptr_vector &r_assign_as_parent)
417 const std::string & full_name = object.getFullName();
419 if (!object.valid()) {
422 << ": object is invalid, skipping it and all its children.\n";
423 return std::make_pair(false, static_cast<AbcObjectReader *>(NULL));
426 /* The interpretation of data by the children determine the role of this
427 * object. This is especially important for Xform objects, as they can be
428 * either part of a Blender object or a Blender object (Empty) themselves.
430 size_t children_claiming_this_object = 0;
431 size_t num_children = object.getNumChildren();
432 AbcObjectReader::ptr_vector claiming_child_readers;
433 AbcObjectReader::ptr_vector nonclaiming_child_readers;
434 AbcObjectReader::ptr_vector assign_as_parent;
435 for (size_t i = 0; i < num_children; ++i) {
436 const IObject ichild = object.getChild(i);
438 /* TODO: When we only support C++11, use std::tie() instead. */
439 std::pair<bool, AbcObjectReader *> child_result;
440 child_result = visit_object(ichild, readers, settings, assign_as_parent);
442 bool child_claims_this_object = child_result.first;
443 AbcObjectReader *child_reader = child_result.second;
445 if (child_reader == NULL) {
446 BLI_assert(!child_claims_this_object);
449 if (child_claims_this_object) {
450 claiming_child_readers.push_back(child_reader);
452 nonclaiming_child_readers.push_back(child_reader);
456 children_claiming_this_object += child_claims_this_object ? 1 : 0;
458 BLI_assert(children_claiming_this_object == claiming_child_readers.size());
460 AbcObjectReader *reader = NULL;
461 const MetaData &md = object.getMetaData();
462 bool parent_is_part_of_this_object = false;
464 if (!object.getParent()) {
465 /* The root itself is not an object we should import. */
467 else if (IXform::matches(md)) {
470 /* An xform can either be a Blender Object (if it contains a mesh, for
471 * example), but it can also be an Empty. Its correct translation to
472 * Blender's data model depends on its children. */
474 /* Check whether or not this object is a Maya locator, which is
475 * similar to empties used as parent object in Blender. */
476 if (has_property(object.getProperties(), "locator")) {
480 create_empty = claiming_child_readers.empty();
484 reader = new AbcEmptyReader(object, settings);
487 else if (IPolyMesh::matches(md)) {
488 reader = new AbcMeshReader(object, settings);
489 parent_is_part_of_this_object = true;
491 else if (ISubD::matches(md)) {
492 reader = new AbcSubDReader(object, settings);
493 parent_is_part_of_this_object = true;
495 else if (INuPatch::matches(md)) {
497 /* TODO(kevin): importing cyclic NURBS from other software crashes
498 * at the moment. This is due to the fact that NURBS in other
499 * software have duplicated points which causes buffer overflows in
500 * Blender. Need to figure out exactly how these points are
501 * duplicated, in all cases (cyclic U, cyclic V, and cyclic UV).
502 * Until this is fixed, disabling NURBS reading. */
503 reader = new AbcNurbsReader(object, settings);
504 parent_is_part_of_this_object = true;
507 else if (ICamera::matches(md)) {
508 reader = new AbcCameraReader(object, settings);
509 parent_is_part_of_this_object = true;
511 else if (IPoints::matches(md)) {
512 reader = new AbcPointsReader(object, settings);
513 parent_is_part_of_this_object = true;
515 else if (IMaterial::matches(md)) {
518 else if (ILight::matches(md)) {
521 else if (IFaceSet::matches(md)) {
522 /* Pass, those are handled in the mesh reader. */
524 else if (ICurves::matches(md)) {
525 reader = new AbcCurveReader(object, settings);
526 parent_is_part_of_this_object = true;
529 std::cerr << "Alembic object " << full_name
530 << " is of unsupported schema type '"
531 << object.getMetaData().get("schemaObjTitle") << "'"
536 /* We have created a reader, which should imply that this object is
537 * not claimed as part of any child Alembic object. */
538 BLI_assert(claiming_child_readers.empty());
540 readers.push_back(reader);
543 AlembicObjectPath *abc_path = static_cast<AlembicObjectPath *>(
544 MEM_callocN(sizeof(AlembicObjectPath), "AlembicObjectPath"));
545 BLI_strncpy(abc_path->path, full_name.c_str(), PATH_MAX);
546 BLI_addtail(&settings.cache_file->object_paths, abc_path);
548 /* We can now assign this reader as parent for our children. */
549 if (nonclaiming_child_readers.size() + assign_as_parent.size() > 0) {
550 /* TODO: When we only support C++11, use for (a: b) instead. */
551 BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
552 child_reader->parent_reader = reader;
554 BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
555 child_reader->parent_reader = reader;
559 else if (object.getParent()) {
560 if (claiming_child_readers.size() > 0) {
561 /* The first claiming child will serve just fine as parent to
562 * our non-claiming children. Since all claiming children share
563 * the same XForm, it doesn't really matter which one we pick. */
564 AbcObjectReader *claiming_child = claiming_child_readers[0];
565 BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
566 child_reader->parent_reader = claiming_child;
568 BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
569 child_reader->parent_reader = claiming_child;
571 /* Claiming children should have our parent set as their parent. */
572 BOOST_FOREACH(AbcObjectReader *child_reader, claiming_child_readers) {
573 r_assign_as_parent.push_back(child_reader);
577 /* This object isn't claimed by any child, and didn't produce
578 * a reader. Odd situation, could be the top Alembic object, or
579 * an unsupported Alembic schema. Delegate to our parent. */
580 BOOST_FOREACH(AbcObjectReader *child_reader, claiming_child_readers) {
581 r_assign_as_parent.push_back(child_reader);
583 BOOST_FOREACH(AbcObjectReader *child_reader, nonclaiming_child_readers) {
584 r_assign_as_parent.push_back(child_reader);
586 BOOST_FOREACH(AbcObjectReader *child_reader, assign_as_parent) {
587 r_assign_as_parent.push_back(child_reader);
592 return std::make_pair(parent_is_part_of_this_object, reader);
600 struct ImportJobData {
605 ImportSettings settings;
607 std::vector<AbcObjectReader *> readers;
618 ABC_INLINE bool is_mesh_and_strands(const IObject &object)
620 bool has_mesh = false;
621 bool has_curve = false;
623 for (int i = 0; i < object.getNumChildren(); ++i) {
624 const IObject &child = object.getChild(i);
626 if (!child.valid()) {
630 const MetaData &md = child.getMetaData();
632 if (IPolyMesh::matches(md)) {
635 else if (ISubD::matches(md)) {
638 else if (ICurves::matches(md)) {
641 else if (IPoints::matches(md)) {
646 return has_mesh && has_curve;
649 static void import_startjob(void *user_data, short *stop, short *do_update, float *progress)
651 SCOPE_TIMER("Alembic import, objects reading and creation");
653 ImportJobData *data = static_cast<ImportJobData *>(user_data);
656 data->do_update = do_update;
657 data->progress = progress;
659 ArchiveReader *archive = new ArchiveReader(data->filename);
661 if (!archive->valid()) {
663 data->error_code = ABC_ARCHIVE_FAIL;
667 CacheFile *cache_file = static_cast<CacheFile *>(BKE_cachefile_add(data->bmain, BLI_path_basename(data->filename)));
669 /* Decrement the ID ref-count because it is going to be incremented for each
670 * modifier and constraint that it will be attached to, so since currently
671 * it is not used by anyone, its use count will off by one. */
672 id_us_min(&cache_file->id);
674 cache_file->is_sequence = data->settings.is_sequence;
675 cache_file->scale = data->settings.scale;
676 cache_file->handle = handle_from_archive(archive);
677 BLI_strncpy(cache_file->filepath, data->filename, 1024);
679 data->settings.cache_file = cache_file;
681 *data->do_update = true;
682 *data->progress = 0.05f;
684 /* Parse Alembic Archive. */
685 AbcObjectReader::ptr_vector assign_as_parent;
686 visit_object(archive->getTop(), data->readers, data->settings, assign_as_parent);
688 /* There shouldn't be any orphans. */
689 BLI_assert(assign_as_parent.size() == 0);
692 data->was_cancelled = true;
696 *data->do_update = true;
697 *data->progress = 0.1f;
699 /* Create objects and set scene frame range. */
701 const float size = static_cast<float>(data->readers.size());
704 chrono_t min_time = std::numeric_limits<chrono_t>::max();
705 chrono_t max_time = std::numeric_limits<chrono_t>::min();
707 std::vector<AbcObjectReader *>::iterator iter;
708 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
709 AbcObjectReader *reader = *iter;
711 if (reader->valid()) {
712 reader->readObjectData(data->bmain, 0.0f);
714 min_time = std::min(min_time, reader->minTime());
715 max_time = std::max(max_time, reader->maxTime());
718 std::cerr << "Object " << reader->name() << " in Alembic file "
719 << data->filename << " is invalid.\n";
722 *data->progress = 0.1f + 0.3f * (++i / size);
723 *data->do_update = true;
726 data->was_cancelled = true;
731 if (data->settings.set_frame_range) {
732 Scene *scene = data->scene;
734 if (data->settings.is_sequence) {
735 SFRA = data->settings.offset;
736 EFRA = SFRA + (data->settings.sequence_len - 1);
739 else if (min_time < max_time) {
740 SFRA = static_cast<int>(round(min_time * FPS));
741 EFRA = static_cast<int>(round(max_time * FPS));
746 /* Setup parenthood. */
747 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
748 const AbcObjectReader *reader = *iter;
749 const AbcObjectReader *parent_reader = reader->parent_reader;
750 Object *ob = reader->object();
752 if (parent_reader == NULL) {
756 ob->parent = parent_reader->object();
760 /* Setup transformations and constraints. */
762 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
763 AbcObjectReader *reader = *iter;
764 reader->setupObjectTransform(0.0f);
766 *data->progress = 0.7f + 0.3f * (++i / size);
767 *data->do_update = true;
770 data->was_cancelled = true;
776 static void import_endjob(void *user_data)
778 SCOPE_TIMER("Alembic import, cleanup");
780 ImportJobData *data = static_cast<ImportJobData *>(user_data);
782 std::vector<AbcObjectReader *>::iterator iter;
784 /* Delete objects on cancelation. */
785 if (data->was_cancelled) {
786 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
787 Object *ob = (*iter)->object();
789 /* It's possible that cancellation occured between the creation of
790 * the reader and the creation of the Blender object. */
791 if (ob == NULL) continue;
793 BKE_libblock_free_us(data->bmain, ob);
797 /* Add object to scene. */
798 BKE_scene_base_deselect_all(data->scene);
800 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
801 Object *ob = (*iter)->object();
802 ob->lay = data->scene->lay;
804 BKE_scene_base_add(data->scene, ob);
806 DAG_id_tag_update_ex(data->bmain, &ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
809 DAG_relations_tag_update(data->bmain);
812 for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
813 AbcObjectReader *reader = *iter;
816 if (reader->refcount() == 0) {
821 switch (data->error_code) {
824 data->import_ok = !data->was_cancelled;
826 case ABC_ARCHIVE_FAIL:
827 WM_report(RPT_ERROR, "Could not open Alembic archive for reading! See console for detail.");
831 WM_main_add_notifier(NC_SCENE | ND_FRAME, data->scene);
834 static void import_freejob(void *user_data)
836 ImportJobData *data = static_cast<ImportJobData *>(user_data);
840 bool ABC_import(bContext *C, const char *filepath, float scale, bool is_sequence,
841 bool set_frame_range, int sequence_len, int offset,
842 bool validate_meshes, bool as_background_job)
844 /* Using new here since MEM_* funcs do not call ctor to properly initialize
846 ImportJobData *job = new ImportJobData();
847 job->bmain = CTX_data_main(C);
848 job->scene = CTX_data_scene(C);
849 job->import_ok = false;
850 BLI_strncpy(job->filename, filepath, 1024);
852 job->settings.scale = scale;
853 job->settings.is_sequence = is_sequence;
854 job->settings.set_frame_range = set_frame_range;
855 job->settings.sequence_len = sequence_len;
856 job->settings.offset = offset;
857 job->settings.validate_meshes = validate_meshes;
858 job->error_code = ABC_NO_ERROR;
859 job->was_cancelled = false;
863 if (as_background_job) {
864 wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
869 WM_JOB_TYPE_ALEMBIC);
872 WM_jobs_customdata_set(wm_job, job, import_freejob);
873 WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
874 WM_jobs_callbacks(wm_job, import_startjob, NULL, NULL, import_endjob);
876 WM_jobs_start(CTX_wm_manager(C), wm_job);
879 /* Fake a job context, so that we don't need NULL pointer checks while importing. */
880 short stop = 0, do_update = 0;
881 float progress = 0.f;
883 import_startjob(job, &stop, &do_update, &progress);
887 return job->import_ok;
890 /* ************************************************************************** */
892 void ABC_get_transform(CacheReader *reader, float r_mat[4][4], float time, float scale)
898 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
900 bool is_constant = false;
901 abc_reader->read_matrix(r_mat, time, scale, is_constant);
904 /* ************************************************************************** */
906 DerivedMesh *ABC_read_mesh(CacheReader *reader,
910 const char **err_str,
913 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
914 IObject iobject = abc_reader->iobject();
916 if (!iobject.valid()) {
917 *err_str = "Invalid object: verify object path";
921 const ObjectHeader &header = iobject.getHeader();
923 if (IPolyMesh::matches(header)) {
924 if (ob->type != OB_MESH) {
925 *err_str = "Object type mismatch: object path points to a mesh!";
929 return abc_reader->read_derivedmesh(dm, time, read_flag, err_str);
931 else if (ISubD::matches(header)) {
932 if (ob->type != OB_MESH) {
933 *err_str = "Object type mismatch: object path points to a subdivision mesh!";
937 return abc_reader->read_derivedmesh(dm, time, read_flag, err_str);
939 else if (IPoints::matches(header)) {
940 if (ob->type != OB_MESH) {
941 *err_str = "Object type mismatch: object path points to a point cloud (requires a mesh object)!";
945 return abc_reader->read_derivedmesh(dm, time, read_flag, err_str);
947 else if (ICurves::matches(header)) {
948 if (ob->type != OB_CURVE) {
949 *err_str = "Object type mismatch: object path points to a curve!";
953 return abc_reader->read_derivedmesh(dm, time, read_flag, err_str);
956 *err_str = "Unsupported object type: verify object path"; // or poke developer
960 /* ************************************************************************** */
962 void CacheReader_free(CacheReader *reader)
964 AbcObjectReader *abc_reader = reinterpret_cast<AbcObjectReader *>(reader);
965 abc_reader->decref();
967 if (abc_reader->refcount() == 0) {
972 CacheReader *CacheReader_open_alembic_object(AbcArchiveHandle *handle, CacheReader *reader, Object *object, const char *object_path)
974 if (object_path[0] == '\0') {
978 ArchiveReader *archive = archive_from_handle(handle);
980 if (!archive || !archive->valid()) {
985 find_iobject(archive->getTop(), iobject, object_path);
988 CacheReader_free(reader);
991 ImportSettings settings;
992 AbcObjectReader *abc_reader = create_reader(iobject, settings);
993 abc_reader->object(object);
994 abc_reader->incref();
996 return reinterpret_cast<CacheReader *>(abc_reader);