Alembic: use object-oriented approach in ABC_read_mesh()
authorSybren A. Stüvel <sybren@stuvel.eu>
Fri, 28 Apr 2017 11:48:00 +0000 (13:48 +0200)
committerSybren A. Stüvel <sybren@stuvel.eu>
Fri, 28 Apr 2017 13:33:57 +0000 (15:33 +0200)
This is easier to extend than the if/else if/else chain that was in place,
and allows for somewhat more granular error messages.

12 files changed:
source/blender/alembic/intern/abc_camera.cc
source/blender/alembic/intern/abc_camera.h
source/blender/alembic/intern/abc_curves.cc
source/blender/alembic/intern/abc_curves.h
source/blender/alembic/intern/abc_mesh.cc
source/blender/alembic/intern/abc_mesh.h
source/blender/alembic/intern/abc_object.h
source/blender/alembic/intern/abc_points.cc
source/blender/alembic/intern/abc_points.h
source/blender/alembic/intern/abc_transform.cc
source/blender/alembic/intern/abc_transform.h
source/blender/alembic/intern/alembic_capi.cc

index 33344f6c9c53bef9d6be3cfac24327d44d54399d..16416205983a88425380f2cf7443cbb917c30707 100644 (file)
@@ -117,6 +117,23 @@ bool AbcCameraReader::valid() const
        return m_schema.valid();
 }
 
+bool AbcCameraReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                          const Object *const ob,
+                                          const char **err_str) const
+{
+       if (!Alembic::AbcGeom::ICamera::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to Camera when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_CAMERA) {
+               *err_str = "Object type mismatch, Alembic object path points to Camera.";
+               return false;
+       }
+
+       return true;
+}
+
 void AbcCameraReader::readObjectData(Main *bmain, const ISampleSelector &sample_sel)
 {
        Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, m_data_name.c_str()));
index 0201dd9d6e1fe34bc3b3d8bff22565a1823a0e0f..16c5cccd5ea51bab098046bfcc5395b16e0ebfa7 100644 (file)
@@ -54,6 +54,9 @@ public:
        AbcCameraReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                const Object *const ob,
+                                const char **err_str) const;
 
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
 };
index d85d8df4d774e5efcbe4c6f1d49c007c6239e7cf..67b2cb8ae8c1b545a3c2ade33258f8ab4088e9e2 100644 (file)
@@ -211,6 +211,23 @@ bool AbcCurveReader::valid() const
        return m_curves_schema.valid();
 }
 
+bool AbcCurveReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                         const Object *const ob,
+                                         const char **err_str) const
+{
+       if (!Alembic::AbcGeom::ICurves::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to Curves when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_EMPTY) {
+               *err_str = "Object type mismatch, Alembic object path points to Curves.";
+               return false;
+       }
+
+       return true;
+}
+
 void AbcCurveReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
 {
        Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_CURVE);
index 1ad6d0bf2781452c0cd32379cb7f15fec087b577..a9231f947b23d0f869ce1d1555c87d687b2bc337 100644 (file)
@@ -54,6 +54,9 @@ public:
        AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                const Object *const ob,
+                                const char **err_str) const;
 
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
        DerivedMesh *read_derivedmesh(DerivedMesh *dm,
index 39186ec55dc980074cdcca61657c9ce31da7da29..32795e70e9237fbfb0f6c00acd4d94da455b0b59 100644 (file)
@@ -1048,6 +1048,23 @@ void AbcMeshReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelec
        }
 }
 
+bool AbcMeshReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                        const Object *const ob,
+                                        const char **err_str) const
+{
+       if (!Alembic::AbcGeom::IPolyMesh::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to PolyMesh when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_MESH) {
+               *err_str = "Object type mismatch, Alembic object path points to PolyMesh.";
+               return false;
+       }
+
+       return true;
+}
+
 DerivedMesh *AbcMeshReader::read_derivedmesh(DerivedMesh *dm,
                                              const ISampleSelector &sample_sel,
                                              int read_flag,
@@ -1239,6 +1256,23 @@ bool AbcSubDReader::valid() const
        return m_schema.valid();
 }
 
+bool AbcSubDReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                        const Object *const ob,
+                                        const char **err_str) const
+{
+       if (!Alembic::AbcGeom::ISubD::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to SubD when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_MESH) {
+               *err_str = "Object type mismatch, Alembic object path points to SubD.";
+               return false;
+       }
+
+       return true;
+}
+
 void AbcSubDReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
 {
        Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
index 1b67c80cc72a475632eacb84ceeeaefbb561e9b4..6bf1dde3d1dc47ce629ea24fea5d424fdd418305 100644 (file)
@@ -99,7 +99,9 @@ public:
        AbcMeshReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
-
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                              const Object *const ob,
+                              const char **err_str) const;
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
 
        DerivedMesh *read_derivedmesh(DerivedMesh *dm,
@@ -123,7 +125,9 @@ public:
        AbcSubDReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
-
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                const Object *const ob,
+                                const char **err_str) const;
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
        DerivedMesh *read_derivedmesh(DerivedMesh *dm,
                                      const Alembic::Abc::ISampleSelector &sample_sel,
index c5da84d3c2addfd3b661c1b6ec0072ca4dcd0a48..1462f93a4225df92d8f45d89beb22bcfe464f8c1 100644 (file)
@@ -169,6 +169,9 @@ public:
        const std::string & data_name() const { return m_data_name; }
 
        virtual bool valid() const = 0;
+       virtual bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                        const Object *const ob,
+                                        const char **err_str) const = 0;
 
        virtual void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel) = 0;
 
index d5ecf16b79382c5f6cfd947071f41d5d82ce806a..3a2ca8a1b47adf3a0f7a931d6ddd233e6c09215b 100644 (file)
@@ -151,6 +151,23 @@ bool AbcPointsReader::valid() const
        return m_schema.valid();
 }
 
+bool AbcPointsReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                          const Object *const ob,
+                                          const char **err_str) const
+{
+       if (!Alembic::AbcGeom::IPoints::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to Points when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_EMPTY) {
+               *err_str = "Object type mismatch, Alembic object path points to Points.";
+               return false;
+       }
+
+       return true;
+}
+
 void AbcPointsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
 {
        Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
index 1f74351d30297bc5cbdecf8ac0475807b14f2af3..369a802d763caecbf236441c1eab9e4ba0ee633a 100644 (file)
@@ -58,6 +58,9 @@ public:
        AbcPointsReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                const Object *const ob,
+                                const char **err_str) const;
 
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
 
index c48be361877ec3c6db36c27364b5e51aac041495..e2369e80618769587b4c28fee1d020b12f20c457 100644 (file)
@@ -154,6 +154,23 @@ bool AbcEmptyReader::valid() const
        return m_schema.valid();
 }
 
+bool AbcEmptyReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                         const Object *const ob,
+                                         const char **err_str) const
+{
+       if (!Alembic::AbcGeom::IXform::matches(alembic_header)) {
+               *err_str = "Object type mismatch, Alembic object path pointed to XForm when importing, but not any more.";
+               return false;
+       }
+
+       if (ob->type != OB_EMPTY) {
+               *err_str = "Object type mismatch, Alembic object path points to XForm.";
+               return false;
+       }
+
+       return true;
+}
+
 void AbcEmptyReader::readObjectData(Main *bmain, const ISampleSelector &UNUSED(sample_sel))
 {
        m_object = BKE_object_add_only_object(bmain, OB_EMPTY,
index f2e2edf8010c930f4d37aa11ac4f207526faf039..753a4247e9fa3a1cb01aaa5da5c2ac85b7f0b496 100644 (file)
@@ -68,6 +68,9 @@ public:
        AbcEmptyReader(const Alembic::Abc::IObject &object, ImportSettings &settings);
 
        bool valid() const;
+       bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
+                                const Object *const ob,
+                                const char **err_str) const;
 
        void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel);
 };
index 74b472fdb4ef78ef91b435045adfb3940f357f24..528647991335034f061347e2a80aa98dfbdf0f7f 100644 (file)
@@ -939,43 +939,13 @@ DerivedMesh *ABC_read_mesh(CacheReader *reader,
        }
 
        const ObjectHeader &header = iobject.getHeader();
-       ISampleSelector sample_sel(time);
-
-       if (IPolyMesh::matches(header)) {
-               if (ob->type != OB_MESH) {
-                       *err_str = "Object type mismatch: object path points to a mesh!";
-                       return NULL;
-               }
-
-               return abc_reader->read_derivedmesh(dm, sample_sel, read_flag, err_str);
-       }
-       else if (ISubD::matches(header)) {
-               if (ob->type != OB_MESH) {
-                       *err_str = "Object type mismatch: object path points to a subdivision mesh!";
-                       return NULL;
-               }
-
-               return abc_reader->read_derivedmesh(dm, sample_sel, read_flag, err_str);
-       }
-       else if (IPoints::matches(header)) {
-               if (ob->type != OB_MESH) {
-                       *err_str = "Object type mismatch: object path points to a point cloud (requires a mesh object)!";
-                       return NULL;
-               }
-
-               return abc_reader->read_derivedmesh(dm, sample_sel, read_flag, err_str);
-       }
-       else if (ICurves::matches(header)) {
-               if (ob->type != OB_CURVE) {
-                       *err_str = "Object type mismatch: object path points to a curve!";
-                       return NULL;
-               }
-
-               return abc_reader->read_derivedmesh(dm, sample_sel, read_flag, err_str);
+       if (!abc_reader->accepts_object_type(header, ob, err_str)) {
+               /* err_str is set by acceptsObjectType() */
+               return NULL;
        }
 
-       *err_str = "Unsupported object type: verify object path"; // or poke developer
-       return NULL;
+       ISampleSelector sample_sel(time);
+       return abc_reader->read_derivedmesh(dm, sample_sel, read_flag, err_str);
 }
 
 /* ************************************************************************** */