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