Blender  V3.3
bmesh_py_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2012 Blender Foundation. All rights reserved. */
3 
10 #include <Python.h>
11 
12 #include "BLI_utildefines.h"
13 
14 #include "bmesh.h"
15 
16 #include "bmesh_py_types.h"
19 #include "bmesh_py_types_select.h"
20 
21 #include "bmesh_py_geometry.h"
22 #include "bmesh_py_ops.h"
23 #include "bmesh_py_utils.h"
24 
25 #include "BKE_editmesh.h"
26 
27 #include "DNA_mesh_types.h"
28 
29 #include "../generic/py_capi_utils.h"
30 
31 #include "bmesh_py_api.h" /* own include */
32 
33 PyDoc_STRVAR(bpy_bm_new_doc,
34  ".. method:: new(use_operators=True)\n"
35  "\n"
36  " :arg use_operators: Support calling operators in :mod:`bmesh.ops` (uses some "
37  "extra memory per vert/edge/face).\n"
38  " :type use_operators: bool\n"
39  " :return: Return a new, empty BMesh.\n"
40  " :rtype: :class:`bmesh.types.BMesh`\n");
41 
42 static PyObject *bpy_bm_new(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
43 {
44  static const char *kwlist[] = {"use_operators", NULL};
45  BMesh *bm;
46 
47  bool use_operators = true;
48 
49  if (!PyArg_ParseTupleAndKeywords(
50  args, kw, "|$O&:new", (char **)kwlist, PyC_ParseBool, &use_operators)) {
51  return NULL;
52  }
53 
55  &((struct BMeshCreateParams){
56  .use_toolflags = use_operators,
57  }));
58 
60 }
61 
62 PyDoc_STRVAR(bpy_bm_from_edit_mesh_doc,
63  ".. method:: from_edit_mesh(mesh)\n"
64  "\n"
65  " Return a BMesh from this mesh, currently the mesh must already be in editmode.\n"
66  "\n"
67  " :arg mesh: The editmode mesh.\n"
68  " :type mesh: :class:`bpy.types.Mesh`\n"
69  " :return: the BMesh associated with this mesh.\n"
70  " :rtype: :class:`bmesh.types.BMesh`\n");
71 static PyObject *bpy_bm_from_edit_mesh(PyObject *UNUSED(self), PyObject *value)
72 {
73  BMesh *bm;
74  Mesh *me = PyC_RNA_AsPointer(value, "Mesh");
75 
76  if (me == NULL) {
77  return NULL;
78  }
79 
80  if (me->edit_mesh == NULL) {
81  PyErr_SetString(PyExc_ValueError, "The mesh must be in editmode");
82  return NULL;
83  }
84 
85  bm = me->edit_mesh->bm;
86 
88 }
89 
90 PyDoc_STRVAR(bpy_bm_update_edit_mesh_doc,
91  ".. method:: update_edit_mesh(mesh, loop_triangles=True, destructive=True)\n"
92  "\n"
93  " Update the mesh after changes to the BMesh in editmode,\n"
94  " optionally recalculating n-gon tessellation.\n"
95  "\n"
96  " :arg mesh: The editmode mesh.\n"
97  " :type mesh: :class:`bpy.types.Mesh`\n"
98  " :arg loop_triangles: Option to recalculate n-gon tessellation.\n"
99  " :type loop_triangles: boolean\n"
100  " :arg destructive: Use when geometry has been added or removed.\n"
101  " :type destructive: boolean\n");
102 static PyObject *bpy_bm_update_edit_mesh(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
103 {
104  static const char *kwlist[] = {"mesh", "loop_triangles", "destructive", NULL};
105  PyObject *py_me;
106  Mesh *me;
107  bool do_loop_triangles = true;
108  bool is_destructive = true;
109 
110  if (!PyArg_ParseTupleAndKeywords(args,
111  kw,
112  "O|$O&O&:update_edit_mesh",
113  (char **)kwlist,
114  &py_me,
116  &do_loop_triangles,
118  &is_destructive)) {
119  return NULL;
120  }
121 
122  me = PyC_RNA_AsPointer(py_me, "Mesh");
123 
124  if (me == NULL) {
125  return NULL;
126  }
127 
128  if (me->edit_mesh == NULL) {
129  PyErr_SetString(PyExc_ValueError, "The mesh must be in editmode");
130  return NULL;
131  }
132 
133  {
134  extern void EDBM_update_extern(
135  struct Mesh * me, const bool do_tessface, const bool is_destructive);
136 
137  EDBM_update_extern(me, do_loop_triangles, is_destructive);
138  }
139 
140  Py_RETURN_NONE;
141 }
142 
143 static struct PyMethodDef BPy_BM_methods[] = {
144  {"new", (PyCFunction)bpy_bm_new, METH_VARARGS | METH_KEYWORDS, bpy_bm_new_doc},
145  {"from_edit_mesh", (PyCFunction)bpy_bm_from_edit_mesh, METH_O, bpy_bm_from_edit_mesh_doc},
146  {"update_edit_mesh",
147  (PyCFunction)bpy_bm_update_edit_mesh,
148  METH_VARARGS | METH_KEYWORDS,
149  bpy_bm_update_edit_mesh_doc},
150  {NULL, NULL, 0, NULL},
151 };
152 
153 PyDoc_STRVAR(BPy_BM_doc,
154  "This module provides access to blenders bmesh data structures.\n"
155  "\n"
156  ".. include:: include__bmesh.rst\n");
157 static struct PyModuleDef BPy_BM_module_def = {
158  PyModuleDef_HEAD_INIT,
159  "bmesh", /* m_name */
160  BPy_BM_doc, /* m_doc */
161  0, /* m_size */
162  BPy_BM_methods, /* m_methods */
163  NULL, /* m_reload */
164  NULL, /* m_traverse */
165  NULL, /* m_clear */
166  NULL, /* m_free */
167 };
168 
169 PyObject *BPyInit_bmesh(void)
170 {
171  PyObject *mod;
172  PyObject *submodule;
173  PyObject *sys_modules = PyImport_GetModuleDict();
174 
179 
180  mod = PyModule_Create(&BPy_BM_module_def);
181 
182  /* bmesh.types */
183  PyModule_AddObject(mod, "types", (submodule = BPyInit_bmesh_types()));
184  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
185 
186  /* bmesh.ops (not a real module, exposes module like access). */
187  PyModule_AddObject(mod, "ops", (submodule = BPyInit_bmesh_ops()));
188  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
189 
190  PyModule_AddObject(mod, "utils", (submodule = BPyInit_bmesh_utils()));
191  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
192 
193  PyModule_AddObject(mod, "geometry", (submodule = BPyInit_bmesh_geometry()));
194  PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule);
195 
196  return mod;
197 }
#define UNUSED(x)
void EDBM_update_extern(struct Mesh *me, bool do_tessellation, bool is_destructive)
ATTR_WARN_UNUSED_RESULT BMesh * bm
const BMAllocTemplate bm_mesh_allocsize_default
Definition: bmesh_mesh.cc:23
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const struct BMeshCreateParams *params)
Definition: bmesh_mesh.cc:125
static PyObject * bpy_bm_from_edit_mesh(PyObject *UNUSED(self), PyObject *value)
Definition: bmesh_py_api.c:71
PyDoc_STRVAR(bpy_bm_new_doc, ".. method:: new(use_operators=True)\n" "\n" " :arg 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 struct PyMethodDef BPy_BM_methods[]
Definition: bmesh_py_api.c:143
static struct PyModuleDef BPy_BM_module_def
Definition: bmesh_py_api.c:157
PyObject * BPyInit_bmesh(void)
Definition: bmesh_py_api.c:169
static PyObject * bpy_bm_update_edit_mesh(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
Definition: bmesh_py_api.c:102
static PyObject * bpy_bm_new(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
Definition: bmesh_py_api.c:42
PyObject * BPyInit_bmesh_geometry(void)
PyObject * BPyInit_bmesh_ops(void)
Definition: bmesh_py_ops.c:276
PyObject * BPyInit_bmesh_types(void)
void BPy_BM_init_types(void)
PyObject * BPy_BMesh_CreatePyObject(BMesh *bm, int flag)
@ BPY_BMFLAG_IS_WRAPPED
@ BPY_BMFLAG_NOP
void BPy_BM_init_types_customdata(void)
void BPy_BM_init_types_meshdata(void)
void BPy_BM_init_types_select(void)
PyObject * BPyInit_bmesh_utils(void)
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
int PyC_ParseBool(PyObject *o, void *p)
struct BMesh * bm
Definition: BKE_editmesh.h:40
struct BMEditMesh * edit_mesh
ccl_device_inline int mod(int x, int m)
Definition: util/math.h:490