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