code cleanup: use uppercase defines and change drawFCurveFade into static function.
[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(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
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(eve, &iter, self->bm, BM_VERTS_OF_MESH, NULL) {
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->bm, 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
1247 PyDoc_STRVAR(bpy_bmedge_other_vert_doc,
1248 ".. method:: other_vert(vert)\n"
1249 "\n"
1250 "   Return the other vertex on this edge or None if the vertex is not used by this edge.\n"
1251 "\n"
1252 "   :arg vert: a vert in this edge.\n"
1253 "   :type vert: :class:`BMVert`\n"
1254 "   :return: The edges other vert.\n"
1255 "   :rtype: :class:`BMVert` or None\n"
1256 );
1257 static PyObject *bpy_bmedge_other_vert(BPy_BMEdge *self, BPy_BMVert *value)
1258 {
1259         BMVert *other;
1260         BPY_BM_CHECK_OBJ(self);
1261
1262         if (!BPy_BMVert_Check(value)) {
1263                 PyErr_Format(PyExc_TypeError,
1264                              "BMEdge.other_vert(vert): BMVert expected, not '%.200s'",
1265                              Py_TYPE(value)->tp_name);
1266                 return NULL;
1267         }
1268
1269         BPY_BM_CHECK_OBJ(value);
1270
1271         if (self->bm != value->bm) {
1272                 PyErr_SetString(PyExc_ValueError,
1273                                 "BMEdge.other_vert(vert): vert is from another mesh");
1274                 return NULL;
1275         }
1276
1277         other = BM_edge_other_vert(self->e, value->v);
1278
1279         if (other) {
1280                 return BPy_BMVert_CreatePyObject(self->bm, other);
1281         }
1282         else {
1283                 /* could raise an exception here */
1284                 Py_RETURN_NONE;
1285         }
1286 }
1287
1288
1289 PyDoc_STRVAR(bpy_bmedge_normal_update_doc,
1290 ".. method:: normal_update()\n"
1291 "\n"
1292 "   Update edges vertex normals.\n"
1293 );
1294 static PyObject *bpy_bmedge_normal_update(BPy_BMEdge *self)
1295 {
1296         BPY_BM_CHECK_OBJ(self);
1297
1298         BM_edge_normals_update(self->bm, self->e);
1299
1300         Py_RETURN_NONE;
1301 }
1302
1303
1304 /* Face
1305  * ---- */
1306
1307 PyDoc_STRVAR(bpy_bmface_copy_from_face_interp_doc,
1308 ".. method:: copy_from_face_interp(face)\n"
1309 "\n"
1310 "   Interpolate the customdata from another face onto this one (faces should overlap).\n"
1311 "\n"
1312 "   :arg face: The face to interpolate data from.\n"
1313 "   :type face: :class:`BMFace`\n"
1314 );
1315 static PyObject *bpy_bmface_copy_from_face_interp(BPy_BMFace *self, PyObject *args)
1316 {
1317         BPy_BMFace *py_face = NULL;
1318
1319         BPY_BM_CHECK_OBJ(self);
1320
1321         if (!PyArg_ParseTuple(args, "O!:BMFace.copy_from_face_interp",
1322                               &BPy_BMFace_Type, &py_face))
1323         {
1324                 return NULL;
1325         }
1326         else {
1327                 BMesh *bm = self->bm;
1328
1329                 BPY_BM_CHECK_OBJ(py_face);
1330
1331                 if (py_face->bm != bm) {
1332                         PyErr_SetString(PyExc_ValueError,
1333                                         "BMFace.copy_from_face_interp(face): face is from another mesh");
1334                         return NULL;
1335                 }
1336
1337                 BM_face_interp_from_face(bm, self->f, py_face->f);
1338
1339                 Py_RETURN_NONE;
1340         }
1341 }
1342
1343
1344 PyDoc_STRVAR(bpy_bmface_copy_doc,
1345 ".. method:: copy(verts=True, edges=True)\n"
1346 "\n"
1347 "   Make a copy of this face.\n"
1348 "\n"
1349 "   :arg verts: When set, the faces verts will be duplicated too.\n"
1350 "   :type verts: boolean\n"
1351 "   :arg edges: When set, the faces edges will be duplicated too.\n"
1352 "   :type edges: boolean\n"
1353 "   :return: The newly created face.\n"
1354 "   :rtype: :class:`BMFace`\n"
1355 );
1356 static PyObject *bpy_bmface_copy(BPy_BMFace *self, PyObject *args, PyObject *kw)
1357 {
1358         static const char *kwlist[] = {"verts", "edges", NULL};
1359
1360         BMesh *bm = self->bm;
1361         int do_verts = TRUE;
1362         int do_edges = TRUE;
1363
1364         BMFace *f_cpy;
1365         BPY_BM_CHECK_OBJ(self);
1366
1367         if (!PyArg_ParseTupleAndKeywords(args, kw,
1368                                          "|ii:BMFace.copy",
1369                                          (char **)kwlist,
1370                                          &do_verts, &do_edges))
1371         {
1372                 return NULL;
1373         }
1374
1375         f_cpy = BM_face_copy(bm, self->f, do_verts, do_edges);
1376
1377         if (f_cpy) {
1378                 return BPy_BMFace_CreatePyObject(bm, f_cpy);
1379         }
1380         else {
1381                 PyErr_SetString(PyExc_ValueError,
1382                                 "BMFace.copy(): couldn't create the new face, internal error");
1383                 return NULL;
1384         }
1385 }
1386
1387
1388 PyDoc_STRVAR(bpy_bmface_calc_area_doc,
1389 ".. method:: calc_area()\n"
1390 "\n"
1391 "   Return the area of the face.\n"
1392 "\n"
1393 "   :return: Return the area of the face.\n"
1394 "   :rtype: float\n"
1395 );
1396 static PyObject *bpy_bmface_calc_area(BPy_BMFace *self)
1397 {
1398         BPY_BM_CHECK_OBJ(self);
1399         return PyFloat_FromDouble(BM_face_area_calc(self->bm, self->f));
1400 }
1401
1402
1403 PyDoc_STRVAR(bpy_bmface_calc_center_mean_doc,
1404 ".. method:: calc_center_median()\n"
1405 "\n"
1406 "   Return median center of the face.\n"
1407 "\n"
1408 "   :return: a 3D vector.\n"
1409 "   :rtype: :class:`mathutils.Vector`\n"
1410 );
1411 static PyObject *bpy_bmface_calc_center_mean(BPy_BMFace *self)
1412 {
1413         float cent[3];
1414
1415         BPY_BM_CHECK_OBJ(self);
1416         BM_face_center_mean_calc(self->bm, self->f, cent);
1417         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
1418 }
1419
1420
1421 PyDoc_STRVAR(bpy_bmface_calc_center_bounds_doc,
1422 ".. method:: calc_center_bounds()\n"
1423 "\n"
1424 "   Return bounds center of the face.\n"
1425 "\n"
1426 "   :return: a 3D vector.\n"
1427 "   :rtype: :class:`mathutils.Vector`\n"
1428 );
1429 static PyObject *bpy_bmface_calc_center_bounds(BPy_BMFace *self)
1430 {
1431         float cent[3];
1432
1433         BPY_BM_CHECK_OBJ(self);
1434         BM_face_center_bounds_calc(self->bm, self->f, cent);
1435         return Vector_CreatePyObject(cent, 3, Py_NEW, NULL);
1436 }
1437
1438
1439 PyDoc_STRVAR(bpy_bmface_normal_update_doc,
1440 ".. method:: normal_update()\n"
1441 "\n"
1442 "   Update faces normal.\n"
1443 );
1444 static PyObject *bpy_bmface_normal_update(BPy_BMFace *self)
1445 {
1446         BPY_BM_CHECK_OBJ(self);
1447
1448         BM_face_normal_update(self->bm, self->f);
1449
1450         Py_RETURN_NONE;
1451 }
1452
1453
1454 /* Loop
1455  * ---- */
1456
1457 PyDoc_STRVAR(bpy_bmloop_copy_from_face_interp_doc,
1458 ".. method:: copy_from_face_interp(face, vert=True, multires=True)\n"
1459 "\n"
1460 "   Interpolate the customdata from a face onto this loop (the loops vert should overlap the face).\n"
1461 "\n"
1462 "   :arg face: The face to interpolate data from.\n"
1463 "   :type face: :class:`BMFace`\n"
1464 "   :arg vert: When enabled, interpolate the loops vertex data (optional).\n"
1465 "   :type vert: boolean\n"
1466 "   :arg multires: When enabled, interpolate the loops multires data (optional).\n"
1467 "   :type multires: boolean\n"
1468 );
1469 static PyObject *bpy_bmloop_copy_from_face_interp(BPy_BMLoop *self, PyObject *args)
1470 {
1471         BPy_BMFace *py_face = NULL;
1472         int do_vertex   = TRUE;
1473         int do_multires = TRUE;
1474
1475         BPY_BM_CHECK_OBJ(self);
1476
1477         if (!PyArg_ParseTuple(args, "O!|ii:BMLoop.copy_from_face_interp",
1478                               &BPy_BMFace_Type, &py_face,
1479                               &do_vertex, &do_multires))
1480         {
1481                 return NULL;
1482         }
1483         else {
1484                 BMesh *bm = self->bm;
1485
1486                 BPY_BM_CHECK_OBJ(py_face);
1487
1488                 if (py_face->bm != bm) {
1489                         PyErr_SetString(PyExc_ValueError,
1490                                         "BMLoop.copy_from_face_interp(face): face is from another mesh");
1491                         return NULL;
1492                 }
1493
1494                 BM_loop_interp_from_face(bm, self->l, py_face->f, do_vertex, do_multires);
1495
1496                 Py_RETURN_NONE;
1497         }
1498 }
1499
1500
1501 PyDoc_STRVAR(bpy_bmloop_calc_angle_doc,
1502 ".. method:: calc_angle()\n"
1503 "\n"
1504 "   Return the angle at this loops corner of the face.\n"
1505 "   This is calculated so sharper corners give lower angles.\n"
1506 "\n"
1507 "   :return: The angle in radians.\n"
1508 "   :rtype: float\n"
1509 );
1510 static PyObject *bpy_bmloop_calc_angle(BPy_BMLoop *self)
1511 {
1512         BPY_BM_CHECK_OBJ(self);
1513         return PyFloat_FromDouble(BM_loop_face_angle(self->l));
1514 }
1515
1516 PyDoc_STRVAR(bpy_bmloop_calc_normal_doc,
1517 ".. method:: calc_normal()\n"
1518 "\n"
1519 "   Return normal at this loops corner of the face.\n"
1520 "   Falls back to the face normal for straignt lines.\n"
1521 "\n"
1522 "   :return: a normalized vector.\n"
1523 "   :rtype: :class:`mathutils.Vector`\n"
1524 );
1525 static PyObject *bpy_bmloop_calc_normal(BPy_BMLoop *self)
1526 {
1527         float vec[3];
1528         BPY_BM_CHECK_OBJ(self);
1529         BM_loop_face_normal(self->l, vec);
1530         return Vector_CreatePyObject(vec, 3, Py_NEW, NULL);
1531 }
1532
1533 PyDoc_STRVAR(bpy_bmloop_calc_tangent_doc,
1534 ".. method:: calc_tangent()\n"
1535 "\n"
1536 "   Return the tangent at this loops corner of the face (pointing inward into the face).\n"
1537 "   Falls back to the face normal for straignt lines.\n"
1538 "\n"
1539 "   :return: a normalized vector.\n"
1540 "   :rtype: :class:`mathutils.Vector`\n"
1541 );
1542 static PyObject *bpy_bmloop_calc_tangent(BPy_BMLoop *self)
1543 {
1544         float vec[3];
1545         BPY_BM_CHECK_OBJ(self);
1546         BM_loop_face_tangent(self->l, vec);
1547         return Vector_CreatePyObject(vec, 3, Py_NEW, NULL);
1548 }
1549
1550 /* Vert Seq
1551  * -------- */
1552 PyDoc_STRVAR(bpy_bmvertseq_new_doc,
1553 ".. method:: new(co=(0.0, 0.0, 0.0), example=None)\n"
1554 "\n"
1555 "   Create a new vertex.\n"
1556 "\n"
1557 "   :arg co: The initial location of the vertex (optional argument).\n"
1558 "   :type co: float triplet\n"
1559 "   :arg example: Existing vert to initialize settings.\n"
1560 "   :type example: :class:`BMVert`\n"
1561 "   :return: The newly created edge.\n"
1562 "   :rtype: :class:`BMVert`\n"
1563 );
1564 static PyObject *bpy_bmvertseq_new(BPy_BMElemSeq *self, PyObject *args)
1565 {
1566         PyObject *py_co = NULL;
1567         BPy_BMVert *py_vert_example = NULL; /* optional */
1568
1569         BPY_BM_CHECK_OBJ(self);
1570
1571         if (!PyArg_ParseTuple(args, "|OO!:verts.new",
1572                               &py_co,
1573                               &BPy_BMVert_Type, &py_vert_example))
1574         {
1575                 return NULL;
1576         }
1577         else {
1578                 BMesh *bm = self->bm;
1579                 BMVert *v;
1580                 float co[3] = {0.0f, 0.0f, 0.0f};
1581
1582                 if (py_vert_example) {
1583                         BPY_BM_CHECK_OBJ(py_vert_example);
1584                 }
1585
1586                 if (py_co && mathutils_array_parse(co, 3, 3, py_co, "verts.new(co)") == -1) {
1587                         return NULL;
1588                 }
1589
1590                 v = BM_vert_create(bm, co, NULL);
1591
1592                 if (v == NULL) {
1593                         PyErr_SetString(PyExc_ValueError,
1594                                         "faces.new(verts): couldn't create the new face, internal error");
1595                         return NULL;
1596                 }
1597
1598                 if (py_vert_example) {
1599                         BM_elem_attrs_copy(py_vert_example->bm, bm, py_vert_example->v, v);
1600                 }
1601
1602                 return BPy_BMVert_CreatePyObject(bm, v);
1603         }
1604 }
1605
1606
1607 /* Edge Seq
1608  * -------- */
1609 PyDoc_STRVAR(bpy_bmedgeseq_new_doc,
1610 ".. method:: new(verts, example=None)\n"
1611 "\n"
1612 "   Create a new edge from a given pair of verts.\n"
1613 "\n"
1614 "   :arg verts: Vertex pair.\n"
1615 "   :type verts: pair of :class:`BMVert`\n"
1616 "   :arg example: Existing edge to initialize settings (optional argument).\n"
1617 "   :type example: :class:`BMEdge`\n"
1618 "   :return: The newly created edge.\n"
1619 "   :rtype: :class:`BMEdge`\n"
1620 );
1621 static PyObject *bpy_bmedgeseq_new(BPy_BMElemSeq *self, PyObject *args)
1622 {
1623         PyObject *vert_seq;
1624         BPy_BMEdge *py_edge_example = NULL; /* optional */
1625
1626         BPY_BM_CHECK_OBJ(self);
1627
1628         if (!PyArg_ParseTuple(args, "O|O!:edges.new",
1629                               &vert_seq,
1630                               &BPy_BMEdge_Type, &py_edge_example))
1631         {
1632                 return NULL;
1633         }
1634         else {
1635                 BMesh *bm = self->bm;
1636                 BMEdge *e;
1637                 BMVert **vert_array = NULL;
1638                 Py_ssize_t vert_seq_len; /* always 2 */
1639                 PyObject *ret = NULL;
1640
1641                 if (py_edge_example) {
1642                         BPY_BM_CHECK_OBJ(py_edge_example);
1643                 }
1644
1645                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 2, 2,
1646                                                        &vert_seq_len, BM_VERT,
1647                                                        TRUE, TRUE, "edges.new(...)");
1648
1649                 if (vert_array == NULL) {
1650                         return NULL;
1651                 }
1652                 
1653                 if (BM_edge_exists(vert_array[0], vert_array[1])) {
1654                         PyErr_SetString(PyExc_ValueError,
1655                                         "edges.new(): this edge exists");
1656                         goto cleanup;
1657                 }
1658
1659                 e = BM_edge_create(bm, vert_array[0], vert_array[1], NULL, FALSE);
1660
1661                 if (e == NULL) {
1662                         PyErr_SetString(PyExc_ValueError,
1663                                         "faces.new(verts): couldn't create the new face, internal error");
1664                         goto cleanup;
1665                 }
1666
1667                 if (py_edge_example) {
1668                         BM_elem_attrs_copy(py_edge_example->bm, bm, py_edge_example->e, e);
1669                 }
1670
1671                 ret = BPy_BMEdge_CreatePyObject(bm, e);
1672
1673 cleanup:
1674                 if (vert_array) PyMem_FREE(vert_array);
1675                 return ret;
1676         }
1677 }
1678
1679
1680 /* Face Seq
1681  * -------- */
1682 PyDoc_STRVAR(bpy_bmfaceseq_new_doc,
1683 ".. method:: new(verts, example=None)\n"
1684 "\n"
1685 "   Create a new face from a given set of verts.\n"
1686 "\n"
1687 "   :arg verts: Sequence of 3 or more verts.\n"
1688 "   :type verts: :class:`BMVert`\n"
1689 "   :arg example: Existing face to initialize settings (optional argument).\n"
1690 "   :type example: :class:`BMFace`\n"
1691 "   :return: The newly created face.\n"
1692 "   :rtype: :class:`BMFace`\n"
1693 );
1694 static PyObject *bpy_bmfaceseq_new(BPy_BMElemSeq *self, PyObject *args)
1695 {
1696         PyObject *vert_seq;
1697         BPy_BMFace *py_face_example = NULL; /* optional */
1698
1699         BPY_BM_CHECK_OBJ(self);
1700
1701         if (!PyArg_ParseTuple(args, "O|O!:faces.new",
1702                               &vert_seq,
1703                               &BPy_BMFace_Type, &py_face_example))
1704         {
1705                 return NULL;
1706         }
1707         else {
1708                 BMesh *bm = self->bm;
1709                 Py_ssize_t vert_seq_len;
1710                 Py_ssize_t i, i_next;
1711
1712                 BMVert **vert_array = NULL;
1713                 BMEdge **edge_array = NULL;
1714
1715                 PyObject *ret = NULL;
1716
1717                 BMFace *f_new;
1718
1719                 if (py_face_example) {
1720                         BPY_BM_CHECK_OBJ(py_face_example);
1721                 }
1722
1723                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 3, PY_SSIZE_T_MAX,
1724                                                        &vert_seq_len, BM_VERT,
1725                                                        TRUE, TRUE, "faces.new(...)");
1726
1727                 if (vert_array == NULL) {
1728                         return NULL;
1729                 }
1730
1731                 /* check if the face exists */
1732                 if (BM_face_exists(bm, vert_array, vert_seq_len, NULL)) {
1733                         PyErr_SetString(PyExc_ValueError,
1734                                         "faces.new(verts): face already exists");
1735                         goto cleanup;
1736                 }
1737
1738                 /* Go ahead and make the face!
1739                  * --------------------------- */
1740
1741                 edge_array = (BMEdge **)PyMem_MALLOC(vert_seq_len * sizeof(BMEdge **));
1742
1743                 /* ensure edges */
1744                 for (i = vert_seq_len - 1, i_next = 0; i_next < vert_seq_len; (i = i_next++)) {
1745                         edge_array[i] = BM_edge_create(bm, vert_array[i], vert_array[i_next], NULL, TRUE);
1746                 }
1747
1748                 f_new = BM_face_create(bm, vert_array, edge_array, vert_seq_len, FALSE);
1749
1750                 if (f_new == NULL) {
1751                         PyErr_SetString(PyExc_ValueError,
1752                                         "faces.new(verts): couldn't create the new face, internal error");
1753                         goto cleanup;
1754                 }
1755
1756                 if (py_face_example) {
1757                         BM_elem_attrs_copy(py_face_example->bm, bm, py_face_example->f, f_new);
1758                 }
1759
1760                 ret = BPy_BMFace_CreatePyObject(bm, f_new);
1761
1762                 /* pass through */
1763 cleanup:
1764                 if (vert_array) PyMem_FREE(vert_array);
1765                 if (edge_array) PyMem_FREE(edge_array);
1766                 return ret;
1767         }
1768 }
1769
1770 /* Elem Seq
1771  * -------- */
1772
1773 PyDoc_STRVAR(bpy_bmvertseq_remove_doc,
1774 ".. method:: remove(vert)\n"
1775 "\n"
1776 "   Remove a vert.\n"
1777 );
1778 static PyObject *bpy_bmvertseq_remove(BPy_BMElemSeq *self, BPy_BMVert *value)
1779 {
1780         BPY_BM_CHECK_OBJ(self);
1781
1782         if (!BPy_BMVert_Check(value)) {
1783                 return NULL;
1784         }
1785         else {
1786                 BMesh *bm = self->bm;
1787
1788                 BPY_BM_CHECK_OBJ(value);
1789
1790                 if (value->bm != bm) {
1791                         PyErr_SetString(PyExc_ValueError,
1792                                         "verts.remove(vert): vert is from another mesh");
1793                         return NULL;
1794                 }
1795
1796                 BM_vert_kill(bm, value->v);
1797                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1798
1799                 Py_RETURN_NONE;
1800         }
1801 }
1802
1803 PyDoc_STRVAR(bpy_bmedgeseq_remove_doc,
1804 ".. method:: remove(edge)\n"
1805 "\n"
1806 "   Remove a edge.\n"
1807 );
1808 static PyObject *bpy_bmedgeseq_remove(BPy_BMElemSeq *self, BPy_BMEdge *value)
1809 {
1810         BPY_BM_CHECK_OBJ(self);
1811
1812         if (!BPy_BMEdge_Check(value)) {
1813                 return NULL;
1814         }
1815         else {
1816                 BMesh *bm = self->bm;
1817
1818                 BPY_BM_CHECK_OBJ(value);
1819
1820                 if (value->bm != bm) {
1821                         PyErr_SetString(PyExc_ValueError,
1822                                         "edges.remove(edge): edge is from another mesh");
1823                         return NULL;
1824                 }
1825
1826                 BM_edge_kill(bm, value->e);
1827                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1828
1829                 Py_RETURN_NONE;
1830         }
1831 }
1832
1833 PyDoc_STRVAR(bpy_bmfaceseq_remove_doc,
1834 ".. method:: remove(face)\n"
1835 "\n"
1836 "   Remove a face.\n"
1837 );
1838 static PyObject *bpy_bmfaceseq_remove(BPy_BMElemSeq *self, BPy_BMFace *value)
1839 {
1840         BPY_BM_CHECK_OBJ(self);
1841
1842         if (!BPy_BMFace_Check(value)) {
1843                 return NULL;
1844         }
1845         else {
1846                 BMesh *bm = self->bm;
1847
1848                 BPY_BM_CHECK_OBJ(value);
1849
1850                 if (value->bm != bm) {
1851                         PyErr_SetString(PyExc_ValueError,
1852                                         "faces.remove(face): face is from another mesh");
1853                         return NULL;
1854                 }
1855
1856                 BM_face_kill(bm, value->f);
1857                 bpy_bm_generic_invalidate((BPy_BMGeneric *)value);
1858
1859                 Py_RETURN_NONE;
1860         }
1861 }
1862
1863 PyDoc_STRVAR(bpy_bmedgeseq_get__method_doc,
1864 ".. method:: get(verts, fallback=None)\n"
1865 "\n"
1866 "   Return a edge which uses the **verts** passed.\n"
1867 "\n"
1868 "   :arg verts: Sequence of verts.\n"
1869 "   :type verts: :class:`BMVert`\n"
1870 "   :arg fallback: Return this value if nothing is found.\n"
1871 "   :return: The edge found or None\n"
1872 "   :rtype: :class:`BMEdge`\n"
1873 );
1874 static PyObject *bpy_bmedgeseq_get__method(BPy_BMElemSeq *self, PyObject *args)
1875 {
1876         PyObject *vert_seq;
1877         PyObject *fallback = Py_None; /* optional */
1878
1879         BPY_BM_CHECK_OBJ(self);
1880
1881         if (!PyArg_ParseTuple(args, "O|O:edges.get",
1882                               &vert_seq,
1883                               &fallback))
1884         {
1885                 return NULL;
1886         }
1887         else {
1888                 BMesh *bm = self->bm;
1889                 BMEdge *e;
1890                 BMVert **vert_array = NULL;
1891                 Py_ssize_t vert_seq_len; /* always 2 */
1892                 PyObject *ret = NULL;
1893
1894                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 2, 2,
1895                                                        &vert_seq_len, BM_VERT,
1896                                                        TRUE, TRUE, "edges.get(...)");
1897
1898                 if (vert_array == NULL) {
1899                         return NULL;
1900                 }
1901
1902                 if ((e = BM_edge_exists(vert_array[0], vert_array[1]))) {
1903                         ret = BPy_BMEdge_CreatePyObject(bm, e);
1904                 }
1905                 else {
1906                         ret = fallback;
1907                         Py_INCREF(ret);
1908                 }
1909
1910                 PyMem_FREE(vert_array);
1911                 return ret;
1912         }
1913 }
1914
1915 PyDoc_STRVAR(bpy_bmfaceseq_get__method_doc,
1916 ".. method:: get(verts, fallback=None)\n"
1917 "\n"
1918 "   Return a face which uses the **verts** passed.\n"
1919 "\n"
1920 "   :arg verts: Sequence of verts.\n"
1921 "   :type verts: :class:`BMVert`\n"
1922 "   :arg fallback: Return this value if nothing is found.\n"
1923 "   :return: The face found or None\n"
1924 "   :rtype: :class:`BMFace`\n"
1925 );
1926 static PyObject *bpy_bmfaceseq_get__method(BPy_BMElemSeq *self, PyObject *args)
1927 {
1928         PyObject *vert_seq;
1929         PyObject *fallback = Py_None; /* optional */
1930
1931         BPY_BM_CHECK_OBJ(self);
1932
1933         if (!PyArg_ParseTuple(args, "O|O:faces.get",
1934                               &vert_seq,
1935                               &fallback))
1936         {
1937                 return NULL;
1938         }
1939         else {
1940                 BMesh *bm = self->bm;
1941                 BMFace *f = NULL;
1942                 BMVert **vert_array = NULL;
1943                 Py_ssize_t vert_seq_len;
1944                 PyObject *ret = NULL;
1945
1946                 vert_array = BPy_BMElem_PySeq_As_Array(&bm, vert_seq, 1, PY_SSIZE_T_MAX,
1947                                                        &vert_seq_len, BM_VERT,
1948                                                        TRUE, TRUE, "faces.get(...)");
1949
1950                 if (vert_array == NULL) {
1951                         return NULL;
1952                 }
1953
1954                 if (BM_face_exists(bm, vert_array, vert_seq_len, &f)) {
1955                         ret = BPy_BMFace_CreatePyObject(bm, f);
1956                 }
1957                 else {
1958                         ret = fallback;
1959                         Py_INCREF(ret);
1960                 }
1961
1962                 PyMem_FREE(vert_array);
1963                 return ret;
1964         }
1965 }
1966
1967 PyDoc_STRVAR(bpy_bmelemseq_index_update_doc,
1968 ".. method:: index_update()\n"
1969 "\n"
1970 "   Initialize the index values of this sequence.\n"
1971 "\n"
1972 "   This is the equivalent of looping over all elements and assigning the index values.\n"
1973 "\n"
1974 "   .. code-block:: python\n"
1975 "\n"
1976 "      for index, ele in enumerate(sequence):\n"
1977 "          ele.index = index\n"
1978 "\n"
1979 "   .. note::\n"
1980 "\n"
1981 "      Running this on sequences besides :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces`\n"
1982 "      works but wont result in each element having a valid index, insted its order in the sequence will be set.\n"
1983 );
1984 static PyObject *bpy_bmelemseq_index_update(BPy_BMElemSeq *self)
1985 {
1986         BMesh *bm = self->bm;
1987
1988         BPY_BM_CHECK_OBJ(self);
1989
1990         switch ((BMIterType)self->itype) {
1991                 case BM_VERTS_OF_MESH:
1992                         BM_mesh_elem_index_ensure(self->bm, BM_VERT);
1993                         break;
1994                 case BM_EDGES_OF_MESH:
1995                         BM_mesh_elem_index_ensure(self->bm, BM_EDGE);
1996                         break;
1997                 case BM_FACES_OF_MESH:
1998                         BM_mesh_elem_index_ensure(self->bm, BM_FACE);
1999                         break;
2000                 default:
2001                 {
2002                         BMIter iter;
2003                         BMElem *ele;
2004                         int index = 0;
2005                         const char htype = bm_iter_itype_htype_map[self->itype];
2006
2007                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
2008                                 BM_elem_index_set(ele, index); /* set_dirty! */
2009                                 index++;
2010                         }
2011
2012                         if (htype & (BM_VERT | BM_EDGE | BM_FACE)) {
2013                                 /* since this isn't the normal vert/edge/face loops,
2014                                  * we're setting dirty values here. so tag as dirty. */
2015                                 bm->elem_index_dirty |= htype;
2016                         }
2017
2018                         break;
2019                 }
2020         }
2021
2022         Py_RETURN_NONE;
2023 }
2024
2025
2026 static struct PyMethodDef bpy_bmesh_methods[] = {
2027     /* utility */
2028     {"copy",  (PyCFunction)bpy_bmesh_copy,  METH_NOARGS, bpy_bmesh_copy_doc},
2029     {"clear", (PyCFunction)bpy_bmesh_clear, METH_NOARGS, bpy_bmesh_clear_doc},
2030     {"free",  (PyCFunction)bpy_bmesh_free,  METH_NOARGS, bpy_bmesh_free_doc},
2031
2032     /* conversion */
2033     {"from_object", (PyCFunction)bpy_bmesh_from_object, METH_VARARGS | METH_KEYWORDS, bpy_bmesh_from_object_doc},
2034     {"from_mesh",   (PyCFunction)bpy_bmesh_from_mesh,   METH_VARARGS | METH_KEYWORDS, bpy_bmesh_from_mesh_doc},
2035     {"to_mesh",     (PyCFunction)bpy_bmesh_to_mesh,     METH_VARARGS,                 bpy_bmesh_to_mesh_doc},
2036
2037     /* meshdata */
2038     {"select_flush_mode", (PyCFunction)bpy_bmesh_select_flush_mode, METH_NOARGS, bpy_bmesh_select_flush_mode_doc},
2039     {"select_flush", (PyCFunction)bpy_bmesh_select_flush, METH_O, bpy_bmesh_select_flush_doc},
2040     {"normal_update", (PyCFunction)bpy_bmesh_normal_update, METH_VARARGS, bpy_bmesh_normal_update_doc},
2041     {"transform", (PyCFunction)bpy_bmesh_transform, METH_VARARGS|METH_KEYWORDS, bpy_bmesh_transform_doc},
2042     {NULL, NULL, 0, NULL}
2043 };
2044
2045 static struct PyMethodDef bpy_bmvert_methods[] = {
2046     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2047     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2048     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2049     {"copy_from_face_interp", (PyCFunction)bpy_bmvert_copy_from_face_interp, METH_VARARGS, bpy_bmvert_copy_from_face_interp_doc},
2050     {"copy_from_vert_interp", (PyCFunction)bpy_bmvert_copy_from_vert_interp, METH_VARARGS, bpy_bmvert_copy_from_vert_interp_doc},
2051
2052     {"calc_vert_angle", (PyCFunction)bpy_bmvert_calc_edge_angle, METH_NOARGS, bpy_bmvert_calc_edge_angle_doc},
2053
2054     {"normal_update",  (PyCFunction)bpy_bmvert_normal_update,  METH_NOARGS,  bpy_bmvert_normal_update_doc},
2055
2056     {NULL, NULL, 0, NULL}
2057 };
2058
2059 static struct PyMethodDef bpy_bmedge_methods[] = {
2060     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2061     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2062     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2063
2064     {"other_vert", (PyCFunction)bpy_bmedge_other_vert, METH_O, bpy_bmedge_other_vert_doc},
2065
2066     {"calc_length",     (PyCFunction)bpy_bmedge_calc_length,     METH_NOARGS, bpy_bmedge_calc_length_doc},
2067     {"calc_face_angle", (PyCFunction)bpy_bmedge_calc_face_angle, METH_NOARGS, bpy_bmedge_calc_face_angle_doc},
2068
2069     {"normal_update",  (PyCFunction)bpy_bmedge_normal_update,  METH_NOARGS,  bpy_bmedge_normal_update_doc},
2070
2071     {NULL, NULL, 0, NULL}
2072 };
2073
2074 static struct PyMethodDef bpy_bmface_methods[] = {
2075     {"select_set", (PyCFunction)bpy_bm_elem_select_set, METH_O, bpy_bm_elem_select_set_doc},
2076     {"hide_set", (PyCFunction)bpy_bm_elem_hide_set, METH_O, bpy_bm_elem_hide_set_doc},
2077
2078     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2079     {"copy_from_face_interp", (PyCFunction)bpy_bmface_copy_from_face_interp, METH_O, bpy_bmface_copy_from_face_interp_doc},
2080
2081     {"copy", (PyCFunction)bpy_bmface_copy, METH_VARARGS|METH_KEYWORDS, bpy_bmface_copy_doc},
2082
2083     {"calc_area",          (PyCFunction)bpy_bmface_calc_area,          METH_NOARGS, bpy_bmface_calc_area_doc},
2084     {"calc_center_median", (PyCFunction)bpy_bmface_calc_center_mean,   METH_NOARGS, bpy_bmface_calc_center_mean_doc},
2085     {"calc_center_bounds", (PyCFunction)bpy_bmface_calc_center_bounds, METH_NOARGS, bpy_bmface_calc_center_bounds_doc},
2086
2087     {"normal_update",  (PyCFunction)bpy_bmface_normal_update,  METH_NOARGS,  bpy_bmface_normal_update_doc},
2088
2089     {NULL, NULL, 0, NULL}
2090 };
2091
2092 static struct PyMethodDef bpy_bmloop_methods[] = {
2093     {"copy_from", (PyCFunction)bpy_bm_elem_copy_from, METH_O, bpy_bm_elem_copy_from_doc},
2094     {"copy_from_face_interp", (PyCFunction)bpy_bmloop_copy_from_face_interp, METH_O, bpy_bmloop_copy_from_face_interp_doc},
2095
2096     {"calc_angle",   (PyCFunction)bpy_bmloop_calc_angle,   METH_NOARGS, bpy_bmloop_calc_angle_doc},
2097     {"calc_normal",  (PyCFunction)bpy_bmloop_calc_normal,  METH_NOARGS, bpy_bmloop_calc_normal_doc},
2098     {"calc_tangent", (PyCFunction)bpy_bmloop_calc_tangent, METH_NOARGS, bpy_bmloop_calc_tangent_doc},
2099     {NULL, NULL, 0, NULL}
2100 };
2101
2102 static struct PyMethodDef bpy_bmelemseq_methods[] = {
2103     /* odd function, initializes index values */
2104     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2105     {NULL, NULL, 0, NULL}
2106 };
2107
2108 static struct PyMethodDef bpy_bmvertseq_methods[] = {
2109     {"new",     (PyCFunction)bpy_bmvertseq_new,         METH_VARARGS, bpy_bmvertseq_new_doc},
2110     {"remove",  (PyCFunction)bpy_bmvertseq_remove,      METH_O,       bpy_bmvertseq_remove_doc},
2111
2112     /* odd function, initializes index values */
2113     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2114     {NULL, NULL, 0, NULL}
2115 };
2116
2117 static struct PyMethodDef bpy_bmedgeseq_methods[] = {
2118     {"new",     (PyCFunction)bpy_bmedgeseq_new,         METH_VARARGS, bpy_bmedgeseq_new_doc},
2119     {"remove",  (PyCFunction)bpy_bmedgeseq_remove,      METH_O,       bpy_bmedgeseq_remove_doc},
2120     /* 'bpy_bmelemseq_get' for different purpose */
2121     {"get",     (PyCFunction)bpy_bmedgeseq_get__method, METH_VARARGS, bpy_bmedgeseq_get__method_doc},
2122
2123     /* odd function, initializes index values */
2124     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2125     {NULL, NULL, 0, NULL}
2126 };
2127
2128 static struct PyMethodDef bpy_bmfaceseq_methods[] = {
2129     {"new",     (PyCFunction)bpy_bmfaceseq_new,         METH_VARARGS, bpy_bmfaceseq_new_doc},
2130     {"remove",  (PyCFunction)bpy_bmfaceseq_remove,      METH_O,       bpy_bmfaceseq_remove_doc},
2131     /* 'bpy_bmelemseq_get' for different purpose */
2132     {"get",     (PyCFunction)bpy_bmfaceseq_get__method, METH_VARARGS, bpy_bmfaceseq_get__method_doc},
2133
2134     /* odd function, initializes index values */
2135     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2136     {NULL, NULL, 0, NULL}
2137 };
2138
2139 static struct PyMethodDef bpy_bmloopseq_methods[] = {
2140     /* odd function, initializes index values */
2141     {"index_update", (PyCFunction)bpy_bmelemseq_index_update, METH_NOARGS, bpy_bmelemseq_index_update_doc},
2142     {NULL, NULL, 0, NULL}
2143 };
2144
2145 /* Sequences
2146  * ========= */
2147
2148 /* BMElemSeq / Iter
2149  * ---------------- */
2150
2151 static PyTypeObject *bpy_bm_itype_as_pytype(const char itype)
2152 {
2153         /* should cover all types */
2154         switch ((BMIterType)itype) {
2155                 case BM_VERTS_OF_MESH:
2156                 case BM_VERTS_OF_FACE:
2157                 case BM_VERTS_OF_EDGE:
2158                         return &BPy_BMVert_Type;
2159
2160                 case BM_EDGES_OF_MESH:
2161                 case BM_EDGES_OF_FACE:
2162                 case BM_EDGES_OF_VERT:
2163                         return &BPy_BMEdge_Type;
2164
2165                 case BM_FACES_OF_MESH:
2166                 case BM_FACES_OF_EDGE:
2167                 case BM_FACES_OF_VERT:
2168                         return &BPy_BMFace_Type;
2169
2170                 case BM_ALL_LOOPS_OF_FACE:
2171                 case BM_LOOPS_OF_FACE:
2172                 case BM_LOOPS_OF_EDGE:
2173                 case BM_LOOPS_OF_VERT:
2174                 case BM_LOOPS_OF_LOOP:
2175                         return &BPy_BMLoop_Type;
2176         }
2177
2178         return NULL;
2179 }
2180
2181 static Py_ssize_t bpy_bmelemseq_length(BPy_BMElemSeq *self)
2182 {
2183         BPY_BM_CHECK_INT(self);
2184
2185         /* first check if the size is known */
2186         switch ((BMIterType)self->itype) {
2187                 /* main-types */
2188                 case BM_VERTS_OF_MESH:
2189                         return self->bm->totvert;
2190                 case BM_EDGES_OF_MESH:
2191                         return self->bm->totedge;
2192                 case BM_FACES_OF_MESH:
2193                         return self->bm->totface;
2194
2195                         /* sub-types */
2196                 case BM_VERTS_OF_FACE:
2197                 case BM_EDGES_OF_FACE:
2198                 case BM_LOOPS_OF_FACE:
2199                         BPY_BM_CHECK_INT(self->py_ele);
2200                         return ((BMFace *)self->py_ele->ele)->len;
2201
2202                 case BM_VERTS_OF_EDGE:
2203                         return 2;
2204
2205                 default:
2206                         /* quiet compiler */
2207                         break;
2208         }
2209
2210
2211         /* loop over all items, avoid this if we can */
2212         {
2213                 BMIter iter;
2214                 BMHeader *ele;
2215                 Py_ssize_t tot = 0;
2216
2217                 BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
2218                         tot++;
2219                 }
2220                 return tot;
2221         }
2222 }
2223
2224 static PyObject *bpy_bmelemseq_subscript_int(BPy_BMElemSeq *self, int keynum)
2225 {
2226         BPY_BM_CHECK_OBJ(self);
2227
2228         if (keynum < 0) keynum += bpy_bmelemseq_length(self); /* only get length on negative value, may loop entire seq */
2229         if (keynum >= 0) {
2230                 BMHeader *ele = BM_iter_at_index(self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL, keynum);
2231                 if (ele) {
2232                         return BPy_BMElem_CreatePyObject(self->bm, ele);
2233                 }
2234         }
2235
2236         PyErr_Format(PyExc_IndexError,
2237                      "BMElemSeq[index]: index %d out of range", keynum);
2238         return NULL;
2239 }
2240
2241 static PyObject *bpy_bmelemseq_subscript_slice(BPy_BMElemSeq *self, Py_ssize_t start, Py_ssize_t stop)
2242 {
2243         BMIter iter;
2244         int count = 0;
2245         int ok;
2246
2247         PyObject *list;
2248         PyObject *item;
2249         BMHeader *ele;
2250
2251         BPY_BM_CHECK_OBJ(self);
2252
2253         list = PyList_New(0);
2254
2255         ok = BM_iter_init(&iter, self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
2256
2257         BLI_assert(ok == TRUE);
2258
2259         if (UNLIKELY(ok == FALSE)) {
2260                 return list;
2261         }
2262
2263         /* first loop up-until the start */
2264         for (ok = TRUE; ok; ok = (BM_iter_step(&iter) != NULL)) {
2265                 if (count == start) {
2266                         break;
2267                 }
2268                 count++;
2269         }
2270
2271         /* add items until stop */
2272         while ((ele = BM_iter_step(&iter))) {
2273                 item = BPy_BMElem_CreatePyObject(self->bm, ele);
2274                 PyList_Append(list, item);
2275                 Py_DECREF(item);
2276
2277                 count++;
2278                 if (count == stop) {
2279                         break;
2280                 }
2281         }
2282
2283         return list;
2284 }
2285
2286 static PyObject *bpy_bmelemseq_subscript(BPy_BMElemSeq *self, PyObject *key)
2287 {
2288         /* don't need error check here */
2289         if (PyIndex_Check(key)) {
2290                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2291                 if (i == -1 && PyErr_Occurred())
2292                         return NULL;
2293                 return bpy_bmelemseq_subscript_int(self, i);
2294         }
2295         else if (PySlice_Check(key)) {
2296                 PySliceObject *key_slice = (PySliceObject *)key;
2297                 Py_ssize_t step = 1;
2298
2299                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2300                         return NULL;
2301                 }
2302                 else if (step != 1) {
2303                         PyErr_SetString(PyExc_TypeError,
2304                                         "BMElemSeq[slice]: slice steps not supported");
2305                         return NULL;
2306                 }
2307                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2308                         return bpy_bmelemseq_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2309                 }
2310                 else {
2311                         Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2312
2313                         /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2314                         if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2315                         if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop))    return NULL;
2316
2317                         if (start < 0 || stop < 0) {
2318                                 /* only get the length for negative values */
2319                                 Py_ssize_t len = bpy_bmelemseq_length(self);
2320                                 if (start < 0) start += len;
2321                                 if (stop < 0) start += len;
2322                         }
2323
2324                         if (stop - start <= 0) {
2325                                 return PyList_New(0);
2326                         }
2327                         else {
2328                                 return bpy_bmelemseq_subscript_slice(self, start, stop);
2329                         }
2330                 }
2331         }
2332         else {
2333                 PyErr_SetString(PyExc_AttributeError,
2334                                 "BMElemSeq[key]: invalid key, key must be an int");
2335                 return NULL;
2336         }
2337 }
2338
2339 static int bpy_bmelemseq_contains(BPy_BMElemSeq *self, PyObject *value)
2340 {
2341         BPY_BM_CHECK_INT(self);
2342
2343         if (Py_TYPE(value) == bpy_bm_itype_as_pytype(self->itype)) {
2344                 BPy_BMElem *value_bm_ele = (BPy_BMElem *)value;
2345                 if (value_bm_ele->bm == self->bm) {
2346                         BMElem *ele, *ele_test = value_bm_ele->ele;
2347                         BMIter iter;
2348                         BM_ITER_BPY_BM_SEQ(ele, &iter, self) {
2349                                 if (ele == ele_test) {
2350                                         return 1;
2351                                 }
2352                         }
2353                 }
2354         }
2355
2356         return 0;
2357 }
2358
2359 /* BMElem (customdata)
2360  * ------------------- */
2361
2362 static PyObject *bpy_bmelem_subscript(BPy_BMElem *self, BPy_BMLayerItem *key)
2363 {
2364         BPY_BM_CHECK_OBJ(self);
2365
2366         return BPy_BMLayerItem_GetItem(self, key);
2367 }
2368
2369 static int bpy_bmelem_ass_subscript(BPy_BMElem *self, BPy_BMLayerItem *key, PyObject *value)
2370 {
2371         BPY_BM_CHECK_INT(self);
2372
2373         return BPy_BMLayerItem_SetItem(self, key, value);
2374 }
2375
2376 static PySequenceMethods bpy_bmelemseq_as_sequence = {
2377     (lenfunc)bpy_bmelemseq_length,                  /* sq_length */
2378     NULL,                                        /* sq_concat */
2379     NULL,                                        /* sq_repeat */
2380     (ssizeargfunc)bpy_bmelemseq_subscript_int,      /* sq_item */ /* Only set this so PySequence_Check() returns True */
2381     NULL,                                        /* sq_slice */
2382     (ssizeobjargproc)NULL,                       /* sq_ass_item */
2383     NULL,                                        /* *was* sq_ass_slice */
2384     (objobjproc)bpy_bmelemseq_contains,             /* sq_contains */
2385     (binaryfunc) NULL,                           /* sq_inplace_concat */
2386     (ssizeargfunc) NULL,                         /* sq_inplace_repeat */
2387 };
2388
2389 static PyMappingMethods bpy_bmelemseq_as_mapping = {
2390     (lenfunc)bpy_bmelemseq_length,                  /* mp_length */
2391     (binaryfunc)bpy_bmelemseq_subscript,            /* mp_subscript */
2392     (objobjargproc)NULL,                         /* mp_ass_subscript */
2393 };
2394
2395 /* for customdata access */
2396 static PyMappingMethods bpy_bm_elem_as_mapping = {
2397     (lenfunc)NULL,                           /* mp_length */ /* keep this empty, messes up 'if elem: ...' test */
2398     (binaryfunc)bpy_bmelem_subscript,        /* mp_subscript */
2399     (objobjargproc)bpy_bmelem_ass_subscript, /* mp_ass_subscript */
2400 };
2401
2402 /* Iterator
2403  * -------- */
2404
2405 static PyObject *bpy_bmelemseq_iter(BPy_BMElemSeq *self)
2406 {
2407         BPy_BMIter *py_iter;
2408
2409         BPY_BM_CHECK_OBJ(self);
2410         py_iter = (BPy_BMIter *)BPy_BMIter_CreatePyObject(self->bm);
2411         BM_iter_init(&(py_iter->iter), self->bm, self->itype, self->py_ele ? self->py_ele->ele : NULL);
2412         return (PyObject *)py_iter;
2413 }
2414
2415 static PyObject *bpy_bmiter_next(BPy_BMIter *self)
2416 {
2417         BMHeader *ele = BM_iter_step(&self->iter);
2418         if (ele == NULL) {
2419                 PyErr_SetString(PyExc_StopIteration,
2420                                 "bpy_bmiter_next stop");
2421                 return NULL;
2422         }
2423         else {
2424                 return (PyObject *)BPy_BMElem_CreatePyObject(self->bm, ele);
2425         }
2426 }
2427
2428
2429 /* Dealloc Functions
2430  * ================= */
2431
2432 static void bpy_bmesh_dealloc(BPy_BMesh *self)
2433 {
2434         BMesh *bm = self->bm;
2435
2436         /* have have been freed by bmesh */
2437         if (bm) {
2438                 BM_data_layer_free(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2439                 BM_data_layer_free(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2440                 BM_data_layer_free(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2441                 BM_data_layer_free(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2442
2443                 bm->py_handle = NULL;
2444
2445                 if ((self->flag & BPY_BMFLAG_IS_WRAPPED) == 0) {
2446                         BM_mesh_free(bm);
2447                 }
2448         }
2449
2450         PyObject_DEL(self);
2451 }
2452
2453 static void bpy_bmvert_dealloc(BPy_BMElem *self)
2454 {
2455         BMesh *bm = self->bm;
2456         if (bm) {
2457                 void **ptr = CustomData_bmesh_get(&bm->vdata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2458                 *ptr = NULL;
2459         }
2460         PyObject_DEL(self);
2461 }
2462
2463 static void bpy_bmedge_dealloc(BPy_BMElem *self)
2464 {
2465         BMesh *bm = self->bm;
2466         if (bm) {
2467                 void **ptr = CustomData_bmesh_get(&bm->edata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2468                 *ptr = NULL;
2469         }
2470         PyObject_DEL(self);
2471 }
2472
2473 static void bpy_bmface_dealloc(BPy_BMElem *self)
2474 {
2475         BMesh *bm = self->bm;
2476         if (bm) {
2477                 void **ptr = CustomData_bmesh_get(&bm->pdata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2478                 *ptr = NULL;
2479         }
2480         PyObject_DEL(self);
2481 }
2482
2483 static void bpy_bmloop_dealloc(BPy_BMElem *self)
2484 {
2485         BMesh *bm = self->bm;
2486         if (bm) {
2487                 void **ptr = CustomData_bmesh_get(&bm->ldata, self->ele->head.data, CD_BM_ELEM_PYPTR);
2488                 *ptr = NULL;
2489         }
2490         PyObject_DEL(self);
2491 }
2492
2493 static void bpy_bmelemseq_dealloc(BPy_BMElemSeq *self)
2494 {
2495         Py_XDECREF(self->py_ele);
2496
2497         PyObject_DEL(self);
2498 }
2499
2500 /* not sure where this should go */
2501 static Py_hash_t bpy_bm_elem_hash(PyObject *self)
2502 {
2503         return _Py_HashPointer(((BPy_BMElem *)self)->ele);
2504 }
2505
2506 static Py_hash_t bpy_bm_hash(PyObject *self)
2507 {
2508         return _Py_HashPointer(((BPy_BMesh *)self)->bm);
2509 }
2510
2511 /* Type Docstrings
2512  * =============== */
2513
2514 PyDoc_STRVAR(bpy_bmesh_doc,
2515 "The BMesh data structure\n"
2516 );
2517 PyDoc_STRVAR(bpy_bmvert_doc,
2518 "The BMesh vertex type\n"
2519 );
2520 PyDoc_STRVAR(bpy_bmedge_doc,
2521 "The BMesh edge connecting 2 verts\n"
2522 );
2523 PyDoc_STRVAR(bpy_bmface_doc,
2524 "The BMesh face with 3 or more sides\n"
2525 );
2526 PyDoc_STRVAR(bpy_bmloop_doc,
2527 "This is normally accessed from :class:`BMFace.loops` where each face corner represents a corner of a face.\n"
2528 );
2529 PyDoc_STRVAR(bpy_bmelemseq_doc,
2530 "General sequence type used for accessing any sequence of \n"
2531 ":class:`BMVert`, :class:`BMEdge`, :class:`BMFace`, :class:`BMLoop`.\n"
2532 "\n"
2533 "When accessed via :class:`BMesh.verts`, :class:`BMesh.edges`, :class:`BMesh.faces` \n"
2534 "there are also functions to create/remomove items.\n"
2535 );
2536 PyDoc_STRVAR(bpy_bmiter_doc,
2537 "Internal BMesh type for looping over verts/faces/edges,\n"
2538 "used for iterating over :class:`BMElemSeq` types.\n"
2539 );
2540
2541 static PyObject *bpy_bmesh_repr(BPy_BMesh *self)
2542 {
2543         BMesh *bm = self->bm;
2544
2545         if (bm) {
2546                 return PyUnicode_FromFormat("<BMesh(%p), totvert=%d, totedge=%d, totface=%d, totloop=%d>",
2547                                             bm, bm->totvert, bm->totedge, bm->totface, bm->totloop);
2548         }
2549         else {
2550                 return PyUnicode_FromFormat("<BMesh dead at %p>", self);
2551         }
2552 }
2553
2554 static PyObject *bpy_bmvert_repr(BPy_BMVert *self)
2555 {
2556         BMesh *bm = self->bm;
2557
2558         if (bm) {
2559                 BMVert *v = self->v;
2560                 return PyUnicode_FromFormat("<BMVert(%p), index=%d>",
2561                                             v, BM_elem_index_get(v));
2562         }
2563         else {
2564                 return PyUnicode_FromFormat("<BMVert dead at %p>", self);
2565         }
2566 }
2567
2568 static PyObject *bpy_bmedge_repr(BPy_BMEdge *self)
2569 {
2570         BMesh *bm = self->bm;
2571
2572         if (bm) {
2573                 BMEdge *e = self->e;
2574                 return PyUnicode_FromFormat("<BMEdge(%p), index=%d, verts=(%p/%d, %p/%d)>",
2575                                             e, BM_elem_index_get(e),
2576                                             e->v1, BM_elem_index_get(e->v1),
2577                                             e->v2, BM_elem_index_get(e->v2));
2578         }
2579         else {
2580                 return PyUnicode_FromFormat("<BMEdge dead at %p>", self);
2581         }
2582 }
2583
2584 static PyObject *bpy_bmface_repr(BPy_BMFace *self)
2585 {
2586         BMesh *bm = self->bm;
2587
2588         if (bm) {
2589                 BMFace *f = self->f;
2590                 return PyUnicode_FromFormat("<BMFace(%p), index=%d, totverts=%d>",
2591                                             f, BM_elem_index_get(f),
2592                                             f->len);
2593         }
2594         else {
2595                 return PyUnicode_FromFormat("<BMFace dead at %p>", self);
2596         }
2597 }
2598
2599 static PyObject *bpy_bmloop_repr(BPy_BMLoop *self)
2600 {
2601         BMesh *bm = self->bm;
2602
2603         if (bm) {
2604                 BMLoop *l = self->l;
2605                 return PyUnicode_FromFormat("<BMLoop(%p), index=%d, vert=%p/%d, edge=%p/%d, face=%p/%d>",
2606                                             l, BM_elem_index_get(l),
2607                                             l->v, BM_elem_index_get(l->v),
2608                                             l->e, BM_elem_index_get(l->e),
2609                                             l->f, BM_elem_index_get(l->f));
2610         }
2611         else {
2612                 return PyUnicode_FromFormat("<BMLoop dead at %p>", self);
2613         }
2614 }
2615
2616 /* Types
2617  * ===== */
2618
2619 PyTypeObject BPy_BMesh_Type     = {{{0}}};
2620 PyTypeObject BPy_BMVert_Type    = {{{0}}};
2621 PyTypeObject BPy_BMEdge_Type    = {{{0}}};
2622 PyTypeObject BPy_BMFace_Type    = {{{0}}};
2623 PyTypeObject BPy_BMLoop_Type    = {{{0}}};
2624 PyTypeObject BPy_BMElemSeq_Type = {{{0}}};
2625 PyTypeObject BPy_BMVertSeq_Type = {{{0}}};
2626 PyTypeObject BPy_BMEdgeSeq_Type = {{{0}}};
2627 PyTypeObject BPy_BMFaceSeq_Type = {{{0}}};
2628 PyTypeObject BPy_BMLoopSeq_Type = {{{0}}};
2629 PyTypeObject BPy_BMIter_Type    = {{{0}}};
2630
2631
2632
2633 void BPy_BM_init_types(void)
2634 {
2635         BPy_BMesh_Type.tp_basicsize     = sizeof(BPy_BMesh);
2636         BPy_BMVert_Type.tp_basicsize    = sizeof(BPy_BMVert);
2637         BPy_BMEdge_Type.tp_basicsize    = sizeof(BPy_BMEdge);
2638         BPy_BMFace_Type.tp_basicsize    = sizeof(BPy_BMFace);
2639         BPy_BMLoop_Type.tp_basicsize    = sizeof(BPy_BMLoop);
2640         BPy_BMElemSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2641         BPy_BMVertSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2642         BPy_BMEdgeSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2643         BPy_BMFaceSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2644         BPy_BMLoopSeq_Type.tp_basicsize = sizeof(BPy_BMElemSeq);
2645         BPy_BMIter_Type.tp_basicsize    = sizeof(BPy_BMIter);
2646
2647
2648         BPy_BMesh_Type.tp_name     = "BMesh";
2649         BPy_BMVert_Type.tp_name    = "BMVert";
2650         BPy_BMEdge_Type.tp_name    = "BMEdge";
2651         BPy_BMFace_Type.tp_name    = "BMFace";
2652         BPy_BMLoop_Type.tp_name    = "BMLoop";
2653         BPy_BMElemSeq_Type.tp_name = "BMElemSeq";
2654         BPy_BMVertSeq_Type.tp_name = "BMVertSeq";
2655         BPy_BMEdgeSeq_Type.tp_name = "BMEdgeSeq";
2656         BPy_BMFaceSeq_Type.tp_name = "BMFaceSeq";
2657         BPy_BMLoopSeq_Type.tp_name = "BMLoopSeq";
2658         BPy_BMIter_Type.tp_name    = "BMIter";
2659
2660
2661         BPy_BMesh_Type.tp_doc     = bpy_bmesh_doc;
2662         BPy_BMVert_Type.tp_doc    = bpy_bmvert_doc;
2663         BPy_BMEdge_Type.tp_doc    = bpy_bmedge_doc;
2664         BPy_BMFace_Type.tp_doc    = bpy_bmface_doc;
2665         BPy_BMLoop_Type.tp_doc    = bpy_bmloop_doc;
2666         BPy_BMElemSeq_Type.tp_doc = bpy_bmelemseq_doc;
2667         BPy_BMVertSeq_Type.tp_doc = NULL;
2668         BPy_BMEdgeSeq_Type.tp_doc = NULL;
2669         BPy_BMFaceSeq_Type.tp_doc = NULL;
2670         BPy_BMLoopSeq_Type.tp_doc = NULL;
2671         BPy_BMIter_Type.tp_doc    = bpy_bmiter_doc;
2672
2673
2674         BPy_BMesh_Type.tp_repr     = (reprfunc)bpy_bmesh_repr;
2675         BPy_BMVert_Type.tp_repr    = (reprfunc)bpy_bmvert_repr;
2676         BPy_BMEdge_Type.tp_repr    = (reprfunc)bpy_bmedge_repr;
2677         BPy_BMFace_Type.tp_repr    = (reprfunc)bpy_bmface_repr;
2678         BPy_BMLoop_Type.tp_repr    = (reprfunc)bpy_bmloop_repr;
2679         BPy_BMElemSeq_Type.tp_repr = NULL;
2680         BPy_BMVertSeq_Type.tp_repr = NULL;
2681         BPy_BMEdgeSeq_Type.tp_repr = NULL;
2682         BPy_BMFaceSeq_Type.tp_repr = NULL;
2683         BPy_BMLoopSeq_Type.tp_repr = NULL;
2684         BPy_BMIter_Type.tp_repr    = NULL;
2685
2686
2687         BPy_BMesh_Type.tp_getset     = bpy_bmesh_getseters;
2688         BPy_BMVert_Type.tp_getset    = bpy_bmvert_getseters;
2689         BPy_BMEdge_Type.tp_getset    = bpy_bmedge_getseters;
2690         BPy_BMFace_Type.tp_getset    = bpy_bmface_getseters;
2691         BPy_BMLoop_Type.tp_getset    = bpy_bmloop_getseters;
2692         BPy_BMElemSeq_Type.tp_getset = NULL;
2693         BPy_BMVertSeq_Type.tp_getset = bpy_bmvertseq_getseters;
2694         BPy_BMEdgeSeq_Type.tp_getset = bpy_bmedgeseq_getseters;
2695         BPy_BMFaceSeq_Type.tp_getset = bpy_bmfaceseq_getseters;
2696         BPy_BMLoopSeq_Type.tp_getset = bpy_bmloopseq_getseters;
2697         BPy_BMIter_Type.tp_getset    = NULL;
2698
2699
2700         BPy_BMesh_Type.tp_methods     = bpy_bmesh_methods;
2701         BPy_BMVert_Type.tp_methods    = bpy_bmvert_methods;
2702         BPy_BMEdge_Type.tp_methods    = bpy_bmedge_methods;
2703         BPy_BMFace_Type.tp_methods    = bpy_bmface_methods;
2704         BPy_BMLoop_Type.tp_methods    = bpy_bmloop_methods;
2705         BPy_BMElemSeq_Type.tp_methods = bpy_bmelemseq_methods;
2706         BPy_BMVertSeq_Type.tp_methods = bpy_bmvertseq_methods;
2707         BPy_BMEdgeSeq_Type.tp_methods = bpy_bmedgeseq_methods;
2708         BPy_BMFaceSeq_Type.tp_methods = bpy_bmfaceseq_methods;
2709         BPy_BMLoopSeq_Type.tp_methods = bpy_bmloopseq_methods;
2710         BPy_BMIter_Type.tp_methods    = NULL;
2711
2712
2713         BPy_BMesh_Type.tp_hash     = bpy_bm_hash;
2714         BPy_BMVert_Type.tp_hash    = bpy_bm_elem_hash;
2715         BPy_BMEdge_Type.tp_hash    = bpy_bm_elem_hash;
2716         BPy_BMFace_Type.tp_hash    = bpy_bm_elem_hash;
2717         BPy_BMLoop_Type.tp_hash    = bpy_bm_elem_hash;
2718         BPy_BMElemSeq_Type.tp_hash = NULL;
2719         BPy_BMVertSeq_Type.tp_hash = NULL;
2720         BPy_BMEdgeSeq_Type.tp_hash = NULL;
2721         BPy_BMFaceSeq_Type.tp_hash = NULL;
2722         BPy_BMLoopSeq_Type.tp_hash = NULL;
2723         BPy_BMIter_Type.tp_hash    = NULL;
2724
2725         BPy_BMElemSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2726         BPy_BMVertSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2727         BPy_BMEdgeSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2728         BPy_BMFaceSeq_Type.tp_as_sequence = &bpy_bmelemseq_as_sequence;
2729         BPy_BMLoopSeq_Type.tp_as_sequence = NULL; /* this is not a seq really, only for layer access */
2730
2731         BPy_BMElemSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2732         BPy_BMVertSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2733         BPy_BMEdgeSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2734         BPy_BMFaceSeq_Type.tp_as_mapping = &bpy_bmelemseq_as_mapping;
2735         BPy_BMLoopSeq_Type.tp_as_mapping = NULL; /* this is not a seq really, only for layer access */
2736
2737         /* layer access */
2738         BPy_BMVert_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2739         BPy_BMEdge_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2740         BPy_BMFace_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2741         BPy_BMLoop_Type.tp_as_mapping    = &bpy_bm_elem_as_mapping;
2742
2743         BPy_BMElemSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2744         BPy_BMVertSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2745         BPy_BMEdgeSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2746         BPy_BMFaceSeq_Type.tp_iter = (getiterfunc)bpy_bmelemseq_iter;
2747         BPy_BMLoopSeq_Type.tp_iter = NULL; /* no mapping */
2748
2749         /* only 1 iteratir so far */
2750         BPy_BMIter_Type.tp_iternext = (iternextfunc)bpy_bmiter_next;
2751         BPy_BMIter_Type.tp_iter     = PyObject_SelfIter;
2752
2753         BPy_BMesh_Type.tp_dealloc     = (destructor)bpy_bmesh_dealloc;
2754         BPy_BMVert_Type.tp_dealloc    = (destructor)bpy_bmvert_dealloc;
2755         BPy_BMEdge_Type.tp_dealloc    = (destructor)bpy_bmedge_dealloc;
2756         BPy_BMFace_Type.tp_dealloc    = (destructor)bpy_bmface_dealloc;
2757         BPy_BMLoop_Type.tp_dealloc    = (destructor)bpy_bmloop_dealloc;
2758         BPy_BMElemSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2759         BPy_BMVertSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2760         BPy_BMEdgeSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2761         BPy_BMFaceSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2762         BPy_BMLoopSeq_Type.tp_dealloc = (destructor)bpy_bmelemseq_dealloc;
2763         BPy_BMIter_Type.tp_dealloc    = NULL;
2764
2765         BPy_BMesh_Type.tp_flags     = Py_TPFLAGS_DEFAULT;
2766         BPy_BMVert_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2767         BPy_BMEdge_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2768         BPy_BMFace_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2769         BPy_BMLoop_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2770         BPy_BMElemSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2771         BPy_BMVertSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2772         BPy_BMEdgeSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2773         BPy_BMFaceSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2774         BPy_BMLoopSeq_Type.tp_flags = Py_TPFLAGS_DEFAULT;
2775         BPy_BMIter_Type.tp_flags    = Py_TPFLAGS_DEFAULT;
2776
2777
2778         PyType_Ready(&BPy_BMesh_Type);
2779         PyType_Ready(&BPy_BMVert_Type);
2780         PyType_Ready(&BPy_BMEdge_Type);
2781         PyType_Ready(&BPy_BMFace_Type);
2782         PyType_Ready(&BPy_BMLoop_Type);
2783         PyType_Ready(&BPy_BMElemSeq_Type);
2784         PyType_Ready(&BPy_BMVertSeq_Type);
2785         PyType_Ready(&BPy_BMEdgeSeq_Type);
2786         PyType_Ready(&BPy_BMFaceSeq_Type);
2787         PyType_Ready(&BPy_BMLoopSeq_Type);
2788         PyType_Ready(&BPy_BMIter_Type);
2789 }
2790
2791 /* bmesh.types submodule
2792  * ********************* */
2793
2794 static struct PyModuleDef BPy_BM_types_module_def = {
2795     PyModuleDef_HEAD_INIT,
2796     "bmesh.types",  /* m_name */
2797     NULL,  /* m_doc */
2798     0,  /* m_size */
2799     NULL,  /* m_methods */
2800     NULL,  /* m_reload */
2801     NULL,  /* m_traverse */
2802     NULL,  /* m_clear */
2803     NULL,  /* m_free */
2804 };
2805
2806 PyObject *BPyInit_bmesh_types(void)
2807 {
2808         PyObject *submodule;
2809
2810         submodule = PyModule_Create(&BPy_BM_types_module_def);
2811
2812 #define MODULE_TYPE_ADD(s, t) \
2813         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
2814
2815         /* bmesh_py_types.c */
2816         MODULE_TYPE_ADD(submodule, BPy_BMesh_Type);
2817         MODULE_TYPE_ADD(submodule, BPy_BMVert_Type);
2818         MODULE_TYPE_ADD(submodule, BPy_BMEdge_Type);
2819         MODULE_TYPE_ADD(submodule, BPy_BMFace_Type);
2820         MODULE_TYPE_ADD(submodule, BPy_BMLoop_Type);
2821         MODULE_TYPE_ADD(submodule, BPy_BMElemSeq_Type);
2822         MODULE_TYPE_ADD(submodule, BPy_BMVertSeq_Type);
2823         MODULE_TYPE_ADD(submodule, BPy_BMEdgeSeq_Type);
2824         MODULE_TYPE_ADD(submodule, BPy_BMFaceSeq_Type);
2825         MODULE_TYPE_ADD(submodule, BPy_BMLoopSeq_Type);
2826         MODULE_TYPE_ADD(submodule, BPy_BMIter_Type);
2827         /* bmesh_py_types_select.c */
2828         MODULE_TYPE_ADD(submodule, BPy_BMEditSelSeq_Type);
2829         MODULE_TYPE_ADD(submodule, BPy_BMEditSelIter_Type);
2830         /* bmesh_py_types_customdata.c */
2831         MODULE_TYPE_ADD(submodule, BPy_BMLayerAccessVert_Type);
2832         MODULE_TYPE_ADD(submodule, BPy_BMLayerAccessEdge_Type);
2833         MODULE_TYPE_ADD(submodule, BPy_BMLayerAccessFace_Type);
2834         MODULE_TYPE_ADD(submodule, BPy_BMLayerAccessLoop_Type);
2835         MODULE_TYPE_ADD(submodule, BPy_BMLayerCollection_Type);
2836         MODULE_TYPE_ADD(submodule, BPy_BMLayerItem_Type);
2837         /* bmesh_py_types_meshdata.c */
2838         MODULE_TYPE_ADD(submodule, BPy_BMLoopUV_Type);
2839         MODULE_TYPE_ADD(submodule, BPy_BMDeformVert_Type);
2840
2841 #undef MODULE_TYPE_ADD
2842
2843         return submodule;
2844 }
2845
2846 /* Utility Functions
2847  * ***************** */
2848
2849 PyObject *BPy_BMesh_CreatePyObject(BMesh *bm, int flag)
2850 {
2851         BPy_BMesh *self;
2852
2853         if (bm->py_handle) {
2854                 self = bm->py_handle;
2855                 Py_INCREF(self);
2856         }
2857         else {
2858                 self = PyObject_New(BPy_BMesh, &BPy_BMesh_Type);
2859                 self->bm = bm;
2860                 self->flag = flag;
2861
2862                 bm->py_handle = self; /* point back */
2863
2864                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2865                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2866                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2867                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2868         }
2869
2870         return (PyObject *)self;
2871 }
2872
2873
2874
2875 PyObject *BPy_BMVert_CreatePyObject(BMesh *bm, BMVert *v)
2876 {
2877         BPy_BMVert *self;
2878
2879         void **ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
2880
2881         /* bmesh may free layers, ensure we have one to store ourself */
2882         if (UNLIKELY(ptr == NULL)) {
2883                 BM_data_layer_add(bm, &bm->vdata, CD_BM_ELEM_PYPTR);
2884                 ptr = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BM_ELEM_PYPTR);
2885         }
2886
2887         if (*ptr != NULL) {
2888                 self = *ptr;
2889                 Py_INCREF(self);
2890         }
2891         else {
2892                 self = PyObject_New(BPy_BMVert, &BPy_BMVert_Type);
2893                 BLI_assert(v != NULL);
2894                 self->bm = bm;
2895                 self->v  = v;
2896                 *ptr = self;
2897         }
2898         return (PyObject *)self;
2899 }
2900
2901 PyObject *BPy_BMEdge_CreatePyObject(BMesh *bm, BMEdge *e)
2902 {
2903         BPy_BMEdge *self;
2904
2905         void **ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
2906
2907         /* bmesh may free layers, ensure we have one to store ourself */
2908         if (UNLIKELY(ptr == NULL)) {
2909                 BM_data_layer_add(bm, &bm->edata, CD_BM_ELEM_PYPTR);
2910                 ptr = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BM_ELEM_PYPTR);
2911         }
2912
2913         if (*ptr != NULL) {
2914                 self = *ptr;
2915                 Py_INCREF(self);
2916         }
2917         else {
2918                 self = PyObject_New(BPy_BMEdge, &BPy_BMEdge_Type);
2919                 BLI_assert(e != NULL);
2920                 self->bm = bm;
2921                 self->e  = e;
2922                 *ptr = self;
2923         }
2924         return (PyObject *)self;
2925 }
2926
2927 PyObject *BPy_BMFace_CreatePyObject(BMesh *bm, BMFace *f)
2928 {
2929         BPy_BMFace *self;
2930
2931         void **ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
2932
2933         /* bmesh may free layers, ensure we have one to store ourself */
2934         if (UNLIKELY(ptr == NULL)) {
2935                 BM_data_layer_add(bm, &bm->pdata, CD_BM_ELEM_PYPTR);
2936                 ptr = CustomData_bmesh_get(&bm->pdata, f->head.data, CD_BM_ELEM_PYPTR);
2937         }
2938
2939         if (*ptr != NULL) {
2940                 self = *ptr;
2941                 Py_INCREF(self);
2942         }
2943         else {
2944                 self = PyObject_New(BPy_BMFace, &BPy_BMFace_Type);
2945                 BLI_assert(f != NULL);
2946                 self->bm = bm;
2947                 self->f  = f;
2948                 *ptr = self;
2949         }
2950         return (PyObject *)self;
2951 }
2952
2953 PyObject *BPy_BMLoop_CreatePyObject(BMesh *bm, BMLoop *l)
2954 {
2955         BPy_BMLoop *self;
2956
2957         void **ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
2958
2959         /* bmesh may free layers, ensure we have one to store ourself */
2960         if (UNLIKELY(ptr == NULL)) {
2961                 BM_data_layer_add(bm, &bm->ldata, CD_BM_ELEM_PYPTR);
2962                 ptr = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_BM_ELEM_PYPTR);
2963         }
2964
2965         if (*ptr != NULL) {
2966                 self = *ptr;
2967                 Py_INCREF(self);
2968         }
2969         else {
2970                 self = PyObject_New(BPy_BMLoop, &BPy_BMLoop_Type);
2971                 BLI_assert(l != NULL);
2972                 self->bm = bm;
2973                 self->l  = l;
2974                 *ptr = self;
2975         }
2976         return (PyObject *)self;
2977 }
2978
2979 PyObject *BPy_BMElemSeq_CreatePyObject(BMesh *bm, BPy_BMElem *py_ele, const char itype)
2980 {
2981         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMElemSeq_Type);
2982         self->bm = bm;
2983         self->py_ele = py_ele; /* can be NULL */
2984         self->itype = itype;
2985         Py_XINCREF(py_ele);
2986         return (PyObject *)self;
2987 }
2988
2989 PyObject *BPy_BMVertSeq_CreatePyObject(BMesh *bm)
2990 {
2991         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMVertSeq_Type);
2992         self->bm = bm;
2993         self->py_ele = NULL; /* unused */
2994         self->itype = BM_VERTS_OF_MESH;
2995         return (PyObject *)self;
2996 }
2997
2998 PyObject *BPy_BMEdgeSeq_CreatePyObject(BMesh *bm)
2999 {
3000         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMEdgeSeq_Type);
3001         self->bm = bm;
3002         self->py_ele = NULL; /* unused */
3003         self->itype = BM_EDGES_OF_MESH;
3004         return (PyObject *)self;
3005 }
3006
3007 PyObject *BPy_BMFaceSeq_CreatePyObject(BMesh *bm)
3008 {
3009         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMFaceSeq_Type);
3010         self->bm = bm;
3011         self->py_ele = NULL; /* unused */
3012         self->itype = BM_FACES_OF_MESH;
3013         return (PyObject *)self;
3014 }
3015
3016 PyObject *BPy_BMLoopSeq_CreatePyObject(BMesh *bm)
3017 {
3018         BPy_BMElemSeq *self = PyObject_New(BPy_BMElemSeq, &BPy_BMLoopSeq_Type);
3019         self->bm = bm;
3020         self->py_ele = NULL; /* unused */
3021         self->itype = 0; /* should never be passed to the iterator function */
3022         return (PyObject *)self;
3023 }
3024
3025 PyObject *BPy_BMIter_CreatePyObject(BMesh *bm)
3026 {
3027         BPy_BMIter *self = PyObject_New(BPy_BMIter, &BPy_BMIter_Type);
3028         self->bm = bm;
3029         /* caller must initialize 'iter' member */
3030         return (PyObject *)self;
3031 }
3032
3033 /* this is just a helper func */
3034 PyObject *BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele)
3035 {
3036         switch (ele->htype) {
3037                 case BM_VERT:
3038                         return BPy_BMVert_CreatePyObject(bm, (BMVert *)ele);
3039                 case BM_EDGE:
3040                         return BPy_BMEdge_CreatePyObject(bm, (BMEdge *)ele);
3041                 case BM_FACE:
3042                         return BPy_BMFace_CreatePyObject(bm, (BMFace *)ele);
3043                 case BM_LOOP:
3044                         return BPy_BMLoop_CreatePyObject(bm, (BMLoop *)ele);
3045                 default:
3046                         PyErr_SetString(PyExc_SystemError, "internal error");
3047                         return NULL;
3048         }
3049 }
3050
3051 int bpy_bm_generic_valid_check(BPy_BMGeneric *self)
3052 {
3053         if (LIKELY(self->bm)) {
3054                 return 0;
3055         }
3056         else {
3057                 PyErr_Format(PyExc_ReferenceError,
3058                              "BMesh data of type %.200s has been removed",
3059                              Py_TYPE(self)->tp_name);
3060                 return -1;
3061         }
3062 }
3063
3064 void bpy_bm_generic_invalidate(BPy_BMGeneric *self)
3065 {
3066         self->bm = NULL;
3067 }
3068
3069 /* generic python seq as BMVert/Edge/Face array,
3070  * return value must be freed with PyMem_FREE(...);
3071  *
3072  * The 'bm_r' value is assigned when empty, and used when set.
3073  */
3074 void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_size,
3075                                 const char htype,
3076                                 const char do_unique_check, const char do_bm_check,
3077                                 const char *error_prefix)
3078 {
3079         BMesh *bm = (r_bm && *r_bm) ? *r_bm : NULL;
3080         PyObject *seq_fast;
3081         *r_size = 0;
3082
3083         if (!(seq_fast = PySequence_Fast(seq, error_prefix))) {
3084                 return NULL;
3085         }
3086         else {
3087                 Py_ssize_t seq_len;
3088                 Py_ssize_t i;
3089
3090                 BPy_BMElem *item;
3091                 BMElem **alloc;
3092
3093                 seq_len = PySequence_Fast_GET_SIZE(seq_fast);
3094
3095                 if (seq_len < min || seq_len > max) {
3096                         PyErr_Format(PyExc_TypeError,
3097                                      "%s: sequence incorrect size, expected [%d - %d], given %d",
3098                                      error_prefix, min, max, seq_len);
3099                         return NULL;
3100                 }
3101
3102
3103                 /* from now on, use goto */
3104                 alloc = PyMem_MALLOC(seq_len * sizeof(BPy_BMElem **));
3105
3106                 for (i = 0; i < seq_len; i++) {
3107                         item = (BPy_BMElem *)PySequence_Fast_GET_ITEM(seq_fast, i);
3108
3109                         if (!BPy_BMElem_CheckHType(Py_TYPE(item), htype)) {
3110                                 PyErr_Format(PyExc_TypeError,
3111                                              "%s: expected %.200s, not '%.200s'",
3112                                              error_prefix, BPy_BMElem_StringFromHType(htype), Py_TYPE(item)->tp_name);
3113                                 goto err_cleanup;
3114                         }
3115                         else if (!BPY_BM_IS_VALID(item)) {
3116                                 PyErr_Format(PyExc_TypeError,
3117                                              "%s: %d %s has been removed",
3118                                              error_prefix, i, Py_TYPE(item)->tp_name);
3119                                 goto err_cleanup;
3120                         }
3121                         /* trick so we can ensure all items have the same mesh,
3122                          * and allows us to pass the 'bm' as NULL. */
3123                         else if (do_bm_check && (bm  && bm != item->bm)) {
3124                                 PyErr_Format(PyExc_ValueError,
3125                                              "%s: %d %s is from another mesh",
3126                                              error_prefix, i, BPy_BMElem_StringFromHType(htype));
3127                                 goto err_cleanup;
3128                         }
3129
3130                         if (bm == NULL) {
3131                                 bm = item->bm;
3132                         }
3133
3134                         alloc[i] = item->ele;
3135
3136                         if (do_unique_check) {
3137                                 BM_elem_flag_enable(item->ele, BM_ELEM_INTERNAL_TAG);
3138                         }
3139                 }
3140
3141                 if (do_unique_check) {
3142                         /* check for double verts! */
3143                         int ok = TRUE;
3144                         for (i = 0; i < seq_len; i++) {
3145                                 if (UNLIKELY(BM_elem_flag_test(alloc[i], BM_ELEM_INTERNAL_TAG) == FALSE)) {
3146                                         ok = FALSE;
3147                                 }
3148
3149                                 /* ensure we don't leave this enabled */
3150                                 BM_elem_flag_disable(alloc[i], BM_ELEM_INTERNAL_TAG);
3151                         }
3152
3153                         if (ok == FALSE) {
3154                                 PyErr_Format(PyExc_ValueError,
3155                                              "%s: found the same %.200s used multiple times",
3156                                              error_prefix, BPy_BMElem_StringFromHType(htype));
3157                                 goto err_cleanup;
3158                         }
3159                 }
3160
3161                 Py_DECREF(seq_fast);
3162                 *r_size = seq_len;
3163                 if (r_bm) *r_bm = bm;
3164                 return alloc;
3165
3166 err_cleanup:
3167                 Py_DECREF(seq_fast);
3168                 PyMem_FREE(alloc);
3169                 return NULL;
3170         }
3171 }
3172
3173
3174 PyObject *BPy_BMElem_Array_As_Tuple(BMesh *bm, BMHeader **elem, Py_ssize_t elem_len)
3175 {
3176         Py_ssize_t i;
3177         PyObject *ret = PyTuple_New(elem_len);
3178         for (i = 0; i < elem_len; i++) {
3179                 PyTuple_SET_ITEM(ret, i, BPy_BMElem_CreatePyObject(bm, elem[i]));
3180         }
3181
3182         return ret;
3183 }
3184
3185 int BPy_BMElem_CheckHType(PyTypeObject *type, const char htype)
3186 {
3187         return (((htype & BM_VERT) && (type == &BPy_BMVert_Type)) ||
3188                 ((htype & BM_EDGE) && (type == &BPy_BMEdge_Type)) ||
3189                 ((htype & BM_FACE) && (type == &BPy_BMFace_Type)) ||
3190                 ((htype & BM_LOOP) && (type == &BPy_BMLoop_Type)));
3191 }
3192
3193 /**
3194  * Use for error strings only, not thread safe,
3195  *
3196  * \return a sting like '(BMVert/BMEdge/BMFace/BMLoop)'
3197  */
3198 char *BPy_BMElem_StringFromHType_ex(const char htype, char ret[32])
3199 {
3200         /* zero to ensure string is always NULL terminated */
3201         char *ret_ptr = ret;
3202         if (htype & BM_VERT) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMVert_Type.tp_name);
3203         if (htype & BM_EDGE) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMEdge_Type.tp_name);
3204         if (htype & BM_FACE) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMFace_Type.tp_name);
3205         if (htype & BM_LOOP) ret_ptr += sprintf(ret_ptr, "/%s", BPy_BMLoop_Type.tp_name);
3206         ret[0]   = '(';
3207         *ret_ptr = ')';
3208         return ret;
3209 }
3210 char *BPy_BMElem_StringFromHType(const char htype)
3211 {
3212         /* zero to ensure string is always NULL terminated */
3213         static char ret[32];
3214         return BPy_BMElem_StringFromHType_ex(htype, ret);
3215 }