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