bmesh py api:
[blender.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 "DNA_mesh_types.h"
35 #include "DNA_object_types.h"
36
37 #include "BKE_depsgraph.h"
38 #include "BKE_customdata.h"
39 #include "BKE_DerivedMesh.h"
40
41 #include "bmesh.h"
42
43 #include "../mathutils/mathutils.h"
44
45 #include "../generic/py_capi_utils.h"
46
47 #include "bmesh_py_types.h" /* own include */
48 #include "bmesh_py_types_select.h"
49 #include "bmesh_py_types_customdata.h"
50 #include "bmesh_py_types_meshdata.h"
51
52 /* Common Flags
53  * ************ */
54
55 /* scene does not use BM_* flags. */
56 PyC_FlagSet bpy_bm_scene_vert_edge_face_flags[] = {
57     {1, "VERT"},
58     {2, "EDGE"},
59     {4, "FACE"},
60     {0, NULL}
61 };
62
63 PyC_FlagSet bpy_bm_htype_vert_edge_face_flags[] = {
64     {BM_VERT, "VERT"},
65     {BM_EDGE, "EDGE"},
66     {BM_FACE, "FACE"},
67     {0, NULL}
68 };
69
70 PyC_FlagSet bpy_bm_htype_all_flags[] = {
71     {BM_VERT, "VERT"},
72     {BM_LOOP, "EDGE"},
73     {BM_FACE, "FACE"},
74     {BM_LOOP, "LOOP"},
75     {0, NULL}
76 };
77
78 PyC_FlagSet bpy_bm_hflag_all_flags[] = {
79     {BM_ELEM_SELECT,  "SELECT"},
80     {BM_ELEM_HIDDEN,  "HIDE"},
81     {BM_ELEM_SEAM,    "SEAM"},
82     {BM_ELEM_SMOOTH,  "SMOOTH"},
83     {BM_ELEM_TAG,     "TAG"},
84     {0, NULL}
85 };
86
87 /* py-type definitions
88  * ******************* */
89
90 /* getseters
91  * ========= */
92
93
94 /* bmesh elems
95  * ----------- */
96
97 PyDoc_STRVAR(bpy_bm_elem_select_doc,  "Selected state of this element.\n\n:type: boolean");
98 PyDoc_STRVAR(bpy_bm_elem_hide_doc,    "Hidden state of this element.\n\n:type: boolean");
99 PyDoc_STRVAR(bpy_bm_elem_tag_doc,     "Generic attribute scripts can use for own logic\n\n:type: boolean");
100 PyDoc_STRVAR(bpy_bm_elem_smooth_doc,  "Smooth state of this element.\n\n:type: boolean");
101
102
103 static PyObject *bpy_bm_elem_hflag_get(BPy_BMElem *self, void *flag)
104 {
105         const char hflag = (char)GET_INT_FROM_POINTER(flag);
106
107         BPY_BM_CHECK_OBJ(self);
108
109         return PyBool_FromLong(BM_elem_flag_test(self->ele, hflag));
110 }
111
112 static int bpy_bm_elem_hflag_set(BPy_BMElem *self, PyObject *value, void *flag)
113 {
114         const char hflag = (char)GET_INT_FROM_POINTER(flag);
115         int param;
116
117         BPY_BM_CHECK_INT(self);
118
119         param = PyLong_AsLong(value);
120
121         if (param == TRUE) {
122                 BM_elem_flag_enable(self->ele, hflag);
123                 return 0;
124         }
125         else if (param == FALSE) {
126                 BM_elem_flag_disable(self->ele, hflag);
127                 return 0;
128         }
129         else {
130                 PyErr_Format(PyExc_TypeError,
131                              "expected True/False or 0/1, not %.200s",
132                              Py_TYPE(value)->tp_name);
133                 return -1;
134         }
135 }
136
137
138 PyDoc_STRVAR(bpy_bm_elem_index_doc,
139 "Index of this element.\n"
140 "\n"
141 ":type: int\n"
142 "\n"
143 ".. note::\n"
144 "\n"
145 "   This value is not necessarily valid, while editing the mesh it can become *dirty*.\n"
146 "\n"
147 "   It's also possible to assign any number to this attribute for a scripts internal logic.\n"
148 "\n"
149 "   To ensure the value is up to date - see :class:`BMElemSeq.index_update`.\n"
150 );
151 static PyObject *bpy_bm_elem_index_get(BPy_BMElem *self, void *UNUSED(flag))
152 {
153         BPY_BM_CHECK_OBJ(self);
154
155         return PyLong_FromLong(BM_elem_index_get(self->ele));
156 }
157
158 static int bpy_bm_elem_index_set(BPy_BMElem *self, PyObject *value, void *UNUSED(flag))
159 {
160         int param;
161
162         BPY_BM_CHECK_INT(self);
163
164         param = PyLong_AsLong(value);
165
166         if (param == -1 && PyErr_Occurred()) {
167                 PyErr_SetString(PyExc_TypeError,
168                                 "expected an int type");
169                 return -1;
170         }
171         else {
172                 BM_elem_index_set(self->ele, param); /* set_dirty! */
173
174                 /* when setting the index assume its set invalid */
175                 if (self->ele->head.htype & (BM_VERT | BM_EDGE | BM_FACE)) {
176                         self->bm->elem_index_dirty |= self->ele->head.htype;
177                 }
178
179                 return 0;
180         }
181 }
182
183 /* type spesific get/sets
184  * ---------------------- */
185
186
187 /* Mesh
188  * ^^^^ */
189
190 /* doc-strings for all uses of this funcion */
191
192 PyDoc_STRVAR(bpy_bmvertseq_doc,
193 "This meshes vert sequence (read-only).\n\n:type: :class:`BMVertSeq`"
194 );
195 static PyObject *bpy_bmvertseq_get(BPy_BMesh *self, void *UNUSED(closure))
196 {
197         BPY_BM_CHECK_OBJ(self);
198         return BPy_BMVertSeq_CreatePyObject(self->bm);
199 }
200
201 PyDoc_STRVAR(bpy_bmedgeseq_doc,
202 "This meshes edge sequence (read-only).\n\n:type: :class:`BMEdgeSeq`"
203 );
204 static PyObject *bpy_bmedgeseq_get(BPy_BMesh *self, void *UNUSED(closure))
205 {
206         BPY_BM_CHECK_OBJ(self);
207         return BPy_BMEdgeSeq_CreatePyObject(self->bm);
208 }
209
210 PyDoc_STRVAR(bpy_bmfaceseq_doc,
211 "This meshes face sequence (read-only).\n\n:type: :class:`BMFaceSeq`"
212 );
213 static PyObject *bpy_bmfaceseq_get(BPy_BMesh *self, void *UNUSED(closure))
214 {
215         BPY_BM_CHECK_OBJ(self);
216         return BPy_BMFaceSeq_CreatePyObject(self->bm);
217 }
218
219 PyDoc_STRVAR(bpy_bmloopseq_doc,
220 "This meshes face sequence (read-only).\n\n:type: :class:`BMLoopSeq`"
221 );
222 static PyObject *bpy_bmloopseq_get(BPy_BMesh *self, void *UNUSED(closure))
223 {
224         BPY_BM_CHECK_OBJ(self);
225         return BPy_BMLoopSeq_CreatePyObject(self->bm);
226 }
227
228 /* vert */
229 PyDoc_STRVAR(bpy_bmvert_link_edges_doc,
230 "Edges connected to this vertex (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
231 );
232 PyDoc_STRVAR(bpy_bmvert_link_faces_doc,
233 "Faces connected to this vertex (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMFace`"
234 );
235 PyDoc_STRVAR(bpy_bmvert_link_loops_doc,
236 "Loops that use this vertex (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
237 );
238 /* edge */
239 PyDoc_STRVAR(bpy_bmedge_verts_doc,
240 "Verts this edge uses (always 2), (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
241 );
242 PyDoc_STRVAR(bpy_bmedge_link_faces_doc,
243 "Faces connected to this edge, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMFace`"
244 );
245 PyDoc_STRVAR(bpy_bmedge_link_loops_doc,
246 "Loops connected to this edge, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
247 );
248 /* face */
249 PyDoc_STRVAR(bpy_bmface_verts_doc,
250 "Verts of this face, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMVert`"
251 );
252 PyDoc_STRVAR(bpy_bmface_edges_doc,
253 "Edges of this face, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMEdge`"
254 );
255 PyDoc_STRVAR(bpy_bmface_loops_doc,
256 "Loops of this face, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
257 );
258 /* loop */
259 PyDoc_STRVAR(bpy_bmloops_link_loops_doc,
260 "Loops connected to this loop, (read-only).\n\n:type: :class:`BMElemSeq` of :class:`BMLoop`"
261 );
262
263 static PyObject *bpy_bmelemseq_elem_get(BPy_BMElem *self, void *itype)
264 {
265         BPY_BM_CHECK_OBJ(self);
266         return BPy_BMElemSeq_CreatePyObject(self->bm, self, GET_INT_FROM_POINTER(itype));
267 }
268
269
270 PyDoc_STRVAR(bpy_bm_is_valid_doc,
271 "True when this element is valid (hasn't been removed).\n\n:type: boolean"
272 );
273 static PyObject *bpy_bm_is_valid_get(BPy_BMGeneric *self)
274 {
275         return PyBool_FromLong(BPY_BM_IS_VALID(self));
276 }
277
278 PyDoc_STRVAR(bpy_bmesh_is_wrapped_doc,
279 "True when this mesh is owned by blender (typically the editmode BMesh).\n\n:type: boolean"
280 );
281 static PyObject *bpy_bmesh_is_wrapped_get(BPy_BMesh *self)
282 {
283         BPY_BM_CHECK_OBJ(self);
284
285         return PyBool_FromLong(self->flag & BPY_BMFLAG_IS_WRAPPED);
286 }
287
288 PyDoc_STRVAR(bpy_bmesh_select_mode_doc,
289 "The selection mode, values can be {'VERT', 'EDGE', 'FACE'}, can't be assigned an empty set.\n\n:type: set"
290 );
291 static PyObject *bpy_bmesh_select_mode_get(BPy_BMesh *self)
292 {
293         BPY_BM_CHECK_OBJ(self);
294
295         return PyC_FlagSet_FromBitfield(bpy_bm_scene_vert_edge_face_flags, self->bm->selectmode);
296 }
297
298 static int bpy_bmesh_select_mode_set(BPy_BMesh *self, PyObject *value)
299 {
300         int flag = 0;
301         BPY_BM_CHECK_INT(self);
302
303         if (PyC_FlagSet_ToBitfield(bpy_bm_scene_vert_edge_face_flags, value, &flag, "bm.select_mode") == -1) {
304                 return -1;
305         }
306         else if (flag == 0) {
307                 PyErr_SetString(PyExc_TypeError,
308                                 "bm.select_mode: cant assignt an empty value");
309                 return -1;
310         }
311         else {
312                 self->bm->selectmode = flag;
313                 return 0;
314         }
315 }
316
317 PyDoc_STRVAR(bpy_bmesh_select_history_doc,
318 "Sequence of selected items (the last is displayed as active).\n\n:type: :class:`BMEditSelSeq`"
319 );
320 static PyObject *bpy_bmesh_select_history_get(BPy_BMesh *self)
321 {
322         BPY_BM_CHECK_OBJ(self);
323
324         return BPy_BMEditSel_CreatePyObject(self->bm);
325 }
326
327 static int bpy_bmesh_select_history_set(BPy_BMesh *self, PyObject *value)
328 {
329         BPY_BM_CHECK_INT(self);
330
331         return BPy_BMEditSel_Assign(self, value);
332 }
333
334 /* Vert
335  * ^^^^ */
336
337 PyDoc_STRVAR(bpy_bmvert_co_doc,
338 "The coordinates for this vertex as a 3D, wrapped vector.\n\n:type: :class:`mathutils.Vector`"
339 );
340 static PyObject *bpy_bmvert_co_get(BPy_BMVert *self)
341 {
342         BPY_BM_CHECK_OBJ(self);
343         return Vector_CreatePyObject(self->v->co, 3, Py_WRAP, NULL);
344 }
345
346 static int bpy_bmvert_co_set(BPy_BMVert *self, PyObject *value)
347 {
348         BPY_BM_CHECK_INT(self);
349
350         if (mathutils_array_parse(self->v->co, 3, 3, value, "BMVert.co") != -1) {
351                 return 0;
352         }
353         else {
354                 return -1;
355         }
356 }
357
358
359 PyDoc_STRVAR(bpy_bmvert_normal_doc,
360 "The normal for this vertex as a 3D, wrapped vector.\n\n:type: :class:`mathutils.Vector`"
361 );
362 static PyObject *bpy_bmvert_normal_get(BPy_BMVert *self)
363 {
364         BPY_BM_CHECK_OBJ(self);
365         return Vector_CreatePyObject(self->v->no, 3, Py_WRAP, NULL);
366 }
367
368 static int bpy_bmvert_normal_set(BPy_BMVert *self, PyObject *value)
369 {
370         BPY_BM_CHECK_INT(self);
371
372         if (mathutils_array_parse(self->v->no, 3, 3, value, "BMVert.normal") != -1) {
373                 return 0;
374         }
375         else {
376                 return -1;
377         }
378 }
379
380
381 PyDoc_STRVAR(bpy_bmvert_is_manifold_doc,
382 "True when this vertex is manifold (read-only).\n\n:type: boolean"
383 );
384 static PyObject *bpy_bmvert_is_manifold_get(BPy_BMVert *self)
385 {
386         BPY_BM_CHECK_OBJ(self);
387         return PyBool_FromLong(BM_vert_is_manifold(self->bm, self->v));
388 }
389
390
391 PyDoc_STRVAR(bpy_bmvert_is_wire_doc,
392 "True when this vertex is not connected to any faces (read-only).\n\n:type: boolean"
393 );
394 static PyObject *bpy_bmvert_is_wire_get(BPy_BMVert *self)
395 {
396         BPY_BM_CHECK_OBJ(self);
397         return PyBool_FromLong(BM_vert_is_wire(self->bm, self->v));
398 }
399
400
401 /* Edge
402  * ^^^^ */
403
404 PyDoc_STRVAR(bpy_bmedge_is_manifold_doc,
405 "True when this edge is manifold (read-only).\n\n:type: boolean"
406 );
407 static PyObject *bpy_bmedge_is_manifold_get(BPy_BMEdge *self)
408 {
409         BPY_BM_CHECK_OBJ(self);
410         return PyBool_FromLong(BM_edge_is_manifold(self->bm, self->e));
411 }
412
413
414 PyDoc_STRVAR(bpy_bmedge_is_wire_doc,
415 "True when this edge is not connected to any faces (read-only).\n\n:type: boolean"
416 );
417 static PyObject *bpy_bmedge_is_wire_get(BPy_BMEdge *self)
418 {
419         BPY_BM_CHECK_OBJ(self);
420         return PyBool_FromLong(BM_edge_is_wire(self->bm, self->e));
421 }
422
423
424 PyDoc_STRVAR(bpy_bmedge_is_boundary_doc,
425 "True when this edge is at the boundary of a face (read-only).\n\n:type: boolean"
426 );
427 static PyObject *bpy_bmedge_is_boundary_get(BPy_BMEdge *self)
428 {
429         BPY_BM_CHECK_OBJ(self);
430         return PyBool_FromLong(BM_edge_is_boundary(self->e));
431 }
432
433
434 /* Face
435  * ^^^^ */
436
437 PyDoc_STRVAR(bpy_bmface_normal_doc,
438 "The normal for this face as a 3D, wrapped vector.\n\n:type: :class:`mathutils.Vector`"
439 );
440 static PyObject *bpy_bmface_normal_get(BPy_BMFace *self)
441 {
442         BPY_BM_CHECK_OBJ(self);
443         return Vector_CreatePyObject(self->f->no, 3, Py_WRAP, NULL);
444 }
445
446 static int bpy_bmface_normal_set(BPy_BMFace *self, PyObject *value)
447 {
448         BPY_BM_CHECK_INT(self);
449
450         if (mathutils_array_parse(self->f->no, 3, 3, value, "BMFace.normal") != -1) {
451                 return 0;
452         }
453         else {
454                 return -1;
455         }
456 }
457
458 /* Loop
459  * ^^^^ */
460
461 PyDoc_STRVAR(bpy_bmloop_vert_doc,
462 "The loops vertex (read-only).\n\n:type: :class:`BMVert`"
463 );
464 static PyObject *bpy_bmloop_vert_get(BPy_BMLoop *self)
465 {
466         BPY_BM_CHECK_OBJ(self);
467         return BPy_BMVert_CreatePyObject(self->bm, self->l->v);
468 }
469
470
471 PyDoc_STRVAR(bpy_bmloop_edge_doc,
472 "The loops edge (between this loop and the next), (read-only).\n\n:type: :class:`BMEdge`"
473 );
474 static PyObject *bpy_bmloop_edge_get(BPy_BMLoop *self)
475 {
476         BPY_BM_CHECK_OBJ(self);
477         return BPy_BMEdge_CreatePyObject(self->bm, self->l->e);
478 }
479
480
481 PyDoc_STRVAR(bpy_bmloop_face_doc,
482 "The face this loop makes (read-only).\n\n:type: :class:`BMFace`"
483 );
484 static PyObject *bpy_bmloop_face_get(BPy_BMLoop *self)
485 {
486         BPY_BM_CHECK_OBJ(self);
487         return BPy_BMFace_CreatePyObject(self->bm, self->l->f);
488 }
489
490 PyDoc_STRVAR(bpy_bmloop_link_loop_next_doc,
491 "The next face corner (read-only).\n\n:type: :class:`BMLoop`"
492 );
493 static PyObject *bpy_bmloop_link_loop_next_get(BPy_BMLoop *self)
494 {
495         BPY_BM_CHECK_OBJ(self);
496         return BPy_BMLoop_CreatePyObject(self->bm, self->l->next);
497 }
498
499 PyDoc_STRVAR(bpy_bmloop_link_loop_prev_doc,
500 "The previous face corner (read-only).\n\n:type: :class:`BMLoop`"
501 );
502 static PyObject *bpy_bmloop_link_loop_prev_get(BPy_BMLoop *self)
503 {
504         BPY_BM_CHECK_OBJ(self);
505         return BPy_BMLoop_CreatePyObject(self->bm, self->l->prev);
506 }
507
508 /* ElemSeq
509  * ^^^^^^^ */
510
511 /* note: use for bmvert/edge/face/loop seq's use these, not bmelemseq directly */
512 PyDoc_STRVAR(bpy_bmelemseq_layers_doc,
513 "blah blah (read-only).\n\n:type: :class:`BMLayerAccess`"
514 );
515 static PyObject *bpy_bmelemseq_layers_get(BPy_BMElemSeq *self, void *htype)
516 {
517         BPY_BM_CHECK_OBJ(self);
518
519         return BPy_BMLayerAccess_CreatePyObject(self->bm, GET_INT_FROM_POINTER(htype));
520 }
521
522 static PyGetSetDef bpy_bmesh_getseters[] = {
523     {(char *)"verts", (getter)bpy_bmvertseq_get, (setter)NULL, (char *)bpy_bmvertseq_doc, NULL},
524     {(char *)"edges", (getter)bpy_bmedgeseq_get, (setter)NULL, (char *)bpy_bmedgeseq_doc, NULL},
525     {(char *)"faces", (getter)bpy_bmfaceseq_get, (setter)NULL, (char *)bpy_bmfaceseq_doc, NULL},
526     {(char *)"loops", (getter)bpy_bmloopseq_get, (setter)NULL, (char *)bpy_bmloopseq_doc, NULL},
527     {(char *)"select_mode", (getter)bpy_bmesh_select_mode_get, (setter)bpy_bmesh_select_mode_set, (char *)bpy_bmesh_select_mode_doc, NULL},
528
529     {(char *)"select_history", (getter)bpy_bmesh_select_history_get, (setter)bpy_bmesh_select_history_set, (char *)bpy_bmesh_select_history_doc, NULL},
530
531     /* readonly checks */
532     {(char *)"is_wrapped", (getter)bpy_bmesh_is_wrapped_get, (setter)NULL, (char *)bpy_bmesh_is_wrapped_doc, NULL}, /* as with mathutils */
533     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get,   (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
534
535     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
536 };
537
538 static PyGetSetDef bpy_bmvert_getseters[] = {
539     /* generic */
540     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
541     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
542     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
543     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
544
545     {(char *)"co",     (getter)bpy_bmvert_co_get,     (setter)bpy_bmvert_co_set,     (char *)bpy_bmvert_co_doc, NULL},
546     {(char *)"normal", (getter)bpy_bmvert_normal_get, (setter)bpy_bmvert_normal_set, (char *)bpy_bmvert_normal_doc, NULL},
547
548     /* connectivity data */
549     {(char *)"link_edges", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_edges_doc, (void *)BM_EDGES_OF_VERT},
550     {(char *)"link_faces", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_faces_doc, (void *)BM_FACES_OF_VERT},
551     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmvert_link_loops_doc, (void *)BM_LOOPS_OF_VERT},
552
553     /* readonly checks */
554     {(char *)"is_manifold",  (getter)bpy_bmvert_is_manifold_get,  (setter)NULL, (char *)bpy_bmvert_is_manifold_doc, NULL},
555     {(char *)"is_wire",      (getter)bpy_bmvert_is_wire_get,      (setter)NULL, (char *)bpy_bmvert_is_wire_doc, NULL},
556     {(char *)"is_valid",     (getter)bpy_bm_is_valid_get,         (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
557
558     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
559 };
560
561 static PyGetSetDef bpy_bmedge_getseters[] = {
562     /* generic */
563     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
564     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
565     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
566     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
567
568     {(char *)"smooth", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SMOOTH},
569     {(char *)"seam",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SEAM},
570
571     /* connectivity data */
572     {(char *)"verts", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_verts_doc, (void *)BM_VERTS_OF_EDGE},
573
574     {(char *)"link_faces", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_link_faces_doc, (void *)BM_FACES_OF_EDGE},
575     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmedge_link_loops_doc, (void *)BM_LOOPS_OF_EDGE},
576
577     /* readonly checks */
578     {(char *)"is_manifold",  (getter)bpy_bmedge_is_manifold_get,  (setter)NULL, (char *)bpy_bmedge_is_manifold_doc, NULL},
579     {(char *)"is_wire",      (getter)bpy_bmedge_is_wire_get,      (setter)NULL, (char *)bpy_bmedge_is_wire_doc, NULL},
580     {(char *)"is_boundary",   (getter)bpy_bmedge_is_boundary_get,   (setter)NULL, (char *)bpy_bmedge_is_boundary_doc, NULL},
581     {(char *)"is_valid",     (getter)bpy_bm_is_valid_get,         (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
582
583     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
584 };
585
586 static PyGetSetDef bpy_bmface_getseters[] = {
587     /* generic */
588     {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
589     {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
590     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
591     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
592
593     {(char *)"smooth", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_smooth_doc, (void *)BM_ELEM_SMOOTH},
594
595     {(char *)"normal", (getter)bpy_bmface_normal_get, (setter)bpy_bmface_normal_set, (char *)bpy_bmface_normal_doc, NULL},
596
597     /* connectivity data */
598     {(char *)"verts", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_verts_doc, (void *)BM_VERTS_OF_FACE},
599     {(char *)"edges", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_edges_doc, (void *)BM_EDGES_OF_FACE},
600     {(char *)"loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmface_loops_doc, (void *)BM_LOOPS_OF_FACE},
601
602     /* readonly checks */
603     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get, (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
604
605     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
606 };
607
608 static PyGetSetDef bpy_bmloop_getseters[] = {
609     /* generic */
610     // flags are available but not used for loops.
611     // {(char *)"select", (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_select_doc, (void *)BM_ELEM_SELECT},
612     // {(char *)"hide",   (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_hide_doc,   (void *)BM_ELEM_SELECT},
613     {(char *)"tag",    (getter)bpy_bm_elem_hflag_get, (setter)bpy_bm_elem_hflag_set, (char *)bpy_bm_elem_tag_doc,    (void *)BM_ELEM_TAG},
614     {(char *)"index",  (getter)bpy_bm_elem_index_get, (setter)bpy_bm_elem_index_set, (char *)bpy_bm_elem_index_doc,  NULL},
615
616     {(char *)"vert", (getter)bpy_bmloop_vert_get, (setter)NULL, (char *)bpy_bmloop_vert_doc, NULL},
617     {(char *)"edge", (getter)bpy_bmloop_edge_get, (setter)NULL, (char *)bpy_bmloop_edge_doc, NULL},
618     {(char *)"face", (getter)bpy_bmloop_face_get, (setter)NULL, (char *)bpy_bmloop_face_doc, NULL},
619
620     /* connectivity data */
621     {(char *)"link_loops", (getter)bpy_bmelemseq_elem_get, (setter)NULL, (char *)bpy_bmloops_link_loops_doc, (void *)BM_LOOPS_OF_LOOP},
622     {(char *)"link_loop_next", (getter)bpy_bmloop_link_loop_next_get, (setter)NULL, (char *)bpy_bmloop_link_loop_next_doc, NULL},
623     {(char *)"link_loop_prev", (getter)bpy_bmloop_link_loop_prev_get, (setter)NULL, (char *)bpy_bmloop_link_loop_prev_doc, NULL},
624
625     /* readonly checks */
626     {(char *)"is_valid",   (getter)bpy_bm_is_valid_get, (setter)NULL, (char *)bpy_bm_is_valid_doc, NULL},
627
628     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
629 };
630
631 static PyGetSetDef bpy_bmvertseq_getseters[] = {
632     {(char *)"layers",    (getter)bpy_bmelemseq_layers_get, (setter)NULL, (char *)bpy_bmelemseq_layers_doc, (void *)BM_VERT},
633     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
634 };
635 static PyGetSetDef bpy_bmedgeseq_getseters[] = {
636     {(char *)"layers",    (getter)bpy_bmelemseq_layers_get, (setter)NULL, (char *)bpy_bmelemseq_layers_doc, (void *)BM_EDGE},
637     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
638 };
639 static PyGetSetDef bpy_bmfaceseq_getseters[] = {
640     {(char *)"layers",    (getter)bpy_bmelemseq_layers_get, (setter)NULL, (char *)bpy_bmelemseq_layers_doc, (void *)BM_FACE},
641     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
642 };
643 static PyGetSetDef bpy_bmloopseq_getseters[] = {
644     {(char *)"layers",    (getter)bpy_bmelemseq_layers_get, (setter)NULL, (char *)bpy_bmelemseq_layers_doc, (void *)BM_LOOP},
645     {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
646 };
647
648
649 /* Methods
650  * ======= */
651
652
653 /* Mesh
654  * ---- */
655
656 PyDoc_STRVAR(bpy_bmesh_copy_doc,
657 ".. method:: copy()\n"
658 "\n"
659 "   :return: A copy of this BMesh.\n"
660 "   :rtype: :class:`BMesh`\n"
661 );
662 static PyObject *bpy_bmesh_copy(BPy_BMesh *self)
663 {
664         BMesh *bm;
665         BMesh *bm_copy;
666
667         BPY_BM_CHECK_OBJ(self);
668
669         bm = self->bm;
670
671         bm_copy = BM_mesh_copy(bm);
672
673         if (bm_copy) {
674                 return BPy_BMesh_CreatePyObject(bm_copy, BPY_BMFLAG_NOP);
675         }
676         else {
677                 PyErr_SetString(PyExc_SystemError, "Unable to copy BMesh, internal error");
678                 return NULL;
679         }
680 }
681
682 PyDoc_STRVAR(bpy_bmesh_clear_doc,
683 ".. method:: clear()\n"
684 "\n"
685 "   Clear all mesh data.\n"
686 );
687 static PyObject *bpy_bmesh_clear(BPy_BMesh *self)
688 {
689         BMesh *bm;
690
691         BPY_BM_CHECK_OBJ(self);
692
693         bm = self->bm;
694
695         BM_mesh_clear(bm);
696
697         Py_RETURN_NONE;
698 }
699
700 PyDoc_STRVAR(bpy_bmesh_free_doc,
701 ".. method:: free()\n"
702 "\n"
703 "   Explicitly free the BMesh data from memory, causing exceptions on further access.\n"
704 "\n"
705 "   .. note::\n"
706 "\n"
707 "      The BMesh is freed automatically, typically when the script finishes executing.\n"
708 "      However in some cases its hard to predict when this will be and its useful to\n"
709 "      explicitly free the data.\n"
710 );
711 static PyObject *bpy_bmesh_free(BPy_BMesh *self)
712 {
713         if (self->bm) {
714                 BMesh *bm = self->bm;
715
716                 if ((self->flag & BPY_BMFLAG_IS_WRAPPED) == 0) {
717                         BM_mesh_free(bm);
718                 }
719
720                 bpy_bm_generic_invalidate((BPy_BMGeneric *)self);
721         }
722
723         Py_RETURN_NONE;
724 }
725
726 PyDoc_STRVAR(bpy_bmesh_to_mesh_doc,
727 ".. method:: to_mesh(mesh)\n"
728 "\n"
729 "   Writes this BMesh data into an existing Mesh datablock.\n"
730 "\n"
731 "   :arg mesh: The mesh data to write into.\n"
732 "   :type mesh: :class:`Mesh`\n"
733 );
734 static PyObject *bpy_bmesh_to_mesh(BPy_BMesh *self, PyObject *args)
735 {
736         PyObject *py_mesh;
737         Mesh *me;
738         BMesh *bm;
739
740         BPY_BM_CHECK_OBJ(self);
741
742         if (!PyArg_ParseTuple(args, "O:to_mesh", &py_mesh) ||
743             !(me = PyC_RNA_AsPointer(py_mesh, "Mesh")))
744         {
745                 return NULL;
746         }
747
748         /* we could allow this but its almost certainly _not_ what script authors want */
749         if (me->edit_btmesh) {
750                 PyErr_Format(PyExc_ValueError,
751                              "to_mesh(): Mesh '%s' is in editmode", me->id.name + 2);
752                 return NULL;
753         }
754
755         bm = self->bm;
756
757         BM_mesh_bm_to_me(bm, me, FALSE);
758
759         /* we could have the user do this but if they forget blender can easy crash
760          * since the references arrays for the objects derived meshes are now invalid */
761         DAG_id_tag_update(&me->id, OB_RECALC_DATA);
762
763         Py_RETURN_NONE;
764 }
765
766 /* note: rna_Object_to_mesh() also has apply_modifiers arg that works the same way */
767 PyDoc_STRVAR(bpy_bmesh_from_object_doc,
768 ".. method:: from_object(mesh, apply_modifiers=True)\n"
769 "\n"
770 "   Initialize this bmesh from existing object datablock.\n"
771 "\n"
772 "   :arg object: The object data to load.\n"
773 "   :type object: :class:`Object`\n"
774 "   :arg apply_modifiers: Use the final display mesh rather then the deformed cage.\n"
775 "   :type apply_modifiers: boolean\n"
776 );
777 static PyObject *bpy_bmesh_from_object(BPy_BMesh *self, PyObject *args)
778 {
779         PyObject *py_object;
780         Object *ob;
781         BMesh *bm;
782         int apply_modifiers = TRUE;
783         DerivedMesh *dm;
784
785         BPY_BM_CHECK_OBJ(self);
786
787         if (!PyArg_ParseTuple(args, "O|i:from_object", &py_object, &apply_modifiers) ||
788             !(ob = PyC_RNA_AsPointer(py_object, "Object")))
789         {
790                 return NULL;
791         }
792
793         dm = apply_modifiers ? ob->derivedFinal : ob->derivedDeform;
794
795         if (dm == NULL) {
796                 PyErr_Format(PyExc_ValueError,
797                              "from_object(...): Object '%s' has no usable mesh data", ob->id.name + 2);
798                 return NULL;
799         }
800
801         bm = self->bm;
802
803         DM_to_bmesh_ex(dm, bm);
804
805         Py_RETURN_NONE;
806 }
807
808
809 PyDoc_STRVAR(bpy_bmesh_from_mesh_doc,
810 ".. method:: from_mesh(mesh, use_shape_key=False, shape_key_index=0)\n"
811 "\n"
812 "   Initialize this bmesh from existing mesh datablock.\n"
813 "\n"
814 "   :arg mesh: The mesh data to load.\n"
815 "   :type mesh: :class:`Mesh`\n"
816 "   :arg use_shape_key: Use the locations from a shape key.\n"
817 "   :type use_shape_key: boolean\n"
818 "   :arg shape_key_index: The shape key index to use.\n"
819 "   :type shape_key_index: int\n"
820 );
821 static PyObject *bpy_bmesh_from_mesh(BPy_BMesh *self, PyObject *args, PyObject *kw)
822 {
823         static const char *kwlist[] = {"mesh", "use_shape_key", "shape_key_index", NULL};
824         BMesh *bm;
825         PyObject *py_mesh;
826         Mesh *me;
827         int use_shape_key = FALSE;
828         int shape_key_index = 0;
829
830         if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:from_mesh", (char **)kwlist,
831                                          &py_mesh, &use_shape_key, &shape_key_index) ||
832             !(me = PyC_RNA_AsPointer(py_mesh, "Mesh")))
833         {
834                 return NULL;
835         }
836
837         bm = self->bm;
838
839         BM_mesh_bm_from_me(bm, me, use_shape_key, shape_key_index + 1);
840
841         Py_RETURN_NONE;
842 }
843
844
845 PyDoc_STRVAR(bpy_bmesh_select_flush_mode_doc,
846 ".. method:: select_flush_mode()\n"
847 "\n"
848 "   flush selection based on the current mode current :class:`BMesh.select_mode`.\n"
849 );
850 static PyObject *bpy_bmesh_select_flush_mode(BPy_BMesh *self)
851 {
852         BPY_BM_CHECK_OBJ(self);
853
854         BM_mesh_select_mode_flush(self->bm);
855
856         Py_RETURN_NONE;
857 }
858
859
860 PyDoc_STRVAR(bpy_bmesh_select_flush_doc,
861 ".. method:: select_flush(select)\n"
862 "\n"
863 "   Flush selection, independent of the current selection mode.\n"
864 "\n"
865 "   :arg select: flush selection or de-selected elements.\n"
866 "   :type select: boolean\n"
867 );
868 static PyObject *bpy_bmesh_select_flush(BPy_BMesh *self, PyObject *value)
869 {
870         int param;
871
872         BPY_BM_CHECK_OBJ(self);
873
874         param = PyLong_AsLong(value);
875         if (param != FALSE && param != TRUE) {
876                 PyErr_SetString(PyExc_TypeError,
877                                 "expected a boolean type 0/1");
878                 return NULL;
879         }
880
881         if (param)  BM_mesh_select_flush(self->bm);
882         else        BM_mesh_deselect_flush(self->bm);
883
884         Py_RETURN_NONE;
885 }
886
887
888 PyDoc_STRVAR(bpy_bmesh_normal_update_doc,
889 ".. method:: normal_update(skip_hidden=False)\n"
890 "\n"
891 "   Update mesh normals.\n"
892 "\n"
893 "   :arg skip_hidden: When True hidden elements are ignored.\n"
894 "   :type skip_hidden: boolean\n"
895 );
896 static PyObject *bpy_bmesh_normal_update(BPy_BMesh *self, PyObject *args)
897 {
898
899         int skip_hidden = FALSE;
900
901         BPY_BM_CHECK_OBJ(self);
902
903         if (!PyArg_ParseTuple(args, "|i:normal_update", &skip_hidden)) {
904                 return NULL;
905         }
906
907         BM_mesh_normals_update(self->bm, skip_hidden);
908
909         Py_RETURN_NONE;
910 }
911
912
913 PyDoc_STRVAR(bpy_bmesh_transform_doc,
914 ".. method:: transform(matrix, filter=None)\n"
915 "\n"
916 "   Transform the mesh (optionally filtering flagged data only).\n"
917 "\n"
918 "   :arg matrix: transform matrix.\n"
919 "   :type matrix: 4x4 :class:`mathutils.Matrix`\n"
920 "   :arg filter: set of values in ('SELECT', 'HIDE', 'SEAM', 'SMOOTH', 'TAG').\n"
921 "   :type filter: set\n"
922 );
923 static PyObject *bpy_bmesh_transform(BPy_BMElem *self, PyObject *args, PyObject *kw)
924 {
925         static const char *kwlist[] = {"matrix", "filter", NULL};
926
927         MatrixObject *mat;
928         PyObject *filter = NULL;
929         int filter_flags = 0;
930
931         BPY_BM_CHECK_OBJ(self);
932
933         if (!PyArg_ParseTupleAndKeywords(args, kw,
934                                          "O!|O!:transform",
935                                          (char **)kwlist,
936                                          &matrix_Type, &mat,
937                                          &PySet_Type,  &filter))
938         {
939                 return NULL;
940         }
941         else {
942                 BMVert *eve;
943                 BMIter iter;
944                 void *mat_ptr;
945
946                 if (BaseMath_ReadCallback(mat) == -1) {
947                         return NULL;
948                 }
949                 else if (mat->num_col != 4 || mat->num_row != 4) {
950                         PyErr_SetString(PyExc_ValueError,
951                                         "expected a 4x4 matrix");
952                         return NULL;
953                 }
954
955                 if (filter != NULL && PyC_FlagSet_ToBitfield(bpy_bm_hflag_all_flags, filter,
956                                                              &filter_flags, "bm.transform") == -1)
957                 {
958                         return NULL;
959                 }
960
961                 mat_ptr = mat->matrix;
962
963                 if (!filter_flags) {
964                         BM_ITER(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
965                                 mul_m4_v3((float (*)[4])mat_ptr, eve->co);
966                         }
967                 }
968                 else {
969                         char filter_flags_ch = (char)filter_flags;
970                         BM_ITER(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
971                                 if (eve->head.hflag & filter_flags_ch) {
972                                         mul_m4_v3((float (*)[4])mat_ptr, eve->co);
973                                 }
974                         }
975                 }
976         }
977
978         Py_RETURN_NONE;
979 }
980
981
982 /* Elem
983  * ---- */
984
985 PyDoc_STRVAR(bpy_bm_elem_select_set_doc,
986 ".. method:: select_set(select)\n"
987 "\n"
988 "   Set the selection.\n"
989 "   This is different from the *select* attribute because it updates the selection state of assosiated geometry.\n"
990 "\n"
991 "   :arg select: Select or de-select.\n"
992 "   :type select: boolean\n"
993 "\n"
994 "   .. note::\n"
995 "\n"
996 "      Currently this only flushes down, so selecting a face will select all its vertices but de-selecting a vertex "
997 "      won't de-select all the faces that use it, before finishing with a mesh typically flushing is still needed.\n"
998 );
999 static PyObject *bpy_bm_elem_select_set(BPy_BMElem *self, PyObject *value)
1000 {
1001         int param;
1002
1003         BPY_BM_CHECK_OBJ(self);
1004
1005         param = PyLong_AsLong(value);
1006         if (param != FALSE && param != TRUE) {
1007                 PyErr_SetString(PyExc_TypeError,
1008                                 "expected a boolean type 0/1");
1009                 return NULL;
1010         }
1011
1012         BM_elem_select_set(self->bm, self->ele, param);
1013
1014         Py_RETURN_NONE;
1015 }
1016
1017
1018 PyDoc_STRVAR(bpy_bm_elem_hide_set_doc,
1019 ".. method:: hide_set(hide)\n"
1020 "\n"
1021 "   Set the hide state.\n"
1022 "   This is different from the *hide* attribute because it updates the selection and hide state of assosiated geometry.\n"
1023 "\n"
1024 "   :arg hide: Hidden or visible.\n"
1025 "   :type hide: boolean\n"
1026 );
1027 static PyObject *bpy_bm_elem_hide_set(BPy_BMElem *self, PyObject *value)
1028 {
1029         int param;
1030
1031         BPY_BM_CHECK_OBJ(self);
1032
1033         param = PyLong_AsLong(value);
1034         if (param != FALSE && param != TRUE) {
1035                 PyErr_SetString(PyExc_TypeError,
1036                                 "expected a boolean type 0/1");
1037                 return NULL;
1038         }
1039
1040         BM_elem_hide_set(self->bm, self->ele, param);
1041
1042         Py_RETURN_NONE;
1043 }
1044
1045
1046 PyDoc_STRVAR(bpy_bm_elem_copy_from_doc,
1047 ".. method:: copy_from(other)\n"
1048 "\n"
1049 "   Copy values from another element of matching type.\n"
1050 );
1051 static PyObject *bpy_bm_elem_copy_from(BPy_BMElem *self, BPy_BMElem *value)
1052 {
1053         BPY_BM_CHECK_OBJ(self);
1054
1055         if (Py_TYPE(self) != Py_TYPE(value)) {
1056                 PyErr_Format(PyExc_TypeError,
1057                              "expected element of type '%.200s' not '%.200s'",
1058                              Py_TYPE(self)->tp_name, Py_TYPE(value)->tp_name);
1059                 return NULL;
1060         }
1061
1062         if (value->ele != self->ele) {
1063                 BM_elem_attrs_copy(value->bm, self->bm, value->ele, self->ele);
1064         }
1065
1066         Py_RETURN_NONE;
1067 }
1068
1069
1070 /* Vert
1071  * ---- */
1072
1073
1074 PyDoc_STRVAR(bpy_bmvert_copy_from_vert_interp_doc,
1075 ".. method:: copy_from_vert_interp(vert_pair, fac)\n"
1076 "\n"
1077 "   Interpolate the customdata from a vert between 2 other verts.\n"
1078 "\n"
1079 "   :arg vert_pair: The vert to interpolate data from.\n"
1080 "   :type vert_pair: :class:`BMVert`\n"
1081 );
1082 static PyObject *bpy_bmvert_copy_from_vert_interp(BPy_BMVert *self, PyObject *args)
1083 {
1084         PyObject *vert_seq;
1085         float fac;
1086
1087         BPY_BM_CHECK_OBJ(self);
1088
1089         if (!PyArg_ParseTuple(args, "Of:BMVert.copy_from_vert_interp",
1090                               &vert_seq, &fac))
1091         {
1092                 return NULL;
1093         }
1094         else {
1095                 BMesh *bm = self->bm;
1096                 BMVert **vert_array = NULL;
1097                 Py_ssize_t vert_seq_len; /* always 2 */
1098
1099                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 2, 2,
1100                                                        &vert_seq_len, BM_VERT,
1101                                                        TRUE, TRUE, "BMVert.copy_from_vert_interp(...)");
1102
1103                 if (vert_array == NULL) {
1104                         return NULL;
1105                 }
1106
1107                 BM_data_interp_from_verts(bm, vert_array[0], vert_array[1], self->v, CLAMPIS(fac, 0.0f, 1.0f));
1108
1109                 PyMem_FREE(vert_array);
1110                 Py_RETURN_NONE;
1111         }
1112 }
1113
1114
1115 PyDoc_STRVAR(bpy_bmvert_copy_from_face_interp_doc,
1116 ".. method:: copy_from_face_interp(face)\n"
1117 "\n"
1118 "   Interpolate the customdata from a face onto this loop (the loops vert should overlap the face).\n"
1119 "\n"
1120 "   :arg face: The face to interpolate data from.\n"
1121 "   :type face: :class:`BMFace`\n"
1122 );
1123 static PyObject *bpy_bmvert_copy_from_face_interp(BPy_BMVert *self, PyObject *args)
1124 {
1125         BPy_BMFace *py_face = NULL;
1126
1127         BPY_BM_CHECK_OBJ(self);
1128
1129         if (!PyArg_ParseTuple(args, "O!:BMVert.copy_from_face_interp",
1130                               &BPy_BMFace_Type, &py_face))
1131         {
1132                 return NULL;
1133         }
1134         else {
1135                 BMesh *bm = self->bm;
1136
1137                 BPY_BM_CHECK_OBJ(py_face);
1138
1139                 if (py_face->bm != bm) {
1140                         PyErr_SetString(PyExc_ValueError,
1141                                         "BMVert.copy_from_face_interp(face): face is from another mesh");
1142                         return NULL;
1143                 }
1144
1145                 BM_vert_interp_from_face(bm, self->v, py_face->f);
1146
1147                 Py_RETURN_NONE;
1148         }
1149 }
1150
1151
1152 PyDoc_STRVAR(bpy_bmvert_calc_edge_angle_doc,
1153 ".. method:: calc_edge_angle()\n"
1154 "\n"
1155 "   Return the angle between 2 connected edges.\n"
1156 "\n"
1157 "   :return: The angle between both edges in radians.\n"
1158 "   :rtype: float\n"
1159 );
1160 static PyObject *bpy_bmvert_calc_edge_angle(BPy_BMVert *self)
1161 {
1162         BPY_BM_CHECK_OBJ(self);
1163         return PyFloat_FromDouble(BM_vert_edge_angle(self->bm, self->v));
1164 }
1165
1166
1167 PyDoc_STRVAR(bpy_bmvert_normal_update_doc,
1168 ".. method:: normal_update()\n"
1169 "\n"
1170 "   Update vertex normal.\n"
1171 );
1172 static PyObject *bpy_bmvert_normal_update(BPy_BMVert *self)
1173 {
1174         BPY_BM_CHECK_OBJ(self);
1175
1176         BM_vert_normal_update(self->bm, self->v);
1177
1178         Py_RETURN_NONE;
1179 }
1180
1181
1182 /* Edge
1183  * ---- */
1184
1185 PyDoc_STRVAR(bpy_bmedge_calc_length_doc,
1186 ".. method:: calc_length()\n"
1187 "\n"
1188 "   :return: The length between both verts.\n"
1189 "   :rtype: float\n"
1190 );
1191 static PyObject *bpy_bmedge_calc_length(BPy_BMEdge *self)
1192 {
1193         BPY_BM_CHECK_OBJ(self);
1194         return PyFloat_FromDouble(len_v3v3(self->e->v1->co, self->e->v2->co));
1195 }
1196
1197 PyDoc_STRVAR(bpy_bmedge_calc_face_angle_doc,
1198 ".. method:: calc_face_angle()\n"
1199 "\n"
1200 "   :return: The angle between 2 connected faces in radians.\n"
1201 "   :rtype: float\n"
1202 );
1203 static PyObject *bpy_bmedge_calc_face_angle(BPy_BMEdge *self)
1204 {
1205         BPY_BM_CHECK_OBJ(self);
1206         return PyFloat_FromDouble(BM_edge_face_angle(self->bm, self->e));
1207 }
1208
1209
1210 PyDoc_STRVAR(bpy_bmedge_other_vert_doc,
1211 ".. method:: other_vert(vert)\n"
1212 "\n"
1213 "   Return the other vertex on this edge or None if the vertex is not used by this edge.\n"
1214 "\n"
1215 "   :arg vert: a vert in this edge.\n"
1216 "   :type vert: :class:`BMVert`\n"
1217 "   :return: The edges other vert.\n"
1218 "   :rtype: :class:`BMVert` or None\n"
1219 );
1220 static PyObject *bpy_bmedge_other_vert(BPy_BMEdge *self, BPy_BMVert *value)
1221 {
1222         BMVert *other;
1223         BPY_BM_CHECK_OBJ(self);
1224
1225         if (!BPy_BMVert_Check(value)) {
1226                 PyErr_Format(PyExc_TypeError,
1227                              "BMEdge.other_vert(vert): BMVert expected, not '%.200s'",
1228                              Py_TYPE(value)->tp_name);
1229                 return NULL;
1230         }
1231
1232         BPY_BM_CHECK_OBJ(value);
1233
1234         if (self->bm != value->bm) {
1235                 PyErr_SetString(PyExc_ValueError,
1236                                 "BMEdge.other_vert(vert): vert is from another mesh");
1237                 return NULL;
1238         }
1239
1240         other = BM_edge_other_vert(self->e, value->v);
1241
1242         if (other) {
1243                 return BPy_BMVert_CreatePyObject(self->bm, other);
1244         }
1245         else {
1246                 /* could raise an exception here */
1247                 Py_RETURN_NONE;
1248         }
1249 }
1250
1251
1252 PyDoc_STRVAR(bpy_bmedge_normal_update_doc,
1253 ".. method:: normal_update()\n"
1254 "\n"
1255 "   Update edges vertex normals.\n"
1256 );
1257 static PyObject *bpy_bmedge_normal_update(BPy_BMEdge *self)
1258 {
1259         BPY_BM_CHECK_OBJ(self);
1260
1261         BM_edge_normals_update(self->bm, self->e);
1262
1263         Py_RETURN_NONE;
1264 }
1265
1266
1267 /* Face
1268  * ---- */
1269
1270 PyDoc_STRVAR(bpy_bmface_copy_from_face_interp_doc,
1271 ".. method:: copy_from_face_interp(face)\n"
1272 "\n"
1273 "   Interpolate the customdata from another face onto this one (faces should overlap).\n"
1274 "\n"
1275 "   :arg face: The face to interpolate data from.\n"
1276 "   :type face: :class:`BMFace`\n"
1277 );
1278 static PyObject *bpy_bmface_copy_from_face_interp(BPy_BMFace *self, PyObject *args)
1279 {
1280         BPy_BMFace *py_face = NULL;
1281
1282         BPY_BM_CHECK_OBJ(self);
1283
1284         if (!PyArg_ParseTuple(args, "O!:BMFace.copy_from_face_interp",
1285                               &BPy_BMFace_Type, &py_face))
1286         {
1287                 return NULL;
1288         }
1289         else {
1290                 BMesh *bm = self->bm;
1291
1292                 BPY_BM_CHECK_OBJ(py_face);
1293
1294                 if (py_face->bm != bm) {
1295                         PyErr_SetString(PyExc_ValueError,
1296                                         "BMFace.copy_from_face_interp(face): face is from another mesh");
1297                         return NULL;
1298                 }
1299
1300                 BM_face_interp_from_face(bm, self->f, py_face->f);
1301
1302                 Py_RETURN_NONE;
1303         }
1304 }
1305
1306
1307 PyDoc_STRVAR(bpy_bmface_copy_doc,
1308 ".. method:: copy(verts=True, edges=True)\n"
1309 "\n"
1310 "   Make a copy of this face.\n"
1311 "\n"
1312 "   :arg verts: When set, the faces verts will be duplicated too.\n"
1313 "   :type verts: boolean\n"
1314 "   :arg edges: When set, the faces edges will be duplicated too.\n"
1315 "   :type edges: boolean\n"
1316 "   :return: The newly created face.\n"
1317 "   :rtype: :class:`BMFace`\n"
1318 );
1319 static PyObject *bpy_bmface_copy(BPy_BMFace *self, PyObject *args, PyObject *kw)
1320 {
1321         static const char *kwlist[] = {"verts", "edges", NULL};
1322
1323         BMesh *bm = self->bm;
1324         int do_verts = TRUE;
1325         int do_edges = TRUE;
1326
1327         BMFace *f_cpy;
1328         BPY_BM_CHECK_OBJ(self);
1329
1330         if (!PyArg_ParseTupleAndKeywords(args, kw,
1331                                          "|ii:BMFace.copy",
1332                                          (char **)kwlist,
1333                                          &do_verts, &do_edges))
1334         {
1335                 return NULL;
1336         }
1337
1338         f_cpy = BM_face_copy(bm, self->f, do_verts, do_edges);
1339
1340         if (f_cpy) {
1341                 return BPy_BMFace_CreatePyObject(bm, f_cpy);
1342         }
1343         else {
1344                 PyErr_SetString(PyExc_ValueError,
1345                                 "BMFace.copy(): couldn't create the new face, internal error");
1346                 return NULL;
1347         }
1348 }
1349
1350
1351 PyDoc_STRVAR(bpy_bmface_calc_area_doc,
1352 ".. method:: calc_area()\n"
1353 "\n"
1354 "   Return the area of the face.\n"
1355 "\n"
1356 "   :return: Return the area of the face.\n"
1357 "   :rtype: float\n"
1358 );
1359 static PyObject *bpy_bmface_calc_area(BPy_BMFace *self)
1360 {
1361         BPY_BM_CHECK_OBJ(self);
1362         return PyFloat_FromDouble(BM_face_area_calc(self->bm, self->f));
1363 }
1364
1365
1366 PyDoc_STRVAR(bpy_bmface_calc_center_mean_doc,
1367 ".. method:: calc_center_median()\n"
1368 "\n"
1369 "   Return median center of the face.\n"
1370 "\n"
1371 "   :return: a 3D vector.\n"
1372 "   :rtype: :class:`mathutils.Vector`\n"
1373 );
1374 static PyObject *bpy_bmface_calc_center_mean(BPy_BMFace *self)
1375 {
1376         float cent[3];
1377
1378         BPY_BM_CHECK_OBJ(self);
1379         BM_face_center_mean_calc(self->bm, self->f, cent);
1380         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
1381 }
1382
1383
1384 PyDoc_STRVAR(bpy_bmface_calc_center_bounds_doc,
1385 ".. method:: calc_center_bounds()\n"
1386 "\n"
1387 "   Return bounds center of the face.\n"
1388 "\n"
1389 "   :return: a 3D vector.\n"
1390 "   :rtype: :class:`mathutils.Vector`\n"
1391 );
1392 static PyObject *bpy_bmface_calc_center_bounds(BPy_BMFace *self)
1393 {
1394         float cent[3];
1395
1396         BPY_BM_CHECK_OBJ(self);
1397         BM_face_center_bounds_calc(self->bm, self->f, cent);
1398         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
1399 }
1400
1401
1402 PyDoc_STRVAR(bpy_bmface_normal_update_doc,
1403 ".. method:: normal_update()\n"
1404 "\n"
1405 "   Update faces normal.\n"
1406 );
1407 static PyObject *bpy_bmface_normal_update(BPy_BMFace *self)
1408 {
1409         BPY_BM_CHECK_OBJ(self);
1410
1411         BM_face_normal_update(self->bm, self->f);
1412
1413         Py_RETURN_NONE;
1414 }
1415
1416
1417 /* Loop
1418  * ---- */
1419
1420 PyDoc_STRVAR(bpy_bmloop_copy_from_face_interp_doc,
1421 ".. method:: copy_from_face_interp(face, vert=True, multires=True)\n"
1422 "\n"
1423 "   Interpolate the customdata from a face onto this loop (the loops vert should overlap the face).\n"
1424 "\n"
1425 "   :arg face: The face to interpolate data from.\n"
1426 "   :type face: :class:`BMFace`\n"
1427 "   :arg vert: When enabled, interpolate the loops vertex data (optional).\n"
1428 "   :type vert: boolean\n"
1429 "   :arg multires: When enabled, interpolate the loops multires data (optional).\n"
1430 "   :type multires: boolean\n"
1431 );
1432 static PyObject *bpy_bmloop_copy_from_face_interp(BPy_BMLoop *self, PyObject *args)
1433 {
1434         BPy_BMFace *py_face = NULL;
1435         int do_vertex   = TRUE;
1436         int do_multires = TRUE;
1437
1438         BPY_BM_CHECK_OBJ(self);
1439
1440         if (!PyArg_ParseTuple(args, "O!|ii:BMLoop.copy_from_face_interp",
1441                               &BPy_BMFace_Type, &py_face,
1442                               &do_vertex, &do_multires))
1443         {
1444                 return NULL;
1445         }
1446         else {
1447                 BMesh *bm = self->bm;
1448
1449                 BPY_BM_CHECK_OBJ(py_face);
1450
1451                 if (py_face->bm != bm) {
1452                         PyErr_SetString(PyExc_ValueError,
1453                                         "BMLoop.copy_from_face_interp(face): face is from another mesh");
1454                         return NULL;
1455                 }
1456
1457                 BM_loop_interp_from_face(bm, self->l, py_face->f, do_vertex, do_multires);
1458
1459                 Py_RETURN_NONE;
1460         }
1461 }
1462
1463
1464 PyDoc_STRVAR(bpy_bmloop_calc_angle_doc,
1465 ".. method:: calc_angle()\n"
1466 "\n"
1467 "   Return the angle at this loops corner of the face.\n"
1468 "   This is calculated so sharper corners give lower angles.\n"
1469 "\n"
1470 "   :return: The angle in radians.\n"
1471 "   :rtype: float\n"
1472 );
1473 static PyObject *bpy_bmloop_calc_angle(BPy_BMLoop *self)
1474 {
1475         BPY_BM_CHECK_OBJ(self);
1476         return PyFloat_FromDouble(BM_loop_face_angle(self->bm, self->l));
1477 }
1478
1479 PyDoc_STRVAR(bpy_bmloop_calc_normal_doc,
1480 ".. method:: calc_normal()\n"
1481 "\n"
1482 "   Return normal at this loops corner of the face.\n"
1483 "   Falls back to the face normal for straignt lines.\n"
1484 "\n"
1485 "   :return: a normalized vector.\n"
1486 "   :rtype: :class:`mathutils.Vector`\n"
1487 );
1488 static PyObject *bpy_bmloop_calc_normal(BPy_BMLoop *self)
1489 {
1490         float vec[3];
1491         BPY_BM_CHECK_OBJ(self);
1492         BM_loop_face_normal(self->bm, self->l, vec);
1493         return Vector_CreatePyObject(vec, 3, Py_NEW, NULL);
1494 }
1495
1496 PyDoc_STRVAR(bpy_bmloop_calc_tangent_doc,
1497 ".. method:: calc_tangent()\n"
1498 "\n"
1499 "   Return the tangent at this loops corner of the face (pointing inward into the face).\n"
1500 "   Falls back to the face normal for straignt lines.\n"
1501 "\n"
1502 "   :return: a normalized vector.\n"
1503 "   :rtype: :class:`mathutils.Vector`\n"
1504 );
1505 static PyObject *bpy_bmloop_calc_tangent(BPy_BMLoop *self)
1506 {
1507         float vec[3];
1508         BPY_BM_CHECK_OBJ(self);
1509         BM_loop_face_tangent(self->bm, self->l, vec);
1510         return Vector_CreatePyObject(vec, 3, Py_NEW, NULL);
1511 }
1512
1513 /* Vert Seq
1514  * -------- */
1515 PyDoc_STRVAR(bpy_bmvertseq_new_doc,
1516 ".. method:: new(co=(0.0, 0.0, 0.0), example=None)\n"
1517 "\n"
1518 "   Create a new vertex.\n"
1519 "\n"
1520 "   :arg co: The initial location of the vertex (optional argument).\n"
1521 "   :type co: float triplet\n"
1522 "   :arg example: Existing vert to initialize settings.\n"
1523 "   :type example: :class:`BMVert`\n"
1524 "   :return: The newly created edge.\n"
1525 "   :rtype: :class:`BMVert`\n"
1526 );
1527 static PyObject *bpy_bmvertseq_new(BPy_BMElemSeq *self, PyObject *args)
1528 {
1529         PyObject *py_co = NULL;
1530         BPy_BMVert *py_vert_example = NULL; /* optional */
1531
1532         BPY_BM_CHECK_OBJ(self);
1533
1534         if (!PyArg_ParseTuple(args, "|OO!:verts.new",
1535                               &py_co,
1536                               &BPy_BMVert_Type, &py_vert_example))
1537         {
1538                 return NULL;
1539         }
1540         else {
1541                 BMesh *bm = self->bm;
1542                 BMVert *v;
1543                 float co[3] = {0.0f, 0.0f, 0.0f};
1544
1545                 if (py_vert_example) {
1546                         BPY_BM_CHECK_OBJ(py_vert_example);
1547                 }
1548
1549                 if (py_co && mathutils_array_parse(co, 3, 3, py_co, "verts.new(co)") == -1) {
1550                         return NULL;
1551                 }
1552
1553                 v = BM_vert_create(bm, co, NULL);
1554
1555                 if (v == NULL) {
1556                         PyErr_SetString(PyExc_ValueError,
1557                                         "faces.new(verts): couldn't create the new face, internal error");
1558                         return NULL;
1559                 }
1560
1561                 if (py_vert_example) {
1562                         BM_elem_attrs_copy(py_vert_example->bm, bm, py_vert_example->v, v);
1563                 }
1564
1565                 return BPy_BMVert_CreatePyObject(bm, v);
1566         }
1567 }
1568
1569
1570 /* Edge Seq
1571  * -------- */
1572 PyDoc_STRVAR(bpy_bmedgeseq_new_doc,
1573 ".. method:: new(verts, example=None)\n"
1574 "\n"
1575 "   Create a new edge from a given pair of verts.\n"
1576 "\n"
1577 "   :arg verts: Vertex pair.\n"
1578 "   :type verts: pair of :class:`BMVert`\n"
1579 "   :arg example: Existing edge to initialize settings (optional argument).\n"
1580 "   :type example: :class:`BMEdge`\n"
1581 "   :return: The newly created edge.\n"
1582 "   :rtype: :class:`BMEdge`\n"
1583 );
1584 static PyObject *bpy_bmedgeseq_new(BPy_BMElemSeq *self, PyObject *args)
1585 {
1586         PyObject *vert_seq;
1587         BPy_BMEdge *py_edge_example = NULL; /* optional */
1588
1589         BPY_BM_CHECK_OBJ(self);
1590
1591         if (!PyArg_ParseTuple(args, "O|O!:edges.new",
1592                               &vert_seq,
1593                               &BPy_BMEdge_Type, &py_edge_example))
1594         {
1595                 return NULL;
1596         }
1597         else {
1598                 BMesh *bm = self->bm;
1599                 BMEdge *e;
1600                 BMVert **vert_array = NULL;
1601                 Py_ssize_t vert_seq_len; /* always 2 */
1602                 PyObject *ret = NULL;
1603
1604                 if (py_edge_example) {
1605                         BPY_BM_CHECK_OBJ(py_edge_example);
1606                 }
1607
1608                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 2, 2,
1609                                                        &vert_seq_len, BM_VERT,
1610                                                        TRUE, TRUE, "edges.new(...)");
1611
1612                 if (vert_array == NULL) {
1613                         return NULL;
1614                 }
1615                 
1616                 if (BM_edge_exists(vert_array[0], vert_array[1])) {
1617                         PyErr_SetString(PyExc_ValueError,
1618                                         "edges.new(): this edge exists");
1619                         goto cleanup;
1620                 }
1621
1622                 e = BM_edge_create(bm, vert_array[0], vert_array[1], NULL, FALSE);
1623
1624                 if (e == NULL) {
1625                         PyErr_SetString(PyExc_ValueError,
1626                                         "faces.new(verts): couldn't create the new face, internal error");
1627                         goto cleanup;
1628                 }
1629
1630                 if (py_edge_example) {
1631                         BM_elem_attrs_copy(py_edge_example->bm, bm, py_edge_example->e, e);
1632                 }
1633
1634                 ret = BPy_BMEdge_CreatePyObject(bm, e);
1635
1636 cleanup:
1637                 if (vert_array) PyMem_FREE(vert_array);
1638                 return ret;
1639         }
1640 }
1641
1642
1643 /* Face Seq
1644  * -------- */
1645 PyDoc_STRVAR(bpy_bmfaceseq_new_doc,
1646 ".. method:: new(verts, example=None)\n"
1647 "\n"
1648 "   Create a new face from a given set of verts.\n"
1649 "\n"
1650 "   :arg verts: Sequence of 3 or more verts.\n"
1651 "   :type verts: :class:`BMVert`\n"
1652 "   :arg example: Existing face to initialize settings (optional argument).\n"
1653 "   :type example: :class:`BMFace`\n"
1654 "   :return: The newly created face.\n"
1655 "   :rtype: :class:`BMFace`\n"
1656 );
1657 static PyObject *bpy_bmfaceseq_new(BPy_BMElemSeq *self, PyObject *args)
1658 {
1659         PyObject *vert_seq;
1660         BPy_BMFace *py_face_example = NULL; /* optional */
1661
1662         BPY_BM_CHECK_OBJ(self);
1663
1664         if (!PyArg_ParseTuple(args, "O|O!:faces.new",
1665                               &vert_seq,
1666                               &BPy_BMFace_Type, &py_face_example))
1667         {
1668                 return NULL;
1669         }
1670         else {
1671                 BMesh *bm = self->bm;
1672                 Py_ssize_t vert_seq_len;
1673                 Py_ssize_t i, i_next;
1674
1675                 BMVert **vert_array = NULL;
1676                 BMEdge **edge_array = NULL;
1677
1678                 PyObject *ret = NULL;
1679
1680                 BMFace *f_new;
1681
1682                 if (py_face_example) {
1683                         BPY_BM_CHECK_OBJ(py_face_example);
1684                 }
1685
1686                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 3, PY_SSIZE_T_MAX,
1687                                                        &vert_seq_len, BM_VERT,
1688                                                        TRUE, TRUE, "faces.new(...)");
1689
1690                 if (vert_array == NULL) {
1691                         return NULL;
1692                 }
1693
1694                 /* check if the face exists */
1695                 if (BM_face_exists(bm, vert_array, vert_seq_len, NULL)) {
1696                         PyErr_SetString(PyExc_ValueError,
1697                                         "faces.new(verts): face already exists");
1698                         goto cleanup;
1699                 }
1700
1701                 /* Go ahead and make the face!
1702                  * --------------------------- */
1703
1704                 edge_array = (BMEdge **)PyMem_MALLOC(vert_seq_len * sizeof(BMEdge **));
1705
1706                 /* ensure edges */
1707                 for (i = vert_seq_len - 1, i_next = 0; i_next < vert_seq_len; (i = i_next++)) {
1708                         edge_array[i] = BM_edge_create(bm, vert_array[i], vert_array[i_next], NULL, TRUE);
1709                 }
1710
1711                 f_new = BM_face_create(bm, vert_array, edge_array, vert_seq_len, FALSE);
1712
1713                 if (f_new == NULL) {
1714                         PyErr_SetString(PyExc_ValueError,
1715                                         "faces.new(verts): couldn't create the new face, internal error");
1716                         goto cleanup;
1717                 }
1718
1719                 if (py_face_example) {
1720                         BM_elem_attrs_copy(py_face_example->bm, bm, py_face_example->f, f_new);
1721                 }
1722
1723                 ret = BPy_BMFace_CreatePyObject(bm, f_new);
1724
1725                 /* pass through */
1726 cleanup:
1727                 if (vert_array) PyMem_FREE(vert_array);
1728                 if (edge_array) PyMem_FREE(edge_array);
1729                 return ret;
1730         }
1731 }
1732
1733 /* Elem Seq
1734  * -------- */
1735
1736 PyDoc_STRVAR(bpy_bmvertseq_remove_doc,
1737 ".. method:: remove(vert)\n"
1738 "\n"
1739 "   Remove a vert.\n"
1740 );
1741 static PyObject *bpy_bmvertseq_remove(BPy_BMElemSeq *self, BPy_BMVert *value)
1742 {
1743         BPY_BM_CHECK_OBJ(self);
1744
1745         if (!BPy_BMVert_Check(value)) {
1746                 return NULL;
1747         }
1748         else {
1749                 BMesh *bm = self->bm;
1750
1751                 BPY_BM_CHECK_OBJ(value);
1752
1753                 if (value->bm != bm) {
1754                         PyErr_SetString(PyExc_ValueError,
1755                                         "verts.remove(vert): vert is from another mesh");
1756                         return NULL;
1757                 }
1758
1759                 BM_vert_kill(bm, value->v);
1760                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1761
1762                 Py_RETURN_NONE;
1763         }
1764 }
1765
1766 PyDoc_STRVAR(bpy_bmedgeseq_remove_doc,
1767 ".. method:: remove(edge)\n"
1768 "\n"
1769 "   Remove a edge.\n"
1770 );
1771 static PyObject *bpy_bmedgeseq_remove(BPy_BMElemSeq *self, BPy_BMEdge *value)
1772 {
1773         BPY_BM_CHECK_OBJ(self);
1774
1775         if (!BPy_BMEdge_Check(value)) {
1776                 return NULL;
1777         }
1778         else {
1779                 BMesh *bm = self->bm;
1780
1781                 BPY_BM_CHECK_OBJ(value);
1782
1783                 if (value->bm != bm) {
1784                         PyErr_SetString(PyExc_ValueError,
1785                                         "edges.remove(edge): edge is from another mesh");
1786                         return NULL;
1787                 }
1788
1789                 BM_edge_kill(bm, value->e);
1790                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1791
1792                 Py_RETURN_NONE;
1793         }
1794 }
1795
1796 PyDoc_STRVAR(bpy_bmfaceseq_remove_doc,
1797 ".. method:: remove(face)\n"
1798 "\n"
1799 "   Remove a face.\n"
1800 );
1801 static PyObject *bpy_bmfaceseq_remove(BPy_BMElemSeq *self, BPy_BMFace *value)
1802 {
1803         BPY_BM_CHECK_OBJ(self);
1804
1805         if (!BPy_BMFace_Check(value)) {
1806                 return NULL;
1807         }
1808         else {
1809                 BMesh *bm = self->bm;
1810
1811                 BPY_BM_CHECK_OBJ(value);
1812
1813                 if (value->bm != bm) {
1814                         PyErr_SetString(PyExc_ValueError,
1815                                         "faces.remove(face): face is from another mesh");
1816                         return NULL;
1817                 }
1818
1819                 BM_face_kill(bm, value->f);
1820                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1821
1822                 Py_RETURN_NONE;
1823         }
1824 }
1825
1826 PyDoc_STRVAR(bpy_bmedgeseq_get__method_doc,
1827 ".. method:: get(verts, fallback=None)\n"
1828 "\n"
1829 "   Return a edge which uses the **verts** passed.\n"
1830 "\n"
1831 "   :arg verts: Sequence of verts.\n"
1832 "   :type verts: :class:`BMVert`\n"
1833 "   :arg fallback: Return this value if nothing is found.\n"
1834 "   :return: The edge found or None\n"
1835 "   :rtype: :class:`BMEdge`\n"
1836 );
1837 static PyObject *bpy_bmedgeseq_get__method(BPy_BMElemSeq *self, PyObject *args)
1838 {
1839         PyObject *vert_seq;
1840         PyObject *fallback = Py_None; /* optional */
1841
1842         BPY_BM_CHECK_OBJ(self);
1843
1844         if (!PyArg_ParseTuple(args, "O|O:edges.get",
1845                               &vert_seq,
1846                               &fallback))
1847         {
1848                 return NULL;
1849         }
1850         else {
1851                 BMesh *bm = self->bm;
1852                 BMEdge *e;
1853                 BMVert **vert_array = NULL;
1854                 Py_ssize_t vert_seq_len; /* always 2 */
1855                 PyObject *ret = NULL;
1856
1857                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 2, 2,
1858                                                        &vert_seq_len, BM_VERT,
1859                                                        TRUE, TRUE, "edges.get(...)");
1860
1861                 if (vert_array == NULL) {
1862                         return NULL;
1863                 }
1864
1865                 if ((e = BM_edge_exists(vert_array[0], vert_array[1]))) {
1866                         ret = BPy_BMEdge_CreatePyObject(bm, e);
1867                 }
1868                 else {
1869                         ret = fallback;
1870                         Py_INCREF(ret);
1871                 }
1872
1873                 PyMem_FREE(vert_array);
1874                 return ret;
1875         }
1876 }
1877
1878 PyDoc_STRVAR(bpy_bmfaceseq_get__method_doc,
1879 ".. method:: get(verts, fallback=None)\n"
1880 "\n"
1881 "   Return a face which uses the **verts** passed.\n"
1882 "\n"
1883 "   :arg verts: Sequence of verts.\n"
1884 "   :type verts: :class:`BMVert`\n"
1885 "   :arg fallback: Return this value if nothing is found.\n"
1886 "   :return: The face found or None\n"
1887 "   :rtype: :class:`BMFace`\n"
1888 );
1889 static PyObject *bpy_bmfaceseq_get__method(BPy_BMElemSeq *self, PyObject *args)
1890 {
1891         PyObject *vert_seq;
1892         PyObject *fallback = Py_None; /* optional */
1893
1894         BPY_BM_CHECK_OBJ(self);
1895
1896         if (!PyArg_ParseTuple(args, "O|O:faces.get",
1897                               &vert_seq,
1898                               &fallback))
1899         {
1900                 return NULL;
1901         }
1902         else {
1903                 BMesh *bm = self->bm;
1904                 BMFace *f = NULL;
1905                 BMVert **vert_array = NULL;
1906                 Py_ssize_t vert_seq_len;
1907                 PyObject *ret = NULL;
1908
1909                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 1, PY_SSIZE_T_MAX,
1910                                                        &vert_seq_len, BM_VERT,
1911                                                        TRUE, TRUE, "faces.get(...)");
1912
1913                 if (vert_array == NULL) {
1914                         return NULL;
1915                 }
1916
1917                 if (BM_face_exists(bm, vert_array, vert_seq_len, &f)) {
1918                         ret = BPy_BMFace_CreatePyObject(bm, f);
1919                 }
1920                 else {
1921                         ret = fallback;
1922                         Py_INCREF(ret);
1923                 }
1924
1925                 PyMem_FREE(vert_array);
1926                 return ret;
1927         }
1928 }
1929
1930 PyDoc_STRVAR(bpy_bmelemseq_index_update_doc,
1931 ".. method:: index_update()\n"
1932 "\n"
1933 "   Initialize the index values of this sequence.\n"
1934 "\n"
1935 "   This is the equivalent of looping over all elements and assigning the index values.\n"
1936 "\n"
1937 "   .. code-block:: python\n"
1938 "\n"
1939 "      for index, ele in enumerate(sequence):\n"
1940 "          ele.index = index\n"
1941 "\n"
1942 "   .. note::\n"
1943 "\n"
1944 "      Running this on sequences besides :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces`\n"
1945 "      works but wont result in each element having a valid index, insted its order in the sequence will be set.\n"
1946 );
1947 static PyObject *bpy_bmelemseq_index_update(BPy_BMElemSeq *self)
1948 {
1949         BMesh *bm = self->bm;
1950
1951         BPY_BM_CHECK_OBJ(self);
1952
1953         switch ((BMIterType)self->itype) {
1954                 case BM_VERTS_OF_MESH:
1955                         BM_mesh_elem_index_ensure(self->bm, BM_VERT);
1956                         break;
1957                 case BM_EDGES_OF_MESH:
1958                         BM_mesh_elem_index_ensure(self->bm, BM_EDGE);
1959                         break;
1960                 case BM_FACES_OF_MESH:
1961                         BM_mesh_elem_index_ensure(self->bm, BM_FACE);
1962                         break;
1963                 default:
1964                 {
1965                         BMIter iter;
1966                         BMElem *ele;
1967                         int index = 0;
1968                         const char htype = bm_iter_itype_htype_map[self->itype];
1969
1970                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
1971                                 BM_elem_index_set(ele, index); /* set_dirty! */
1972                                 index++;
1973                         }
1974
1975                         if (htype & (BM_VERT | BM_EDGE | BM_FACE)) {
1976                                 /* since this isnt the normal vert/edge/face loops,
1977                                  * we're setting dirty values here. so tag as dirty. */
1978                                 bm->elem_index_dirty |= htype;
1979                         }
1980
1981                         break;
1982                 }
1983         }
1984
1985         Py_RETURN_NONE;
1986 }
1987
1988
1989 static struct PyMethodDef bpy_bmesh_methods[] = {
1990     /* utility */
1991     {"copy",  (PyCFunction)bpy_bmesh_copy,  METH_NOARGS, bpy_bmesh_copy_doc},
1992     {"clear", (PyCFunction)bpy_bmesh_clear, METH_NOARGS, bpy_bmesh_clear_doc},
1993     {"free",  (PyCFunction)bpy_bmesh_free,  METH_NOARGS, bpy_bmesh_free_doc},
1994
1995     /* conversion */
1996     {"from_object", (PyCFunction)bpy_bmesh_from_object, METH_VARARGS | METH_KEYWORDS, bpy_bmesh_from_object_doc},
1997     {"from_mesh",   (PyCFunction)bpy_bmesh_from_mesh,   METH_VARARGS | METH_KEYWORDS, bpy_bmesh_from_mesh_doc},
1998     {"to_mesh",     (PyCFunction)bpy_bmesh_to_mesh,     METH_VARARGS,                 bpy_bmesh_to_mesh_doc},
1999
2000     /* meshdata */
2001     {"select_flush_mode", (PyCFunction)bpy_bmesh_select_flush_mode, METH_NOARGS, bpy_bmesh_select_flush_mode_doc},
2002     {"select_flush", (PyCFunction)bpy_bmesh_select_flush, METH_O, bpy_bmesh_select_flush_doc},
2003     {"normal_update", (PyCFunction)bpy_bmesh_normal_update, METH_VARARGS, bpy_bmesh_normal_update_doc},
2004     {"transform", (PyCFunction)bpy_bmesh_transform, METH_VARARGS|METH_KEYWORDS, bpy_bmesh_transform_doc},
2005     {NULL, NULL, 0, NULL}
2006 };
2007
2008 static struct PyMethodDef bpy_bmvert_methods[] = {
2009     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2010     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2011     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2012     {"copy_from_face_interp", (PyCFunction)bpy_bmvert_copy_from_face_interp, METH_VARARGS, bpy_bmvert_copy_from_face_interp_doc},
2013     {"copy_from_vert_interp", (PyCFunction)bpy_bmvert_copy_from_vert_interp, METH_VARARGS, bpy_bmvert_copy_from_vert_interp_doc},
2014
2015     {"calc_vert_angle", (PyCFunction)bpy_bmvert_calc_edge_angle, METH_NOARGS, bpy_bmvert_calc_edge_angle_doc},
2016
2017     {"normal_update",  (PyCFunction)bpy_bmvert_normal_update,  METH_NOARGS,  bpy_bmvert_normal_update_doc},
2018
2019     {NULL, NULL, 0, NULL}
2020 };
2021
2022 static struct PyMethodDef bpy_bmedge_methods[] = {
2023     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2024     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2025     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2026
2027     {"other_vert", (PyCFunction)bpy_bmedge_other_vert, METH_O, bpy_bmedge_other_vert_doc},
2028
2029     {"calc_length",     (PyCFunction)bpy_bmedge_calc_length,     METH_NOARGS, bpy_bmedge_calc_length_doc},
2030     {"calc_face_angle", (PyCFunction)bpy_bmedge_calc_face_angle, METH_NOARGS, bpy_bmedge_calc_face_angle_doc},
2031
2032     {"normal_update",  (PyCFunction)bpy_bmedge_normal_update,  METH_NOARGS,  bpy_bmedge_normal_update_doc},
2033
2034     {NULL, NULL, 0, NULL}
2035 };
2036
2037 static struct PyMethodDef bpy_bmface_methods[] = {
2038     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2039     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2040
2041     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2042     {"copy_from_face_interp", (PyCFunction)bpy_bmface_copy_from_face_interp, METH_O, bpy_bmface_copy_from_face_interp_doc},
2043
2044     {"copy", (PyCFunction)bpy_bmface_copy, METH_VARARGS|METH_KEYWORDS, bpy_bmface_copy_doc},
2045
2046     {"calc_area",          (PyCFunction)bpy_bmface_calc_area,          METH_NOARGS, bpy_bmface_calc_area_doc},
2047     {"calc_center_median", (PyCFunction)bpy_bmface_calc_center_mean,   METH_NOARGS, bpy_bmface_calc_center_mean_doc},
2048     {"calc_center_bounds", (PyCFunction)bpy_bmface_calc_center_bounds, METH_NOARGS, bpy_bmface_calc_center_bounds_doc},
2049
2050     {"normal_update",  (PyCFunction)bpy_bmface_normal_update,  METH_NOARGS,  bpy_bmface_normal_update_doc},
2051
2052     {NULL, NULL, 0, NULL}
2053 };
2054
2055 static struct PyMethodDef bpy_bmloop_methods[] = {
2056     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2057     {"copy_from_face_interp", (PyCFunction)bpy_bmloop_copy_from_face_interp, METH_O, bpy_bmloop_copy_from_face_interp_doc},
2058
2059     {"calc_angle",   (PyCFunction)bpy_bmloop_calc_angle,   METH_NOARGS, bpy_bmloop_calc_angle_doc},
2060     {"calc_normal",  (PyCFunction)bpy_bmloop_calc_normal,  METH_NOARGS, bpy_bmloop_calc_normal_doc},
2061     {"calc_tangent", (PyCFunction)bpy_bmloop_calc_tangent, METH_NOARGS, bpy_bmloop_calc_tangent_doc},
2062     {NULL, NULL, 0, NULL}
2063 };
2064
2065 static struct PyMethodDef bpy_bmelemseq_methods[] = {
2066     /* odd function, initializes index values */
2067     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2068     {NULL, NULL, 0, NULL}
2069 };
2070
2071 static struct PyMethodDef bpy_bmvertseq_methods[] = {
2072     {"new",     (PyCFunction)bpy_bmvertseq_new,         METH_VARARGS, bpy_bmvertseq_new_doc},
2073     {"remove",  (PyCFunction)bpy_bmvertseq_remove,      METH_O,       bpy_bmvertseq_remove_doc},
2074
2075     /* odd function, initializes index values */
2076     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2077     {NULL, NULL, 0, NULL}
2078 };
2079
2080 static struct PyMethodDef bpy_bmedgeseq_methods[] = {
2081     {"new",     (PyCFunction)bpy_bmedgeseq_new,         METH_VARARGS, bpy_bmedgeseq_new_doc},
2082     {"remove",  (PyCFunction)bpy_bmedgeseq_remove,      METH_O,       bpy_bmedgeseq_remove_doc},
2083     /* 'bpy_bmelemseq_get' for different purpose */
2084     {"get",     (PyCFunction)bpy_bmedgeseq_get__method, METH_VARARGS, bpy_bmedgeseq_get__method_doc},
2085
2086     /* odd function, initializes index values */
2087     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2088     {NULL, NULL, 0, NULL}
2089 };
2090
2091 static struct PyMethodDef bpy_bmfaceseq_methods[] = {
2092     {"new",     (PyCFunction)bpy_bmfaceseq_new,         METH_VARARGS, bpy_bmfaceseq_new_doc},
2093     {"remove",  (PyCFunction)bpy_bmfaceseq_remove,      METH_O,       bpy_bmfaceseq_remove_doc},
2094     /* 'bpy_bmelemseq_get' for different purpose */
2095     {"get",     (PyCFunction)bpy_bmfaceseq_get__method, METH_VARARGS, bpy_bmfaceseq_get__method_doc},
2096
2097     /* odd function, initializes index values */
2098     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2099     {NULL, NULL, 0, NULL}
2100 };
2101
2102 static struct PyMethodDef bpy_bmloopseq_methods[] = {
2103     /* odd function, initializes index values */
2104     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2105     {NULL, NULL, 0, NULL}
2106 };
2107
2108 /* Sequences
2109  * ========= */
2110
2111 /* BMElemSeq / Iter
2112  * ---------------- */
2113
2114 static PyTypeObject *bpy_bm_itype_as_pytype(const char itype)
2115 {
2116         /* should cover all types */
2117         switch ((BMIterType)itype) {
2118                 case BM_VERTS_OF_MESH:
2119                 case BM_VERTS_OF_FACE:
2120                 case BM_VERTS_OF_EDGE:
2121                         return &BPy_BMVert_Type;
2122
2123                 case BM_EDGES_OF_MESH:
2124                 case BM_EDGES_OF_FACE:
2125                 case BM_EDGES_OF_VERT:
2126                         return &BPy_BMEdge_Type;
2127
2128                 case BM_FACES_OF_MESH:
2129                 case BM_FACES_OF_EDGE:
2130                 case BM_FACES_OF_VERT:
2131                         return &BPy_BMFace_Type;
2132
2133                 case BM_ALL_LOOPS_OF_FACE:
2134                 case BM_LOOPS_OF_FACE:
2135                 case BM_LOOPS_OF_EDGE:
2136                 case BM_LOOPS_OF_VERT:
2137                 case BM_LOOPS_OF_LOOP:
2138                         return &BPy_BMLoop_Type;
2139         }
2140
2141         return NULL;
2142 }
2143
2144 static Py_ssize_t bpy_bmelemseq_length(BPy_BMElemSeq *self)
2145 {
2146         BPY_BM_CHECK_INT(self);
2147
2148         /* first check if the size is known */
2149         switch ((BMIterType)self->itype) {
2150                 /* main-types */
2151                 case BM_VERTS_OF_MESH:
2152                         return self->bm->totvert;
2153                 case BM_EDGES_OF_MESH:
2154                         return self->bm->totedge;
2155                 case BM_FACES_OF_MESH:
2156                         return self->bm->totface;
2157
2158                         /* sub-types */
2159                 case BM_VERTS_OF_FACE:
2160                 case BM_EDGES_OF_FACE:
2161                 case BM_LOOPS_OF_FACE:
2162                         BPY_BM_CHECK_INT(self->py_ele);
2163                         return ((BMFace *)self->py_ele->ele)->len;
2164
2165                 case BM_VERTS_OF_EDGE:
2166                         return 2;
2167
2168                 default:
2169                         /* quiet compiler */
2170                         break;
2171         }
2172
2173
2174         /* loop over all items, avoid this if we can */
2175         {
2176                 BMIter iter;
2177                 BMHeader *ele;
2178                 Py_ssize_t tot = 0;
2179
2180                 BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
2181                         tot++;
2182                 }
2183                 return tot;
2184         }
2185 }
2186
2187 static PyObject *bpy_bmelemseq_subscript_int(BPy_BMElemSeq *self, int keynum)
2188 {
2189         BPY_BM_CHECK_OBJ(self);
2190
2191         if (keynum < 0) keynum += bpy_bmelemseq_length(self); /* only get length on negative value, may loop entire seq */
2192         if (keynum >= 0) {
2193                 BMHeader *ele = BM_iter_at_index(self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL, keynum);
2194                 if (ele) {
2195                         return BPy_BMElem_CreatePyObject(self->bm, ele);
2196                 }
2197         }
2198
2199         PyErr_Format(PyExc_IndexError,
2200                      "BMElemSeq[index]: index %d out of range", keynum);
2201         return NULL;
2202 }
2203
2204 static PyObject *bpy_bmelemseq_subscript_slice(BPy_BMElemSeq *self, Py_ssize_t start, Py_ssize_t stop)
2205 {
2206         BMIter iter;
2207         int count = 0;
2208         int ok;
2209
2210         PyObject *list;
2211         PyObject *item;
2212         BMHeader *ele;
2213
2214         BPY_BM_CHECK_OBJ(self);
2215
2216         list = PyList_New(0);
2217
2218         ok = BM_iter_init(&iter, self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
2219
2220         BLI_assert(ok == TRUE);
2221
2222         if (UNLIKELY(ok == FALSE)) {
2223                 return list;
2224         }
2225
2226         /* first loop up-until the start */
2227         for (ok = TRUE; ok; ok = (BM_iter_step(&iter) != NULL)) {
2228                 if (count == start) {
2229                         break;
2230                 }
2231                 count++;
2232         }
2233
2234         /* add items until stop */
2235         while ((ele = BM_iter_step(&iter))) {
2236                 item = BPy_BMElem_CreatePyObject(self->bm, ele);
2237                 PyList_Append(list, item);
2238                 Py_DECREF(item);
2239
2240                 count++;
2241                 if (count == stop) {
2242                         break;
2243                 }
2244         }
2245
2246         return list;
2247 }
2248
2249 static PyObject *bpy_bmelemseq_subscript(BPy_BMElemSeq *self, PyObject *key)
2250 {
2251         /* dont need error check here */
2252         if (PyIndex_Check(key)) {
2253                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2254                 if (i == -1 && PyErr_Occurred())
2255                         return NULL;
2256                 return bpy_bmelemseq_subscript_int(self, i);
2257         }
2258         else if (PySlice_Check(key)) {
2259                 PySliceObject *key_slice = (PySliceObject *)key;
2260                 Py_ssize_t step = 1;
2261
2262                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2263                         return NULL;
2264                 }
2265                 else if (step != 1) {
2266                         PyErr_SetString(PyExc_TypeError,
2267                                         "BMElemSeq[slice]: slice steps not supported");
2268                         return NULL;
2269                 }
2270                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2271                         return bpy_bmelemseq_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2272                 }
2273                 else {
2274                         Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2275
2276                         /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2277                         if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2278                         if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop))    return NULL;
2279
2280                         if (start < 0 || stop < 0) {
2281                                 /* only get the length for negative values */
2282                                 Py_ssize_t len = bpy_bmelemseq_length(self);
2283                                 if (start < 0) start += len;
2284                                 if (stop < 0) start += len;
2285                         }
2286
2287                         if (stop - start <= 0) {
2288                                 return PyList_New(0);
2289                         }
2290                         else {
2291                                 return bpy_bmelemseq_subscript_slice(self, start, stop);
2292                         }
2293                 }
2294         }
2295         else {
2296                 PyErr_SetString(PyExc_AttributeError,
2297                                 "BMElemSeq[key]: invalid key, key must be an int");
2298                 return NULL;
2299         }
2300 }
2301
2302 static int bpy_bmelemseq_contains(BPy_BMElemSeq *self, PyObject *value)
2303 {
2304         BPY_BM_CHECK_INT(self);
2305
2306         if (Py_TYPE(value) == bpy_bm_itype_as_pytype(self->itype)) {
2307                 BPy_BMElem *value_bm_ele = (BPy_BMElem *)value;
2308                 if (value_bm_ele->bm == self->bm) {
2309                         BMElem *ele, *ele_test = value_bm_ele->ele;
2310                         BMIter iter;
2311                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
2312                                 if (ele == ele_test) {
2313                                         return 1;
2314                                 }
2315                         }
2316                 }
2317         }
2318
2319         return 0;
2320 }
2321
2322 /* BMElem (customdata)
2323  * ------------------- */
2324
2325 static PyObject *bpy_bmelem_subscript(BPy_BMElem *self, BPy_BMLayerItem *key)
2326 {
2327         BPY_BM_CHECK_OBJ(self);
2328
2329         return BPy_BMLayerItem_GetItem(self, key);
2330 }
2331
2332 static int bpy_bmelem_ass_subscript(BPy_BMElem *self, BPy_BMLayerItem *key, PyObject *value)
2333 {
2334         BPY_BM_CHECK_INT(self);
2335
2336         return BPy_BMLayerItem_SetItem(self, key, value);
2337 }
2338
2339 static PySequenceMethods bpy_bmelemseq_as_sequence = {
2340     (lenfunc)bpy_bmelemseq_length,                  /* sq_length */
2341     NULL,                                        /* sq_concat */
2342     NULL,                                        /* sq_repeat */
2343     (ssizeargfunc)bpy_bmelemseq_subscript_int,      /* sq_item */ /* Only set this so PySequence_Check() returns True */
2344     NULL,                                        /* sq_slice */
2345     (ssizeobjargproc)NULL,                       /* sq_ass_item */
2346     NULL,                                        /* *was* sq_ass_slice */
2347     (objobjproc)bpy_bmelemseq_contains,             /* sq_contains */
2348     (binaryfunc) NULL,                           /* sq_inplace_concat */
2349     (ssizeargfunc) NULL,                         /* sq_inplace_repeat */
2350 };
2351
2352 static PyMappingMethods bpy_bmelemseq_as_mapping = {
2353     (lenfunc)bpy_bmelemseq_length,                  /* mp_length */
2354     (binaryfunc)bpy_bmelemseq_subscript,            /* mp_subscript */
2355     (objobjargproc)NULL,                         /* mp_ass_subscript */
2356 };
2357
2358 /* for customdata access */
2359 static PyMappingMethods bpy_bm_elem_as_mapping = {
2360     (lenfunc)NULL,                           /* mp_length */ /* keep this empty, messes up 'if elem: ...' test */
2361     (binaryfunc)bpy_bmelem_subscript,        /* mp_subscript */
2362     (objobjargproc)bpy_bmelem_ass_subscript, /* mp_ass_subscript */
2363 };
2364
2365 /* Iterator
2366  * -------- */
2367
2368 static PyObject *bpy_bmelemseq_iter(BPy_BMElemSeq *self)
2369 {
2370         BPy_BMIter *py_iter;
2371
2372         BPY_BM_CHECK_OBJ(self);
2373         py_iter = (BPy_BMIter *)BPy_BMIter_CreatePyObject(self->bm);
2374         BM_iter_init(&(py_iter->iter), self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
2375         return (PyObject *)py_iter;
2376 }
2377
2378 static PyObject *bpy_bmiter_next(BPy_BMIter *self)
2379 {
2380         BMHeader *ele = BM_iter_step(&self->iter);
2381         if (ele == NULL) {
2382                 PyErr_SetString(PyExc_StopIteration,
2383                                 "bpy_bmiter_next stop");
2384                 return NULL;
2385         }
2386         else {
2387                 return (PyObject *)BPy_BMElem_CreatePyObject(self->bm, ele);
2388         }
2389 }
2390
2391
2392 /* Dealloc Functions
2393  * ================= */
2394
2395 static void bpy_bmesh_dealloc(BPy_BMesh *self)
2396 {
2397         BMesh *bm = self->bm;
2398
2399         /* have have been freed by bmesh */
2400         if (bm) {
2401                 BM_data_layer_free(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2402                 BM_data_layer_free(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2403                 BM_data_layer_free(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2404                 BM_data_layer_free(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2405
2406                 bm->py_handle = NULL;
2407
2408                 if ((self->flag & BPY_BMFLAG_IS_WRAPPED) == 0) {
2409                         BM_mesh_free(bm);
2410                 }
2411         }
2412
2413         PyObject_DEL(self);
2414 }
2415
2416 static void bpy_bmvert_dealloc(BPy_BMElem *self)
2417 {
2418         BMesh *bm = self->bm;
2419         if (bm) {
2420                 void **ptr = CustomData_bmesh_get(&bm->vdata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2421                 *ptr = NULL;
2422         }
2423         PyObject_DEL(self);
2424 }
2425
2426 static void bpy_bmedge_dealloc(BPy_BMElem *self)
2427 {
2428         BMesh *bm = self->bm;
2429         if (bm) {
2430                 void **ptr = CustomData_bmesh_get(&bm->edata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2431                 *ptr = NULL;
2432         }
2433         PyObject_DEL(self);
2434 }
2435
2436 static void bpy_bmface_dealloc(BPy_BMElem *self)
2437 {
2438         BMesh *bm = self->bm;
2439         if (bm) {
2440                 void **ptr = CustomData_bmesh_get(&bm->pdata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2441                 *ptr = NULL;
2442         }
2443         PyObject_DEL(self);
2444 }
2445
2446 static void bpy_bmloop_dealloc(BPy_BMElem *self)
2447 {
2448         BMesh *bm = self->bm;
2449         if (bm) {
2450                 void **ptr = CustomData_bmesh_get(&bm->ldata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2451                 *ptr = NULL;
2452         }
2453         PyObject_DEL(self);
2454 }
2455
2456 static void bpy_bmelemseq_dealloc(BPy_BMElemSeq *self)
2457 {
2458         Py_XDECREF(self->py_ele);
2459
2460         PyObject_DEL(self);
2461 }
2462
2463 /* not sure where this should go */
2464 static Py_hash_t bpy_bm_elem_hash(PyObject *self)
2465 {
2466         return _Py_HashPointer(((BPy_BMElem *)self)->ele);
2467 }
2468
2469 static Py_hash_t bpy_bm_hash(PyObject *self)
2470 {
2471         return _Py_HashPointer(((BPy_BMesh *)self)->bm);
2472 }
2473
2474 /* Type Docstrings
2475  * =============== */
2476
2477 PyDoc_STRVAR(bpy_bmesh_doc,
2478 "The BMesh data structure\n"
2479 );
2480 PyDoc_STRVAR(bpy_bmvert_doc,
2481 "The BMesh vertex type\n"
2482 );
2483 PyDoc_STRVAR(bpy_bmedge_doc,
2484 "The BMesh edge connecting 2 verts\n"
2485 );
2486 PyDoc_STRVAR(bpy_bmface_doc,
2487 "The BMesh face with 3 or more sides\n"
2488 );
2489 PyDoc_STRVAR(bpy_bmloop_doc,
2490 "This is normally accessed from :class:`BMFace.loops` where each face corner represents a corner of a face.\n"
2491 );
2492 PyDoc_STRVAR(bpy_bmelemseq_doc,
2493 "General sequence type used for accessing any sequence of \n"
2494 ":class:`BMVert`, :class:`BMEdge`, :class:`BMFace`, :class:`BMLoop`.\n"
2495 "\n"
2496 "When accessed via :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces` \n"
2497 "there are also functions to create/remomove items.\n"
2498 );
2499 PyDoc_STRVAR(bpy_bmiter_doc,
2500 "Internal BMesh type for looping over verts/faces/edges,\n"
2501 "used for iterating over :class:`BMElemSeq` types.\n"
2502 );
2503
2504 static PyObject *bpy_bmesh_repr(BPy_BMesh *self)
2505 {
2506         BMesh *bm = self->bm;
2507
2508         if (bm) {
2509                 return PyUnicode_FromFormat("<BMesh(%p), totvert=%d, totedge=%d, totface=%d, totloop=%d>",
2510                                             bm, bm->totvert, bm->totedge, bm->totface, bm->totloop);
2511         }
2512         else {
2513                 return PyUnicode_FromFormat("<BMesh dead at %p>", self);
2514         }
2515 }
2516
2517 static PyObject *bpy_bmvert_repr(BPy_BMVert *self)
2518 {
2519         BMesh *bm = self->bm;
2520
2521         if (bm) {
2522                 return PyUnicode_FromFormat("<BMVert(%p), index=%d>",
2523                                             self->v, BM_elem_index_get(self->v));
2524         }
2525         else {
2526                 return PyUnicode_FromFormat("<BMVert dead at %p>", self);
2527         }
2528 }
2529
2530 static PyObject *bpy_bmedge_repr(BPy_BMEdge *self)
2531 {
2532         BMesh *bm = self->bm;
2533
2534         if (bm) {
2535                 return PyUnicode_FromFormat("<BMEdge(%p), index=%d, verts=(%p/%d, %p/%d)>",
2536                                             self->e, BM_elem_index_get(self->e),
2537                                             self->e->v1, BM_elem_index_get(self->e->v1),
2538                                             self->e->v2, BM_elem_index_get(self->e->v2));
2539         }
2540         else {
2541                 return PyUnicode_FromFormat("<BMEdge dead at %p>", self);
2542         }
2543 }
2544
2545 static PyObject *bpy_bmface_repr(BPy_BMFace *self)
2546 {
2547         BMesh *bm = self->bm;
2548
2549         if (bm) {
2550                 return PyUnicode_FromFormat("<BMFace(%p), index=%d, totverts=%d>",
2551                                             self->f, BM_elem_index_get(self->f),
2552                                             self->f->len);
2553         }
2554         else {
2555                 return PyUnicode_FromFormat("<BMFace dead at %p>", self);
2556         }
2557 }
2558
2559 static PyObject *bpy_bmloop_repr(BPy_BMLoop *self)
2560 {
2561         BMesh *bm = self->bm;
2562
2563         if (bm) {
2564                 return PyUnicode_FromFormat("<BMLoop(%p), index=%d, vert=%p/%d, edge=%p/%d, face=%p/%d>",
2565                                             self->l, BM_elem_index_get(self->l),
2566                                             self->l->v, BM_elem_index_get(self->l->v),
2567                                             self->l->e, BM_elem_index_get(self->l->e),
2568                                             self->l->f, BM_elem_index_get(self->l->f));
2569         }
2570         else {
2571                 return PyUnicode_FromFormat("<BMLoop dead at %p>", self);
2572         }
2573 }
2574
2575 /* Types
2576  * ===== */
2577
2578 PyTypeObject BPy_BMesh_Type     = {{{0}}};
2579 PyTypeObject BPy_BMVert_Type    = {{{0}}};
2580 PyTypeObject BPy_BMEdge_Type    = {{{0}}};
2581 PyTypeObject BPy_BMFace_Type    = {{{0}}};
2582 PyTypeObject BPy_BMLoop_Type    = {{{0}}};
2583 PyTypeObject BPy_BMElemSeq_Type = {{{0}}};
2584 PyTypeObject BPy_BMVertSeq_Type = {{{0}}};
2585 PyTypeObject BPy_BMEdgeSeq_Type = {{{0}}};
2586 PyTypeObject BPy_BMFaceSeq_Type = {{{0}}};
2587 PyTypeObject BPy_BMLoopSeq_Type = {{{0}}};
2588 PyTypeObject BPy_BMIter_Type    = {{{0}}};
2589
2590
2591
2592 void BPy_BM_init_types(void)
2593 {
2594         BPy_BMesh_Type.tp_basicsize     = sizeof(BPy_BMesh);
2595         BPy_BMVert_Type.tp_basicsize    = sizeof(BPy_BMVert);
2596         BPy_BMEdge_Type.tp_basicsize    = sizeof(BPy_BMEdge);
2597         BPy_BMFace_Type.tp_basicsize    = sizeof(BPy_BMFace);
2598         BPy_BMLoop_Type.tp_basicsize    = sizeof(BPy_BMLoop);
2599         BPy_BMElemSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2600         BPy_BMVertSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2601         BPy_BMEdgeSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2602         BPy_BMFaceSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2603         BPy_BMLoopSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2604         BPy_BMIter_Type.tp_basicsize    = sizeof(BPy_BMIter);
2605
2606
2607         BPy_BMesh_Type.tp_name     = "BMesh";
2608         BPy_BMVert_Type.tp_name    = "BMVert";
2609         BPy_BMEdge_Type.tp_name    = "BMEdge";
2610         BPy_BMFace_Type.tp_name    = "BMFace";
2611         BPy_BMLoop_Type.tp_name    = "BMLoop";
2612         BPy_BMElemSeq_Type.tp_name = "BMElemSeq";
2613         BPy_BMVertSeq_Type.tp_name = "BMVertSeq";
2614         BPy_BMEdgeSeq_Type.tp_name = "BMEdgeSeq";
2615         BPy_BMFaceSeq_Type.tp_name = "BMFaceSeq";
2616         BPy_BMLoopSeq_Type.tp_name = "BMLoopSeq";
2617         BPy_BMIter_Type.tp_name    = "BMIter";
2618
2619
2620         BPy_BMesh_Type.tp_doc     = bpy_bmesh_doc;
2621         BPy_BMVert_Type.tp_doc    = bpy_bmvert_doc;
2622         BPy_BMEdge_Type.tp_doc    = bpy_bmedge_doc;
2623         BPy_BMFace_Type.tp_doc    = bpy_bmface_doc;
2624         BPy_BMLoop_Type.tp_doc    = bpy_bmloop_doc;
2625         BPy_BMElemSeq_Type.tp_doc = bpy_bmelemseq_doc;
2626         BPy_BMVertSeq_Type.tp_doc = NULL;
2627         BPy_BMEdgeSeq_Type.tp_doc = NULL;
2628         BPy_BMFaceSeq_Type.tp_doc = NULL;
2629         BPy_BMLoopSeq_Type.tp_doc = NULL;
2630         BPy_BMIter_Type.tp_doc    = bpy_bmiter_doc;
2631
2632
2633         BPy_BMesh_Type.tp_repr     = (reprfunc)bpy_bmesh_repr;
2634         BPy_BMVert_Type.tp_repr    = (reprfunc)bpy_bmvert_repr;
2635         BPy_BMEdge_Type.tp_repr    = (reprfunc)bpy_bmedge_repr;
2636         BPy_BMFace_Type.tp_repr    = (reprfunc)bpy_bmface_repr;
2637         BPy_BMLoop_Type.tp_repr    = (reprfunc)bpy_bmloop_repr;
2638         BPy_BMElemSeq_Type.tp_repr = NULL;
2639         BPy_BMVertSeq_Type.tp_repr = NULL;
2640         BPy_BMEdgeSeq_Type.tp_repr = NULL;
2641         BPy_BMFaceSeq_Type.tp_repr = NULL;
2642         BPy_BMLoopSeq_Type.tp_repr = NULL;
2643         BPy_BMIter_Type.tp_repr    = NULL;
2644
2645
2646         BPy_BMesh_Type.tp_getset     = bpy_bmesh_getseters;
2647         BPy_BMVert_Type.tp_getset    = bpy_bmvert_getseters;
2648         BPy_BMEdge_Type.tp_getset    = bpy_bmedge_getseters;
2649         BPy_BMFace_Type.tp_getset    = bpy_bmface_getseters;
2650         BPy_BMLoop_Type.tp_getset    = bpy_bmloop_getseters;
2651         BPy_BMElemSeq_Type.tp_getset = NULL;
2652         BPy_BMVertSeq_Type.tp_getset = bpy_bmvertseq_getseters;
2653         BPy_BMEdgeSeq_Type.tp_getset = bpy_bmedgeseq_getseters;
2654         BPy_BMFaceSeq_Type.tp_getset = bpy_bmfaceseq_getseters;
2655         BPy_BMLoopSeq_Type.tp_getset = bpy_bmloopseq_getseters;
2656         BPy_BMIter_Type.tp_getset    = NULL;
2657
2658
2659         BPy_BMesh_Type.tp_methods     = bpy_bmesh_methods;
2660         BPy_BMVert_Type.tp_methods    = bpy_bmvert_methods;
2661         BPy_BMEdge_Type.tp_methods    = bpy_bmedge_methods;
2662         BPy_BMFace_Type.tp_methods    = bpy_bmface_methods;
2663         BPy_BMLoop_Type.tp_methods    = bpy_bmloop_methods;
2664         BPy_BMElemSeq_Type.tp_methods = bpy_bmelemseq_methods;
2665         BPy_BMVertSeq_Type.tp_methods = bpy_bmvertseq_methods;
2666         BPy_BMEdgeSeq_Type.tp_methods = bpy_bmedgeseq_methods;
2667         BPy_BMFaceSeq_Type.tp_methods = bpy_bmfaceseq_methods;
2668         BPy_BMLoopSeq_Type.tp_methods = bpy_bmloopseq_methods;
2669         BPy_BMIter_Type.tp_methods    = NULL;
2670
2671
2672         BPy_BMesh_Type.tp_hash     = bpy_bm_hash;
2673         BPy_BMVert_Type.tp_hash    = bpy_bm_elem_hash;
2674         BPy_BMEdge_Type.tp_hash    = bpy_bm_elem_hash;
2675         BPy_BMFace_Type.tp_hash    = bpy_bm_elem_hash;
2676         BPy_BMLoop_Type.tp_hash    = bpy_bm_elem_hash;
2677         BPy_BMElemSeq_Type.tp_hash = NULL;
2678         BPy_BMVertSeq_Type.tp_hash = NULL;
2679         BPy_BMEdgeSeq_Type.tp_hash = NULL;
2680         BPy_BMFaceSeq_Type.tp_hash = NULL;
2681         BPy_BMLoopSeq_Type.tp_hash = NULL;
2682         BPy_BMIter_Type.tp_hash    = NULL;
2683
2684         BPy_BMElemSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2685         BPy_BMVertSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2686         BPy_BMEdgeSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2687         BPy_BMFaceSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2688         BPy_BMLoopSeq_Type.tp_as_sequence = NULL; /* this is not a seq really, only for layer access */
2689
2690         BPy_BMElemSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2691         BPy_BMVertSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2692         BPy_BMEdgeSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2693         BPy_BMFaceSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2694         BPy_BMLoopSeq_Type.tp_as_mapping = NULL; /* this is not a seq really, only for layer access */
2695
2696         /* layer access */
2697         BPy_BMVert_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2698         BPy_BMEdge_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2699         BPy_BMFace_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2700         BPy_BMLoop_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2701
2702         BPy_BMElemSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2703         BPy_BMVertSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2704         BPy_BMEdgeSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2705         BPy_BMFaceSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2706         BPy_BMLoopSeq_Type.tp_iter = NULL; /* no mapping */
2707
2708         /* only 1 iteratir so far */
2709         BPy_BMIter_Type.tp_iternext = (iternextfunc)bpy_bmiter_next;
2710         BPy_BMIter_Type.tp_iter     = PyObject_SelfIter;
2711
2712         BPy_BMesh_Type.tp_dealloc     = (destructor)bpy_bmesh_dealloc;
2713         BPy_BMVert_Type.tp_dealloc    = (destructor)bpy_bmvert_dealloc;
2714         BPy_BMEdge_Type.tp_dealloc    = (destructor)bpy_bmedge_dealloc;
2715         BPy_BMFace_Type.tp_dealloc    = (destructor)bpy_bmface_dealloc;
2716         BPy_BMLoop_Type.tp_dealloc    = (destructor)bpy_bmloop_dealloc;
2717         BPy_BMElemSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2718         BPy_BMVertSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2719         BPy_BMEdgeSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2720         BPy_BMFaceSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2721         BPy_BMLoopSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2722         BPy_BMIter_Type.tp_dealloc    = NULL;
2723
2724         BPy_BMesh_Type.tp_flags     = Py_TPFLAGS_DEFAULT;
2725         BPy_BMVert_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2726         BPy_BMEdge_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2727         BPy_BMFace_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2728         BPy_BMLoop_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2729         BPy_BMElemSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2730         BPy_BMVertSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2731         BPy_BMEdgeSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2732         BPy_BMFaceSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2733         BPy_BMLoopSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2734         BPy_BMIter_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2735
2736
2737         PyType_Ready(&BPy_BMesh_Type);
2738         PyType_Ready(&BPy_BMVert_Type);
2739         PyType_Ready(&BPy_BMEdge_Type);
2740         PyType_Ready(&BPy_BMFace_Type);
2741         PyType_Ready(&BPy_BMLoop_Type);
2742         PyType_Ready(&BPy_BMElemSeq_Type);
2743         PyType_Ready(&BPy_BMVertSeq_Type);
2744         PyType_Ready(&BPy_BMEdgeSeq_Type);
2745         PyType_Ready(&BPy_BMFaceSeq_Type);
2746         PyType_Ready(&BPy_BMLoopSeq_Type);
2747         PyType_Ready(&BPy_BMIter_Type);
2748 }
2749
2750 /* bmesh.types submodule
2751  * ********************* */
2752
2753 static struct PyModuleDef BPy_BM_types_module_def = {
2754     PyModuleDef_HEAD_INIT,
2755     "bmesh.types",  /* m_name */
2756     NULL,  /* m_doc */
2757     0,  /* m_size */
2758     NULL,  /* m_methods */
2759     NULL,  /* m_reload */
2760     NULL,  /* m_traverse */
2761     NULL,  /* m_clear */
2762     NULL,  /* m_free */
2763 };
2764
2765 PyObject *BPyInit_bmesh_types(void)
2766 {
2767         PyObject *submodule;
2768
2769         submodule = PyModule_Create(&BPy_BM_types_module_def);
2770
2771 #define mod_type_add(s, t) \
2772         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
2773
2774         /* bmesh_py_types.c */
2775         mod_type_add(submodule, BPy_BMesh_Type);
2776         mod_type_add(submodule, BPy_BMVert_Type);
2777         mod_type_add(submodule, BPy_BMEdge_Type);
2778         mod_type_add(submodule, BPy_BMFace_Type);
2779         mod_type_add(submodule, BPy_BMLoop_Type);
2780         mod_type_add(submodule, BPy_BMElemSeq_Type);
2781         mod_type_add(submodule, BPy_BMVertSeq_Type);
2782         mod_type_add(submodule, BPy_BMEdgeSeq_Type);
2783         mod_type_add(submodule, BPy_BMFaceSeq_Type);
2784         mod_type_add(submodule, BPy_BMLoopSeq_Type);
2785         mod_type_add(submodule, BPy_BMIter_Type);
2786         /* bmesh_py_types_select.c */
2787         mod_type_add(submodule, BPy_BMEditSelSeq_Type);
2788         mod_type_add(submodule, BPy_BMEditSelIter_Type);
2789         /* bmesh_py_types_customdata.c */
2790         mod_type_add(submodule, BPy_BMLayerAccess_Type);
2791         mod_type_add(submodule, BPy_BMLayerCollection_Type);
2792         mod_type_add(submodule, BPy_BMLayerItem_Type);
2793         /* bmesh_py_types_meshdata.c */
2794         mod_type_add(submodule, BPy_BMLoopUV_Type);
2795
2796 #undef mod_type_add
2797
2798         return submodule;
2799 }
2800
2801 /* Utility Functions
2802  * ***************** */
2803
2804 PyObject *BPy_BMesh_CreatePyObject(BMesh *bm, int flag)
2805 {
2806         BPy_BMesh *self;
2807
2808         if (bm->py_handle) {
2809                 self = bm->py_handle;
2810                 Py_INCREF(self);
2811         }
2812         else {
2813                 self = PyObject_New(BPy_BMesh, &BPy_BMesh_Type);
2814                 self->bm = bm;
2815                 self->flag = flag;
2816
2817                 bm->py_handle = self; /* point back */
2818
2819                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2820                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2821                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2822                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2823         }
2824
2825         return (PyObject *)self;
2826 }
2827
2828
2829
2830 PyObject *BPy_BMVert_CreatePyObject(BMesh *bm, BMVert *v)
2831 {
2832         BPy_BMVert *self;
2833
2834         void **ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
2835
2836         /* bmesh may free layers, ensure we have one to store ourself */
2837         if (UNLIKELY(ptr == NULL)) {
2838                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2839                 ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
2840         }
2841
2842         if (*ptr != NULL) {
2843                 self = *ptr;
2844                 Py_INCREF(self);
2845         }
2846         else {
2847                 self = PyObject_New(BPy_BMVert, &BPy_BMVert_Type);
2848                 BLI_assert(v != NULL);
2849                 self->bm = bm;
2850                 self->v  = v;
2851                 *ptr = self;
2852         }
2853         return (PyObject *)self;
2854 }
2855
2856 PyObject *BPy_BMEdge_CreatePyObject(BMesh *bm, BMEdge *e)
2857 {
2858         BPy_BMEdge *self;
2859
2860         void **ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
2861
2862         /* bmesh may free layers, ensure we have one to store ourself */
2863         if (UNLIKELY(ptr == NULL)) {
2864                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2865                 ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
2866         }
2867
2868         if (*ptr != NULL) {
2869                 self = *ptr;
2870                 Py_INCREF(self);
2871         }
2872         else {
2873                 self = PyObject_New(BPy_BMEdge, &BPy_BMEdge_Type);
2874                 BLI_assert(e != NULL);
2875                 self->bm = bm;
2876                 self->e  = e;
2877                 *ptr = self;
2878         }
2879         return (PyObject *)self;
2880 }
2881
2882 PyObject *BPy_BMFace_CreatePyObject(BMesh *bm, BMFace *f)
2883 {
2884         BPy_BMFace *self;
2885
2886         void **ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
2887
2888         /* bmesh may free layers, ensure we have one to store ourself */
2889         if (UNLIKELY(ptr == NULL)) {
2890                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2891                 ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
2892         }
2893
2894         if (*ptr != NULL) {
2895                 self = *ptr;
2896                 Py_INCREF(self);
2897         }
2898         else {
2899                 self = PyObject_New(BPy_BMFace, &BPy_BMFace_Type);
2900                 BLI_assert(f != NULL);
2901                 self->bm = bm;
2902                 self->f  = f;
2903                 *ptr = self;
2904         }
2905         return (PyObject *)self;
2906 }
2907
2908 PyObject *BPy_BMLoop_CreatePyObject(BMesh *bm, BMLoop *l)
2909 {
2910         BPy_BMLoop *self;
2911
2912         void **ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
2913
2914         /* bmesh may free layers, ensure we have one to store ourself */
2915         if (UNLIKELY(ptr == NULL)) {
2916                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2917                 ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
2918         }
2919
2920         if (*ptr != NULL) {
2921                 self = *ptr;
2922                 Py_INCREF(self);
2923         }
2924         else {
2925                 self = PyObject_New(BPy_BMLoop, &BPy_BMLoop_Type);
2926                 BLI_assert(l != NULL);
2927                 self->bm = bm;
2928                 self->l  = l;
2929                 *ptr = self;
2930         }
2931         return (PyObject *)self;
2932 }
2933
2934 PyObject *BPy_BMElemSeq_CreatePyObject(BMesh *bm, BPy_BMElem *py_ele, const char itype)
2935 {
2936         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMElemSeq_Type);
2937         self->bm = bm;
2938         self->py_ele = py_ele; /* can be NULL */
2939         self->itype = itype;
2940         Py_XINCREF(py_ele);
2941         return (PyObject *)self;
2942 }
2943
2944 PyObject *BPy_BMVertSeq_CreatePyObject(BMesh *bm)
2945 {
2946         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMVertSeq_Type);
2947         self->bm = bm;
2948         self->py_ele = NULL; /* unused */
2949         self->itype = BM_VERTS_OF_MESH;
2950         return (PyObject *)self;
2951 }
2952
2953 PyObject *BPy_BMEdgeSeq_CreatePyObject(BMesh *bm)
2954 {
2955         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMEdgeSeq_Type);
2956         self->bm = bm;
2957         self->py_ele = NULL; /* unused */
2958         self->itype = BM_EDGES_OF_MESH;
2959         return (PyObject *)self;
2960 }
2961
2962 PyObject *BPy_BMFaceSeq_CreatePyObject(BMesh *bm)
2963 {
2964         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMFaceSeq_Type);
2965         self->bm = bm;
2966         self->py_ele = NULL; /* unused */
2967         self->itype = BM_FACES_OF_MESH;
2968         return (PyObject *)self;
2969 }
2970
2971 PyObject *BPy_BMLoopSeq_CreatePyObject(BMesh *bm)
2972 {
2973         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMLoopSeq_Type);
2974         self->bm = bm;
2975         self->py_ele = NULL; /* unused */
2976         self->itype = 0; /* should never be passed to the iterator function */
2977         return (PyObject *)self;
2978 }
2979
2980 PyObject *BPy_BMIter_CreatePyObject(BMesh *bm)
2981 {
2982         BPy_BMIter *self = PyObject_New(BPy_BMIter, &BPy_BMIter_Type);
2983         self->bm = bm;
2984         /* caller must initialize 'iter' member */
2985         return (PyObject *)self;
2986 }
2987
2988 /* this is just a helper func */
2989 PyObject *BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele)
2990 {
2991         switch (ele->htype) {
2992                 case BM_VERT:
2993                         return BPy_BMVert_CreatePyObject(bm, (BMVert *)ele);
2994                 case BM_EDGE:
2995                         return BPy_BMEdge_CreatePyObject(bm, (BMEdge *)ele);
2996                 case BM_FACE:
2997                         return BPy_BMFace_CreatePyObject(bm, (BMFace *)ele);
2998                 case BM_LOOP:
2999                         return BPy_BMLoop_CreatePyObject(bm, (BMLoop *)ele);
3000                 default:
3001                         PyErr_SetString(PyExc_SystemError, "internal error");
3002                         return NULL;
3003         }
3004 }
3005
3006 int bpy_bm_generic_valid_check(BPy_BMGeneric *self)
3007 {
3008         if (LIKELY(self->bm)) {
3009                 return 0;
3010         }
3011         else {
3012                 PyErr_Format(PyExc_ReferenceError,
3013                                          "BMesh data of type %.200s has been removed",
3014                                          Py_TYPE(self)->tp_name);
3015                 return -1;
3016         }
3017 }
3018
3019 void bpy_bm_generic_invalidate(BPy_BMGeneric *self)
3020 {
3021         self->bm = NULL;
3022 }
3023
3024 /* generic python seq as BMVert/Edge/Face array,
3025  * return value must be freed with PyMem_FREE(...);
3026  *
3027  * The 'bm_r' value is assigned when empty, and used when set.
3028  */
3029 void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_size,
3030                                 const char htype,
3031                                 const char do_unique_check, const char do_bm_check,
3032                                 const char *error_prefix)
3033 {
3034         BMesh *bm = (r_bm && *r_bm) ? *r_bm : NULL;
3035         PyObject *seq_fast;
3036         *r_size = 0;
3037
3038         if (!(seq_fast = PySequence_Fast(seq, error_prefix))) {
3039                 return NULL;
3040         }
3041         else {
3042                 Py_ssize_t seq_len;
3043                 Py_ssize_t i;
3044
3045                 BPy_BMElem *item;
3046                 BMElem **alloc;
3047
3048                 seq_len = PySequence_Fast_GET_SIZE(seq_fast);
3049
3050                 if (seq_len < min || seq_len > max) {
3051                         PyErr_Format(PyExc_TypeError,
3052                                      "%s: sequence incorrect size, expected [%d - %d], given %d",
3053                                      error_prefix, min, max, seq_len);
3054                         return NULL;
3055                 }
3056
3057
3058                 /* from now on, use goto */
3059                 alloc = PyMem_MALLOC(seq_len * sizeof(BPy_BMElem **));
3060
3061                 for (i = 0; i < seq_len; i++) {
3062                         item = (BPy_BMElem *)PySequence_Fast_GET_ITEM(seq_fast, i);
3063
3064                         if (!BPy_BMElem_CheckHType(Py_TYPE(item), htype)) {
3065                                 PyErr_Format(PyExc_TypeError,
3066                                              "%s: expected %.200s, not '%.200s'",
3067                                              error_prefix, BPy_BMElem_StringFromHType(htype), Py_TYPE(item)->tp_name);
3068                                 goto err_cleanup;
3069                         }
3070                         else if (!BPY_BM_IS_VALID(item)) {
3071                                 PyErr_Format(PyExc_TypeError,
3072                                              "%s: %d %s has been removed",
3073                                              error_prefix, i, Py_TYPE(item)->tp_name);
3074                                 goto err_cleanup;
3075                         }
3076                         /* trick so we can ensure all items have the same mesh,
3077                          * and allows us to pass the 'bm' as NULL. */
3078                         else if (do_bm_check && (bm  && bm != item->bm)) {
3079                                 PyErr_Format(PyExc_ValueError,
3080                                              "%s: %d %s is from another mesh",
3081                                              error_prefix, i, BPy_BMElem_StringFromHType(htype));
3082                                 goto err_cleanup;
3083                         }
3084
3085                         if (bm == NULL) {
3086                                 bm = item->bm;
3087                         }
3088
3089                         alloc[i] = item->ele;
3090
3091                         if (do_unique_check) {
3092                                 BM_elem_flag_enable(item->ele, BM_ELEM_INTERNAL_TAG);
3093                         }
3094                 }
3095
3096                 if (do_unique_check) {
3097                         /* check for double verts! */
3098                         int ok = TRUE;
3099                         for (i = 0; i < seq_len; i++) {
3100                                 if (UNLIKELY(BM_elem_flag_test(alloc[i], BM_ELEM_INTERNAL_TAG) == FALSE)) {
3101                                         ok = FALSE;
3102                                 }
3103
3104                                 /* ensure we dont leave this enabled */
3105                                 BM_elem_flag_disable(alloc[i], BM_ELEM_INTERNAL_TAG);
3106                         }
3107
3108                         if (ok == FALSE) {
3109                                 PyErr_Format(PyExc_ValueError,
3110                                              "%s: found the same %.200s used multiple times",
3111                                              error_prefix, BPy_BMElem_StringFromHType(htype));
3112                                 goto err_cleanup;
3113                         }
3114                 }
3115
3116                 Py_DECREF(seq_fast);
3117                 *r_size = seq_len;
3118                 if (r_bm) *r_bm = bm;
3119                 return alloc;
3120
3121 err_cleanup:
3122                 Py_DECREF(seq_fast);
3123                 PyMem_FREE(alloc);
3124                 return NULL;
3125         }
3126 }
3127
3128
3129 PyObject *BPy_BMElem_Array_As_Tuple(BMesh *bm, BMHeader **elem, Py_ssize_t elem_len)
3130 {
3131         Py_ssize_t i;
3132         PyObject *ret = PyTuple_New(elem_len);
3133         for (i = 0; i < elem_len; i++) {
3134                 PyTuple_SET_ITEM(ret, i, BPy_BMElem_CreatePyObject(bm, elem[i]));
3135         }
3136
3137         return ret;
3138 }
3139
3140 int BPy_BMElem_CheckHType(PyTypeObject *type, const char htype)
3141 {
3142         return (((htype & BM_VERT) && (type == &BPy_BMVert_Type)) ||
3143                 ((htype & BM_EDGE) && (type == &BPy_BMEdge_Type)) ||
3144                 ((htype & BM_FACE) && (type == &BPy_BMFace_Type)) ||
3145                 ((htype & BM_LOOP) && (type == &BPy_BMLoop_Type)));
3146 }
3147
3148 /**
3149  * Use for error strings only, not thread safe,
3150  *
3151  * \return a sting like '(BMVert/BMEdge/BMFace/BMLoop)'
3152  */
3153 char *BPy_BMElem_StringFromHType_ex(const char htype, char ret[32])
3154 {
3155         /* zero to ensure string is always NULL terminated */
3156         char *ret_ptr = ret;
3157         if (htype & BM_VERT) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMVert_Type.tp_name);
3158         if (htype & BM_EDGE) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMEdge_Type.tp_name);
3159         if (htype & BM_FACE) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMFace_Type.tp_name);
3160         if (htype & BM_LOOP) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMLoop_Type.tp_name);
3161         ret[0]   = '(';
3162         *ret_ptr = ')';
3163         return ret;
3164 }
3165 char *BPy_BMElem_StringFromHType(const char htype)
3166 {
3167         /* zero to ensure string is always NULL terminated */
3168         static char ret[32];
3169         return BPy_BMElem_StringFromHType_ex(htype, ret);
3170 }