Blender V5.3
bmesh_py_ops.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_dynstr.hh"
15
16#include "MEM_guardedalloc.h"
17
18#include "bmesh.hh"
19
20#include "bmesh_py_ops.hh" /* own include */
21#include "bmesh_py_ops_call.hh"
22
23namespace blender {
24
25/* bmesh operator 'bmesh.ops.*' callable types
26 * ******************************************* */
27
28static PyObject *bpy_bmesh_op_repr(BPy_BMeshOpFunc *self)
29{
30 return PyUnicode_FromFormat("<%.200s bmesh.ops.%.200s()>", Py_TYPE(self)->tp_name, self->opname);
31}
32
33/* methods
34 * ======= */
35
36/* __doc__
37 * ------- */
38
39static char *bmp_slots_as_args(const BMOSlotType slot_types[BMO_OP_MAX_SLOTS], const bool is_out)
40{
41 DynStr *dyn_str = BLI_dynstr_new();
42 char *ret;
43 bool quoted;
44 bool set;
45
46 int i = 0;
47
48 while (*slot_types[i].name) {
49 quoted = false;
50 set = false;
51 /* Cut off `.out` by using a string size argument. */
52 const int name_len = is_out ? (strchr(slot_types[i].name, '.') - slot_types[i].name) :
53 sizeof(slot_types[i].name);
54 const char *value = "<Unknown>";
55 switch (slot_types[i].type) {
57 value = "False";
58 break;
59 case BMO_OP_SLOT_INT:
60 if (slot_types[i].subtype.intg == BMO_OP_SLOT_SUBTYPE_INT_ENUM) {
61 value = slot_types[i].enum_flags[0].identifier;
62 quoted = true;
63 }
64 else if (slot_types[i].subtype.intg == BMO_OP_SLOT_SUBTYPE_INT_FLAG) {
65 value = "";
66 set = true;
67 }
68 else {
69 value = "0";
70 }
71 break;
72 case BMO_OP_SLOT_FLT:
73 value = "0.0";
74 break;
75 case BMO_OP_SLOT_PTR:
76 value = "None";
77 break;
78 case BMO_OP_SLOT_MAT:
79 value = "Matrix()";
80 break;
81 case BMO_OP_SLOT_VEC:
82 value = "Vector()";
83 break;
85 value = (slot_types[i].subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) ? "None" : "[]";
86 break;
88 value = "{}";
89 break;
90 }
91 BLI_dynstr_appendf(dyn_str,
92 i ? ", %.*s=%s%s%s%s%s" : "%.*s=%s%s%s%s%s",
93 name_len,
94 slot_types[i].name,
95 set ? "{" : "",
96 quoted ? "'" : "",
97 value,
98 quoted ? "'" : "",
99 set ? "}" : "");
100 i++;
101 }
102
103 ret = BLI_dynstr_get_cstring(dyn_str);
104 BLI_dynstr_free(dyn_str);
105 return ret;
106}
107
108static PyObject *bpy_bmesh_op_doc_get(BPy_BMeshOpFunc *self, void * /*closure*/)
109{
110 PyObject *ret;
111 char *slot_in;
112 char *slot_out;
113 int i;
114
116
117 slot_in = bmp_slots_as_args(bmo_opdefines[i]->slot_types_in, false);
118 slot_out = bmp_slots_as_args(bmo_opdefines[i]->slot_types_out, true);
119
120 ret = PyUnicode_FromFormat("%.200s bmesh.ops.%.200s(bmesh, %s)\n -> dict(%s)",
121 Py_TYPE(self)->tp_name,
122 self->opname,
123 slot_in,
124 slot_out);
125
126 MEM_delete(slot_in);
127 MEM_delete(slot_out);
128
129 return ret;
130}
131
132static PyGetSetDef bpy_bmesh_op_getseters[] = {
133 {"__doc__",
134 reinterpret_cast<getter>(bpy_bmesh_op_doc_get),
135 static_cast<setter>(nullptr),
136 nullptr,
137 nullptr},
138 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
139};
140
141/* Types
142 * ===== */
143
144static PyTypeObject bmesh_op_Type = {
145 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
146 /*tp_name*/ "BMeshOpFunc",
147 /*tp_basicsize*/ sizeof(BPy_BMeshOpFunc),
148 /*tp_itemsize*/ 0,
149 /*tp_dealloc*/ nullptr,
150 /*tp_vectorcall_offset*/ 0,
151 /*tp_getattr*/ nullptr,
152 /*tp_setattr*/ nullptr,
153 /*tp_as_async*/ nullptr,
154 /*tp_repr*/ reinterpret_cast<reprfunc>(bpy_bmesh_op_repr),
155 /*tp_as_number*/ nullptr,
156 /*tp_as_sequence*/ nullptr,
157 /*tp_as_mapping*/ nullptr,
158 /*tp_hash*/ nullptr,
159 /*tp_call*/ reinterpret_cast<ternaryfunc>(BPy_BMO_call),
160 /*tp_str*/ nullptr,
161 /*tp_getattro*/ nullptr,
162 /*tp_setattro*/ nullptr,
163 /*tp_as_buffer*/ nullptr,
164 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
165 /*tp_doc*/ nullptr,
166 /*tp_traverse*/ nullptr,
167 /*tp_clear*/ nullptr,
168 /*tp_richcompare*/ nullptr,
169 /*tp_weaklistoffset*/ 0,
170 /*tp_iter*/ nullptr,
171 /*tp_iternext*/ nullptr,
172 /*tp_methods*/ nullptr,
173 /*tp_members*/ nullptr,
174 /*tp_getset*/ bpy_bmesh_op_getseters,
175 /*tp_base*/ nullptr,
176 /*tp_dict*/ nullptr,
177 /*tp_descr_get*/ nullptr,
178 /*tp_descr_set*/ nullptr,
179 /*tp_dictoffset*/ 0,
180 /*tp_init*/ nullptr,
181 /*tp_alloc*/ nullptr,
182 /*tp_new*/ nullptr,
183 /*tp_free*/ nullptr,
184 /*tp_is_gc*/ nullptr,
185 /*tp_bases*/ nullptr,
186 /*tp_mro*/ nullptr,
187 /*tp_cache*/ nullptr,
188 /*tp_subclasses*/ nullptr,
189 /*tp_weaklist*/ nullptr,
190 /*tp_del*/ nullptr,
191 /*tp_version_tag*/ 0,
192 /*tp_finalize*/ nullptr,
193 /*tp_vectorcall*/ nullptr,
194};
195
196/* bmesh module 'bmesh.ops'
197 * ************************ */
198
199static PyObject *bpy_bmesh_op_CreatePyObject(const char *opname)
200{
201 BPy_BMeshOpFunc *self = PyObject_New(BPy_BMeshOpFunc, &bmesh_op_Type);
202
203 self->opname = opname;
204
205 return reinterpret_cast<PyObject *>(self);
206}
207
208static PyObject *bpy_bmesh_ops_module_getattro(PyObject * /*self*/, PyObject *pyname)
209{
210 const char *opname = PyUnicode_AsUTF8(pyname);
211
212 const int opcode = BMO_opcode_from_opname(opname);
213 if (opcode != -1) {
214 /* Use the static name as `pyname` may be freed once this call returns. */
215 return bpy_bmesh_op_CreatePyObject(bmo_opdefines[opcode]->opname);
216 }
217
218 PyErr_Format(PyExc_AttributeError, "BMeshOpsModule: operator \"%.200s\" doesn't exist", opname);
219 return nullptr;
220}
221
222static PyObject *bpy_bmesh_ops_module_dir(PyObject * /*self*/)
223{
224 const uint tot = bmo_opdefines_total;
225 uint i;
226 PyObject *ret;
227
228 ret = PyList_New(bmo_opdefines_total);
229
230 for (i = 0; i < tot; i++) {
231 PyList_SET_ITEM(ret, i, PyUnicode_FromString(bmo_opdefines[i]->opname));
232 }
233
234 return ret;
235}
236
237#ifdef __GNUC__
238# ifdef __clang__
239# pragma clang diagnostic push
240# pragma clang diagnostic ignored "-Wcast-function-type"
241# else
242# pragma GCC diagnostic push
243# pragma GCC diagnostic ignored "-Wcast-function-type"
244# endif
245#endif
246
247static PyMethodDef BPy_BM_ops_methods[] = {
248 {"__getattr__", static_cast<PyCFunction>(bpy_bmesh_ops_module_getattro), METH_O, nullptr},
249 {"__dir__", reinterpret_cast<PyCFunction>(bpy_bmesh_ops_module_dir), METH_NOARGS, nullptr},
250 {nullptr, nullptr, 0, nullptr},
251};
252
253#ifdef __GNUC__
254# ifdef __clang__
255# pragma clang diagnostic pop
256# else
257# pragma GCC diagnostic pop
258# endif
259#endif
260
262 /* Wrap. */
263 BPy_BM_ops_doc,
264 "Access to BMesh operators.");
265static PyModuleDef BPy_BM_ops_module_def = {
266 /*m_base*/ PyModuleDef_HEAD_INIT,
267 /*m_name*/ "bmesh.ops",
268 /*m_doc*/ BPy_BM_ops_doc,
269 /*m_size*/ 0,
270 /*m_methods*/ BPy_BM_ops_methods,
271 /*m_slots*/ nullptr,
272 /*m_traverse*/ nullptr,
273 /*m_clear*/ nullptr,
274 /*m_free*/ nullptr,
275};
276
278{
279 if (PyType_Ready(&bmesh_op_Type) < 0) {
280 return nullptr;
281 }
282
283 PyObject *submodule = PyModule_Create(&BPy_BM_ops_module_def);
284 return submodule;
285}
286
287} // namespace blender
A dynamically sized string ADT.
Read Blender memory allocation and freeing.
#define BMO_OP_MAX_SLOTS
BMesh Iterators.
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
unsigned int uint
Definition BKE_text.h:55
static PyObject * bpy_bmesh_op_doc_get(BPy_BMeshOpFunc *self, void *)
PyObject * BPyInit_bmesh_ops()
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 char * bmp_slots_as_args(const BMOSlotType slot_types[BMO_OP_MAX_SLOTS], const bool is_out)
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
static PyGetSetDef bpy_bmesh_op_getseters[]
const int bmo_opdefines_total
static PyMethodDef BPy_BM_ops_methods[]
static PyObject * bpy_bmesh_op_repr(BPy_BMeshOpFunc *self)
static PyTypeObject bmesh_op_Type
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE
static PyObject * bpy_bmesh_op_CreatePyObject(const char *opname)
const BMOpDefine * bmo_opdefines[]
static PyObject * bpy_bmesh_ops_module_getattro(PyObject *, PyObject *pyname)
int BMO_opcode_from_opname(const char *opname)
PyObject * BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
static PyObject * bpy_bmesh_ops_module_dir(PyObject *)
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
static PyModuleDef BPy_BM_ops_module_def
PyObject_HEAD const char * opname