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