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