Blender V5.3
bmesh_py_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include "BLI_math_base_c.hh"
15#include "BLI_utildefines.hh"
16
17#include "MEM_guardedalloc.h"
18
20
21#include "bmesh.hh"
22#include "bmesh_py_types.hh"
23#include "bmesh_py_utils.hh" /* own include */
24
28
29namespace blender {
30
32 /* Wrap. */
33 bpy_bm_utils_vert_collapse_edge_doc,
34 ".. function:: vert_collapse_edge(vert, edge)\n"
35 "\n"
36 " Collapse a vertex into an edge.\n"
37 "\n"
38 " :param vert: The vert that will be collapsed.\n"
39 " :type vert: :class:`bmesh.types.BMVert`\n"
40 " :param edge: The edge to collapse into.\n"
41 " :type edge: :class:`bmesh.types.BMEdge`\n"
42 " :return: The resulting edge from the collapse operation.\n"
43 " :rtype: :class:`bmesh.types.BMEdge`\n");
44static PyObject *bpy_bm_utils_vert_collapse_edge(PyObject * /*self*/, PyObject *args)
45{
46 BPy_BMEdge *py_edge;
47 BPy_BMVert *py_vert;
48
49 BMesh *bm;
50 BMEdge *e_new = nullptr;
51
52 if (!PyArg_ParseTuple(args,
53 "O!" /* `vert` */
54 "O!" /* `edge` */
55 ":vert_collapse_edge",
57 &py_vert,
59 &py_edge))
60 {
61 return nullptr;
62 }
63
64 BPY_BM_CHECK_OBJ(py_edge);
65 BPY_BM_CHECK_OBJ(py_vert);
66
67 /* this doubles for checking that the verts are in the same mesh */
68 if (!(py_edge->e->v1 == py_vert->v || py_edge->e->v2 == py_vert->v)) {
69 PyErr_SetString(PyExc_ValueError,
70 "vert_collapse_edge(vert, edge): the vertex is not found in the edge");
71 return nullptr;
72 }
73
74 if (BM_vert_edge_count_is_over(py_vert->v, 2)) {
75 PyErr_SetString(PyExc_ValueError,
76 "vert_collapse_edge(vert, edge): vert has more than 2 connected edges");
77 return nullptr;
78 }
79
80 bm = py_edge->bm;
81
82 e_new = BM_vert_collapse_edge(bm, py_edge->e, py_vert->v, true, true, true);
83
84 if (e_new) {
85 return BPy_BMEdge_CreatePyObject(bm, e_new);
86 }
87
88 PyErr_SetString(PyExc_ValueError,
89 "vert_collapse_edge(vert, edge): no new edge created, internal error");
90 return nullptr;
91}
92
94 /* Wrap. */
95 bpy_bm_utils_vert_collapse_faces_doc,
96 ".. function:: vert_collapse_faces(vert, edge, fac, join_faces)\n"
97 "\n"
98 " Collapses a vertex that has only two manifold edges onto a vertex it shares an "
99 "edge with.\n"
100 "\n"
101 " :param vert: The vert that will be collapsed.\n"
102 " :type vert: :class:`bmesh.types.BMVert`\n"
103 " :param edge: The edge to collapse into.\n"
104 " :type edge: :class:`bmesh.types.BMEdge`\n"
105 " :param fac: The factor to use when merging customdata [0 - 1].\n"
106 " :type fac: float\n"
107 " :param join_faces: When true the faces around the vertex will be joined otherwise "
108 "collapse the vertex by merging the 2 edges this vertex connects to into one.\n"
109 " :type join_faces: bool\n"
110 " :return: The resulting edge from the collapse operation.\n"
111 " :rtype: :class:`bmesh.types.BMEdge`\n");
112static PyObject *bpy_bm_utils_vert_collapse_faces(PyObject * /*self*/, PyObject *args)
113{
114 BPy_BMEdge *py_edge;
115 BPy_BMVert *py_vert;
116
117 float fac;
118 int do_join_faces;
119
120 BMesh *bm;
121 BMEdge *e_new = nullptr;
122
123 if (!PyArg_ParseTuple(args,
124 "O!" /* `vert` */
125 "O!" /* `edge` */
126 "f" /* `fac` */
127 "i" /* `join_faces` */
128 ":vert_collapse_faces",
130 &py_vert,
132 &py_edge,
133 &fac,
134 &do_join_faces))
135 {
136 return nullptr;
137 }
138
139 BPY_BM_CHECK_OBJ(py_edge);
140 BPY_BM_CHECK_OBJ(py_vert);
141
142 /* this doubles for checking that the verts are in the same mesh */
143 if (!(py_edge->e->v1 == py_vert->v || py_edge->e->v2 == py_vert->v)) {
144 PyErr_SetString(PyExc_ValueError,
145 "vert_collapse_faces(vert, edge): the vertex is not found in the edge");
146 return nullptr;
147 }
148
149 if (BM_vert_edge_count_is_over(py_vert->v, 2)) {
150 PyErr_SetString(PyExc_ValueError,
151 "vert_collapse_faces(vert, edge): vert has more than 2 connected edges");
152 return nullptr;
153 }
154
155 bm = py_edge->bm;
156
158 bm, py_edge->e, py_vert->v, clamp_f(fac, 0.0f, 1.0f), true, do_join_faces, true, true);
159
160 if (e_new) {
161 return BPy_BMEdge_CreatePyObject(bm, e_new);
162 }
163
164 PyErr_SetString(PyExc_ValueError,
165 "vert_collapse_faces(vert, edge): no new edge created, internal error");
166 return nullptr;
167}
168
170 /* Wrap. */
171 bpy_bm_utils_vert_dissolve_doc,
172 ".. function:: vert_dissolve(vert)\n"
173 "\n"
174 " Dissolve this vertex (will be removed).\n"
175 "\n"
176 " :param vert: The vert to be dissolved.\n"
177 " :type vert: :class:`bmesh.types.BMVert`\n"
178 " :return: True when the vertex dissolve is successful.\n"
179 " :rtype: bool\n");
180static PyObject *bpy_bm_utils_vert_dissolve(PyObject * /*self*/, PyObject *args)
181{
182 BPy_BMVert *py_vert;
183
184 BMesh *bm;
185
186 if (!PyArg_ParseTuple(args,
187 "O!" /* `vert` */
188 ":vert_dissolve",
190 &py_vert))
191 {
192 return nullptr;
193 }
194
195 BPY_BM_CHECK_OBJ(py_vert);
196
197 bm = py_vert->bm;
198
199 return PyBool_FromLong(BM_vert_dissolve(bm, py_vert->v));
200}
201
203 /* Wrap. */
204 bpy_bm_utils_vert_splice_doc,
205 ".. function:: vert_splice(vert, vert_target)\n"
206 "\n"
207 " Splice vert into vert_target, merging them.\n"
208 "\n"
209 " :param vert: The vertex to be removed.\n"
210 " :type vert: :class:`bmesh.types.BMVert`\n"
211 " :param vert_target: The vertex to merge into.\n"
212 " :type vert_target: :class:`bmesh.types.BMVert`\n"
213 "\n"
214 " .. note:: The verts mustn't share an edge or face.\n");
215static PyObject *bpy_bm_utils_vert_splice(PyObject * /*self*/, PyObject *args)
216{
217 const char *error_prefix = "vert_splice(...)";
218 BPy_BMVert *py_vert;
219 BPy_BMVert *py_vert_target;
220
221 BMesh *bm;
222
223 bool ok;
224
225 if (!PyArg_ParseTuple(args,
226 "O!" /* `vert` */
227 "O!" /* `vert_target` */
228 ":vert_splice",
230 &py_vert,
232 &py_vert_target))
233 {
234 return nullptr;
235 }
236
237 BPY_BM_CHECK_OBJ(py_vert);
238 BPY_BM_CHECK_OBJ(py_vert_target);
239
240 bm = py_vert->bm;
241 BPY_BM_CHECK_SOURCE_OBJ(bm, error_prefix, py_vert_target);
242
243 if (py_vert->v == py_vert_target->v) {
244 PyErr_Format(PyExc_ValueError, "%s: vert arguments match", error_prefix);
245 return nullptr;
246 }
247
248 if (BM_edge_exists(py_vert->v, py_vert_target->v)) {
249 PyErr_Format(PyExc_ValueError, "%s: verts cannot share an edge", error_prefix);
250 return nullptr;
251 }
252
253 if (BM_vert_pair_share_face_check(py_vert->v, py_vert_target->v)) {
254 PyErr_Format(PyExc_ValueError, "%s: verts cannot share a face", error_prefix);
255 return nullptr;
256 }
257
258 /* should always succeed */
259 ok = BM_vert_splice(bm, py_vert_target->v, py_vert->v);
260 BLI_assert(ok == true);
262
263 Py_RETURN_NONE;
264}
265
267 /* Wrap. */
268 bpy_bm_utils_vert_separate_doc,
269 ".. function:: vert_separate(vert, edges)\n"
270 "\n"
271 " Separate this vertex at every edge.\n"
272 "\n"
273 " :param vert: The vert to be separated.\n"
274 " :type vert: :class:`bmesh.types.BMVert`\n"
275 " :param edges: The edges to separate.\n"
276 " :type edges: Sequence[:class:`bmesh.types.BMEdge`]\n"
277 " :return: The newly separated verts (including the vertex passed).\n"
278 " :rtype: tuple[:class:`bmesh.types.BMVert`, ...]\n");
279static PyObject *bpy_bm_utils_vert_separate(PyObject * /*self*/, PyObject *args)
280{
281 const char *error_prefix = "vert_separate(...)";
282 BPy_BMVert *py_vert;
283 PyObject *edge_seq;
284
285 BMesh *bm;
286 BMVert **elem;
287 int elem_len;
288
289 PyObject *ret;
290
291 if (!PyArg_ParseTuple(args,
292 "O!" /* `vert` */
293 "O" /* `edges` */
294 ":vert_separate",
296 &py_vert,
297 &edge_seq))
298 {
299 return nullptr;
300 }
301
302 BPY_BM_CHECK_OBJ(py_vert);
303
304 bm = py_vert->bm;
305
306 /* Edges to split. */
307 Py_ssize_t edge_array_num;
308 BMEdge **edge_array = BPy_BMEdge_PySeq_As_Array(
309 &bm, edge_seq, 0, PY_SSIZE_T_MAX, &edge_array_num, true, true, error_prefix);
310
311 if (edge_array == nullptr) {
312 return nullptr;
313 }
314
315 BM_vert_separate(bm, py_vert->v, edge_array, edge_array_num, false, &elem, &elem_len);
316 /* return collected verts */
317 ret = BPy_BMVert_Array_As_Tuple(bm, elem, elem_len);
318 MEM_delete(elem);
319
320 PyMem_FREE(edge_array);
321
322 return ret;
323}
324
326 /* Wrap. */
327 bpy_bm_utils_edge_split_doc,
328 ".. function:: edge_split(edge, vert, fac)\n"
329 "\n"
330 " Split an edge, return the newly created data.\n"
331 "\n"
332 " :param edge: The edge to split.\n"
333 " :type edge: :class:`bmesh.types.BMEdge`\n"
334 " :param vert: One of the verts on the edge, defines the split direction.\n"
335 " :type vert: :class:`bmesh.types.BMVert`\n"
336 " :param fac: The point on the edge where the new vert will be created [0 - 1].\n"
337 " :type fac: float\n"
338 " :return: The newly created (edge, vert) pair.\n"
339 " :rtype: tuple[:class:`bmesh.types.BMEdge`, :class:`bmesh.types.BMVert`]\n");
340static PyObject *bpy_bm_utils_edge_split(PyObject * /*self*/, PyObject *args)
341{
342 BPy_BMEdge *py_edge;
343 BPy_BMVert *py_vert;
344 float fac;
345
346 BMesh *bm;
347 BMVert *v_new = nullptr;
348 BMEdge *e_new = nullptr;
349
350 if (!PyArg_ParseTuple(args,
351 "O!" /* `edge` */
352 "O!" /* `vert` */
353 "f" /* `fac` */
354 ":edge_split",
356 &py_edge,
358 &py_vert,
359 &fac))
360 {
361 return nullptr;
362 }
363
364 BPY_BM_CHECK_OBJ(py_edge);
365 BPY_BM_CHECK_OBJ(py_vert);
366
367 /* this doubles for checking that the verts are in the same mesh */
368 if (!(py_edge->e->v1 == py_vert->v || py_edge->e->v2 == py_vert->v)) {
369 PyErr_SetString(PyExc_ValueError,
370 "edge_split(edge, vert): the vertex is not found in the edge");
371 return nullptr;
372 }
373
374 bm = py_edge->bm;
375
376 v_new = BM_edge_split(bm, py_edge->e, py_vert->v, &e_new, clamp_f(fac, 0.0f, 1.0f));
377
378 if (v_new && e_new) {
379 PyObject *ret = PyTuple_New(2);
382 return ret;
383 }
384
385 PyErr_SetString(PyExc_ValueError,
386 "edge_split(edge, vert): couldn't split the edge, internal error");
387 return nullptr;
388}
389
391 /* Wrap. */
392 bpy_bm_utils_edge_rotate_doc,
393 ".. function:: edge_rotate(edge, ccw=False)\n"
394 "\n"
395 " Rotate the edge and return the newly created edge.\n"
396 " If rotating the edge fails, None will be returned.\n"
397 "\n"
398 " :param edge: The edge to rotate.\n"
399 " :type edge: :class:`bmesh.types.BMEdge`\n"
400 " :param ccw: When True the edge will be rotated counter clockwise.\n"
401 " :type ccw: bool\n"
402 " :return: The newly rotated edge.\n"
403 " :rtype: :class:`bmesh.types.BMEdge` | None\n");
404static PyObject *bpy_bm_utils_edge_rotate(PyObject * /*self*/, PyObject *args)
405{
406 BPy_BMEdge *py_edge;
407 bool do_ccw = false;
408
409 BMesh *bm;
410 BMEdge *e_new = nullptr;
411
412 if (!PyArg_ParseTuple(args,
413 "O!" /* `edge` */
414 "|" /* Optional arguments. */
415 "O&" /* `ccw` */
416 ":edge_rotate",
418 &py_edge,
420 &do_ccw))
421 {
422 return nullptr;
423 }
424
425 BPY_BM_CHECK_OBJ(py_edge);
426
427 bm = py_edge->bm;
428
429 e_new = BM_edge_rotate(bm, py_edge->e, do_ccw, 0);
430
431 if (e_new) {
432 return BPy_BMEdge_CreatePyObject(bm, e_new);
433 }
434
435 Py_RETURN_NONE;
436}
437
439 /* Wrap. */
440 bpy_bm_utils_face_split_doc,
441 ".. function:: face_split(face, vert_a, vert_b, *, coords=(), use_exist=True, source=None)\n"
442 "\n"
443 " Face split with optional intermediate points.\n"
444 "\n"
445 " :param face: The face to cut.\n"
446 " :type face: :class:`bmesh.types.BMFace`\n"
447 " :param vert_a: First vertex to cut in the face (face must contain the vert).\n"
448 " :type vert_a: :class:`bmesh.types.BMVert`\n"
449 " :param vert_b: Second vertex to cut in the face (face must contain the vert).\n"
450 " :type vert_b: :class:`bmesh.types.BMVert`\n"
451 " :param coords: Optional sequence of 3D points in between *vert_a* and *vert_b*.\n"
452 " :type coords: Sequence[Sequence[float]]\n"
453 " :param use_exist: Use an existing edge if it exists (only used when *coords* argument is "
454 "empty or omitted)\n"
455 " :type use_exist: bool\n"
456 " :param source: Newly created edge will copy settings from this one.\n"
457 " :type source: :class:`bmesh.types.BMEdge` | None\n"
458 " :return: The newly created face and loop.\n"
459 " :rtype: tuple[:class:`bmesh.types.BMFace`, :class:`bmesh.types.BMLoop`]\n");
460static PyObject *bpy_bm_utils_face_split(PyObject * /*self*/, PyObject *args, PyObject *kw)
461{
462 const char *error_prefix = "face_split(...)";
463
464 BPy_BMFace *py_face;
465 BPy_BMVert *py_vert_a;
466 BPy_BMVert *py_vert_b;
467
468 /* optional */
469 PyObject *py_coords = nullptr;
470 bool edge_exists = true;
471 BPy_BMEdge *py_edge_source = nullptr;
472 PyC_TypeOrNone py_edge_source_or_none = PyC_TYPE_OR_NONE_INIT(&BPy_BMEdge_Type, &py_edge_source);
473
474 float *coords;
475 int ncoords = 0;
476
477 BMesh *bm;
478 BMFace *f_new = nullptr;
479 BMLoop *l_new = nullptr;
480 BMLoop *l_a, *l_b;
481
482 static const char *_keywords[] = {
483 "face", "vert_a", "vert_b", "coords", "use_exist", "source", nullptr};
484 static _PyArg_Parser _parser = {
485 "O!" /* `face` */
486 "O!" /* `vert_a` */
487 "O!" /* `vert_b` */
488 "|$" /* Optional, keyword only arguments. */
489 "O" /* `coords` */
490 "O&" /* `use_exist` */
491 "O&" /* `source` */
492 ":face_split",
493 _keywords,
494 nullptr,
495 };
496 if (!_PyArg_ParseTupleAndKeywordsFast(args,
497 kw,
498 &_parser,
500 &py_face,
502 &py_vert_a,
504 &py_vert_b,
505 &py_coords,
507 &edge_exists,
509 &py_edge_source_or_none))
510 {
511 return nullptr;
512 }
513
514 BPY_BM_CHECK_OBJ(py_face);
515 BPY_BM_CHECK_OBJ(py_vert_a);
516 BPY_BM_CHECK_OBJ(py_vert_b);
517
518 if (py_edge_source) {
519 /* Check the source edge is from the same mesh,
520 * its custom-data is read using the destination mesh's layout. */
521 BPY_BM_CHECK_SOURCE_OBJ(py_face->bm, error_prefix, py_edge_source);
522 }
523
524 /* this doubles for checking that the verts are in the same mesh */
525 if ((l_a = BM_face_vert_share_loop(py_face->f, py_vert_a->v)) &&
526 (l_b = BM_face_vert_share_loop(py_face->f, py_vert_b->v)))
527 {
528 /* pass */
529 }
530 else {
531 PyErr_Format(
532 PyExc_ValueError, "%s: one of the verts passed is not found in the face", error_prefix);
533 return nullptr;
534 }
535
536 if (py_vert_a->v == py_vert_b->v) {
537 PyErr_Format(PyExc_ValueError, "%s: vert arguments must differ", error_prefix);
538 return nullptr;
539 }
540
541 if (py_coords) {
542 ncoords = mathutils_array_parse_alloc_v(&coords, 3, py_coords, "face_split(...): ");
543 if (ncoords == -1) {
544 return nullptr;
545 }
546 }
547 else {
548 if (BM_loop_is_adjacent(l_a, l_b)) {
549 PyErr_Format(PyExc_ValueError, "%s: verts are adjacent in the face", error_prefix);
550 return nullptr;
551 }
552 }
553
554 /* --- main function body --- */
555 bm = py_face->bm;
556
557 if (ncoords) {
558 f_new = BM_face_split_n(bm,
559 py_face->f,
560 l_a,
561 l_b,
562 (float (*)[3])coords,
563 ncoords,
564 &l_new,
565 py_edge_source ? py_edge_source->e : nullptr);
566 PyMem_Free(coords);
567 }
568 else {
569 f_new = BM_face_split(bm,
570 py_face->f,
571 l_a,
572 l_b,
573 &l_new,
574 py_edge_source ? py_edge_source->e : nullptr,
575 edge_exists);
576 }
577
578 if (f_new && l_new) {
579 PyObject *ret = PyTuple_New(2);
582 return ret;
583 }
584
585 PyErr_Format(PyExc_ValueError, "%s: couldn't split the face, internal error", error_prefix);
586 return nullptr;
587}
588
590 /* Wrap. */
591 bpy_bm_utils_face_split_edgenet_doc,
592 ".. function:: face_split_edgenet(face, edgenet)\n"
593 "\n"
594 " Splits a face into any number of regions defined by an edgenet.\n"
595 "\n"
596 " :param face: The face to split.\n"
597 " :type face: :class:`bmesh.types.BMFace`\n"
598 " :param edgenet: Sequence of edges.\n"
599 " :type edgenet: Sequence[:class:`bmesh.types.BMEdge`]\n"
600 " :return: The newly created faces.\n"
601 " :rtype: tuple[:class:`bmesh.types.BMFace`, ...]\n"
602 "\n"
603 " .. note::\n"
604 "\n"
605 " Regions defined by edges need to connect to the face, otherwise they're "
606 "ignored as loose edges.\n");
607static PyObject *bpy_bm_utils_face_split_edgenet(PyObject * /*self*/, PyObject *args, PyObject *kw)
608{
609 const char *error_prefix = "face_split_edgenet(...)";
610 static const char *kwlist[] = {"face", "edgenet", nullptr};
611
612 BPy_BMFace *py_face;
613 PyObject *edge_seq;
614
615 BMesh *bm;
616
617 bool ok;
618
619 if (!PyArg_ParseTupleAndKeywords(args,
620 kw,
621 "O!" /* `face` */
622 "O" /* `edgenet` */
623 ":face_split_edgenet",
624 const_cast<char **>(kwlist),
626 &py_face,
627 &edge_seq))
628 {
629 return nullptr;
630 }
631
632 BPY_BM_CHECK_OBJ(py_face);
633
634 bm = py_face->bm;
635
636 Py_ssize_t edge_array_num;
637 BMEdge **edge_array = BPy_BMEdge_PySeq_As_Array(
638 &bm, edge_seq, 1, PY_SSIZE_T_MAX, &edge_array_num, true, true, error_prefix);
639
640 if (edge_array == nullptr) {
641 return nullptr;
642 }
643
644 /* --- main function body --- */
645 Vector<BMFace *> face_arr;
646 ok = BM_face_split_edgenet(bm, py_face->f, edge_array, edge_array_num, &face_arr);
647
648 PyMem_FREE(edge_array);
649
650 if (ok) {
651 PyObject *ret = BPy_BMFace_Array_As_Tuple(bm, face_arr.data(), face_arr.size());
652 return ret;
653 }
654
655 PyErr_SetString(PyExc_ValueError,
656 "face_split_edgenet(...): couldn't split the face, internal error");
657 return nullptr;
658}
659
661 /* Wrap. */
662 bpy_bm_utils_face_join_doc,
663 ".. function:: face_join(faces, remove=True)\n"
664 "\n"
665 " Joins a sequence of faces.\n"
666 "\n"
667 " :param faces: Sequence of faces.\n"
668 " :type faces: Sequence[:class:`bmesh.types.BMFace`]\n"
669 " :param remove: Remove the edges and vertices between the faces.\n"
670 " :type remove: bool\n"
671 " :return: The newly created face or None on failure.\n"
672 " :rtype: :class:`bmesh.types.BMFace` | None\n");
673static PyObject *bpy_bm_utils_face_join(PyObject * /*self*/, PyObject *args)
674{
675 const char *error_prefix = "face_join(...)";
676 BMesh *bm = nullptr;
677 PyObject *py_face_array;
678 BMFace *f_new;
679 bool do_remove = true;
680
681 if (!PyArg_ParseTuple(args,
682 "O" /* `faces` */
683 "|" /* Optional arguments. */
684 "O&" /* `remove` */
685 ":face_join",
686 &py_face_array,
688 &do_remove))
689 {
690 return nullptr;
691 }
692
693 Py_ssize_t face_seq_len = 0;
694 BMFace **face_array = BPy_BMFace_PySeq_As_Array(
695 &bm, py_face_array, 2, PY_SSIZE_T_MAX, &face_seq_len, true, true, error_prefix);
696 if (face_array == nullptr) {
697 return nullptr; /* error will be set */
698 }
699
700 /* Go ahead and join the face!
701 * --------------------------- */
702 BMFace *f_double;
703 f_new = BM_faces_join(bm, face_array, int(face_seq_len), do_remove, &f_double);
704 /* See #BM_faces_join note on callers asserting when `r_double` is non-null. */
705 BLI_assert_msg(f_double == nullptr,
706 "Doubled face detected at " AT ". Resulting mesh may be corrupt.");
707
708 PyMem_FREE(face_array);
709
710 if (f_new) {
711 return BPy_BMFace_CreatePyObject(bm, f_new);
712 }
713
714 Py_RETURN_NONE;
715}
716
718 /* Wrap. */
719 bpy_bm_utils_face_vert_separate_doc,
720 ".. function:: face_vert_separate(face, vert)\n"
721 "\n"
722 " Rip a vertex in a face away and add a new vertex.\n"
723 "\n"
724 " :param face: The face to separate.\n"
725 " :type face: :class:`bmesh.types.BMFace`\n"
726 " :param vert: A vertex in the face to separate.\n"
727 " :type vert: :class:`bmesh.types.BMVert`\n"
728 " :return: The newly created vertex or None on failure.\n"
729 " :rtype: :class:`bmesh.types.BMVert` | None\n"
730 "\n"
731 " .. note::\n"
732 "\n"
733 " This is the same as loop_separate, and has only been added for convenience.\n");
734static PyObject *bpy_bm_utils_face_vert_separate(PyObject * /*self*/, PyObject *args)
735{
736 const char *error_prefix = "face_vert_separate()";
737 BPy_BMFace *py_face;
738 BPy_BMVert *py_vert;
739
740 BMesh *bm;
741 BMLoop *l;
742 BMVert *v_old, *v_new;
743
744 if (!PyArg_ParseTuple(args,
745 "O!" /* `face` */
746 "O!" /* `vert` */
747 ":face_vert_separate",
749 &py_face,
751 &py_vert))
752 {
753 return nullptr;
754 }
755
756 bm = py_face->bm;
757
758 BPY_BM_CHECK_OBJ(py_face);
759 BPY_BM_CHECK_SOURCE_OBJ(bm, error_prefix, py_vert);
760
761 l = BM_face_vert_share_loop(py_face->f, py_vert->v);
762
763 if (l == nullptr) {
764 PyErr_Format(PyExc_ValueError, "%s: vertex not found in face", error_prefix);
765 return nullptr;
766 }
767
768 v_old = l->v;
769 v_new = BM_face_loop_separate(bm, l);
770
771 if (v_new != v_old) {
772 return BPy_BMVert_CreatePyObject(bm, v_new);
773 }
774
775 Py_RETURN_NONE;
776}
777
779 /* Wrap. */
780 bpy_bm_utils_face_flip_doc,
781 ".. function:: face_flip(face)\n"
782 "\n"
783 " Flip the face's direction.\n"
784 "\n"
785 " :param face: Face to flip.\n"
786 " :type face: :class:`bmesh.types.BMFace`\n");
787static PyObject *bpy_bm_utils_face_flip(PyObject * /*self*/, BPy_BMFace *value)
788{
789 if (!BPy_BMFace_Check(value)) {
790 PyErr_Format(PyExc_TypeError,
791 "face_flip(face): BMFace expected, not '%.200s'",
792 Py_TYPE(value)->tp_name);
793 return nullptr;
794 }
795
796 BPY_BM_CHECK_OBJ(value);
797
798 BM_face_normal_flip(value->bm, value->f);
799
800 Py_RETURN_NONE;
801}
802
804 /* Wrap. */
805 bpy_bm_utils_loop_separate_doc,
806 ".. function:: loop_separate(loop)\n"
807 "\n"
808 " Rip a vertex in a face away and add a new vertex.\n"
809 "\n"
810 " :param loop: The loop to separate.\n"
811 " :type loop: :class:`bmesh.types.BMLoop`\n"
812 " :return: The newly created vertex or None on failure.\n"
813 " :rtype: :class:`bmesh.types.BMVert` | None\n");
814static PyObject *bpy_bm_utils_loop_separate(PyObject * /*self*/, BPy_BMLoop *value)
815{
816 BMesh *bm;
817 BMLoop *l;
818 BMVert *v_old, *v_new;
819
820 if (!BPy_BMLoop_Check(value)) {
821 PyErr_Format(PyExc_TypeError,
822 "loop_separate(loop): BMLoop expected, not '%.200s'",
823 Py_TYPE(value)->tp_name);
824 return nullptr;
825 }
826
827 BPY_BM_CHECK_OBJ(value);
828
829 bm = value->bm;
830 l = value->l;
831
832 v_old = l->v;
833 v_new = BM_face_loop_separate(bm, l);
834
835 if (v_new != v_old) {
836 return BPy_BMVert_CreatePyObject(bm, v_new);
837 }
838
839 Py_RETURN_NONE;
840}
841
843 /* Wrap. */
844 bpy_bm_utils_uv_select_check_doc,
845 ".. function:: uv_select_check(bm, /, *, sync=True, flush=False, contiguous=False)\n"
846 "\n"
847 " Check UV selection state for consistency issues.\n"
848 "\n"
849 " :param bm: The BMesh to check.\n"
850 " :type bm: :class:`bmesh.types.BMesh`\n"
851 " :param sync: Check the data is properly synchronized "
852 "between UV's and the underlying mesh. "
853 "Failure to synchronize with the mesh selection "
854 "may cause tools not to behave properly.\n"
855 " :type sync: bool\n"
856 " :param flush: Check the selection has been properly flushed between elements "
857 "(based on the current :attr:`bmesh.types.BMesh.select_mode`).\n"
858 " :type flush: bool\n"
859 " :param contiguous: Check connected UV's and edges have a matching selection state.\n"
860 " :type contiguous: bool\n"
861 " :return: An error dictionary or None when there are no errors found.\n"
862 " :rtype: dict[str, int] | None\n");
863static PyObject *bpy_bm_utils_uv_select_check(PyObject * /*self*/, PyObject *args, PyObject *kwds)
864{
865 const char *error_prefix = "uv_select_check(...)";
866 BPy_BMesh *py_bm;
867 bool check_sync = true;
868 bool check_contiguous = false;
869 bool check_flush = false;
870
871 static const char *_keywords[] = {
872 "",
873 "sync",
874 "flush",
875 "contiguous",
876 nullptr,
877 };
878 static _PyArg_Parser _parser = {
879 "O!" /* `bm` */
880 "|$" /* Optional, keyword only arguments. */
881 "O&" /* `sync` */
882 "O&" /* `flush` */
883 "O&" /* `contiguous` */
884 ":uv_select_check",
885 _keywords,
886 nullptr,
887 };
888 if (!_PyArg_ParseTupleAndKeywordsFast(args,
889 kwds,
890 &_parser,
892 &py_bm,
894 &check_sync,
896 &check_flush,
898 &check_contiguous))
899 {
900 return nullptr;
901 }
902
903 BPY_BM_CHECK_OBJ(py_bm);
904
905 BMesh *bm = py_bm->bm;
906 if (check_sync) {
907 if (bpy_bm_check_uv_select_sync_valid(bm, error_prefix) == -1) {
908 return nullptr;
909 }
910 }
911
912 const int cd_loop_uv_offset = check_contiguous ?
914 -1;
915 if (check_contiguous) {
916 if (cd_loop_uv_offset == -1) {
917 PyErr_SetString(PyExc_ValueError, "contiguous=True for a mesh without UV coordinates");
918 return nullptr;
919 }
920 }
921
922 UVSelectValidateInfo info = {};
923 const bool is_valid = BM_mesh_uvselect_is_valid(
924 bm, cd_loop_uv_offset, check_sync, check_flush, check_contiguous, &info);
925 if (is_valid) {
926 Py_RETURN_NONE;
927 }
928
929 PyObject *result = PyDict_New();
930
931#define DICT_ADD_INT_MEMBER(info_struct, member) \
932 { \
933 PyObject *value = PyLong_FromLong(info_struct.member); \
934 PyDict_SetItemString(result, STRINGIFY(member), value); \
935 Py_DECREF(value); \
936 } \
937 ((void)0)
938
939 {
940 UVSelectValidateInfo_Sync &info_sub = info.sync;
941 DICT_ADD_INT_MEMBER(info_sub, count_uv_vert_any_selected_with_vert_unselected);
942 DICT_ADD_INT_MEMBER(info_sub, count_uv_vert_none_selected_with_vert_selected);
943
944 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_any_selected_with_edge_unselected);
945 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_none_selected_with_edge_selected);
946 }
947
948 if (check_flush) {
949 UVSelectValidateInfo_Flush &info_sub = info.flush;
950 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_selected_with_any_verts_unselected);
951 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_unselected_with_all_verts_selected);
952
953 DICT_ADD_INT_MEMBER(info_sub, count_uv_face_selected_with_any_verts_unselected);
954 DICT_ADD_INT_MEMBER(info_sub, count_uv_face_unselected_with_all_verts_selected);
955
956 DICT_ADD_INT_MEMBER(info_sub, count_uv_face_selected_with_any_edges_unselected);
957 DICT_ADD_INT_MEMBER(info_sub, count_uv_face_unselected_with_all_edges_selected);
958 }
959
960 if (check_contiguous) {
962 DICT_ADD_INT_MEMBER(info_sub, count_uv_vert_non_contiguous_selected);
963 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_non_contiguous_selected);
964 }
965
966 if (check_flush && check_contiguous) {
968 DICT_ADD_INT_MEMBER(info_sub, count_uv_vert_isolated_in_edge_or_face_mode);
969 DICT_ADD_INT_MEMBER(info_sub, count_uv_vert_isolated_in_face_mode);
970 DICT_ADD_INT_MEMBER(info_sub, count_uv_edge_isolated_in_face_mode);
971 }
972
973#undef DICT_ADD_INT_MEMBER
974
975 return result;
976}
977
978#ifdef __GNUC__
979# ifdef __clang__
980# pragma clang diagnostic push
981# pragma clang diagnostic ignored "-Wcast-function-type"
982# else
983# pragma GCC diagnostic push
984# pragma GCC diagnostic ignored "-Wcast-function-type"
985# endif
986#endif
987
988static PyMethodDef BPy_BM_utils_methods[] = {
989 {"vert_collapse_edge",
990 static_cast<PyCFunction>(bpy_bm_utils_vert_collapse_edge),
991 METH_VARARGS,
992 bpy_bm_utils_vert_collapse_edge_doc},
993 {"vert_collapse_faces",
994 static_cast<PyCFunction>(bpy_bm_utils_vert_collapse_faces),
995 METH_VARARGS,
996 bpy_bm_utils_vert_collapse_faces_doc},
997 {"vert_dissolve",
998 static_cast<PyCFunction>(bpy_bm_utils_vert_dissolve),
999 METH_VARARGS,
1000 bpy_bm_utils_vert_dissolve_doc}, /* could use METH_O */
1001 {"vert_splice",
1002 static_cast<PyCFunction>(bpy_bm_utils_vert_splice),
1003 METH_VARARGS,
1004 bpy_bm_utils_vert_splice_doc},
1005 {"vert_separate",
1006 static_cast<PyCFunction>(bpy_bm_utils_vert_separate),
1007 METH_VARARGS,
1008 bpy_bm_utils_vert_separate_doc},
1009 {"edge_split",
1010 static_cast<PyCFunction>(bpy_bm_utils_edge_split),
1011 METH_VARARGS,
1012 bpy_bm_utils_edge_split_doc},
1013 {"edge_rotate",
1014 static_cast<PyCFunction>(bpy_bm_utils_edge_rotate),
1015 METH_VARARGS,
1016 bpy_bm_utils_edge_rotate_doc},
1017 {"face_split",
1018 reinterpret_cast<PyCFunction>(bpy_bm_utils_face_split),
1019 METH_VARARGS | METH_KEYWORDS,
1020 bpy_bm_utils_face_split_doc},
1021 {"face_split_edgenet",
1022 reinterpret_cast<PyCFunction>(bpy_bm_utils_face_split_edgenet),
1023 METH_VARARGS | METH_KEYWORDS,
1024 bpy_bm_utils_face_split_edgenet_doc},
1025 {"face_join",
1026 static_cast<PyCFunction>(bpy_bm_utils_face_join),
1027 METH_VARARGS,
1028 bpy_bm_utils_face_join_doc},
1029 {"face_vert_separate",
1030 static_cast<PyCFunction>(bpy_bm_utils_face_vert_separate),
1031 METH_VARARGS,
1032 bpy_bm_utils_face_vert_separate_doc},
1033 {"face_flip",
1034 reinterpret_cast<PyCFunction>(bpy_bm_utils_face_flip),
1035 METH_O,
1036 bpy_bm_utils_face_flip_doc},
1037 {"loop_separate",
1038 reinterpret_cast<PyCFunction>(bpy_bm_utils_loop_separate),
1039 METH_O,
1040 bpy_bm_utils_loop_separate_doc},
1041 {"uv_select_check",
1042 reinterpret_cast<PyCFunction>(bpy_bm_utils_uv_select_check),
1043 METH_VARARGS | METH_KEYWORDS,
1044 bpy_bm_utils_uv_select_check_doc},
1045 {nullptr, nullptr, 0, nullptr},
1046};
1047
1048#ifdef __GNUC__
1049# ifdef __clang__
1050# pragma clang diagnostic pop
1051# else
1052# pragma GCC diagnostic pop
1053# endif
1054#endif
1055
1057 /* Wrap. */
1058 BPy_BM_utils_doc,
1059 "This module provides bmesh utility functions for splitting, joining, "
1060 "and modifying mesh elements.");
1061static PyModuleDef BPy_BM_utils_module_def = {
1062 /*m_base*/ PyModuleDef_HEAD_INIT,
1063 /*m_name*/ "bmesh.utils",
1064 /*m_doc*/ BPy_BM_utils_doc,
1065 /*m_size*/ 0,
1066 /*m_methods*/ BPy_BM_utils_methods,
1067 /*m_slots*/ nullptr,
1068 /*m_traverse*/ nullptr,
1069 /*m_clear*/ nullptr,
1070 /*m_free*/ nullptr,
1071};
1072
1074{
1075 PyObject *submodule;
1076
1077 submodule = PyModule_Create(&BPy_BM_utils_module_def);
1078
1079 return submodule;
1080}
1081
1082} // namespace blender
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
#define AT
#define BLI_assert_msg(a, msg)
#define BPy_BMFace_Check(v)
#define BPY_BM_CHECK_OBJ(obj)
#define BPy_BMLoop_Check(v)
#define BPY_BM_CHECK_SOURCE_OBJ(bm, errmsg,...)
#define DICT_ADD_INT_MEMBER(info_struct, member)
#define BM_vert_edge_count_is_over(v, n)
int64_t size() const
BMesh Iterators.
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * bpy_bm_utils_face_flip(PyObject *, BPy_BMFace *value)
int PyC_ParseBool(PyObject *o, void *p)
static PyObject * bpy_bm_utils_face_join(PyObject *, PyObject *args)
void BM_face_normal_flip(BMesh *bm, BMFace *f)
BLI_INLINE bool BM_loop_is_adjacent(const BMLoop *l_a, const BMLoop *l_b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BMEdge ** BPy_BMEdge_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_seq_num, bool do_unique_check, bool do_bm_check, const char *error_prefix)
bool BM_mesh_uvselect_is_valid(BMesh *bm, const int cd_loop_uv_offset, const bool check_sync, const bool check_flush, const bool check_contiguous, UVSelectValidateInfo *info_p)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
PyTypeObject BPy_BMVert_Type
PyObject * BPy_BMLoop_CreatePyObject(BMesh *bm, BMLoop *l)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * bpy_bm_utils_vert_separate(PyObject *, PyObject *args)
static PyObject * bpy_bm_utils_vert_splice(PyObject *, PyObject *args)
BMFace * BM_faces_join(BMesh *bm, BMFace **faces, int totface, const bool do_del, BMFace **r_double)
Join Connected Faces.
void BM_vert_separate(BMesh *bm, BMVert *v, BMEdge **e_in, int e_in_len, const bool copy_select, BMVert ***r_vout, int *r_vout_len)
PyTypeObject BPy_BMFace_Type
PyTypeObject BPy_BMesh_Type
static PyObject * bpy_bm_utils_uv_select_check(PyObject *, PyObject *args, PyObject *kwds)
BMEdge * BM_edge_rotate(BMesh *bm, BMEdge *e, const bool ccw, const short check_flag)
Rotate Edge.
static PyObject * bpy_bm_utils_loop_separate(PyObject *, BPy_BMLoop *value)
static PyMethodDef BPy_BM_utils_methods[]
static PyModuleDef BPy_BM_utils_module_def
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
bool BM_vert_pair_share_face_check(BMVert *v_a, BMVert *v_b)
static PyObject * bpy_bm_utils_face_split_edgenet(PyObject *, PyObject *args, PyObject *kw)
bool BM_vert_splice(BMesh *bm, BMVert *v_dst, BMVert *v_src)
Splice Vert.
PyObject * BPy_BMVert_CreatePyObject(BMesh *bm, BMVert *v)
BMVert * BM_face_loop_separate(BMesh *bm, BMLoop *l_sep)
Rip a single face from a vertex fan.
BMFace * BM_face_split_n(BMesh *bm, BMFace *f, BMLoop *l_a, BMLoop *l_b, float cos[][3], int n, BMLoop **r_l, BMEdge *example)
Face Split with intermediate points.
static PyObject * bpy_bm_utils_vert_collapse_faces(PyObject *, PyObject *args)
BMEdge * BM_vert_collapse_edge(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, const bool do_del, const bool kill_degenerate_faces, const bool kill_duplicate_faces)
Vert Collapse Faces.
static PyObject * bpy_bm_utils_face_split(PyObject *, PyObject *args, PyObject *kw)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
static PyObject * bpy_bm_utils_vert_dissolve(PyObject *, PyObject *args)
bool BM_vert_dissolve(BMesh *bm, BMVert *v)
Dissolve Vert.
Definition bmesh_mods.cc:22
BMVert * BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float fac)
Edge Split.
PyObject * BPy_BMFace_Array_As_Tuple(BMesh *bm, BMFace **elem, Py_ssize_t elem_num)
BMEdge * BM_vert_collapse_faces(BMesh *bm, BMEdge *e_kill, BMVert *v_kill, float fac, const bool do_del, const bool join_faces, const bool kill_degenerate_faces, const bool kill_duplicate_faces)
Vert Collapse Faces.
static PyObject * bpy_bm_utils_face_vert_separate(PyObject *, PyObject *args)
BMFace * BM_face_split(BMesh *bm, BMFace *f, BMLoop *l_a, BMLoop *l_b, BMLoop **r_l, BMEdge *example, const bool no_double)
Face Split.
bool BM_face_split_edgenet(BMesh *bm, BMFace *f, BMEdge **edge_net, const int edge_net_len, Vector< BMFace * > *r_face_arr)
static PyObject * bpy_bm_utils_edge_split(PyObject *, PyObject *args)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
PyObject * BPy_BMVert_Array_As_Tuple(BMesh *bm, BMVert **elem, Py_ssize_t elem_num)
static PyObject * bpy_bm_utils_edge_rotate(PyObject *, PyObject *args)
int bpy_bm_check_uv_select_sync_valid(BMesh *bm, const char *error_prefix)
PyTypeObject BPy_BMEdge_Type
BMFace ** BPy_BMFace_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_seq_num, bool do_unique_check, bool do_bm_check, const char *error_prefix)
int PyC_ParseTypeOrNone(PyObject *o, void *p)
int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix)
Definition mathutils.cc:262
MINLINE float clamp_f(float value, float min, float max)
static PyObject * bpy_bm_utils_vert_collapse_edge(PyObject *, PyObject *args)
PyObject * BPyInit_bmesh_utils()
PyObject * BPy_BMEdge_CreatePyObject(BMesh *bm, BMEdge *e)
PyObject * BPy_BMFace_CreatePyObject(BMesh *bm, BMFace *f)
#define PyC_TYPE_OR_NONE_INIT(py_type, value_p)
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
PyObject_HEAD BMesh * bm
PyObject_HEAD BMesh * bm
PyObject_HEAD BMesh * bm
PyObject_HEAD BMesh * bm
PyObject_HEAD BMesh * bm
UVSelectValidateInfo_Contiguous contiguous
UVSelectValidateInfo_Sync sync
UVSelectValidateInfo_Flush flush
UVSelectValidateInfo_FlushAndContiguous flush_contiguous