1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2012 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/python/bmesh/bmesh_py_ops.c
* \ingroup pybmesh
*
* This file defines the 'bmesh.ops' module.
* Operators from 'opdefines' are wrapped.
*/
#include <Python.h>
#include "BLI_utildefines.h"
#include "MEM_guardedalloc.h"
#include "../generic/py_capi_utils.h"
#include "../mathutils/mathutils.h"
#include "bmesh.h"
#include "bmesh_py_types.h"
#include "bmesh_py_utils.h" /* own include */
static int bpy_bm_op_as_py_error(BMesh *bm)
{
if (BMO_error_occurred(bm)) {
const char *errmsg;
if (BMO_error_get(bm, &errmsg, NULL)) {
PyErr_Format(PyExc_RuntimeError,
"bmesh operator: %.200s",
errmsg);
return -1;
}
}
return 0;
}
/* bmesh operator 'bmesh.ops.*' callable types
* ******************************************* */
PyTypeObject bmesh_op_Type;
typedef struct {
PyObject_HEAD /* required python macro */
const char *opname;
} BPy_BMeshOpFunc;
PyObject *bpy_bmesh_op_CreatePyObject(const char *opname)
{
BPy_BMeshOpFunc *self = PyObject_New(BPy_BMeshOpFunc, &bmesh_op_Type);
self->opname = opname;
return (PyObject *)self;
}
static PyObject *bpy_bmesh_op_repr(BPy_BMeshOpFunc *self)
{
return PyUnicode_FromFormat("<%.200s bmesh.ops.%.200s()>",
Py_TYPE(self)->tp_name,
self->opname);
}
static PyObject *pyrna_op_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
{
BPy_BMesh *py_bm;
BMesh *bm;
BMOperator bmop;
if ((PyTuple_GET_SIZE(args) == 1) &&
(py_bm = (BPy_BMesh *)PyTuple_GET_ITEM(args, 0)) &&
(BPy_BMesh_Check(py_bm))
)
{
BPY_BM_CHECK_OBJ(py_bm);
bm = py_bm->bm;
}
else {
PyErr_SetString(PyExc_TypeError,
"calling a bmesh operator expects a single BMesh (non keyword) "
"as the first argument");
return NULL;
}
/* TODO - error check this!, though we do the error check on attribute access */
BMO_op_init(bm, &bmop, self->opname);
if (kw && PyDict_Size(kw) > 0) {
/* setup properties, see bpy_rna.c: pyrna_py_to_prop()
* which shares this logic for parsing properties */
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(kw, &pos, &key, &value)) {
const char *slot_name = _PyUnicode_AsString(key);
BMOpSlot *slot = BMO_slot_get(&bmop, slot_name);
if (slot == NULL) {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" is invalid for this operator",
self->opname, slot_name);
return NULL;
}
/* now assign the value */
switch (slot->slot_type) {
case BMO_OP_SLOT_BOOL:
{
int param;
param = PyLong_AsLong(value);
if (param < 0) {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
return NULL;
}
else {
slot->data.i = param;
}
break;
}
case BMO_OP_SLOT_INT:
{
int overflow;
long param = PyLong_AsLongAndOverflow(value, &overflow);
if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
PyErr_Format(PyExc_ValueError,
"%.200s: keyword \"%.200s\" value not in 'int' range "
"(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
self->opname, slot_name, Py_TYPE(value)->tp_name);
return NULL;
}
else if (param == -1 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected an int, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
}
else {
slot->data.i = (int)param;
}
break;
}
case BMO_OP_SLOT_FLT:
{
float param = PyFloat_AsDouble(value);
if (param == -1 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected a float, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
}
else {
slot->data.f = param;
}
break;
}
case BMO_OP_SLOT_ELEMENT_BUF:
{
/* there are many ways we could interpret arguments, for now...
* - verts/edges/faces from the mesh direct,
* this way the operator takes every item.
* - `TODO` a plain python sequence (list) of elements.
* - `TODO` an iterator. eg.
* face.verts
* - `TODO` (type, flag) pair, eg.
* ('VERT', {'TAG'})
*/
#define BPY_BM_GENERIC_MESH_TEST(type_string) \
if (((BPy_BMGeneric *)value)->bm != bm) { \
PyErr_Format(PyExc_NotImplementedError, \
"%.200s: keyword \"%.200s\" " type_string " are from another bmesh", \
self->opname, slot_name, slot->slot_type); \
return NULL; \
}
if (BPy_BMVertSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("verts")
BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_VERT);
}
else if (BPy_BMEdgeSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("edges")
BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_EDGE);
}
else if (BPy_BMFaceSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("faces")
BMO_slot_buffer_from_all(bm, &bmop, slot_name, BM_FACE);
}
else if (BPy_BMElemSeq_Check(value)) {
BPY_BM_GENERIC_MESH_TEST("elements")
PyErr_Format(PyExc_NotImplementedError,
"%.200s: keyword \"%.200s\" parsing element lists not working yet!",
self->opname, slot_name, slot->slot_type);
return NULL;
}
else {
PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected "
"a bmesh sequence, list, (htype, flag) pair, not %.200s",
self->opname, slot_name, Py_TYPE(value)->tp_name);
}
#undef BPY_BM_GENERIC_MESH_TEST
break;
}
default:
/* TODO --- many others */
PyErr_Format(PyExc_NotImplementedError,
"%.200s: keyword \"%.200s\" type %d not working yet!",
self->opname, slot_name, slot->slot_type);
return NULL;
break;
}
}
}
BMO_op_exec(bm, &bmop);
BMO_op_finish(bm, &bmop);
if (bpy_bm_op_as_py_error(bm) == -1) {
return NULL;
}
Py_RETURN_NONE;
}
PyTypeObject bmesh_op_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"BMeshOpFunc", /* tp_name */
sizeof(BPy_BMeshOpFunc), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
NULL, /* tp_dealloc */
NULL, /* printfunc tp_print; */
NULL, /* getattrfunc tp_getattr; */
NULL, /* setattrfunc tp_setattr; */
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
(reprfunc) bpy_bmesh_op_repr, /* tp_repr */
/* Method suites for standard classes */
NULL, /* PyNumberMethods *tp_as_number; */
NULL, /* PySequenceMethods *tp_as_sequence; */
NULL, /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */
NULL, /* hashfunc tp_hash; */
(ternaryfunc)pyrna_op_call, /* ternaryfunc tp_call; */
NULL, /* reprfunc tp_str; */
/* will only use these if this is a subtype of a py class */
NULL, /* getattrofunc tp_getattro; */
NULL, /* setattrofunc tp_setattro; */
/* Functions to access object as input/output buffer */
NULL, /* PyBufferProcs *tp_as_buffer; */
/*** Flags to define presence of optional/expanded features ***/
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
NULL, /* char *tp_doc; Documentation string */
/*** Assigned meaning in release 2.0 ***/
/* call function for all accessible objects */
NULL, /* traverseproc tp_traverse; */
/* delete references to contained objects */
NULL, /* inquiry tp_clear; */
/*** Assigned meaning in release 2.1 ***/
/*** rich comparisons ***/
NULL, /* richcmpfunc tp_richcompare; */
/*** weak reference enabler ***/
0,
/*** Added in release 2.2 ***/
/* Iterators */
NULL, /* getiterfunc tp_iter; */
NULL, /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/
NULL, /* struct PyMethodDef *tp_methods; */
NULL, /* struct PyMemberDef *tp_members; */
NULL, /* struct PyGetSetDef *tp_getset; */
NULL, /* struct _typeobject *tp_base; */
NULL, /* PyObject *tp_dict; */
NULL, /* descrgetfunc tp_descr_get; */
NULL, /* descrsetfunc tp_descr_set; */
0, /* long tp_dictoffset; */
NULL, /* initproc tp_init; */
NULL, /* allocfunc tp_alloc; */
NULL, /* newfunc tp_new; */
/* Low-level free-memory routine */
NULL, /* freefunc tp_free; */
/* For PyObject_IS_GC */
NULL, /* inquiry tp_is_gc; */
NULL, /* PyObject *tp_bases; */
/* method resolution order */
NULL, /* PyObject *tp_mro; */
NULL, /* PyObject *tp_cache; */
NULL, /* PyObject *tp_subclasses; */
NULL, /* PyObject *tp_weaklist; */
NULL
};
/* bmesh fake module 'bmesh.ops'
* ***************************** */
static PyObject *bpy_bmesh_fmod_getattro(PyObject *UNUSED(self), PyObject *pyname)
{
const unsigned int tot = bmesh_total_ops;
unsigned int i;
const char *name = _PyUnicode_AsString(pyname);
for (i = 0; i < tot; i++) {
if (strcmp(opdefines[i]->name, name) == 0) {
return bpy_bmesh_op_CreatePyObject(opdefines[i]->name);
}
}
PyErr_Format(PyExc_AttributeError,
"BMeshOpsModule: , operator \"%.200s\" doesn't exist",
name);
return NULL;
}
static PyObject *bpy_bmesh_fmod_dir(PyObject *UNUSED(self))
{
const unsigned int tot = bmesh_total_ops;
unsigned int i;
PyObject *ret;
ret = PyList_New(bmesh_total_ops);
for (i = 0; i < tot; i++) {
PyList_SET_ITEM(ret, i, PyUnicode_FromString(opdefines[i]->name));
}
return ret;
}
static struct PyMethodDef bpy_bmesh_fmod_methods[] = {
{"__dir__", (PyCFunction)bpy_bmesh_fmod_dir, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyTypeObject bmesh_ops_fakemod_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"BMeshOpsModule", /* tp_name */
0, /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
NULL, /* tp_dealloc */
NULL, /* printfunc tp_print; */
NULL, /* getattrfunc tp_getattr; */
NULL, /* setattrfunc tp_setattr; */
NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
NULL, /* tp_repr */
/* Method suites for standard classes */
NULL, /* PyNumberMethods *tp_as_number; */
NULL, /* PySequenceMethods *tp_as_sequence; */
NULL, /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */
NULL, /* hashfunc tp_hash; */
NULL, /* ternaryfunc tp_call; */
NULL, /* reprfunc tp_str; */
/* will only use these if this is a subtype of a py class */
bpy_bmesh_fmod_getattro, /* getattrofunc tp_getattro; */
NULL, /* setattrofunc tp_setattro; */
/* Functions to access object as input/output buffer */
NULL, /* PyBufferProcs *tp_as_buffer; */
/*** Flags to define presence of optional/expanded features ***/
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
NULL, /* char *tp_doc; Documentation string */
/*** Assigned meaning in release 2.0 ***/
/* call function for all accessible objects */
NULL, /* traverseproc tp_traverse; */
/* delete references to contained objects */
NULL, /* inquiry tp_clear; */
/*** Assigned meaning in release 2.1 ***/
/*** rich comparisons ***/
NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
/*** weak reference enabler ***/
0,
/*** Added in release 2.2 ***/
/* Iterators */
NULL, /* getiterfunc tp_iter; */
NULL, /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/
bpy_bmesh_fmod_methods, /* struct PyMethodDef *tp_methods; */
NULL, /* struct PyMemberDef *tp_members; */
NULL, /* struct PyGetSetDef *tp_getset; */
NULL, /* struct _typeobject *tp_base; */
NULL, /* PyObject *tp_dict; */
NULL, /* descrgetfunc tp_descr_get; */
NULL, /* descrsetfunc tp_descr_set; */
0, /* long tp_dictoffset; */
NULL, /* initproc tp_init; */
NULL, /* allocfunc tp_alloc; */
NULL, /* newfunc tp_new; */
/* Low-level free-memory routine */
NULL, /* freefunc tp_free; */
/* For PyObject_IS_GC */
NULL, /* inquiry tp_is_gc; */
NULL, /* PyObject *tp_bases; */
/* method resolution order */
NULL, /* PyObject *tp_mro; */
NULL, /* PyObject *tp_cache; */
NULL, /* PyObject *tp_subclasses; */
NULL, /* PyObject *tp_weaklist; */
NULL
};
PyObject *BPyInit_bmesh_ops(void)
{
PyObject *submodule;
if (PyType_Ready(&bmesh_ops_fakemod_Type) < 0)
return NULL;
if (PyType_Ready(&bmesh_op_Type) < 0)
return NULL;
submodule = PyObject_New(PyObject, &bmesh_ops_fakemod_Type);
/* prevent further creation of instances */
bmesh_ops_fakemod_Type.tp_init = NULL;
bmesh_ops_fakemod_Type.tp_new = NULL;
return submodule;
}
|