bmesh py api: use generic checking macros to see if an object if from the same bmesh.
authorCampbell Barton <ideasman42@gmail.com>
Thu, 29 Nov 2012 03:25:37 +0000 (03:25 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 29 Nov 2012 03:25:37 +0000 (03:25 +0000)
- was such a common operation so this saves having exceptions set inline all over the place.

source/blender/python/bmesh/bmesh_py_types.c
source/blender/python/bmesh/bmesh_py_types.h
source/blender/python/bmesh/bmesh_py_types_customdata.c
source/blender/python/bmesh/bmesh_py_types_select.c
source/blender/python/bmesh/bmesh_py_utils.c

index 108581ac0da0e1116b60615c88a74e2418570aab..307bbc3f6adb12ed988aaa7f21d485316d60ae05 100644 (file)
@@ -582,17 +582,10 @@ static int bpy_bmfaceseq_active_set(BPy_BMElem *self, PyObject *value, void *UNU
                return 0;
        }
        else if (BPy_BMFace_Check(value)) {
-               BPY_BM_CHECK_INT(value);
+               BPY_BM_CHECK_SOURCE_INT(value, bm, "faces.active = f");
 
-               if (((BPy_BMFace *)value)->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "faces.active = f: f is from another mesh");
-                       return -1;
-               }
-               else {
-                       bm->act_face = ((BPy_BMFace *)value)->f;
-                       return 0;
-               }
+               bm->act_face = ((BPy_BMFace *)value)->f;
+               return 0;
        }
        else {
                PyErr_Format(PyExc_TypeError,
@@ -1281,13 +1274,7 @@ static PyObject *bpy_bmvert_copy_from_face_interp(BPy_BMVert *self, PyObject *ar
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(py_face);
-
-               if (py_face->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "BMVert.copy_from_face_interp(face): face is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(py_face, bm, "copy_from_face_interp()");
 
                BM_vert_interp_from_face(bm, self->v, py_face->f);
 
@@ -1421,13 +1408,7 @@ static PyObject *bpy_bmedge_other_vert(BPy_BMEdge *self, BPy_BMVert *value)
                return NULL;
        }
 
-       BPY_BM_CHECK_OBJ(value);
-
-       if (self->bm != value->bm) {
-               PyErr_SetString(PyExc_ValueError,
-                               "BMEdge.other_vert(vert): vert is from another mesh");
-               return NULL;
-       }
+       BPY_BM_CHECK_SOURCE_OBJ(value, self->bm, "BMEdge.other_vert(vert)");
 
        other = BM_edge_other_vert(self->e, value->v);
 
@@ -1481,13 +1462,7 @@ static PyObject *bpy_bmface_copy_from_face_interp(BPy_BMFace *self, PyObject *ar
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(py_face);
-
-               if (py_face->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "BMFace.copy_from_face_interp(face): face is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(py_face, bm, "BMFace.copy_from_face_interp(face)");
 
                BM_face_interp_from_face(bm, self->f, py_face->f);
 
@@ -1653,13 +1628,7 @@ static PyObject *bpy_bmloop_copy_from_face_interp(BPy_BMLoop *self, PyObject *ar
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(py_face);
-
-               if (py_face->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "BMLoop.copy_from_face_interp(face): face is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(py_face, bm, "BMLoop.copy_from_face_interp(face)");
 
                BM_loop_interp_from_face(bm, self->l, py_face->f, do_vertex, do_multires);
 
@@ -1955,13 +1924,7 @@ static PyObject *bpy_bmvertseq_remove(BPy_BMElemSeq *self, BPy_BMVert *value)
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(value);
-
-               if (value->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "verts.remove(vert): vert is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(value, bm, "verts.remove(vert)");
 
                BM_vert_kill(bm, value->v);
                bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
@@ -1985,13 +1948,7 @@ static PyObject *bpy_bmedgeseq_remove(BPy_BMElemSeq *self, BPy_BMEdge *value)
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(value);
-
-               if (value->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "edges.remove(edge): edge is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(value, bm, "edges.remove(edges)");
 
                BM_edge_kill(bm, value->e);
                bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
@@ -2015,13 +1972,7 @@ static PyObject *bpy_bmfaceseq_remove(BPy_BMElemSeq *self, BPy_BMFace *value)
        else {
                BMesh *bm = self->bm;
 
-               BPY_BM_CHECK_OBJ(value);
-
-               if (value->bm != bm) {
-                       PyErr_SetString(PyExc_ValueError,
-                                       "faces.remove(face): face is from another mesh");
-                       return NULL;
-               }
+               BPY_BM_CHECK_SOURCE_OBJ(value, bm, "faces.remove(face)");
 
                BM_face_kill(bm, value->f);
                bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
@@ -3450,6 +3401,21 @@ int bpy_bm_generic_valid_check(BPy_BMGeneric *self)
        }
 }
 
+int bpy_bm_generic_valid_check_source(BPy_BMGeneric *self, BMesh *bm_source, const char *error_prefix)
+{
+       int ret = bpy_bm_generic_valid_check(self);
+       if (LIKELY(ret == 0)) {
+               if (UNLIKELY(self->bm != bm_source)) {
+                       /* could give more info here */
+                       PyErr_Format(PyExc_ValueError,
+                                    "%.200s: BMesh data of type %.200s is from another mesh",
+                                    error_prefix, Py_TYPE(self)->tp_name);
+                       ret = -1;
+               }
+       }
+       return ret;
+}
+
 void bpy_bm_generic_invalidate(BPy_BMGeneric *self)
 {
        self->bm = NULL;
index 8de69c2d8a2d5308caa0ed2b324f16dbb0b635cf..d15918a3c11027cdc9bb60875380112fe13ac6d2 100644 (file)
@@ -158,9 +158,6 @@ PyObject *BPy_BMIter_CreatePyObject(BMesh *bm);
 
 PyObject *BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele); /* just checks type and creates v/e/f/l */
 
-int  bpy_bm_generic_valid_check(BPy_BMGeneric *self);
-void bpy_bm_generic_invalidate(BPy_BMGeneric *self);
-
 void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_size,
                                 const char htype,
                                 const char do_unique_check, const char do_bm_check,
@@ -171,9 +168,20 @@ int       BPy_BMElem_CheckHType(PyTypeObject *type, const char htype);
 char     *BPy_BMElem_StringFromHType_ex(const char htype, char ret[32]);
 char     *BPy_BMElem_StringFromHType(const char htype);
 
-
-#define BPY_BM_CHECK_OBJ(obj) if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return NULL; } (void)0
-#define BPY_BM_CHECK_INT(obj) if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return -1; }   (void)0
+void bpy_bm_generic_invalidate(BPy_BMGeneric *self);
+int  bpy_bm_generic_valid_check(BPy_BMGeneric *self);
+int  bpy_bm_generic_valid_check_source(BPy_BMGeneric *self, BMesh *bm_source, const char *error_prefix);
+
+#define BPY_BM_CHECK_OBJ(obj) \
+       if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return NULL; } (void)0
+#define BPY_BM_CHECK_INT(obj) \
+       if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return -1; }   (void)0
+
+/* macros like BPY_BM_CHECK_OBJ/BPY_BM_CHECK_INT that ensure we're from the right BMesh */
+#define BPY_BM_CHECK_SOURCE_OBJ(obj, bm, errmsg) \
+       if (UNLIKELY(bpy_bm_generic_valid_check_source((BPy_BMGeneric *)obj, bm, errmsg) == -1)) { return NULL; } (void)0
+#define BPY_BM_CHECK_SOURCE_INT(obj, bm, errmsg) \
+       if (UNLIKELY(bpy_bm_generic_valid_check_source((BPy_BMGeneric *)obj, bm, errmsg) == -1)) { return -1; }   (void)0
 
 #define BPY_BM_IS_VALID(obj) (LIKELY((obj)->bm != NULL))
 
index 14132d08fe64c81e01a069aed59fa50d7f82364b..fd31f3c40cc299c5d6002c4a76b1a06aefc747c6 100644 (file)
@@ -262,16 +262,10 @@ static PyObject *bpy_bmlayeritem_copy_from(BPy_BMLayerItem *self, BPy_BMLayerIte
        }
 
        BPY_BM_CHECK_OBJ(self);
-       BPY_BM_CHECK_OBJ(value);
-
-       if (self->bm != value->bm) {
-               PyErr_SetString(PyExc_ValueError,
-                               "layer.copy_from(): layer is from another mesh");
-               return NULL;
-       }
+       BPY_BM_CHECK_SOURCE_OBJ(value, self->bm, "layer.copy_from()");
 
-       else if ((self->htype != value->htype) ||
-                (self->type  != value->type))
+       if ((self->htype != value->htype) ||
+           (self->type  != value->type))
        {
                PyErr_SetString(PyExc_ValueError,
                                "layer.copy_from(other): layer type mismatch");
index 2ff731559d15437320d623369e375dada4613139..dfcfbeb0ab50d96aec5cdc27b7ebe5fff2ca4136 100644 (file)
@@ -114,13 +114,7 @@ static PyObject *bpy_bmeditselseq_add(BPy_BMEditSelSeq *self, BPy_BMElem *value)
                return NULL;
        }
 
-       BPY_BM_CHECK_OBJ(value);
-
-       if (self->bm != value->bm) {
-               PyErr_SetString(PyExc_ValueError,
-                               "Element is not from this mesh");
-               return NULL;
-       }
+       BPY_BM_CHECK_SOURCE_OBJ(value, self->bm, "select_history.add()");
 
        BM_select_history_store(self->bm, value->ele);
 
@@ -145,11 +139,9 @@ static PyObject *bpy_bmeditselseq_remove(BPy_BMEditSelSeq *self, BPy_BMElem *val
                return NULL;
        }
 
-       BPY_BM_CHECK_OBJ(value);
+       BPY_BM_CHECK_SOURCE_OBJ(value, self->bm, "select_history.remove()");
 
-       if ((self->bm != value->bm) ||
-           (BM_select_history_remove(self->bm, value->ele) == FALSE))
-       {
+       if (BM_select_history_remove(self->bm, value->ele) == FALSE) {
                PyErr_SetString(PyExc_ValueError,
                                "Element not found in selection history");
                return NULL;
index b70df53aff031175ebe7f05884ed2012e3a0a247..f85c33471046f7577c3ed3df6d49f1599422e9fa 100644 (file)
@@ -557,16 +557,10 @@ static PyObject *bpy_bm_utils_face_vert_separate(PyObject *UNUSED(self), PyObjec
                return NULL;
        }
 
-       BPY_BM_CHECK_OBJ(py_face);
-       BPY_BM_CHECK_OBJ(py_vert);
-
        bm = py_face->bm;
 
-       if (bm != py_vert->bm) {
-               PyErr_SetString(PyExc_ValueError,
-                               "mesh elements are from different meshes");
-               return NULL;
-       }
+       BPY_BM_CHECK_OBJ(py_face);
+       BPY_BM_CHECK_SOURCE_OBJ(py_vert, bm, "face_vert_separate()");
 
        l = BM_face_vert_share_loop(py_face->f, py_vert->v);