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