2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2012 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Campbell Barton
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/python/bmesh/bmesh_py_api.c
29 * This file defines the 'bmesh.utils' module.
30 * Utility functions for operating on 'bmesh.types'
37 #include "bmesh_py_types.h"
39 #include "BLI_utildefines.h"
41 #include "bmesh_py_utils.h" /* own include */
44 PyDoc_STRVAR(bpy_bm_utils_vert_collapse_edge_doc,
45 ".. method:: vert_collapse_edge(vert, edge)\n"
47 " Collapse a vertex into an edge.\n"
49 " :arg vert: The vert that will be collapsed.\n"
50 " :type vert: :class:`bmesh.types.BMVert`\n"
51 " :arg edge: The edge to collapse into.\n"
52 " :type edge: :class:`bmesh.types.BMEdge`\n"
53 " :return: The resulting edge from the collapse operation.\n"
54 " :rtype: :class:`bmesh.types.BMEdge`\n"
56 static PyObject *bpy_bm_utils_vert_collapse_edge(PyObject *UNUSED(self), PyObject *args)
64 if (!PyArg_ParseTuple(args, "O!O!:vert_collapse_edge",
65 &BPy_BMVert_Type, &py_vert,
66 &BPy_BMEdge_Type, &py_edge))
71 BPY_BM_CHECK_OBJ(py_edge);
72 BPY_BM_CHECK_OBJ(py_vert);
74 /* this doubles for checking that the verts are in the same mesh */
75 if (!(py_edge->e->v1 == py_vert->v ||
76 py_edge->e->v2 == py_vert->v))
78 PyErr_SetString(PyExc_ValueError,
79 "vert_collapse_edge(vert, edge): the vertex is not found in the edge");
83 if (BM_vert_edge_count(py_vert->v) > 2) {
84 PyErr_SetString(PyExc_ValueError,
85 "vert_collapse_edge(vert, edge): vert has more then 2 connected edges");
91 e_new = BM_vert_collapse_edge(bm, py_edge->e, py_vert->v);
94 return BPy_BMEdge_CreatePyObject(bm, e_new);
97 PyErr_SetString(PyExc_ValueError,
98 "vert_collapse_edge(vert, edge): no new edge created, internal error");
104 PyDoc_STRVAR(bpy_bm_utils_vert_collapse_faces_doc,
105 ".. method:: vert_collapse_faces(vert, edge, fac, join_faces)\n"
107 " Split an edge, return the newly created data.\n"
109 " :arg vert: The vert that will be collapsed.\n"
110 " :type vert: :class:`bmesh.types.BMVert`\n"
111 " :arg edge: The edge to collapse into.\n"
112 " :type edge: :class:`bmesh.types.BMEdge`\n"
113 " :arg fac: The factor to use when merging customdata [0 - 1].\n"
114 " :type fac: float\n"
115 " :return: The resulting edge from the collapse operation.\n"
116 " :rtype: :class:`bmesh.types.BMEdge`\n"
118 static PyObject *bpy_bm_utils_vert_collapse_faces(PyObject *UNUSED(self), PyObject *args)
127 BMEdge *e_new = NULL;
129 if (!PyArg_ParseTuple(args, "O!O!fi:vert_collapse_faces",
130 &BPy_BMVert_Type, &py_vert,
131 &BPy_BMEdge_Type, &py_edge,
132 &fac, &do_join_faces))
137 BPY_BM_CHECK_OBJ(py_edge);
138 BPY_BM_CHECK_OBJ(py_vert);
140 /* this doubles for checking that the verts are in the same mesh */
141 if (!(py_edge->e->v1 == py_vert->v ||
142 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");
149 if (BM_vert_edge_count(py_vert->v) > 2) {
150 PyErr_SetString(PyExc_ValueError,
151 "vert_collapse_faces(vert, edge): vert has more then 2 connected edges");
157 e_new = BM_vert_collapse_faces(bm, py_edge->e, py_vert->v, CLAMPIS(fac, 0.0f, 1.0f), do_join_faces);
160 return BPy_BMEdge_CreatePyObject(bm, e_new);
163 PyErr_SetString(PyExc_ValueError,
164 "vert_collapse_edge(vert, edge): no new edge created, internal error");
170 PyDoc_STRVAR(bpy_bm_utils_vert_dissolve_doc,
171 ".. method:: vert_dissolve(vert)\n"
173 " Dissolve this vertex (will be removed).\n"
175 " :arg vert: The vert to be dissolved.\n"
176 " :type vert: :class:`bmesh.types.BMVert`\n"
177 " :return: True when the vertex dissolve is successful.\n"
180 static PyObject *bpy_bm_utils_vert_dissolve(PyObject *UNUSED(self), PyObject *args)
186 if (!PyArg_ParseTuple(args, "O!:vert_dissolve",
187 &BPy_BMVert_Type, &py_vert))
192 BPY_BM_CHECK_OBJ(py_vert);
196 return PyBool_FromLong((BM_vert_dissolve(bm, py_vert->v)));
200 PyDoc_STRVAR(bpy_bm_utils_edge_split_doc,
201 ".. method:: edge_split(edge, vert, fac)\n"
203 " Split an edge, return the newly created data.\n"
205 " :arg edge: The edge to split.\n"
206 " :type edge: :class:`bmesh.types.BMEdge`\n"
207 " :arg vert: One of the verts on the edge, defines the split direction.\n"
208 " :type vert: :class:`bmesh.types.BMVert`\n"
209 " :arg fac: The point on the edge where the new vert will be created [0 - 1].\n"
210 " :type fac: float\n"
211 " :return: The newly created (edge, vert) pair.\n"
214 static PyObject *bpy_bm_utils_edge_split(PyObject *UNUSED(self), PyObject *args)
221 BMVert *v_new = NULL;
222 BMEdge *e_new = NULL;
224 if (!PyArg_ParseTuple(args, "O!O!f:edge_split",
225 &BPy_BMEdge_Type, &py_edge,
226 &BPy_BMVert_Type, &py_vert,
232 BPY_BM_CHECK_OBJ(py_edge);
233 BPY_BM_CHECK_OBJ(py_vert);
235 /* this doubles for checking that the verts are in the same mesh */
236 if (!(py_edge->e->v1 == py_vert->v ||
237 py_edge->e->v2 == py_vert->v))
239 PyErr_SetString(PyExc_ValueError,
240 "edge_split(edge, vert): the vertex is not found in the edge");
246 v_new = BM_edge_split(bm, py_edge->e, py_vert->v, &e_new, CLAMPIS(fac, 0.0f, 1.0f));
248 if (v_new && e_new) {
249 PyObject *ret = PyTuple_New(2);
250 PyTuple_SET_ITEM(ret, 0, BPy_BMEdge_CreatePyObject(bm, e_new));
251 PyTuple_SET_ITEM(ret, 1, BPy_BMVert_CreatePyObject(bm, v_new));
255 PyErr_SetString(PyExc_ValueError,
256 "edge_split(edge, vert): couldn't split the edge, internal error");
262 PyDoc_STRVAR(bpy_bm_utils_edge_rotate_doc,
263 ".. method:: edge_rotate(edge, ccw=False)\n"
265 " Rotate the edge and return the newly created edge.\n"
266 " If rotating the edge fails, None will be returned.\n"
268 " :arg edge: The edge to rotate.\n"
269 " :type edge: :class:`bmesh.types.BMEdge`\n"
270 " :arg ccw: When True the edge will be rotated counter clockwise.\n"
271 " :type ccw: boolean\n"
272 " :return: The newly rotated edge.\n"
273 " :rtype: :class:`bmesh.types.BMEdge`\n"
275 static PyObject *bpy_bm_utils_edge_rotate(PyObject *UNUSED(self), PyObject *args)
281 BMEdge *e_new = NULL;
283 if (!PyArg_ParseTuple(args, "O!|i:edge_rotate",
284 &BPy_BMEdge_Type, &py_edge,
290 BPY_BM_CHECK_OBJ(py_edge);
294 e_new = BM_edge_rotate(bm, py_edge->e, do_ccw);
297 return BPy_BMEdge_CreatePyObject(bm, e_new);
305 PyDoc_STRVAR(bpy_bm_utils_face_split_doc,
306 ".. method:: face_split(face, vert, vert_a, vert_b, edge_example)\n"
308 " Split an edge, return the newly created data.\n"
310 " :arg face: The face to cut.\n"
311 " :type face: :class:`bmesh.types.BMFace`\n"
312 " :arg vert_a: First vertex to cut in the face (face must contain the vert).\n"
313 " :type vert_a: :class:`bmesh.types.BMVert`\n"
314 " :arg vert_b: Second vertex to cut in the face (face must contain the vert).\n"
315 " :type vert_b: :class:`bmesh.types.BMVert`\n"
316 " :arg edge_example: Optional edge argument, newly created edge will copy settings from this one.\n"
317 " :type edge_example: :class:`bmesh.types.BMEdge`\n"
319 static PyObject *bpy_bm_utils_face_split(PyObject *UNUSED(self), PyObject *args)
322 BPy_BMVert *py_vert_a;
323 BPy_BMVert *py_vert_b;
324 BPy_BMEdge *py_edge_example = NULL; /* optional */
327 BMFace *f_new = NULL;
328 BMLoop *l_new = NULL;
330 if (!PyArg_ParseTuple(args, "O!O!|O!:face_split",
331 &BPy_BMFace_Type, &py_face,
332 &BPy_BMVert_Type, &py_vert_a,
333 &BPy_BMVert_Type, &py_vert_b,
334 &BPy_BMEdge_Type, &py_edge_example))
339 BPY_BM_CHECK_OBJ(py_face);
340 BPY_BM_CHECK_OBJ(py_vert_a);
341 BPY_BM_CHECK_OBJ(py_vert_b);
343 if (py_edge_example) {
344 BPY_BM_CHECK_OBJ(py_edge_example);
347 /* this doubles for checking that the verts are in the same mesh */
348 if (BM_vert_in_face(py_face->f, py_vert_a->v) == FALSE ||
349 BM_vert_in_face(py_face->f, py_vert_b->v) == FALSE)
351 PyErr_SetString(PyExc_ValueError,
352 "face_split(...): one of the verts passed is not found in the face");
356 if (py_vert_a->v == py_vert_b->v) {
357 PyErr_SetString(PyExc_ValueError,
358 "face_split(...): vert arguments must differ");
364 f_new = BM_face_split(bm, py_face->f,
365 py_vert_a->v, py_vert_a->v,
366 &l_new, py_edge_example ? py_edge_example->e : NULL);
368 if (f_new && l_new) {
369 PyObject *ret = PyTuple_New(2);
370 PyTuple_SET_ITEM(ret, 0, BPy_BMFace_CreatePyObject(bm, f_new));
371 PyTuple_SET_ITEM(ret, 1, BPy_BMLoop_CreatePyObject(bm, l_new));
375 PyErr_SetString(PyExc_ValueError,
376 "face_split(...): couldn't split the face, internal error");
381 PyDoc_STRVAR(bpy_bm_utils_face_join_doc,
382 ".. method:: face_join(faces)\n"
384 " Joins a sequence of faces.\n"
386 " :arg faces: Sequence of faces .\n"
387 " :type faces: :class:`bmesh.types.BMFace`\n"
388 " :return: The newly created face or None on failure.\n"
389 " :rtype: :class:`bmesh.types.BMFace`\n"
391 static PyObject *bpy_bm_utils_face_join(PyObject *UNUSED(self), PyObject *value)
395 Py_ssize_t face_seq_len = 0;
398 face_array = BPy_BMElem_PySeq_As_Array(&bm, value, 2, PY_SSIZE_T_MAX,
399 &face_seq_len, &BPy_BMFace_Type,
400 TRUE, TRUE, "face_join(...)");
402 if (face_array == NULL) {
403 return NULL; /* error will be set */
406 /* Go ahead and join the face!
407 * --------------------------- */
408 f_new = BM_faces_join(bm, face_array, (int)face_seq_len);
411 return BPy_BMFace_CreatePyObject(bm, f_new);
418 static struct PyMethodDef BPy_BM_utils_methods[] = {
419 {"vert_collapse_edge", (PyCFunction)bpy_bm_utils_vert_collapse_edge, METH_VARARGS, bpy_bm_utils_vert_collapse_edge_doc},
420 {"vert_collapse_faces", (PyCFunction)bpy_bm_utils_vert_collapse_faces, METH_VARARGS, bpy_bm_utils_vert_collapse_faces_doc},
421 {"vert_dissolve", (PyCFunction)bpy_bm_utils_vert_dissolve, METH_VARARGS, bpy_bm_utils_vert_dissolve_doc},
422 {"edge_split", (PyCFunction)bpy_bm_utils_edge_split, METH_VARARGS, bpy_bm_utils_edge_split_doc},
423 {"edge_rotate", (PyCFunction)bpy_bm_utils_edge_rotate, METH_VARARGS, bpy_bm_utils_edge_rotate_doc},
424 {"face_split", (PyCFunction)bpy_bm_utils_face_split, METH_VARARGS, bpy_bm_utils_face_split_doc},
425 {"face_join", (PyCFunction)bpy_bm_utils_face_join, METH_O, bpy_bm_utils_face_join_doc},
426 {NULL, NULL, 0, NULL}
430 PyDoc_STRVAR(BPy_BM_doc,
431 "This module provides access to blenders bmesh data structures."
433 static struct PyModuleDef BPy_BM_types_module_def = {
434 PyModuleDef_HEAD_INIT,
435 "bmesh.utils", /* m_name */
436 BPy_BM_doc, /* m_doc */
438 BPy_BM_utils_methods, /* m_methods */
440 NULL, /* m_traverse */
445 PyObject *BPyInit_bmesh_utils(void)
449 submodule = PyModule_Create(&BPy_BM_types_module_def);