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