Alembic: Construct ISampleSelector once and pass along
[blender.git] / source / blender / alembic / intern / abc_mesh.cc
index bb5d5ce3566993023674c3ca303dffcc5d81f48d..39186ec55dc980074cdcca61657c9ce31da7da29 100644 (file)
@@ -112,7 +112,7 @@ static void get_vertices(DerivedMesh *dm, std::vector<Imath::V3f> &points)
        MVert *verts = dm->getVertArray(dm);
 
        for (int i = 0, e = dm->getNumVerts(dm); i < e; ++i) {
-               copy_zup_yup(points[i].getValue(), verts[i].co);
+               copy_yup_from_zup(points[i].getValue(), verts[i].co);
        }
 }
 
@@ -182,7 +182,7 @@ static void get_vertex_normals(DerivedMesh *dm, std::vector<Imath::V3f> &normals
 
        for (int i = 0, e = dm->getNumVerts(dm); i < e; ++i) {
                normal_short_to_float_v3(no, verts[i].no);
-               copy_zup_yup(normals[i].getValue(), no);
+               copy_yup_from_zup(normals[i].getValue(), no);
        }
 }
 
@@ -211,7 +211,7 @@ static void get_loop_normals(DerivedMesh *dm, std::vector<Imath::V3f> &normals)
 
                        for (int j = 0; j < mp->totloop; --ml, ++j, ++loop_index) {
                                const int index = ml->v;
-                               copy_zup_yup(normals[loop_index].getValue(), lnors[index]);
+                               copy_yup_from_zup(normals[loop_index].getValue(), lnors[index]);
                        }
                }
        }
@@ -226,14 +226,14 @@ static void get_loop_normals(DerivedMesh *dm, std::vector<Imath::V3f> &normals)
                                BKE_mesh_calc_poly_normal(mp, ml - (mp->totloop - 1), verts, no);
 
                                for (int j = 0; j < mp->totloop; --ml, ++j, ++loop_index) {
-                                       copy_zup_yup(normals[loop_index].getValue(), no);
+                                       copy_yup_from_zup(normals[loop_index].getValue(), no);
                                }
                        }
                        else {
                                /* Smooth shaded, use individual vert normals. */
                                for (int j = 0; j < mp->totloop; --ml, ++j, ++loop_index) {
                                        normal_short_to_float_v3(no, verts[ml->v].no);
-                                       copy_zup_yup(normals[loop_index].getValue(), no);
+                                       copy_yup_from_zup(normals[loop_index].getValue(), no);
                                }
                        }
                }
@@ -590,7 +590,7 @@ void AbcMeshWriter::getVelocities(DerivedMesh *dm, std::vector<Imath::V3f> &vels
                float *mesh_vels = reinterpret_cast<float *>(fss->meshVelocities);
 
                for (int i = 0; i < totverts; ++i) {
-                       copy_zup_yup(vels[i].getValue(), mesh_vels);
+                       copy_yup_from_zup(vels[i].getValue(), mesh_vels);
                        mesh_vels += 3;
                }
        }
@@ -646,75 +646,6 @@ void AbcMeshWriter::getGeoGroups(
 /* Some helpers for mesh generation */
 namespace utils {
 
-void mesh_add_verts(Mesh *mesh, size_t len)
-{
-       if (len == 0) {
-               return;
-       }
-
-       const int totvert = mesh->totvert + len;
-       CustomData vdata;
-       CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
-       CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);
-
-       if (!CustomData_has_layer(&vdata, CD_MVERT)) {
-               CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
-       }
-
-       CustomData_free(&mesh->vdata, mesh->totvert);
-       mesh->vdata = vdata;
-       BKE_mesh_update_customdata_pointers(mesh, false);
-
-       mesh->totvert = totvert;
-}
-
-static void mesh_add_mloops(Mesh *mesh, size_t len)
-{
-       if (len == 0) {
-               return;
-       }
-
-       /* new face count */
-       const int totloops = mesh->totloop + len;
-
-       CustomData ldata;
-       CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloops);
-       CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
-
-       if (!CustomData_has_layer(&ldata, CD_MLOOP)) {
-               CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloops);
-       }
-
-       CustomData_free(&mesh->ldata, mesh->totloop);
-       mesh->ldata = ldata;
-       BKE_mesh_update_customdata_pointers(mesh, false);
-
-       mesh->totloop = totloops;
-}
-
-static void mesh_add_mpolygons(Mesh *mesh, size_t len)
-{
-       if (len == 0) {
-               return;
-       }
-
-       const int totpolys = mesh->totpoly + len;
-
-       CustomData pdata;
-       CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpolys);
-       CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
-
-       if (!CustomData_has_layer(&pdata, CD_MPOLY)) {
-               CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpolys);
-       }
-
-       CustomData_free(&mesh->pdata, mesh->totpoly);
-       mesh->pdata = pdata;
-       BKE_mesh_update_customdata_pointers(mesh, false);
-
-       mesh->totpoly = totpolys;
-}
-
 static void build_mat_map(const Main *bmain, std::map<std::string, Material *> &mat_map)
 {
        Material *material = static_cast<Material *>(bmain->mat.first);
@@ -760,7 +691,7 @@ static void assign_materials(Main *bmain, Object *ob, const std::map<std::string
                                assigned_name = mat_iter->second;
                        }
 
-                       assign_material(ob, assigned_name, it->second, BKE_MAT_ASSIGN_OBJECT);
+                       assign_material(ob, assigned_name, it->second, BKE_MAT_ASSIGN_OBDATA);
                }
        }
 }
@@ -786,45 +717,6 @@ struct AbcMeshData {
        UInt32ArraySamplePtr uvs_indices;
 };
 
-static void *add_customdata_cb(void *user_data, const char *name, int data_type)
-{
-       Mesh *mesh = static_cast<Mesh *>(user_data);
-       CustomDataType cd_data_type = static_cast<CustomDataType>(data_type);
-       void *cd_ptr = NULL;
-
-       int index = -1;
-       if (cd_data_type == CD_MLOOPUV) {
-               index = ED_mesh_uv_texture_add(mesh, name, true);
-               cd_ptr = CustomData_get_layer(&mesh->ldata, cd_data_type);
-       }
-       else if (cd_data_type == CD_MLOOPCOL) {
-               index = ED_mesh_color_add(mesh, name, true);
-               cd_ptr = CustomData_get_layer(&mesh->ldata, cd_data_type);
-       }
-
-       if (index == -1) {
-               return NULL;
-       }
-
-       return cd_ptr;
-}
-
-CDStreamConfig create_config(Mesh *mesh)
-{
-       CDStreamConfig config;
-
-       config.mvert = mesh->mvert;
-       config.mpoly = mesh->mpoly;
-       config.mloop = mesh->mloop;
-       config.totpoly = mesh->totpoly;
-       config.totloop = mesh->totloop;
-       config.user_data = mesh;
-       config.loopdata = &mesh->ldata;
-       config.add_customdata_cb = add_customdata_cb;
-
-       return config;
-}
-
 static void read_mverts_interp(MVert *mverts, const P3fArraySamplePtr &positions, const P3fArraySamplePtr &ceil_positions, const float weight)
 {
        float tmp[3];
@@ -834,7 +726,7 @@ static void read_mverts_interp(MVert *mverts, const P3fArraySamplePtr &positions
                const Imath::V3f &ceil_pos = (*ceil_positions)[i];
 
                interp_v3_v3v3(tmp, floor_pos.getValue(), ceil_pos.getValue(), weight);
-               copy_yup_zup(mvert.co, tmp);
+               copy_zup_from_yup(mvert.co, tmp);
 
                mvert.bweight = 0;
        }
@@ -863,7 +755,7 @@ void read_mverts(MVert *mverts, const P3fArraySamplePtr &positions, const N3fArr
                MVert &mvert = mverts[i];
                Imath::V3f pos_in = (*positions)[i];
 
-               copy_yup_zup(mvert.co, pos_in.getValue());
+               copy_zup_from_yup(mvert.co, pos_in.getValue());
 
                mvert.bweight = 0;
 
@@ -873,7 +765,7 @@ void read_mverts(MVert *mverts, const P3fArraySamplePtr &positions, const N3fArr
                        short no[3];
                        normal_float_to_short_v3(no, nor_in.getValue());
 
-                       copy_yup_zup(mvert.no, no);
+                       copy_zup_from_yup(mvert.no, no);
                }
        }
 }
@@ -976,6 +868,141 @@ ABC_INLINE void read_normals_params(AbcMeshData &abc_data,
        }
 }
 
+static bool check_smooth_poly_flag(DerivedMesh *dm)
+{
+       MPoly *mpolys = dm->getPolyArray(dm);
+
+       for (int i = 0, e = dm->getNumPolys(dm); i < e; ++i) {
+               MPoly &poly = mpolys[i];
+
+               if ((poly.flag & ME_SMOOTH) != 0) {
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+static void set_smooth_poly_flag(DerivedMesh *dm)
+{
+       MPoly *mpolys = dm->getPolyArray(dm);
+
+       for (int i = 0, e = dm->getNumPolys(dm); i < e; ++i) {
+               MPoly &poly = mpolys[i];
+               poly.flag |= ME_SMOOTH;
+       }
+}
+
+static void *add_customdata_cb(void *user_data, const char *name, int data_type)
+{
+       DerivedMesh *dm = static_cast<DerivedMesh *>(user_data);
+       CustomDataType cd_data_type = static_cast<CustomDataType>(data_type);
+       void *cd_ptr;
+       CustomData *loopdata;
+       int numloops;
+
+       /* unsupported custom data type -- don't do anything. */
+       if (!ELEM(cd_data_type, CD_MLOOPUV, CD_MLOOPCOL)) {
+               return NULL;
+       }
+
+       loopdata = dm->getLoopDataLayout(dm);
+       cd_ptr = CustomData_get_layer_named(loopdata, cd_data_type, name);
+       if (cd_ptr != NULL) {
+               /* layer already exists, so just return it. */
+               return cd_ptr;
+       }
+
+       /* create a new layer, taking care to construct the hopefully-soon-to-be-removed
+        * CD_MTEXPOLY layer too, with the same name. */
+       numloops = dm->getNumLoops(dm);
+       cd_ptr = CustomData_add_layer_named(loopdata, cd_data_type, CD_DEFAULT,
+                                           NULL, numloops, name);
+       if (cd_data_type == CD_MLOOPUV) {
+               CustomData_add_layer_named(dm->getPolyDataLayout(dm),
+                                          CD_MTEXPOLY, CD_DEFAULT,
+                                          NULL, numloops, name);
+       }
+
+       return cd_ptr;
+}
+
+static void get_weight_and_index(CDStreamConfig &config,
+                                 Alembic::AbcCoreAbstract::TimeSamplingPtr time_sampling,
+                                 size_t samples_number)
+{
+       Alembic::AbcGeom::index_t i0, i1;
+
+       config.weight = get_weight_and_index(config.time,
+                                            time_sampling,
+                                            samples_number,
+                                            i0,
+                                            i1);
+
+       config.index = i0;
+       config.ceil_index = i1;
+}
+
+static void read_mesh_sample(ImportSettings *settings,
+                             const IPolyMeshSchema &schema,
+                             const ISampleSelector &selector,
+                             CDStreamConfig &config,
+                             bool &do_normals)
+{
+       const IPolyMeshSchema::Sample sample = schema.getValue(selector);
+
+       AbcMeshData abc_mesh_data;
+       abc_mesh_data.face_counts = sample.getFaceCounts();
+       abc_mesh_data.face_indices = sample.getFaceIndices();
+       abc_mesh_data.positions = sample.getPositions();
+
+       read_normals_params(abc_mesh_data, schema.getNormalsParam(), selector);
+
+       do_normals = (abc_mesh_data.face_normals != NULL);
+
+       get_weight_and_index(config, schema.getTimeSampling(), schema.getNumSamples());
+
+       if (config.weight != 0.0f) {
+               Alembic::AbcGeom::IPolyMeshSchema::Sample ceil_sample;
+               schema.get(ceil_sample, Alembic::Abc::ISampleSelector(config.ceil_index));
+               abc_mesh_data.ceil_positions = ceil_sample.getPositions();
+       }
+
+       if ((settings->read_flag & MOD_MESHSEQ_READ_UV) != 0) {
+               read_uvs_params(config, abc_mesh_data, schema.getUVsParam(), selector);
+       }
+
+       if ((settings->read_flag & MOD_MESHSEQ_READ_VERT) != 0) {
+               read_mverts(config, abc_mesh_data);
+       }
+
+       if ((settings->read_flag & MOD_MESHSEQ_READ_POLY) != 0) {
+               read_mpolys(config, abc_mesh_data);
+       }
+
+       if ((settings->read_flag & (MOD_MESHSEQ_READ_UV | MOD_MESHSEQ_READ_COLOR)) != 0) {
+               read_custom_data(schema.getArbGeomParams(), config, selector);
+       }
+
+       /* TODO: face sets */
+}
+
+CDStreamConfig get_config(DerivedMesh *dm)
+{
+       CDStreamConfig config;
+
+       config.user_data = dm;
+       config.mvert = dm->getVertArray(dm);
+       config.mloop = dm->getLoopArray(dm);
+       config.mpoly = dm->getPolyArray(dm);
+       config.totloop = dm->getNumLoops(dm);
+       config.totpoly = dm->getNumPolys(dm);
+       config.loopdata = dm->getLoopDataLayout(dm);
+       config.add_customdata_cb = add_customdata_cb;
+
+       return config;
+}
+
 /* ************************************************************************** */
 
 AbcMeshReader::AbcMeshReader(const IObject &object, ImportSettings &settings)
@@ -994,31 +1021,21 @@ bool AbcMeshReader::valid() const
        return m_schema.valid();
 }
 
-void AbcMeshReader::readObjectData(Main *bmain, float time)
+void AbcMeshReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
 {
        Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
 
        m_object = BKE_object_add_only_object(bmain, OB_MESH, m_object_name.c_str());
        m_object->data = mesh;
 
-       const ISampleSelector sample_sel(time);
-       const IPolyMeshSchema::Sample sample = m_schema.getValue(sample_sel);
+       DerivedMesh *dm = CDDM_from_mesh(mesh);
+       DerivedMesh *ndm = this->read_derivedmesh(dm, sample_sel, MOD_MESHSEQ_READ_ALL, NULL);
 
-       const P3fArraySamplePtr &positions = sample.getPositions();
-       const Int32ArraySamplePtr &face_indices = sample.getFaceIndices();
-    const Int32ArraySamplePtr &face_counts = sample.getFaceCounts();
-
-       utils::mesh_add_verts(mesh, positions->size());
-       utils::mesh_add_mpolygons(mesh, face_counts->size());
-       utils::mesh_add_mloops(mesh, face_indices->size());
-
-       m_mesh_data = create_config(mesh);
-
-       bool has_smooth_normals = false;
-       read_mesh_sample(m_settings, m_schema, sample_sel, m_mesh_data, has_smooth_normals);
+       if (ndm != dm) {
+               dm->release(dm);
+       }
 
-       BKE_mesh_calc_normals(mesh);
-       BKE_mesh_calc_edges(mesh, false, false);
+       DM_to_mesh(ndm, mesh, m_object, CD_MASK_MESH, true);
 
        if (m_settings->validate_meshes) {
                BKE_mesh_validate(mesh, false, false);
@@ -1031,6 +1048,74 @@ void AbcMeshReader::readObjectData(Main *bmain, float time)
        }
 }
 
+DerivedMesh *AbcMeshReader::read_derivedmesh(DerivedMesh *dm,
+                                             const ISampleSelector &sample_sel,
+                                             int read_flag,
+                                             const char **err_str)
+{
+       const IPolyMeshSchema::Sample sample = m_schema.getValue(sample_sel);
+
+       const P3fArraySamplePtr &positions = sample.getPositions();
+       const Alembic::Abc::Int32ArraySamplePtr &face_indices = sample.getFaceIndices();
+       const Alembic::Abc::Int32ArraySamplePtr &face_counts = sample.getFaceCounts();
+
+       DerivedMesh *new_dm = NULL;
+
+       /* Only read point data when streaming meshes, unless we need to create new ones. */
+       ImportSettings settings;
+       settings.read_flag |= read_flag;
+
+       if (dm->getNumVerts(dm) != positions->size()) {
+               new_dm = CDDM_from_template(dm,
+                                           positions->size(),
+                                           0,
+                                           0,
+                                           face_indices->size(),
+                                           face_counts->size());
+
+               settings.read_flag |= MOD_MESHSEQ_READ_ALL;
+       }
+       else {
+               /* If the face count changed (e.g. by triangulation), only read points.
+                * This prevents crash from T49813.
+                * TODO(kevin): perhaps find a better way to do this? */
+               if (face_counts->size() != dm->getNumPolys(dm) ||
+                   face_indices->size() != dm->getNumLoops(dm))
+               {
+                       settings.read_flag = MOD_MESHSEQ_READ_VERT;
+
+                       if (err_str) {
+                               *err_str = "Topology has changed, perhaps by triangulating the"
+                                          " mesh. Only vertices will be read!";
+                       }
+               }
+       }
+
+       CDStreamConfig config = get_config(new_dm ? new_dm : dm);
+       config.time = sample_sel.getRequestedTime();
+
+       bool do_normals = false;
+       read_mesh_sample(&settings, m_schema, sample_sel, config, do_normals);
+
+       if (new_dm) {
+               /* Check if we had ME_SMOOTH flag set to restore it. */
+               if (!do_normals && check_smooth_poly_flag(dm)) {
+                       set_smooth_poly_flag(new_dm);
+               }
+
+               CDDM_calc_normals(new_dm);
+               CDDM_calc_edges(new_dm);
+
+               return new_dm;
+       }
+
+       if (do_normals) {
+               CDDM_calc_normals(dm);
+       }
+
+       return dm;
+}
+
 void AbcMeshReader::readFaceSetsSample(Main *bmain, Mesh *mesh, size_t poly_start,
                                        const ISampleSelector &sample_sel)
 {
@@ -1080,44 +1165,40 @@ void AbcMeshReader::readFaceSetsSample(Main *bmain, Mesh *mesh, size_t poly_star
        utils::assign_materials(bmain, m_object, mat_map);
 }
 
-static void get_weight_and_index(CDStreamConfig &config,
-                                 Alembic::AbcCoreAbstract::TimeSamplingPtr time_sampling,
-                                 size_t samples_number)
+/* ************************************************************************** */
+
+ABC_INLINE MEdge *find_edge(MEdge *edges, int totedge, int v1, int v2)
 {
-       Alembic::AbcGeom::index_t i0, i1;
+       for (int i = 0, e = totedge; i < e; ++i) {
+               MEdge &edge = edges[i];
 
-       config.weight = get_weight_and_index(config.time,
-                                            time_sampling,
-                                            samples_number,
-                                            i0,
-                                            i1);
+               if (edge.v1 == v1 && edge.v2 == v2) {
+                       return &edge;
+               }
+       }
 
-       config.index = i0;
-       config.ceil_index = i1;
+       return NULL;
 }
 
-void read_mesh_sample(ImportSettings *settings,
-                      const IPolyMeshSchema &schema,
-                      const ISampleSelector &selector,
-                      CDStreamConfig &config,
-                      bool &do_normals)
+static void read_subd_sample(ImportSettings *settings,
+                             const ISubDSchema &schema,
+                             const ISampleSelector &selector,
+                             CDStreamConfig &config)
 {
-       const IPolyMeshSchema::Sample sample = schema.getValue(selector);
+       const ISubDSchema::Sample sample = schema.getValue(selector);
 
        AbcMeshData abc_mesh_data;
        abc_mesh_data.face_counts = sample.getFaceCounts();
        abc_mesh_data.face_indices = sample.getFaceIndices();
+       abc_mesh_data.vertex_normals = N3fArraySamplePtr();
+       abc_mesh_data.face_normals = N3fArraySamplePtr();
        abc_mesh_data.positions = sample.getPositions();
 
-       read_normals_params(abc_mesh_data, schema.getNormalsParam(), selector);
-
-       do_normals = (abc_mesh_data.face_normals != NULL);
-
        get_weight_and_index(config, schema.getTimeSampling(), schema.getNumSamples());
 
        if (config.weight != 0.0f) {
-               Alembic::AbcGeom::IPolyMeshSchema::Sample ceil_sample;
-               schema.get(ceil_sample, Alembic::Abc::ISampleSelector(static_cast<Alembic::AbcCoreAbstract::index_t>(config.ceil_index)));
+               Alembic::AbcGeom::ISubDSchema::Sample ceil_sample;
+               schema.get(ceil_sample, Alembic::Abc::ISampleSelector(config.ceil_index));
                abc_mesh_data.ceil_positions = ceil_sample.getPositions();
        }
 
@@ -1142,19 +1223,6 @@ void read_mesh_sample(ImportSettings *settings,
 
 /* ************************************************************************** */
 
-ABC_INLINE MEdge *find_edge(MEdge *edges, int totedge, int v1, int v2)
-{
-       for (int i = 0, e = totedge; i < e; ++i) {
-               MEdge &edge = edges[i];
-
-               if (edge.v1 == v1 && edge.v2 == v2) {
-                       return &edge;
-               }
-       }
-
-       return NULL;
-}
-
 AbcSubDReader::AbcSubDReader(const IObject &object, ImportSettings &settings)
     : AbcObjectReader(object, settings)
 {
@@ -1171,28 +1239,23 @@ bool AbcSubDReader::valid() const
        return m_schema.valid();
 }
 
-void AbcSubDReader::readObjectData(Main *bmain, float time)
+void AbcSubDReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
 {
        Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
 
        m_object = BKE_object_add_only_object(bmain, OB_MESH, m_object_name.c_str());
        m_object->data = mesh;
 
-       const ISampleSelector sample_sel(time);
-       const ISubDSchema::Sample sample = m_schema.getValue(sample_sel);
-
-       const P3fArraySamplePtr &positions = sample.getPositions();
-       const Int32ArraySamplePtr &face_indices = sample.getFaceIndices();
-    const Int32ArraySamplePtr &face_counts = sample.getFaceCounts();
-
-       utils::mesh_add_verts(mesh, positions->size());
-       utils::mesh_add_mpolygons(mesh, face_counts->size());
-       utils::mesh_add_mloops(mesh, face_indices->size());
+       DerivedMesh *dm = CDDM_from_mesh(mesh);
+       DerivedMesh *ndm = this->read_derivedmesh(dm, sample_sel, MOD_MESHSEQ_READ_ALL, NULL);
 
-       m_mesh_data = create_config(mesh);
+       if (ndm != dm) {
+               dm->release(dm);
+       }
 
-       read_subd_sample(m_settings, m_schema, sample_sel, m_mesh_data);
+       DM_to_mesh(ndm, mesh, m_object, CD_MASK_MESH, true);
 
+       const ISubDSchema::Sample sample = m_schema.getValue(sample_sel);
        Int32ArraySamplePtr indices = sample.getCreaseIndices();
        Alembic::Abc::FloatArraySamplePtr sharpnesses = sample.getCreaseSharpnesses();
 
@@ -1222,43 +1285,64 @@ void AbcSubDReader::readObjectData(Main *bmain, float time)
        }
 }
 
-void read_subd_sample(ImportSettings *settings,
-                      const ISubDSchema &schema,
-                      const ISampleSelector &selector,
-                      CDStreamConfig &config)
+DerivedMesh *AbcSubDReader::read_derivedmesh(DerivedMesh *dm,
+                                             const ISampleSelector &sample_sel,
+                                             int read_flag,
+                                             const char **err_str)
 {
-       const ISubDSchema::Sample sample = schema.getValue(selector);
+       const ISubDSchema::Sample sample = m_schema.getValue(sample_sel);
 
-       AbcMeshData abc_mesh_data;
-       abc_mesh_data.face_counts = sample.getFaceCounts();
-       abc_mesh_data.face_indices = sample.getFaceIndices();
-       abc_mesh_data.vertex_normals = N3fArraySamplePtr();
-       abc_mesh_data.face_normals = N3fArraySamplePtr();
-       abc_mesh_data.positions = sample.getPositions();
+       const P3fArraySamplePtr &positions = sample.getPositions();
+       const Alembic::Abc::Int32ArraySamplePtr &face_indices = sample.getFaceIndices();
+       const Alembic::Abc::Int32ArraySamplePtr &face_counts = sample.getFaceCounts();
 
-       get_weight_and_index(config, schema.getTimeSampling(), schema.getNumSamples());
+       DerivedMesh *new_dm = NULL;
 
-       if (config.weight != 0.0f) {
-               Alembic::AbcGeom::ISubDSchema::Sample ceil_sample;
-               schema.get(ceil_sample, Alembic::Abc::ISampleSelector(static_cast<Alembic::AbcCoreAbstract::index_t>(config.ceil_index)));
-               abc_mesh_data.ceil_positions = ceil_sample.getPositions();
-       }
+       ImportSettings settings;
+       settings.read_flag |= read_flag;
 
-       if ((settings->read_flag & MOD_MESHSEQ_READ_UV) != 0) {
-               read_uvs_params(config, abc_mesh_data, schema.getUVsParam(), selector);
-       }
+       if (dm->getNumVerts(dm) != positions->size()) {
+               new_dm = CDDM_from_template(dm,
+                                           positions->size(),
+                                           0,
+                                           0,
+                                           face_indices->size(),
+                                           face_counts->size());
 
-       if ((settings->read_flag & MOD_MESHSEQ_READ_VERT) != 0) {
-               read_mverts(config, abc_mesh_data);
+               settings.read_flag |= MOD_MESHSEQ_READ_ALL;
        }
-
-       if ((settings->read_flag & MOD_MESHSEQ_READ_POLY) != 0) {
-               read_mpolys(config, abc_mesh_data);
+       else {
+               /* If the face count changed (e.g. by triangulation), only read points.
+                * This prevents crash from T49813.
+                * TODO(kevin): perhaps find a better way to do this? */
+               if (face_counts->size() != dm->getNumPolys(dm) ||
+                   face_indices->size() != dm->getNumLoops(dm))
+               {
+                       settings.read_flag = MOD_MESHSEQ_READ_VERT;
+
+                       if (err_str) {
+                               *err_str = "Topology has changed, perhaps by triangulating the"
+                                          " mesh. Only vertices will be read!";
+                       }
+               }
        }
 
-       if ((settings->read_flag & (MOD_MESHSEQ_READ_UV | MOD_MESHSEQ_READ_COLOR)) != 0) {
-               read_custom_data(schema.getArbGeomParams(), config, selector);
+       /* Only read point data when streaming meshes, unless we need to create new ones. */
+       CDStreamConfig config = get_config(new_dm ? new_dm : dm);
+       config.time = sample_sel.getRequestedTime();
+       read_subd_sample(&settings, m_schema, sample_sel, config);
+
+       if (new_dm) {
+               /* Check if we had ME_SMOOTH flag set to restore it. */
+               if (check_smooth_poly_flag(dm)) {
+                       set_smooth_poly_flag(new_dm);
+               }
+
+               CDDM_calc_normals(new_dm);
+               CDDM_calc_edges(new_dm);
+
+               return new_dm;
        }
 
-       /* TODO: face sets */
+       return dm;
 }