Alembic: don't use m_shapes_type
authorSybren A. Stüvel <sybren@stuvel.eu>
Tue, 28 Feb 2017 15:53:48 +0000 (16:53 +0100)
committerSybren A. Stüvel <sybren@stuvel.eu>
Thu, 6 Apr 2017 14:46:51 +0000 (16:46 +0200)
source/blender/alembic/intern/abc_exporter.cc
source/blender/alembic/intern/abc_exporter.h
source/blender/alembic/intern/alembic_capi.cc

index 592678f5db8d8166c6924e37356cf45fd6b2339c..ec9504a2f3a5a53a01d34b1807295b63dd5c23ca 100644 (file)
@@ -161,9 +161,8 @@ AbcExporter::~AbcExporter()
        }
 
        /* Free shapes vector */
-       m_shapes_type::iterator it_s, e_s;
-       for (it_s = m_shapes.begin(), e_s = m_shapes.end(); it_s != e_s; ++it_s) {
-               delete *it_s;
+       for (int i = 0, e = m_shapes.size(); i != e; ++i) {
+               delete m_shapes[i];
        }
 
        delete m_writer;
@@ -317,9 +316,8 @@ void AbcExporter::operator()(Main *bmain, float &progress, bool &was_canceled)
                setCurrentFrame(bmain, frame - m_settings.frame_start);
 
                if (shape_frames.count(frame) != 0) {
-                       m_shapes_type::iterator xit, xe;
-                       for (xit = m_shapes.begin(), xe = m_shapes.end(); xit != xe; ++xit) {
-                               (*xit)->write();
+                       for (int i = 0, e = m_shapes.size(); i != e; ++i) {
+                               m_shapes[i]->write();
                        }
                }
 
index f87347758b9ec72137e9dc00ddd4a41c2e72a186..0f08037fd17852dbd2c77d24d25d20d74ce40ae6 100644 (file)
@@ -93,8 +93,7 @@ class AbcExporter {
        typedef std::map<std::string, AbcTransformWriter *> m_xforms_type;
        m_xforms_type m_xforms;
 
-       typedef std::vector<AbcObjectWriter *> m_shapes_type;
-       m_shapes_type m_shapes;
+       std::vector<AbcObjectWriter *> m_shapes;
 
 public:
        AbcExporter(Scene *scene, const char *filename, ExportSettings &settings);
index 7bc92a7c51620eaef7a89153de8a2fae6bfbfca4..632a9ce01c743db8e9999adf244d762624d35956 100644 (file)
@@ -413,10 +413,10 @@ void ABC_export(
  */
 static bool visit_object(const IObject &object,
                          std::vector<AbcObjectReader *> &readers,
-                         GHash *readers_map,
+                         GHash * readers_map,
                          ImportSettings &settings)
 {
-       const char *full_name = object.getFullName().c_str();
+       const std::string & full_name = object.getFullName();
 
        if (!object.valid()) {
                std::cerr << "  - " << full_name << ": object is invalid, skipping it and all its children.\n";
@@ -514,10 +514,16 @@ static bool visit_object(const IObject &object,
 
                AlembicObjectPath *abc_path = static_cast<AlembicObjectPath *>(
                                                  MEM_callocN(sizeof(AlembicObjectPath), "AlembicObjectPath"));
-               BLI_strncpy(abc_path->path, full_name, PATH_MAX);
+               BLI_strncpy(abc_path->path, full_name.c_str(), PATH_MAX);
                BLI_addtail(&settings.cache_file->object_paths, abc_path);
 
-               BLI_ghash_insert(readers_map, const_cast<char *>(full_name), reader);
+               /* We have to take a copy of the name, because Alembic can reuse
+                * memory, for example when dealing with instances. */
+               char * name_copy = static_cast<char *>(MEM_mallocN(
+                                                          full_name.length() + 1,
+                                                          "Alembic readers_map key"));
+               BLI_strncpy(name_copy, full_name.c_str(), full_name.length() + 1);
+               BLI_ghash_insert(readers_map, name_copy, reader);
        }
 
        return parent_is_part_of_this_object;
@@ -535,7 +541,7 @@ struct ImportJobData {
        char filename[1024];
        ImportSettings settings;
 
-       GHash *reader_map;
+       GHash * readers_map;
        std::vector<AbcObjectReader *> readers;
 
        short *stop;
@@ -612,10 +618,10 @@ static void import_startjob(void *user_data, short *stop, short *do_update, floa
        *data->do_update = true;
        *data->progress = 0.05f;
 
-       data->reader_map = BLI_ghash_str_new("alembic parent ghash");
+       data->readers_map = BLI_ghash_str_new("Alembic readers_map ghash");
 
        /* Parse Alembic Archive. */
-       visit_object(archive->getTop(), data->readers, data->reader_map, data->settings);
+       visit_object(archive->getTop(), data->readers, data->readers_map, data->settings);
 
        if (G.is_break) {
                data->was_cancelled = true;
@@ -685,8 +691,10 @@ static void import_startjob(void *user_data, short *stop, short *do_update, floa
                IObject alembic_parent = iobject.getParent();
 
                while (alembic_parent) {
+                       const char * parent_name = alembic_parent.getFullName().c_str();
                        parent_reader = reinterpret_cast<AbcObjectReader *>(
-                                           BLI_ghash_lookup(data->reader_map, alembic_parent.getFullName().c_str()));
+                                           BLI_ghash_lookup(data->readers_map,
+                                                            parent_name));
                        if (parent_reader != NULL) {
                                break;  // found the parent reader.
                        }
@@ -774,8 +782,8 @@ static void import_endjob(void *user_data)
                }
        }
 
-       if (data->reader_map) {
-               BLI_ghash_free(data->reader_map, NULL, NULL);
+       if (data->readers_map) {
+               BLI_ghash_free(data->readers_map, MEM_freeN, NULL);
        }
 
        switch (data->error_code) {
@@ -811,7 +819,7 @@ void ABC_import(bContext *C, const char *filepath, float scale, bool is_sequence
        job->settings.sequence_len = sequence_len;
        job->settings.offset = offset;
        job->settings.validate_meshes = validate_meshes;
-       job->reader_map = NULL;
+       job->readers_map = NULL;
        job->error_code = ABC_NO_ERROR;
        job->was_cancelled = false;