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