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