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