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