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