22eded1d11b0350c76bd285c0d437041bcc21314
[blender-staging.git] / source / blender / python / bmesh / bmesh_py_types.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/python/bmesh/bmesh_py_types.c
27  *  \ingroup pybmesh
28  */
29
30 #include <Python.h>
31
32 #include "BLI_math.h"
33
34 #include "BKE_customdata.h"
35
36 #include "bmesh.h"
37
38 #include "../mathutils/mathutils.h"
39
40 #include "../generic/py_capi_utils.h"
41
42 #include "bmesh_py_types.h" /* own include */
43
44 /* Common Flags
45  * ************ */
46
47 /* scene does not use BM_* flags. */
48 PyC_FlagSet bpy_bm_scene_vert_edge_face_flags[] = {
49     {1, "VERT"},
50     {2, "EDGE"},
51     {4, "FACE"},
52     {0, NULL}
53 };
54
55 PyC_FlagSet bpy_bm_htype_vert_edge_face_flags[] = {
56     {BM_VERT, "VERT"},
57     {BM_EDGE, "EDGE"},
58     {BM_FACE, "FACE"},
59     {0, NULL}
60 };
61
62 PyC_FlagSet bpy_bm_htype_all_flags[] = {
63     {BM_VERT, "VERT"},
64     {BM_LOOP, "EDGE"},
65     {BM_FACE, "FACE"},
66     {BM_LOOP, "LOOP"},
67     {0, NULL}
68 };
69
70 PyC_FlagSet bpy_bm_hflag_all_flags[] = {
71     {BM_ELEM_SELECT,  "SELECT"},
72     {BM_ELEM_HIDDEN,  "HIDE"},
73     {BM_ELEM_SEAM,    "SEAM"},
74     {BM_ELEM_SMOOTH,  "SMOOTH"},
75     {BM_ELEM_TAG,     "TAG"},
76     {0, NULL}
77 };
78
79 /* py-type definitions
80  * ******************* */
81
82 /* getseters
83  * ========= */
84
85
86 /* bmesh elems
87  * ----------- */
88
89 PyDoc_STRVAR(bpy_bm_elem_select_doc,  "Selected state of this element.\n\n:type: boolean");
90 PyDoc_STRVAR(bpy_bm_elem_hide_doc,    "Hidden state of this element.\n\n:type: boolean");
91 PyDoc_STRVAR(bpy_bm_elem_tag_doc,     "Generic attribute scripts can use for own logic\n\n:type: boolean");
92 PyDoc_STRVAR(bpy_bm_elem_smooth_doc,  "Smooth state of this element.\n\n:type: boolean");
93
94
95 static PyObject *bpy_bm_elem_hflag_get(BPy_BMElem *self, void *flag)
96 {
97         const char hflag = (char)GET_INT_FROM_POINTER(flag);
98
99         BPY_BM_CHECK_OBJ(self);
100
101         return PyBool_FromLong(BM_elem_flag_test(self->ele, hflag));
102 }
103
104 static int bpy_bm_elem_hflag_set(BPy_BMElem *self, PyObject *value, void *flag)
105 {
106         const char hflag = (char)GET_INT_FROM_POINTER(flag);
107         int param;
108
109         BPY_BM_CHECK_INT(self);
110
111         param = PyLong_AsLong(value);
112
113         if (param == TRUE) {
114                 BM_elem_flag_enable(self->ele, hflag);
115                 return 0;
116         }
117         else if (param == FALSE) {
118                 BM_elem_flag_disable(self->ele, hflag);
119                 return 0;
120         }
121         else {
122                 PyErr_Format(PyExc_TypeError,
123                              "expected True/False or 0/1, not %.200s",
124                              Py_TYPE(value)->tp_name);
125                 return -1;
126         }
127 }
128
129 PyDoc_STRVAR(bpy_bm_elem_index_doc,
130 "Index of this element.\n"
131 "\n"
132 ":type: int\n"
133 "\n"
134 ".. note::\n"
135 "\n"
136 "   This value is not necessarily valid, while editing the mesh it can become *dirty*.\n"
137 "\n"
138 "   It's also possible to assign any number to this attribute for a scripts internal logic.\n"
139 "\n"
140 "   To ensure the value is up to date - see :class:`BMesh.update` **index** argument.\n"
141 );
142 static PyObject *bpy_bm_elem_index_get(BPy_BMElem *self, void *UNUSED(flag))
143 {
144         BPY_BM_CHECK_OBJ(self);
145
146         return PyLong_FromLong(BM_elem_index_get(self->ele));
147 }
148
149 static int bpy_bm_elem_index_set(BPy_BMElem *self, PyObject *value, void *UNUSED(flag))
150 {
151         int param;
152
153         BPY_BM_CHECK_INT(self);
154
155         param = PyLong_AsLong(value);
156
157         if (param == -1 && PyErr_Occurred()) {
158                 PyErr_SetString(PyExc_TypeError, "expected an int type");
159                 return -1;
160         }
161         else {
162                 BM_elem_index_set(self->ele, param); /* set_dirty! */
163
164                 /* when setting the index assume its set invalid */
165                 if (self->ele->htype & (BM_VERT | BM_EDGE | BM_FACE)) {
166                         self->bm->elem_index_dirty |= self->ele->htype;
167                 }
168
169                 return 0;
170         }
171 }
172
173 /* type spesific get/sets
174  * ---------------------- */
175
176
177 /* Mesh
178  * ^^^^ */
179
180 /* doc-strings for all uses of this funcion */
181 PyDoc_STRVAR(bpy_bmesh_verts_doc,
182 "This meshes vert sequence (readonly).\n\n:type: :class:`BMElemSeq`"
183 );
184 PyDoc_STRVAR(bpy_bmesh_edges_doc,
185 "This meshes edge sequence (readonly).\n\n:type: :class:`BMElemSeq`"
186 );
187 PyDoc_STRVAR(bpy_bmesh_faces_doc,
188 "This meshes face sequence (readonly).\n\n:type: :class:`BMElemSeq`"
189 );
190
191 static PyObject *bpy_bmelemseq_get(BPy_BMesh *self, void *itype)
192 {
193         BPY_BM_CHECK_OBJ(self);
194         return BPy_BMElemSeq_CreatePyObject(self->bm, NULL, GET_INT_FROM_POINTER(itype));
195 }
196
197 /* vert */
198 PyDoc_STRVAR(bpy_bmvert_link_edges_doc,
199 "Edges connected to this vertex (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
200 );
201 PyDoc_STRVAR(bpy_bmvert_link_faces_doc,
202 "Faces connected to this vertex (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMFace`"
203 );
204 PyDoc_STRVAR(bpy_bmvert_link_loops_doc,
205 "Loops that use this vertex (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
206 );
207 /* edge */
208 PyDoc_STRVAR(bpy_bmedge_verts_doc,
209 "Verts this edge uses (always 2), (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
210 );
211 PyDoc_STRVAR(bpy_bmedge_link_faces_doc,
212 "Faces connected to this edge, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMFace`"
213 );
214 PyDoc_STRVAR(bpy_bmedge_link_loops_doc,
215 "Loops connected to this edge, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
216 );
217 /* face */
218 PyDoc_STRVAR(bpy_bmface_verts_doc,
219 "Verts of this face, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
220 );
221 PyDoc_STRVAR(bpy_bmface_edges_doc,
222 "Edges of this face, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMEdge`"
223 );
224 PyDoc_STRVAR(bpy_bmface_loops_doc,
225 "Loops of this face, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
226 );
227 /* loop */
228 PyDoc_STRVAR(bpy_bmloops_link_loops_doc,
229 "Loops connected to this loop, (readonly).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
230 );
231
232 static PyObject *bpy_bmelemseq_elem_get(BPy_BMElem *self, void *itype)
233 {
234         BPY_BM_CHECK_OBJ(self);
235         return BPy_BMElemSeq_CreatePyObject(self->bm, self, GET_INT_FROM_POINTER(itype));
236 }
237
238 PyDoc_STRVAR(bpy_bm_is_valid_doc,
239 "True when this element is valid (hasn't been removed).\n\n:type: boolean"
240 );
241 static PyObject *bpy_bm_is_valid_get(BPy_BMGeneric *self)
242 {
243         return PyBool_FromLong(self->bm != NULL);
244 }
245
246
247 PyDoc_STRVAR(bpy_bmesh_select_mode_doc,
248 "The selection mode, values can be {'VERT', 'EDGE', 'FACE'}, can't be assigned an empty set.\n\n:type: set"
249 );
250 static PyObject *bpy_bmesh_select_mode_get(BPy_BMesh *self)
251 {
252         BPY_BM_CHECK_OBJ(self);
253
254         return PyC_FlagSet_FromBitfield(bpy_bm_scene_vert_edge_face_flags, self->bm->selectmode);
255 }
256
257 static int bpy_bmesh_select_mode_set(BPy_BMesh *self, PyObject *value)
258 {
259         int flag = 0;
260         BPY_BM_CHECK_INT(self);
261
262         if (PyC_FlagSet_ToBitfield(bpy_bm_scene_vert_edge_face_flags, value, &flag, "bm.select_mode") == -1) {
263                 return -1;
264         }
265         else if (flag == 0) {
266                 PyErr_SetString(PyExc_TypeError, "bm.select_mode: cant assignt an empty value");
267                 return -1;
268         }
269         else {
270                 self->bm->selectmode = flag;
271                 return 0;
272         }
273 }
274
275
276 /* Vert
277  * ^^^^ */
278
279 PyDoc_STRVAR(bpy_bmvert_co_doc,
280 "The coordinates for this vertex as a 3D, wrapped vector.\n\n:type: :class:`mathutils.Vector`"
281 );
282 static PyObject *bpy_bmvert_co_get(BPy_BMVert *self)
283 {
284         BPY_BM_CHECK_OBJ(self);
285         return Vector_CreatePyObject(self->v->co, 3, Py_WRAP, NULL);
286 }
287
288 static int bpy_bmvert_co_set(BPy_BMVert *self, PyObject *value)
289 {
290         BPY_BM_CHECK_INT(self);
291
292         if (mathutils_array_parse(self->v->co, 3, 3, value, "BMVert.co") != -1) {
293                 return 0;
294         }
295         else {
296                 return -1;
297         }
298 }
299
300 PyDoc_STRVAR(bpy_bmvert_normal_doc,
301 "The normal for this vertex as a 3D, wrapped vector.\n\n:type: :class:`mathutils.Vector`"
302 );
303 static PyObject *bpy_bmvert_normal_get(BPy_BMVert *self)
304 {
305         BPY_BM_CHECK_OBJ(self);
306         return Vector_CreatePyObject(self->v->no, 3, Py_WRAP, NULL);
307 }
308
309 static int bpy_bmvert_normal_set(BPy_BMVert *self, PyObject *value)
310 {
311         BPY_BM_CHECK_INT(self);
312
313         if (mathutils_array_parse(self->v->no, 3, 3, value, "BMVert.normal") != -1) {
314                 return 0;
315         }
316         else {
317                 return -1;
318         }
319 }
320
321 PyDoc_STRVAR(bpy_bmvert_is_manifold_doc,
322 "True when this vertex is manifold (readonly).\n\n:type: boolean"
323 );
324 static PyObject *bpy_bmvert_is_manifold_get(BPy_BMVert *self)
325 {
326         BPY_BM_CHECK_OBJ(self);
327         return PyBool_FromLong(BM_vert_is_manifold(self->bm, self->v));
328 }
329
330 PyDoc_STRVAR(bpy_bmvert_is_wire_doc,
331 "True when this vertex is not connected to any faces (readonly).\n\n:type: boolean"
332 );
333 static PyObject *bpy_bmvert_is_wire_get(BPy_BMVert *self)
334 {
335         BPY_BM_CHECK_OBJ(self);
336         return PyBool_FromLong(BM_vert_is_wire(self->bm, self->v));
337 }
338
339 /* Edge
340  * ^^^^ */
341
342 PyDoc_STRVAR(bpy_bmedge_is_manifold_doc,
343 "True when this edge is manifold (readonly).\n\n:type: boolean"
344 );
345 static PyObject *bpy_bmedge_is_manifold_get(BPy_BMEdge *self)
346 {
347         BPY_BM_CHECK_OBJ(self);
348         return PyBool_FromLong(BM_edge_is_manifold(self->bm, self->e));
349 }
350
351 PyDoc_STRVAR(bpy_bmedge_is_wire_doc,
352 "True when this edge is not connected to any faces (readonly).\n\n:type: boolean"
353 );
354 static PyObject *bpy_bmedge_is_wire_get(BPy_BMEdge *self)
355 {
356         BPY_BM_CHECK_OBJ(self);
357         return PyBool_FromLong(BM_edge_is_wire(self->bm, self->e));
358 }
359
360 PyDoc_STRVAR(bpy_bmedge_is_boundry_doc,
361 "True when this edge is at the boundry of a face (readonly).\n\n:type: boolean"
362 );
363 static PyObject *bpy_bmedge_is_boundry_get(BPy_BMEdge *self)
364 {
365         BPY_BM_CHECK_OBJ(self);
366         return PyBool_FromLong(BM_edge_is_boundry(self->e));
367 }
368
369 /* Face
370  * ^^^^ */
371
372 PyDoc_STRVAR(bpy_bmface_normal_doc,
373 "The normal for this face as a 3D, wrapped vector.\n\n:type: boolean"
374 );
375 static PyObject *bpy_bmface_normal_get(BPy_BMFace *self)
376 {
377         BPY_BM_CHECK_OBJ(self);
378         return Vector_CreatePyObject(self->f->no, 3, Py_WRAP, NULL);
379 }
380
381 static int bpy_bmface_normal_set(BPy_BMFace *self, PyObject *value)
382 {
383         BPY_BM_CHECK_INT(self);
384
385         if (mathutils_array_parse(self->f->no, 3, 3, value, "BMFace.normal") != -1) {
386                 return 0;
387         }
388         else {
389                 return -1;
390         }
391 }
392
393 static PyGetSetDef bpy_bmesh_getseters[] = {
394     {(char *)"verts", (getter)bpy_bmelemseq_get, (setter)NULL, (char *)bpy_bmesh_verts_doc, (void *)BM_VERTS_OF_MESH},
395     {(char *)"edges", (getter)bpy_bmelemseq_get, (setter)NULL, (char *)bpy_bmesh_edges_doc, (void *)BM_EDGES_OF_MESH},
396     {(char *)"faces", (getter)bpy_bmelemseq_get, (setter)NULL, (char *)bpy_bmesh_faces_doc, (void *)BM_FACES_OF_MESH},
397     {(char *)"select_mode", (getter)bpy_bmesh_select_mode_get, (setter)bpy_bmesh_select_mode_set, (char *)bpy_bmesh_select_mode_doc, NULL},
398
399     /* readonly checks */
400     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get, (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
401
402     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
403 };
404
405 static PyGetSetDef bpy_bmvert_getseters[] = {
406     /* generic */
407     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
408     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
409     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
410     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
411
412     {(char *)"co",     (getter)bpy_bmvert_co_get,     (setter)bpy_bmvert_co_set,     (char *)bpy_bmvert_co_doc, NULL},
413     {(char *)"normal", (getter)bpy_bmvert_normal_get, (setter)bpy_bmvert_normal_set, (char *)bpy_bmvert_normal_doc, NULL},
414
415     /* connectivity data */
416     {(char *)"link_edges", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_edges_doc, (void *)BM_EDGES_OF_VERT},
417     {(char *)"link_faces", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_faces_doc, (void *)BM_FACES_OF_VERT},
418     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_loops_doc, (void *)BM_LOOPS_OF_VERT},
419
420     /* readonly checks */
421     {(char *)"is_manifold",  (getter)bpy_bmvert_is_manifold_get,  (setter)NULL, (char *)bpy_bmvert_is_manifold_doc, NULL},
422     {(char *)"is_wire",      (getter)bpy_bmvert_is_wire_get,      (setter)NULL, (char *)bpy_bmvert_is_wire_doc, NULL},
423     {(char *)"is_valid",     (getter)bpy_bm_is_valid_get,         (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
424
425     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
426 };
427
428 static PyGetSetDef bpy_bmedge_getseters[] = {
429     /* generic */
430     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
431     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
432     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
433     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
434
435     {(char *)"smooth", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SMOOTH},
436     {(char *)"seam",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SEAM},
437
438     /* connectivity data */
439     {(char *)"verts", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_verts_doc, (void *)BM_VERTS_OF_EDGE},
440
441     {(char *)"link_faces", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_link_faces_doc, (void *)BM_FACES_OF_EDGE},
442     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_link_loops_doc, (void *)BM_LOOPS_OF_EDGE},
443
444     /* readonly checks */
445     {(char *)"is_manifold",  (getter)bpy_bmedge_is_manifold_get,  (setter)NULL, (char *)bpy_bmedge_is_manifold_doc, NULL},
446     {(char *)"is_wire",      (getter)bpy_bmedge_is_wire_get,      (setter)NULL, (char *)bpy_bmedge_is_wire_doc, NULL},
447     {(char *)"is_boundry",   (getter)bpy_bmedge_is_boundry_get,   (setter)NULL, (char *)bpy_bmedge_is_boundry_doc, NULL},
448     {(char *)"is_valid",     (getter)bpy_bm_is_valid_get,         (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
449
450     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
451 };
452
453 static PyGetSetDef bpy_bmface_getseters[] = {
454     /* generic */
455     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
456     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
457     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
458     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
459
460     {(char *)"smooth", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SMOOTH},
461
462     {(char *)"normal", (getter)bpy_bmface_normal_get, (setter)bpy_bmface_normal_set, (char *)bpy_bmface_normal_doc, NULL},
463
464     /* connectivity data */
465     {(char *)"verts", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_verts_doc, (void *)BM_VERTS_OF_FACE},
466     {(char *)"edges", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_edges_doc, (void *)BM_EDGES_OF_FACE},
467     {(char *)"loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_loops_doc, (void *)BM_LOOPS_OF_FACE},
468
469     /* readonly checks */
470     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get, (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
471
472     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
473 };
474
475 static PyGetSetDef bpy_bmloop_getseters[] = {
476     /* generic */
477     // flags are available but not used for loops.
478     // {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
479     // {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
480     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
481     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
482
483     /* connectivity data */
484     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmloops_link_loops_doc, (void *)BM_LOOPS_OF_LOOP},
485
486     /* readonly checks */
487     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get, (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
488
489     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
490 };
491
492
493 /* Methods
494  * ======= */
495
496 /* Mesh
497  * ---- */
498
499 PyDoc_STRVAR(bpy_bmesh_select_flush_mode_doc,
500 ".. method:: select_flush_mode()\n"
501 "\n"
502 "   flush selection based on the current mode current :class:`BMesh.select_mode`.\n"
503 );
504 static PyObject *bpy_bmesh_select_flush_mode(BPy_BMesh *self)
505 {
506         BPY_BM_CHECK_OBJ(self);
507
508         BM_mesh_select_mode_flush(self->bm);
509
510         Py_RETURN_NONE;
511 }
512
513 PyDoc_STRVAR(bpy_bmesh_select_flush_doc,
514 ".. method:: select_flush(select)\n"
515 "\n"
516 "   Flush selection, independant of the current selection mode.\n"
517 "\n"
518 "   :arg select: flush selection or de-selected elements.\n"
519 "   :type select: boolean\n"
520 );
521 static PyObject *bpy_bmesh_select_flush(BPy_BMesh *self, PyObject *value)
522 {
523         int param;
524
525         BPY_BM_CHECK_OBJ(self);
526
527         param = PyLong_AsLong(value);
528         if (param != FALSE && param != TRUE) {
529                 PyErr_SetString(PyExc_TypeError, "expected a boolean type 0/1");
530                 return NULL;
531         }
532
533         if (param)  BM_mesh_select_flush(self->bm);
534         else        BM_mesh_deselect_flush(self->bm);
535
536         Py_RETURN_NONE;
537 }
538
539 PyDoc_STRVAR(bpy_bmesh_normal_update_doc,
540 ".. method:: normal_update(skip_hidden=False)\n"
541 "\n"
542 "   Update mesh normals.\n"
543 "\n"
544 "   :arg skip_hidden: When True hidden elements are ignored.\n"
545 "   :type skip_hidden: boolean\n"
546
547 );
548 static PyObject *bpy_bmesh_normal_update(BPy_BMElem *self, PyObject *args)
549 {
550
551         int skip_hidden = FALSE;
552
553         BPY_BM_CHECK_OBJ(self);
554
555         if (!PyArg_ParseTuple(args, "|i:normal_update", &skip_hidden)) {
556                 return NULL;
557         }
558
559         BM_mesh_normals_update(self->bm, skip_hidden);
560
561         Py_RETURN_NONE;
562 }
563
564
565 PyDoc_STRVAR(bpy_bmesh_transform_doc,
566 ".. method:: transform(matrix, filter=None)\n"
567 "\n"
568 "   Transform the mesh (optionally filtering flagged data only).\n"
569 "\n"
570 "   :arg matrix: transform matrix.\n"
571 "   :type matrix: 4x4 :class:`mathutils.Matrix`\n"
572 "   :arg filter: set of values in ('SELECT', 'HIDE', 'SEAM', 'SMOOTH', 'TAG').\n"
573 "   :type filter: set\n"
574 );
575 static PyObject *bpy_bmesh_transform(BPy_BMElem *self, PyObject *args, PyObject *kw)
576 {
577         static const char *kwlist[] = {"matrix", "filter", NULL};
578
579         MatrixObject *mat;
580         PyObject *filter = NULL;
581         int filter_flags = 0;
582
583         BPY_BM_CHECK_OBJ(self);
584
585         if (!PyArg_ParseTupleAndKeywords(args, kw,
586                                          "O!|O!:transform",
587                                          (char **)kwlist,
588                                          &matrix_Type, &mat,
589                                          &PySet_Type,  &filter))
590         {
591                 return NULL;
592         }
593         else {
594                 BMVert *eve;
595                 BMIter iter;
596                 void *mat_ptr;
597
598                 if (BaseMath_ReadCallback(mat) == -1) {
599                         return NULL;
600                 }
601                 else if (mat->num_col != 4 || mat->num_row != 4) {
602                         PyErr_SetString(PyExc_ValueError, "expected a 4x4 matrix");
603                         return NULL;
604                 }
605
606                 if (filter != NULL && PyC_FlagSet_ToBitfield(bpy_bm_hflag_all_flags, filter,
607                                                              &filter_flags, "bm.transform") == -1)
608                 {
609                         return NULL;
610                 }
611
612                 mat_ptr = mat->matrix;
613
614                 if (!filter_flags) {
615                         BM_ITER(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
616                                 mul_m4_v3((float (*)[4])mat_ptr, eve->co);
617                         }
618                 }
619                 else {
620                         char filter_flags_ch = (char)filter_flags;
621                         BM_ITER(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
622                                 if (eve->head.hflag & filter_flags_ch) {
623                                         mul_m4_v3((float (*)[4])mat_ptr, eve->co);
624                                 }
625                         }
626                 }
627         }
628
629         Py_RETURN_NONE;
630 }
631
632 /* Elem
633  * ---- */
634
635 PyDoc_STRVAR(bpy_bm_elem_select_set_doc,
636 ".. method:: select_set(select)\n"
637 "\n"
638 "   Set the selection.\n"
639 "   This is different from the *select* attribute because it updates the selection state of assosiated geometry..\n"
640 "\n"
641 "   :arg select: Select or de-select.\n"
642 "   :type select: boolean\n"
643 );
644 static PyObject *bpy_bm_elem_select_set(BPy_BMElem *self, PyObject *value)
645 {
646         int param;
647
648         BPY_BM_CHECK_OBJ(self);
649
650         param = PyLong_AsLong(value);
651         if (param != FALSE && param != TRUE) {
652                 PyErr_SetString(PyExc_TypeError, "expected a boolean type 0/1");
653                 return NULL;
654         }
655
656         BM_elem_select_set(self->bm, self->ele, param);
657
658         Py_RETURN_NONE;
659 }
660
661 PyDoc_STRVAR(bpy_bm_elem_copy_from_doc,
662 ".. method:: copy_from(select)\n"
663 "\n"
664 "   Copy values from another element.\n"
665 );
666 static PyObject *bpy_bm_elem_copy_from(BPy_BMElem *self, BPy_BMElem *value)
667 {
668         BPY_BM_CHECK_OBJ(self);
669
670         if (Py_TYPE(self) != Py_TYPE(value)) {
671                 PyErr_Format(PyExc_TypeError,
672                              "expected element of type '%.200s' not '%.200s'",
673                              Py_TYPE(self)->tp_name, Py_TYPE(value)->tp_name);
674                 return NULL;
675         }
676
677         BM_elem_attrs_copy(value->bm, self->bm, value->ele, self->ele);
678
679         Py_RETURN_NONE;
680 }
681
682
683 /* Vert
684  * ---- */
685
686 PyDoc_STRVAR(bpy_bmvert_calc_edge_angle_doc,
687 ".. method:: calc_edge_angle()\n"
688 "\n"
689 "   Return the angle between 2 connected edges.\n"
690 );
691 static PyObject *bpy_bmvert_calc_edge_angle(BPy_BMVert *self)
692 {
693         BPY_BM_CHECK_OBJ(self);
694         return PyFloat_FromDouble(BM_vert_edge_angle(self->bm, self->v));
695 }
696
697 /* Edge
698  * ---- */
699
700 PyDoc_STRVAR(bpy_bmedge_calc_face_angle_doc,
701 ".. method:: calc_face_angle()\n"
702 "\n"
703 "   Return the angle between 2 connected faces.\n"
704 "\n"
705 "   :return: The angle between both faces in radians.\n"
706 "   :rtype: float\n"
707 );
708 static PyObject *bpy_bmedge_calc_face_angle(BPy_BMEdge *self)
709 {
710         BPY_BM_CHECK_OBJ(self);
711         return PyFloat_FromDouble(BM_edge_face_angle(self->bm, self->e));
712 }
713
714 PyDoc_STRVAR(bpy_bmedge_other_vert_doc,
715 ".. method:: other_vert(vert)\n"
716 "\n"
717 "   Return the other vertex on this edge or None if the vertex is not used by this edge.\n"
718 "\n"
719 "   :arg vert: a vert in this edge.\n"
720 "   :type vert: :class:`BMVert`\n"
721 "   :return: The edges other vert.\n"
722 "   :rtype: :class:`BMVert` or None\n"
723 );
724 static PyObject *bpy_bmedge_other_vert(BPy_BMEdge *self, BPy_BMVert *value)
725 {
726         BMVert *other;
727         BPY_BM_CHECK_OBJ(self);
728
729         if (!BPy_BMVert_Check(value)) {
730                 PyErr_Format(PyExc_TypeError,
731                              "BMEdge.other_vert(vert): BMVert expected, not '%.200s'",
732                              Py_TYPE(value)->tp_name);
733                 return NULL;
734         }
735
736         BPY_BM_CHECK_OBJ(value);
737
738         if (self->bm != value->bm) {
739                 PyErr_SetString(PyExc_TypeError,
740                                 "BMEdge.other_vert(vert): vert is from another mesh");
741                 return NULL;
742         }
743
744         other = BM_edge_other_vert(self->e, value->v);
745
746         if (other) {
747                 return BPy_BMVert_CreatePyObject(self->bm, other);
748         }
749         else {
750                 /* could raise an exception here */
751                 Py_RETURN_NONE;
752         }
753 }
754
755 /* Face
756  * ---- */
757
758 PyDoc_STRVAR(bpy_bmface_copy_doc,
759 ".. method:: copy(verts=True, edges=True)\n"
760 "\n"
761 "   Make a copy of this face.\n"
762 "\n"
763 "   :arg verts: When set, the faces verts will be duplicated too.\n"
764 "   :type verts: boolean\n"
765 "   :arg edges: When set, the faces edges will be duplicated too.\n"
766 "   :type edges: boolean\n"
767 "   :return: The newly created face.\n"
768 "   :rtype: :class:`BMFace`\n"
769 );
770 static PyObject *bpy_bmface_copy(BPy_BMFace *self, PyObject *args, PyObject *kw)
771 {
772         static const char *kwlist[] = {"verts", "edges", NULL};
773
774         BMesh *bm = self->bm;
775         int do_verts = TRUE;
776         int do_edges = TRUE;
777
778         BMFace *f_cpy;
779         BPY_BM_CHECK_OBJ(self);
780
781         if (!PyArg_ParseTupleAndKeywords(args, kw,
782                                          "|ii:BMFace.copy",
783                                          (char **)kwlist,
784                                          &do_verts, &do_edges))
785         {
786                 return NULL;
787         }
788
789         f_cpy = BM_face_copy(bm, self->f, do_verts, do_edges);
790
791         if (f_cpy) {
792                 return BPy_BMFace_CreatePyObject(bm, f_cpy);
793         }
794         else {
795                 PyErr_SetString(PyExc_ValueError,
796                                 "BMFace.copy(): couldn't create the new face, internal error");
797                 return NULL;
798         }
799 }
800
801 PyDoc_STRVAR(bpy_bmface_calc_area_doc,
802 ".. method:: calc_area()\n"
803 "\n"
804 "   Return the area of the face.\n"
805 "\n"
806 "   :return: Return the area of the face.\n"
807 "   :rtype: float\n"
808 );
809 static PyObject *bpy_bmface_calc_area(BPy_BMFace *self)
810 {
811         BPY_BM_CHECK_OBJ(self);
812         return PyFloat_FromDouble(BM_face_area_calc(self->bm, self->f));
813 }
814
815 PyDoc_STRVAR(bpy_bmface_calc_center_mean_doc,
816 ".. method:: calc_center_median()\n"
817 "\n"
818 "   Return median center of the face.\n"
819 "\n"
820 "   :return: a 3D vector.\n"
821 "   :rtype: :class:`mathutils.Vector`\n"
822 );
823 static PyObject *bpy_bmface_calc_center_mean(BPy_BMFace *self)
824 {
825         float cent[3];
826
827         BPY_BM_CHECK_OBJ(self);
828         BM_face_center_mean_calc(self->bm, self->f, cent);
829         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
830 }
831
832 PyDoc_STRVAR(bpy_bmface_calc_center_bounds_doc,
833 ".. method:: calc_center_bounds()\n"
834 "\n"
835 "   Return bounds center of the face.\n"
836 "\n"
837 "   :return: a 3D vector.\n"
838 "   :rtype: :class:`mathutils.Vector`\n"
839 );
840 static PyObject *bpy_bmface_calc_center_bounds(BPy_BMFace *self)
841 {
842         float cent[3];
843
844         BPY_BM_CHECK_OBJ(self);
845         BM_face_center_bounds_calc(self->bm, self->f, cent);
846         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
847 }
848
849 /* Loop
850  * ---- */
851
852 PyDoc_STRVAR(bpy_bmloop_calc_face_angle_doc,
853 ".. method:: calc_face_angle()\n"
854 "\n"
855 "   Return angle at this loops corner of the face.\n"
856 "   This is calculated so sharper corners give lower angles.\n"
857 "\n"
858 "   :return: The angle in radians.\n"
859 "   :rtype: float\n"
860 );
861 static PyObject *bpy_bmloop_calc_face_angle(BPy_BMLoop *self)
862 {
863         BPY_BM_CHECK_OBJ(self);
864         return PyFloat_FromDouble(BM_loop_face_angle(self->bm, self->l));
865 }
866
867 /* Vert Seq
868  * -------- */
869
870 static PyObject *bpy_bmvert_seq_new(BPy_BMElemSeq *self, PyObject *args)
871 {
872         PyObject *py_co = NULL;
873         BPy_BMVert *py_vert_example = NULL; /* optional */
874
875         BPY_BM_CHECK_OBJ(self);
876
877         if (!PyArg_ParseTuple(args, "|OO!:verts.new",
878                               py_co,
879                               &BPy_BMVert_Type, &py_vert_example)) {
880                 return NULL;
881         }
882         else {
883                 BMesh *bm = self->bm;
884                 BMVert *v;
885                 float co[3] = {0.0f, 0.0f, 0.0f};
886
887                 if (py_vert_example) {
888                         BPY_BM_CHECK_OBJ(py_vert_example);
889                 }
890
891                 if (py_co && mathutils_array_parse(co, 3, 3, py_co, "verts.new(co)") != -1) {
892                         return NULL;
893                 }
894
895                 v = BM_vert_create(bm, co, NULL);
896
897                 if (v == NULL) {
898                         PyErr_SetString(PyExc_ValueError,
899                                         "faces.new(verts): couldn't create the new face, internal error");
900                         return NULL;
901                 }
902
903                 if (py_vert_example) {
904                         BM_elem_attrs_copy(py_vert_example->bm, bm, py_vert_example->v, v);
905                 }
906
907                 return BPy_BMVert_CreatePyObject(bm, v);
908         }
909 }
910
911
912 /* Edge Seq
913  * -------- */
914
915 static PyObject *bpy_bmedge_seq_new(BPy_BMElemSeq *self, PyObject *args)
916 {
917         BPy_BMVert *v1;
918         BPy_BMVert *v2;
919         BPy_BMEdge *py_edge_example = NULL; /* optional */
920
921         BPY_BM_CHECK_OBJ(self);
922
923         if (!PyArg_ParseTuple(args, "O!O!|O!:edges.new",
924                               &BPy_BMVert_Type, &v1,
925                               &BPy_BMVert_Type, &v2,
926                               &BPy_BMEdge_Type, &py_edge_example))
927         {
928                 return NULL;
929         }
930         else {
931                 BMesh *bm = self->bm;
932                 BMEdge *e;
933
934                 if (py_edge_example) {
935                         BPY_BM_CHECK_OBJ(py_edge_example);
936                 }
937
938                 if (v1->v == v2->v) {
939                         PyErr_SetString(PyExc_ValueError,
940                                         "edges.new(): both verts are the same");
941                 }
942
943                 if (!(bm == v1->bm && bm == v2->bm)) {
944                         PyErr_SetString(PyExc_ValueError,
945                                         "edges.new(): both verts must be from this mesh");
946                 }
947
948                 if (BM_edge_exists(v1->v, v2->v)) {
949                         PyErr_SetString(PyExc_ValueError,
950                                         "edges.new(): this edge exists");
951                 }
952
953                 e = BM_edge_create(bm, v1->v, v2->v, NULL, FALSE);
954
955                 if (e == NULL) {
956                         PyErr_SetString(PyExc_ValueError,
957                                         "faces.new(verts): couldn't create the new face, internal error");
958                         return NULL;
959                 }
960
961                 if (py_edge_example) {
962                         BM_elem_attrs_copy(py_edge_example->bm, bm, py_edge_example->e, e);
963                 }
964
965                 return BPy_BMEdge_CreatePyObject(bm, e);
966         }
967 }
968
969
970 /* Face Seq
971  * -------- */
972
973 static PyObject *bpy_bmface_seq_new(BPy_BMElemSeq *self, PyObject *args)
974 {
975         PyObject *vert_seq;
976         BPy_BMFace *py_face_example = NULL; /* optional */
977
978         BPY_BM_CHECK_OBJ(self);
979
980         if (!PyArg_ParseTuple(args, "O|O!:faces.new",
981                               &vert_seq,
982                               &BPy_BMFace_Type, &py_face_example)) {
983                 return NULL;
984         }
985         else {
986                 BMesh *bm = self->bm;
987                 Py_ssize_t vert_seq_len;
988                 Py_ssize_t i, i_prev;
989
990                 BMVert **vert_array = NULL;
991                 BMEdge **edge_array = NULL;
992
993                 PyObject *ret = NULL;
994
995                 BMFace *f_new;
996
997                 if (py_face_example) {
998                         BPY_BM_CHECK_OBJ(py_face_example);
999                 }
1000
1001                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 3, PY_SSIZE_T_MAX,
1002                                                        &vert_seq_len, &BPy_BMVert_Type,
1003                                                        TRUE, TRUE, "faces.new(...)");
1004
1005                 /* check if the face exists */
1006                 if (BM_face_exists(bm, vert_array, vert_seq_len, NULL)) {
1007                         PyErr_SetString(PyExc_ValueError,
1008                                         "faces.new(verts): face already exists");
1009                         goto cleanup;
1010                 }
1011
1012                 /* Go ahead and make the face!
1013                  * --------------------------- */
1014
1015                 edge_array = (BMEdge **)PyMem_MALLOC(vert_seq_len * sizeof(BMEdge **));
1016
1017                 /* ensure edges */
1018                 for (i = 0, i_prev = vert_seq_len - 1; i < vert_seq_len; (i_prev=i++)) {
1019                         edge_array[i] = BM_edge_create(bm, vert_array[i], vert_array[i_prev], NULL, TRUE);
1020                 }
1021
1022                 f_new = BM_face_create(bm, vert_array, edge_array, vert_seq_len, FALSE);
1023
1024                 if (f_new == NULL) {
1025                         PyErr_SetString(PyExc_ValueError,
1026                                         "faces.new(verts): couldn't create the new face, internal error");
1027                         goto cleanup;
1028                 }
1029
1030                 if (py_face_example) {
1031                         BM_elem_attrs_copy(py_face_example->bm, bm, py_face_example->f, f_new);
1032                 }
1033
1034                 ret = BPy_BMFace_CreatePyObject(bm, f_new);
1035
1036                 /* pass through */
1037 cleanup:
1038                 if (vert_array) PyMem_FREE(vert_array);
1039                 if (edge_array) PyMem_FREE(edge_array);
1040                 return ret;
1041         }
1042 }
1043
1044 /* Elem Seq
1045  * -------- */
1046
1047 /* eek - 3 docstrings in 1!, we might need to split up this seq at some point */
1048 PyDoc_STRVAR(bpy_bmelemseq_new_doc,
1049 ".. method:: new(co=(0.0, 0.0, 0.0), example=None)\n"
1050 "\n"
1051 "   *Vertex Sequence*\n"
1052 "\n"
1053 "   :arg co: The initial location of the vertex (optional argument).\n"
1054 "   :type co: float triplet\n"
1055 "   :arg example: Existing vert to initialize settings.\n"
1056 "   :type example: :class:`BMVert`\n"
1057 "   :return: The newly created edge.\n"
1058 "   :rtype: :class:`BMVert`\n"
1059 "\n"
1060 "\n"
1061 ".. method:: new(v_a, v_b, example=None)\n"
1062 "\n"
1063 "   *Edge Sequence*\n"
1064 "\n"
1065 "   :arg v_a: Edge vertex.\n"
1066 "   :type v_a: :class:`BMEdge`\n"
1067 "   :arg v_b: Edge vertex.\n"
1068 "   :type v_b: :class:`BMEdge`\n"
1069 "   :arg example: Existing edge to initialize settings (optional argument).\n"
1070 "   :type example: :class:`BMEdge`\n"
1071 "   :return: The newly created edge.\n"
1072 "   :rtype: :class:`BMEdge`\n"
1073 "\n"
1074 ".. method:: new(verts, example=None)\n"
1075 "\n"
1076 "   *Face Sequence*\n"
1077 "\n"
1078 "   Create a new vert/edge/face.\n"
1079 "\n"
1080 "   :arg verts: Sequence of 3 or more verts.\n"
1081 "   :type verts: :class:`BMVert`\n"
1082 "   :arg example: Existing face to initialize settings (optional argument).\n"
1083 "   :type example: :class:`BMFace`\n"
1084 "   :return: The newly created face.\n"
1085 "   :rtype: :class:`BMFace`\n"
1086 );
1087 static PyObject *bpy_bmelemseq_new(BPy_BMElemSeq *self, PyObject *args)
1088 {
1089         switch ((BMIterType)self->itype) {
1090                 case BM_VERTS_OF_MESH:
1091                         return bpy_bmvert_seq_new(self, args);
1092                 case BM_EDGES_OF_MESH:
1093                         return bpy_bmedge_seq_new(self, args);
1094                 case BM_FACES_OF_MESH:
1095                         return bpy_bmface_seq_new(self, args);
1096                 default:
1097                         PyErr_SetString(PyExc_TypeError,
1098                                         ".new(...): function is not valid for this sequence");
1099                         return NULL;
1100         }
1101 }
1102
1103 static PyObject *bpy_bmvert_seq_remove(BPy_BMElemSeq *self, BPy_BMVert *value)
1104 {
1105         BPY_BM_CHECK_OBJ(self);
1106
1107         if (!BPy_BMVert_Check(value)) {
1108                 return NULL;
1109         }
1110         else {
1111                 BMesh *bm = self->bm;
1112
1113                 BPY_BM_CHECK_OBJ(value);
1114
1115                 if (value->bm != bm) {
1116                         PyErr_SetString(PyExc_TypeError,
1117                                         "faces.remove(vert): vertex is from another mesh");
1118                 }
1119
1120                 BM_vert_kill(bm, value->v);
1121                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1122
1123                 Py_RETURN_NONE;;
1124         }
1125 }
1126
1127 static PyObject *bpy_bmedge_seq_remove(BPy_BMElemSeq *self, BPy_BMEdge *value)
1128 {
1129         BPY_BM_CHECK_OBJ(self);
1130
1131         if (!BPy_BMEdge_Check(value)) {
1132                 return NULL;
1133         }
1134         else {
1135                 BMesh *bm = self->bm;
1136
1137                 BPY_BM_CHECK_OBJ(value);
1138
1139                 if (value->bm != bm) {
1140                         PyErr_SetString(PyExc_TypeError,
1141                                         "faces.remove(vert): vertex is from another mesh");
1142                 }
1143
1144                 BM_edge_kill(bm, value->e);
1145                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1146
1147                 Py_RETURN_NONE;;
1148         }
1149 }
1150
1151 static PyObject *bpy_bmface_seq_remove(BPy_BMElemSeq *self, BPy_BMFace *value)
1152 {
1153         BPY_BM_CHECK_OBJ(self);
1154
1155         if (!BPy_BMFace_Check(value)) {
1156                 return NULL;
1157         }
1158         else {
1159                 BMesh *bm = self->bm;
1160
1161                 BPY_BM_CHECK_OBJ(value);
1162
1163                 if (value->bm != bm) {
1164                         PyErr_SetString(PyExc_TypeError,
1165                                         "faces.remove(vert): vertex is from another mesh");
1166                 }
1167
1168                 BM_face_kill(bm, value->f);
1169                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1170
1171                 Py_RETURN_NONE;;
1172         }
1173 }
1174
1175
1176 PyDoc_STRVAR(bpy_bmelemseq_remove_doc,
1177 ".. method:: remove(elem)\n"
1178 "\n"
1179 "   Remove a vert/edge/face.\n"
1180 );
1181 static PyObject *bpy_bmelemseq_remove(BPy_BMElemSeq *self, PyObject *value)
1182 {
1183         switch ((BMIterType)self->itype) {
1184                 case BM_VERTS_OF_MESH:
1185                         return bpy_bmvert_seq_remove(self, (BPy_BMVert *)value);
1186                 case BM_EDGES_OF_MESH:
1187                         return bpy_bmedge_seq_remove(self, (BPy_BMEdge *)value);
1188                 case BM_FACES_OF_MESH:
1189                         return bpy_bmface_seq_remove(self, (BPy_BMFace *)value);
1190                 default:
1191                         PyErr_SetString(PyExc_TypeError,
1192                                         ".remove(item): function is not valid for this sequence");
1193                         return NULL;
1194         }
1195 }
1196
1197 PyDoc_STRVAR(bpy_bmelemseq_index_update_doc,
1198 ".. method:: index_update()\n"
1199 "\n"
1200 "   Initialize the index values of this sequence.\n"
1201 "\n"
1202 "   This is the equivalent of looping over all elements and assigning the index values.\n"
1203 "\n"
1204 "   .. code-block:: python\n"
1205 "\n"
1206 "      for index, ele in enumerate(sequence):\n"
1207 "          ele.index = index\n"
1208 "\n"
1209 "   .. note::\n"
1210 "\n"
1211 "      Running this on sequences besides :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces`\n"
1212 "      works but wont result in each element having a valid index, insted its order in the sequence will be set.\n"
1213 );
1214 static PyObject *bpy_bmelemseq_index_update(BPy_BMElemSeq *self)
1215 {
1216         BMesh *bm = self->bm;
1217
1218         BPY_BM_CHECK_OBJ(self);
1219
1220         switch ((BMIterType)self->itype) {
1221                 case BM_VERTS_OF_MESH:
1222                         BM_mesh_elem_index_ensure(self->bm, BM_VERT);
1223                         break;
1224                 case BM_EDGES_OF_MESH:
1225                         BM_mesh_elem_index_ensure(self->bm, BM_EDGE);
1226                         break;
1227                 case BM_FACES_OF_MESH:
1228                         BM_mesh_elem_index_ensure(self->bm, BM_FACE);
1229                         break;
1230                 default:
1231                 {
1232                         BMIter iter;
1233                         BMHeader *ele;
1234                         int index = 0;
1235                         const char htype = bm_iter_itype_htype_map[self->itype];
1236
1237                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
1238                                 BM_elem_index_set(ele, index); /* set_dirty! */
1239                                 index++;
1240                         }
1241
1242                         if (htype & (BM_VERT | BM_EDGE | BM_FACE)) {
1243                                 /* since this isnt the normal vert/edge/face loops,
1244                                  * we're setting dirty values here. so tag as dirty. */
1245                                 bm->elem_index_dirty |= htype;
1246                         }
1247
1248                         break;
1249                 }
1250         }
1251
1252         Py_RETURN_NONE;
1253 }
1254
1255
1256 static struct PyMethodDef bpy_bmesh_methods[] = {
1257     {"select_flush_mode", (PyCFunction)bpy_bmesh_select_flush_mode, METH_NOARGS, bpy_bmesh_select_flush_mode_doc},
1258     {"select_flush", (PyCFunction)bpy_bmesh_select_flush, METH_O, bpy_bmesh_select_flush_doc},
1259     {"normal_update", (PyCFunction)bpy_bmesh_normal_update, METH_VARARGS, bpy_bmesh_normal_update_doc},
1260     {"transform", (PyCFunction)bpy_bmesh_transform, METH_VARARGS|METH_KEYWORDS, bpy_bmesh_transform_doc},
1261     {NULL, NULL, 0, NULL}
1262 };
1263
1264 static struct PyMethodDef bpy_bmvert_methods[] = {
1265     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
1266     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
1267
1268     {"calc_vert_angle", (PyCFunction)bpy_bmvert_calc_edge_angle, METH_NOARGS, bpy_bmvert_calc_edge_angle_doc},
1269     {NULL, NULL, 0, NULL}
1270 };
1271
1272 static struct PyMethodDef bpy_bmedge_methods[] = {
1273     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
1274     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
1275
1276     {"other_vert", (PyCFunction)bpy_bmedge_other_vert, METH_O, bpy_bmedge_other_vert_doc},
1277
1278     {"calc_face_angle", (PyCFunction)bpy_bmedge_calc_face_angle, METH_NOARGS, bpy_bmedge_calc_face_angle_doc},
1279     {NULL, NULL, 0, NULL}
1280 };
1281
1282 static struct PyMethodDef bpy_bmface_methods[] = {
1283     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
1284     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
1285
1286     {"copy", (PyCFunction)bpy_bmface_copy, METH_VARARGS|METH_KEYWORDS, bpy_bmface_copy_doc},
1287
1288     {"calc_area",          (PyCFunction)bpy_bmface_calc_area,          METH_NOARGS, bpy_bmface_calc_area_doc},
1289     {"calc_center_median", (PyCFunction)bpy_bmface_calc_center_mean,   METH_NOARGS, bpy_bmface_calc_center_mean_doc},
1290     {"calc_center_bounds", (PyCFunction)bpy_bmface_calc_center_bounds, METH_NOARGS, bpy_bmface_calc_center_bounds_doc},
1291     {NULL, NULL, 0, NULL}
1292 };
1293
1294 static struct PyMethodDef bpy_bmloop_methods[] = {
1295     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
1296
1297     {"calc_angle", (PyCFunction)bpy_bmloop_calc_face_angle, METH_NOARGS, bpy_bmloop_calc_face_angle_doc},
1298     {NULL, NULL, 0, NULL}
1299 };
1300
1301 static struct PyMethodDef bpy_bmelemseq_methods[] = {
1302     {"new", (PyCFunction)bpy_bmelemseq_new, METH_VARARGS, bpy_bmelemseq_new_doc},
1303     {"remove", (PyCFunction)bpy_bmelemseq_remove, METH_O, bpy_bmelemseq_remove_doc},
1304
1305     /* odd function, initializes index values */
1306     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
1307     {NULL, NULL, 0, NULL}
1308 };
1309
1310 /* Sequences
1311  * ========= */
1312
1313 static PyTypeObject *bpy_bm_itype_as_pytype(const char itype)
1314 {
1315         /* should cover all types */
1316         switch ((BMIterType)itype) {
1317                 case BM_VERTS_OF_MESH:
1318                 case BM_VERTS_OF_FACE:
1319                 case BM_VERTS_OF_EDGE:
1320                         return &BPy_BMVert_Type;
1321
1322                 case BM_EDGES_OF_MESH:
1323                 case BM_EDGES_OF_FACE:
1324                 case BM_EDGES_OF_VERT:
1325                         return &BPy_BMEdge_Type;
1326
1327                 case BM_FACES_OF_MESH:
1328                 case BM_FACES_OF_EDGE:
1329                 case BM_FACES_OF_VERT:
1330                         return &BPy_BMFace_Type;
1331
1332                 case BM_ALL_LOOPS_OF_FACE:
1333                 case BM_LOOPS_OF_FACE:
1334                 case BM_LOOPS_OF_EDGE:
1335                 case BM_LOOPS_OF_VERT:
1336                 case BM_LOOPS_OF_LOOP:
1337                         return &BPy_BMLoop_Type;
1338         }
1339
1340         return NULL;
1341 }
1342
1343 static Py_ssize_t bpy_bmelemseq_length(BPy_BMElemSeq *self)
1344 {
1345         BPY_BM_CHECK_INT(self);
1346
1347         /* first check if the size is known */
1348         switch ((BMIterType)self->itype) {
1349                 /* main-types */
1350                 case BM_VERTS_OF_MESH:
1351                         return self->bm->totvert;
1352                 case BM_EDGES_OF_MESH:
1353                         return self->bm->totedge;
1354                 case BM_FACES_OF_MESH:
1355                         return self->bm->totface;
1356
1357                         /* sub-types */
1358                 case BM_VERTS_OF_FACE:
1359                 case BM_EDGES_OF_FACE:
1360                 case BM_LOOPS_OF_FACE:
1361                         BPY_BM_CHECK_INT(self->py_ele);
1362                         return ((BMFace *)self->py_ele->ele)->len;
1363
1364                 case BM_VERTS_OF_EDGE:
1365                         return 2;
1366
1367                 default:
1368                         /* quiet compiler */
1369                         break;
1370         }
1371
1372
1373         /* loop over all items, avoid this if we can */
1374         {
1375                 BMIter iter;
1376                 BMHeader *ele;
1377                 Py_ssize_t tot = 0;
1378
1379                 BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
1380                         tot++;
1381                 }
1382                 return tot;
1383         }
1384 }
1385
1386 static PyObject *bpy_bmelemseq_subscript_int(BPy_BMElemSeq *self, int keynum)
1387 {
1388         BPY_BM_CHECK_OBJ(self);
1389
1390         if (keynum < 0) keynum += bpy_bmelemseq_length(self); /* only get length on negative value, may loop entire seq */
1391         if (keynum >= 0) {
1392                 BMHeader *ele = BM_iter_at_index(self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL, keynum);
1393                 if (ele) {
1394                         return BPy_BMElem_CreatePyObject(self->bm, ele);
1395                 }
1396         }
1397
1398         PyErr_Format(PyExc_IndexError,
1399                      "BMElemSeq[index]: index %d out of range", keynum);
1400         return NULL;
1401 }
1402
1403 static PyObject *bpy_bmelemseq_subscript_slice(BPy_BMElemSeq *self, Py_ssize_t start, Py_ssize_t stop)
1404 {
1405         BMIter iter;
1406         int count = 0;
1407         int ok;
1408
1409         PyObject *list;
1410         PyObject *item;
1411         BMHeader *ele;
1412
1413         BPY_BM_CHECK_OBJ(self);
1414
1415         list = PyList_New(0);
1416
1417         ok = BM_iter_init(&iter, self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
1418
1419         BLI_assert(ok == TRUE);
1420
1421         if (UNLIKELY(ok == FALSE)) {
1422                 return list;
1423         }
1424
1425         /* first loop up-until the start */
1426         for (ok = TRUE; ok; ok = (BM_iter_step(&iter) != NULL)) {
1427                 /* PointerRNA itemptr = rna_macro_iter.ptr; */
1428                 if (count == start) {
1429                         break;
1430                 }
1431                 count++;
1432         }
1433
1434         /* add items until stop */
1435         while ((ele = BM_iter_step(&iter))) {
1436                 item = BPy_BMElem_CreatePyObject(self->bm, ele);
1437                 PyList_Append(list, item);
1438                 Py_DECREF(item);
1439
1440                 count++;
1441                 if (count == stop) {
1442                         break;
1443                 }
1444         }
1445
1446         return list;
1447 }
1448
1449 static PyObject *bpy_bmelemseq_subscript(BPy_BMElemSeq *self, PyObject *key)
1450 {
1451         /* dont need error check here */
1452         if (PyIndex_Check(key)) {
1453                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1454                 if (i == -1 && PyErr_Occurred())
1455                         return NULL;
1456                 return bpy_bmelemseq_subscript_int(self, i);
1457         }
1458         else if (PySlice_Check(key)) {
1459                 PySliceObject *key_slice = (PySliceObject *)key;
1460                 Py_ssize_t step = 1;
1461
1462                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
1463                         return NULL;
1464                 }
1465                 else if (step != 1) {
1466                         PyErr_SetString(PyExc_TypeError, "BMElemSeq[slice]: slice steps not supported");
1467                         return NULL;
1468                 }
1469                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
1470                         return bpy_bmelemseq_subscript_slice(self, 0, PY_SSIZE_T_MAX);
1471                 }
1472                 else {
1473                         Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
1474
1475                         /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
1476                         if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
1477                         if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop))    return NULL;
1478
1479                         if (start < 0 || stop < 0) {
1480                                 /* only get the length for negative values */
1481                                 Py_ssize_t len = bpy_bmelemseq_length(self);
1482                                 if (start < 0) start += len;
1483                                 if (stop < 0) start += len;
1484                         }
1485
1486                         if (stop - start <= 0) {
1487                                 return PyList_New(0);
1488                         }
1489                         else {
1490                                 return bpy_bmelemseq_subscript_slice(self, start, stop);
1491                         }
1492                 }
1493         }
1494         else {
1495                 PyErr_SetString(PyExc_AttributeError, "BMElemSeq[key]: invalid key, key must be an int");
1496                 return NULL;
1497         }
1498 }
1499
1500 static int bpy_bmelemseq_contains(BPy_BMElemSeq *self, PyObject *value)
1501 {
1502         BPY_BM_CHECK_INT(self);
1503
1504         if (Py_TYPE(value) == bpy_bm_itype_as_pytype(self->itype)) {
1505                 BPy_BMElem *value_bm_ele = (BPy_BMElem *)value;
1506                 if (value_bm_ele->bm == self->bm) {
1507                         BMHeader *ele, *ele_test = value_bm_ele->ele;
1508                         BMIter iter;
1509                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
1510                                 if (ele == ele_test) {
1511                                         return 1;
1512                                 }
1513                         }
1514                 }
1515         }
1516
1517         return 0;
1518 }
1519
1520 static PySequenceMethods bpy_bmelemseq_as_sequence = {
1521     (lenfunc)bpy_bmelemseq_length,                  /* sq_length */
1522     NULL,                                        /* sq_concat */
1523     NULL,                                        /* sq_repeat */
1524     (ssizeargfunc)bpy_bmelemseq_subscript_int,      /* sq_item */ /* Only set this so PySequence_Check() returns True */
1525     NULL,                                        /* sq_slice */
1526     (ssizeobjargproc)NULL,                       /* sq_ass_item */
1527     NULL,                                        /* *was* sq_ass_slice */
1528     (objobjproc)bpy_bmelemseq_contains,             /* sq_contains */
1529     (binaryfunc) NULL,                           /* sq_inplace_concat */
1530     (ssizeargfunc) NULL,                         /* sq_inplace_repeat */
1531 };
1532
1533 static PyMappingMethods bpy_bmelemseq_as_mapping = {
1534     (lenfunc)bpy_bmelemseq_length,                  /* mp_length */
1535     (binaryfunc)bpy_bmelemseq_subscript,            /* mp_subscript */
1536     (objobjargproc)NULL,                         /* mp_ass_subscript */
1537 };
1538
1539 /* Iterator
1540  * -------- */
1541
1542 static PyObject *bpy_bmelemseq_iter(BPy_BMElemSeq *self)
1543 {
1544         BPy_BMIter *py_iter;
1545
1546         BPY_BM_CHECK_OBJ(self);
1547         py_iter = (BPy_BMIter *)BPy_BMIter_CreatePyObject(self->bm);
1548         BM_iter_init(&(py_iter->iter), self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
1549         return (PyObject *)py_iter;
1550 }
1551
1552 static PyObject *bpy_bmiter_next(BPy_BMIter *self)
1553 {
1554         BMHeader *ele = BM_iter_step(&self->iter);
1555         if (ele == NULL) {
1556                 PyErr_SetString(PyExc_StopIteration, "bpy_bmiter_next stop");
1557                 return NULL;
1558         }
1559         else {
1560                 return (PyObject *)BPy_BMElem_CreatePyObject(self->bm, ele);
1561         }
1562 }
1563
1564
1565 /* Dealloc Functions
1566  * ================= */
1567
1568 static void bpy_bmesh_dealloc(BPy_BMesh *self)
1569 {
1570         BMesh *bm = self->bm;
1571
1572         BM_data_layer_free(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
1573         BM_data_layer_free(bm, &bm->edata, CD_BM_ELEM_PYPTR);
1574         BM_data_layer_free(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
1575         BM_data_layer_free(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
1576
1577         bm->py_handle = NULL;
1578
1579         PyObject_DEL(self);
1580 }
1581
1582 static void bpy_bmvert_dealloc(BPy_BMElem *self)
1583 {
1584         BMesh *bm = self->bm;
1585         if (bm) {
1586                 void **ptr = CustomData_bmesh_get(&bm->vdata, self->ele->data, CD_BM_ELEM_PYPTR);
1587                 *ptr = NULL;
1588         }
1589         PyObject_DEL(self);
1590 }
1591
1592 static void bpy_bmedge_dealloc(BPy_BMElem *self)
1593 {
1594         BMesh *bm = self->bm;
1595         if (bm) {
1596                 void **ptr = CustomData_bmesh_get(&bm->edata, self->ele->data, CD_BM_ELEM_PYPTR);
1597                 *ptr = NULL;
1598         }
1599         PyObject_DEL(self);
1600 }
1601
1602 static void bpy_bmface_dealloc(BPy_BMElem *self)
1603 {
1604         BMesh *bm = self->bm;
1605         if (bm) {
1606                 void **ptr = CustomData_bmesh_get(&bm->pdata, self->ele->data, CD_BM_ELEM_PYPTR);
1607                 *ptr = NULL;
1608         }
1609         PyObject_DEL(self);
1610 }
1611
1612 static void bpy_bmloop_dealloc(BPy_BMElem *self)
1613 {
1614         BMesh *bm = self->bm;
1615         if (bm) {
1616                 void **ptr = CustomData_bmesh_get(&bm->ldata, self->ele->data, CD_BM_ELEM_PYPTR);
1617                 *ptr = NULL;
1618         }
1619         PyObject_DEL(self);
1620 }
1621
1622 static void bpy_bmelemseq_dealloc(BPy_BMElemSeq *self)
1623 {
1624         Py_XDECREF(self->py_ele);
1625
1626         PyObject_DEL(self);
1627 }
1628
1629 /* not sure where this should go */
1630 static long bpy_bm_elem_hash(PyObject *self)
1631 {
1632         return _Py_HashPointer(((BPy_BMElem *)self)->ele);
1633 }
1634
1635
1636 /* Type Docstrings
1637  * =============== */
1638
1639 PyDoc_STRVAR(bpy_bmesh_doc,
1640 "The BMesh data structure\n"
1641 );
1642 PyDoc_STRVAR(bpy_bmvert_doc,
1643 "The BMesh vertex type\n"
1644 );
1645 PyDoc_STRVAR(bpy_bmedge_doc,
1646 "The BMesh edge connecting 2 verts\n"
1647 );
1648 PyDoc_STRVAR(bpy_bmface_doc,
1649 "The BMesh face with 3 or more sides\n"
1650 );
1651 PyDoc_STRVAR(bpy_bmloop_doc,
1652 "This is normally accessed from :class:`BMFace.loops` where each face corner represents a corner of a face.\n"
1653 );
1654 PyDoc_STRVAR(bpy_bmelemseq_doc,
1655 "General sequence type used for accessing any sequence of \n"
1656 ":class:`BMVert`, :class:`BMEdge`, :class:`BMFace`, :class:`BMLoop`.\n"
1657 "\n"
1658 "When accessed via :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces` \n"
1659 "there are also functions to create/remomove items.\n"
1660 );
1661 PyDoc_STRVAR(bpy_bmiter_doc,
1662 "Internal BMesh type for looping over verts/faces/edges,\n"
1663 "used for iterating over :class:`BMElemSeq` types.\n"
1664 );
1665
1666 /* Types
1667  * ===== */
1668
1669 PyTypeObject BPy_BMesh_Type     = {{{0}}};
1670 PyTypeObject BPy_BMVert_Type    = {{{0}}};
1671 PyTypeObject BPy_BMEdge_Type    = {{{0}}};
1672 PyTypeObject BPy_BMFace_Type    = {{{0}}};
1673 PyTypeObject BPy_BMLoop_Type    = {{{0}}};
1674 PyTypeObject BPy_BMElemSeq_Type = {{{0}}};
1675 PyTypeObject BPy_BMIter_Type    = {{{0}}};
1676
1677
1678
1679 void BPy_BM_init_types(void)
1680 {
1681         BPy_BMesh_Type.tp_basicsize     = sizeof(BPy_BMesh);
1682         BPy_BMVert_Type.tp_basicsize    = sizeof(BPy_BMVert);
1683         BPy_BMEdge_Type.tp_basicsize    = sizeof(BPy_BMEdge);
1684         BPy_BMFace_Type.tp_basicsize    = sizeof(BPy_BMFace);
1685         BPy_BMLoop_Type.tp_basicsize    = sizeof(BPy_BMLoop);
1686         BPy_BMElemSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
1687         BPy_BMIter_Type.tp_basicsize    = sizeof(BPy_BMIter);
1688
1689
1690         BPy_BMesh_Type.tp_name     = "BMesh";
1691         BPy_BMVert_Type.tp_name    = "BMVert";
1692         BPy_BMEdge_Type.tp_name    = "BMEdge";
1693         BPy_BMFace_Type.tp_name    = "BMFace";
1694         BPy_BMLoop_Type.tp_name    = "BMLoop";
1695         BPy_BMElemSeq_Type.tp_name = "BMElemSeq";
1696         BPy_BMIter_Type.tp_name    = "BMIter";
1697
1698
1699         BPy_BMesh_Type.tp_doc     = bpy_bmesh_doc;
1700         BPy_BMVert_Type.tp_doc    = bpy_bmvert_doc;
1701         BPy_BMEdge_Type.tp_doc    = bpy_bmedge_doc;
1702         BPy_BMFace_Type.tp_doc    = bpy_bmface_doc;
1703         BPy_BMLoop_Type.tp_doc    = bpy_bmloop_doc;
1704         BPy_BMElemSeq_Type.tp_doc = bpy_bmelemseq_doc;
1705         BPy_BMIter_Type.tp_doc    = bpy_bmiter_doc;
1706
1707
1708         BPy_BMesh_Type.tp_getset     = bpy_bmesh_getseters;
1709         BPy_BMVert_Type.tp_getset    = bpy_bmvert_getseters;
1710         BPy_BMEdge_Type.tp_getset    = bpy_bmedge_getseters;
1711         BPy_BMFace_Type.tp_getset    = bpy_bmface_getseters;
1712         BPy_BMLoop_Type.tp_getset    = bpy_bmloop_getseters;
1713         BPy_BMElemSeq_Type.tp_getset = NULL;
1714         BPy_BMIter_Type.tp_getset    = NULL;
1715
1716
1717         BPy_BMesh_Type.tp_methods     = bpy_bmesh_methods;
1718         BPy_BMVert_Type.tp_methods    = bpy_bmvert_methods;
1719         BPy_BMEdge_Type.tp_methods    = bpy_bmedge_methods;
1720         BPy_BMFace_Type.tp_methods    = bpy_bmface_methods;
1721         BPy_BMLoop_Type.tp_methods    = bpy_bmloop_methods;
1722         BPy_BMElemSeq_Type.tp_methods = bpy_bmelemseq_methods;
1723         BPy_BMIter_Type.tp_methods    = NULL;
1724
1725
1726         BPy_BMesh_Type.tp_hash     = NULL;
1727         BPy_BMVert_Type.tp_hash    = bpy_bm_elem_hash;
1728         BPy_BMEdge_Type.tp_hash    = bpy_bm_elem_hash;
1729         BPy_BMFace_Type.tp_hash    = bpy_bm_elem_hash;
1730         BPy_BMLoop_Type.tp_hash    = bpy_bm_elem_hash;
1731         BPy_BMElemSeq_Type.tp_hash = NULL;
1732         BPy_BMIter_Type.tp_hash    = NULL;
1733
1734         BPy_BMElemSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
1735
1736         BPy_BMElemSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
1737
1738         BPy_BMElemSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
1739
1740         /* only 1 iteratir so far */
1741         BPy_BMIter_Type.tp_iternext = (iternextfunc)bpy_bmiter_next;
1742
1743         BPy_BMesh_Type.tp_dealloc     = (destructor)bpy_bmesh_dealloc;
1744         BPy_BMVert_Type.tp_dealloc    = (destructor)bpy_bmvert_dealloc;
1745         BPy_BMEdge_Type.tp_dealloc    = (destructor)bpy_bmedge_dealloc;
1746         BPy_BMFace_Type.tp_dealloc    = (destructor)bpy_bmface_dealloc;
1747         BPy_BMLoop_Type.tp_dealloc    = (destructor)bpy_bmloop_dealloc;
1748         BPy_BMElemSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
1749         BPy_BMIter_Type.tp_dealloc    = NULL;
1750
1751         /*
1752         BPy_BMesh_Type.
1753         BPy_BMVert_Type.
1754         BPy_BMEdge_Type.
1755         BPy_BMFace_Type.
1756         BPy_BMLoop_Type.
1757         BPy_BMElemSeq_Type.
1758         BPy_BMIter_Type.
1759         */
1760
1761         BPy_BMesh_Type.tp_flags     = Py_TPFLAGS_DEFAULT;
1762         BPy_BMVert_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
1763         BPy_BMEdge_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
1764         BPy_BMFace_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
1765         BPy_BMLoop_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
1766         BPy_BMElemSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1767         BPy_BMIter_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
1768
1769
1770         PyType_Ready(&BPy_BMesh_Type);
1771         PyType_Ready(&BPy_BMVert_Type);
1772         PyType_Ready(&BPy_BMEdge_Type);
1773         PyType_Ready(&BPy_BMFace_Type);
1774         PyType_Ready(&BPy_BMLoop_Type);
1775         PyType_Ready(&BPy_BMElemSeq_Type);
1776         PyType_Ready(&BPy_BMIter_Type);
1777 }
1778
1779 /* bmesh.types submodule
1780  * ********************* */
1781
1782 static struct PyModuleDef BPy_BM_types_module_def = {
1783     PyModuleDef_HEAD_INIT,
1784     "bmesh.types",  /* m_name */
1785     NULL,  /* m_doc */
1786     0,  /* m_size */
1787     NULL,  /* m_methods */
1788     NULL,  /* m_reload */
1789     NULL,  /* m_traverse */
1790     NULL,  /* m_clear */
1791     NULL,  /* m_free */
1792 };
1793
1794 PyObject *BPyInit_bmesh_types(void)
1795 {
1796         PyObject *submodule;
1797
1798         submodule = PyModule_Create(&BPy_BM_types_module_def);
1799
1800 #define mod_type_add(s, t) \
1801         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
1802
1803         mod_type_add(submodule, BPy_BMesh_Type);
1804         mod_type_add(submodule, BPy_BMVert_Type);
1805         mod_type_add(submodule, BPy_BMEdge_Type);
1806         mod_type_add(submodule, BPy_BMFace_Type);
1807         mod_type_add(submodule, BPy_BMLoop_Type);
1808         mod_type_add(submodule, BPy_BMElemSeq_Type);
1809         mod_type_add(submodule, BPy_BMIter_Type);
1810
1811 #undef mod_type_add
1812
1813         return submodule;
1814 }
1815
1816 /* Utility Functions
1817  * ***************** */
1818
1819 PyObject *BPy_BMesh_CreatePyObject(BMesh *bm)
1820 {
1821         BPy_BMesh *self;
1822
1823         if (bm->py_handle) {
1824                 self = bm->py_handle;
1825                 Py_INCREF(self);
1826         }
1827         else {
1828                 self = PyObject_New(BPy_BMesh, &BPy_BMesh_Type);
1829                 self->bm = bm;
1830
1831                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
1832                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
1833                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
1834                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
1835         }
1836
1837         return (PyObject *)self;
1838 }
1839
1840
1841
1842 PyObject *BPy_BMVert_CreatePyObject(BMesh *bm, BMVert *v)
1843 {
1844         BPy_BMVert *self;
1845
1846         void **ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
1847
1848         /* bmesh may free layers, ensure we have one to store ourself */
1849         if (UNLIKELY(ptr == NULL)) {
1850                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
1851                 ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
1852         }
1853
1854         if (*ptr != NULL) {
1855                 self = *ptr;
1856                 Py_INCREF(self);
1857         }
1858         else {
1859                 self = PyObject_New(BPy_BMVert, &BPy_BMVert_Type);
1860                 BLI_assert(v != NULL);
1861                 self->bm = bm;
1862                 self->v  = v;
1863                 *ptr = self;
1864         }
1865         return (PyObject *)self;
1866 }
1867
1868 PyObject *BPy_BMEdge_CreatePyObject(BMesh *bm, BMEdge *e)
1869 {
1870         BPy_BMEdge *self;
1871
1872         void **ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
1873
1874         /* bmesh may free layers, ensure we have one to store ourself */
1875         if (UNLIKELY(ptr == NULL)) {
1876                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
1877                 ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
1878         }
1879
1880         if (*ptr != NULL) {
1881                 self = *ptr;
1882                 Py_INCREF(self);
1883         }
1884         else {
1885                 self = PyObject_New(BPy_BMEdge, &BPy_BMEdge_Type);
1886                 BLI_assert(e != NULL);
1887                 self->bm = bm;
1888                 self->e  = e;
1889                 *ptr = self;
1890         }
1891         return (PyObject *)self;
1892 }
1893
1894 PyObject *BPy_BMFace_CreatePyObject(BMesh *bm, BMFace *f)
1895 {
1896         BPy_BMFace *self;
1897
1898         void **ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
1899
1900         /* bmesh may free layers, ensure we have one to store ourself */
1901         if (UNLIKELY(ptr == NULL)) {
1902                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
1903                 ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
1904         }
1905
1906         if (*ptr != NULL) {
1907                 self = *ptr;
1908                 Py_INCREF(self);
1909         }
1910         else {
1911                 self = PyObject_New(BPy_BMFace, &BPy_BMFace_Type);
1912                 BLI_assert(f != NULL);
1913                 self->bm = bm;
1914                 self->f  = f;
1915                 *ptr = self;
1916         }
1917         return (PyObject *)self;
1918 }
1919
1920 PyObject *BPy_BMLoop_CreatePyObject(BMesh *bm, BMLoop *l)
1921 {
1922         BPy_BMLoop *self;
1923
1924         void **ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
1925
1926         /* bmesh may free layers, ensure we have one to store ourself */
1927         if (UNLIKELY(ptr == NULL)) {
1928                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
1929                 ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
1930         }
1931
1932         if (*ptr != NULL) {
1933                 self = *ptr;
1934                 Py_INCREF(self);
1935         }
1936         else {
1937                 self = PyObject_New(BPy_BMLoop, &BPy_BMLoop_Type);
1938                 BLI_assert(l != NULL);
1939                 self->bm = bm;
1940                 self->l  = l;
1941                 *ptr = self;
1942         }
1943         return (PyObject *)self;
1944 }
1945
1946 PyObject *BPy_BMElemSeq_CreatePyObject(BMesh *bm, BPy_BMElem *py_ele, const char itype)
1947 {
1948         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMElemSeq_Type);
1949         self->bm = bm;
1950         self->py_ele = py_ele; /* can be NULL */
1951         self->itype = itype;
1952         Py_XINCREF(py_ele);
1953         return (PyObject *)self;
1954 }
1955
1956 PyObject *BPy_BMIter_CreatePyObject(BMesh *bm)
1957 {
1958         BPy_BMIter *self = PyObject_New(BPy_BMIter, &BPy_BMIter_Type);
1959         self->bm = bm;
1960         /* caller must initialize 'iter' member */
1961         return (PyObject *)self;
1962 }
1963
1964 /* this is just a helper func */
1965 PyObject *BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele)
1966 {
1967         switch (ele->htype) {
1968                 case BM_VERT:
1969                         return BPy_BMVert_CreatePyObject(bm, (BMVert *)ele);
1970                 case BM_EDGE:
1971                         return BPy_BMEdge_CreatePyObject(bm, (BMEdge *)ele);
1972                 case BM_FACE:
1973                         return BPy_BMFace_CreatePyObject(bm, (BMFace *)ele);
1974                 case BM_LOOP:
1975                         return BPy_BMLoop_CreatePyObject(bm, (BMLoop *)ele);
1976                 default:
1977                         PyErr_SetString(PyExc_SystemError, "internal error");
1978                         return NULL;
1979         }
1980 }
1981
1982 int bpy_bm_generic_valid_check(BPy_BMGeneric *self)
1983 {
1984         if (self->bm) {
1985                 return 0;
1986         }
1987         PyErr_Format(PyExc_ReferenceError,
1988                      "BMesh data of type %.200s has been removed",
1989                      Py_TYPE(self)->tp_name);
1990         return -1;
1991 }
1992
1993 void bpy_bm_generic_invalidate(BPy_BMGeneric *self)
1994 {
1995         self->bm = NULL;
1996 }
1997
1998 /* generic python seq as BMVert/Edge/Face array,
1999  * return value must be freed with PyMem_FREE(...);
2000  *
2001  * The 'bm_r' value is assigned when empty, and used when set.
2002  */
2003 void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_size,
2004                                 PyTypeObject *type,
2005                                 const char do_unique_check, const char do_bm_check,
2006                                 const char *error_prefix)
2007 {
2008         BMesh *bm = (r_bm && *r_bm) ? *r_bm : NULL;
2009         PyObject *seq_fast;
2010         *r_size = 0;
2011
2012         if (!(seq_fast=PySequence_Fast(seq, error_prefix))) {
2013                 return NULL;
2014         }
2015         else {
2016                 Py_ssize_t seq_len;
2017                 Py_ssize_t i;
2018
2019                 BPy_BMElem *item;
2020                 BMHeader **alloc;
2021
2022                 seq_len = PySequence_Fast_GET_SIZE(seq_fast);
2023
2024                 if (seq_len < min || seq_len > max) {
2025                         PyErr_Format(PyExc_TypeError,
2026                                      "%s: sequence incorrect size, expected [%d - %d], given %d",
2027                                      error_prefix, min, max, seq_len);
2028                         return NULL;
2029                 }
2030
2031
2032                 /* from now on, use goto */
2033                 alloc = PyMem_MALLOC(seq_len * sizeof(BPy_BMElem **));
2034
2035                 for (i = 0; i < seq_len; i++) {
2036                         item = (BPy_BMElem *)PySequence_Fast_GET_ITEM(seq_fast, i);
2037
2038                         if (Py_TYPE(item) != type) {
2039                                 PyErr_Format(PyExc_TypeError,
2040                                              "%s: expected '%.200', not '%.200s'",
2041                                              error_prefix, type->tp_name, Py_TYPE(item)->tp_name);
2042                                 goto err_cleanup;
2043                         }
2044                         else if (item->bm == NULL) {
2045                                 PyErr_Format(PyExc_TypeError,
2046                                              "%s: %d %s has been removed",
2047                                              error_prefix, i, type->tp_name);
2048                                 goto err_cleanup;
2049                         }
2050                         /* trick so we can ensure all items have the same mesh,
2051                          * and allows us to pass the 'bm' as NULL. */
2052                         else if (do_bm_check && (bm  && bm != item->bm)) {
2053                                 PyErr_Format(PyExc_TypeError,
2054                                              "%s: %d %s is from another mesh",
2055                                              error_prefix, i, type->tp_name);
2056                                 goto err_cleanup;
2057                         }
2058
2059                         if (bm == NULL) {
2060                                 bm = item->bm;
2061                         }
2062
2063                         alloc[i] = item->ele;
2064
2065                         if (do_unique_check) {
2066                                 BM_elem_flag_enable(item->ele, BM_ELEM_TAG);
2067                         }
2068                 }
2069
2070                 if (do_unique_check) {
2071                         /* check for double verts! */
2072                         int ok = TRUE;
2073                         for (i = 0; i < seq_len; i++) {
2074                                 if (UNLIKELY(BM_elem_flag_test(alloc[i], BM_ELEM_TAG) == FALSE)) {
2075                                         ok = FALSE;
2076                                 }
2077
2078                                 /* ensure we dont leave this enabled */
2079                                 BM_elem_flag_disable(alloc[i], BM_ELEM_TAG);
2080                         }
2081
2082                         if (ok == FALSE) {
2083                                 PyErr_Format(PyExc_ValueError,
2084                                              "%s: found the same %s used multiple times",
2085                                              error_prefix, type->tp_name);
2086                                 goto err_cleanup;
2087                         }
2088                 }
2089
2090                 Py_DECREF(seq_fast);
2091                 *r_size = seq_len;
2092                 if (r_bm) *r_bm = bm;
2093                 return alloc;
2094
2095 err_cleanup:
2096                 Py_DECREF(seq_fast);
2097                 PyMem_FREE(alloc);
2098                 return NULL;
2099         }
2100 }