Blender V5.3
bmesh_py_ops_call.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
12
13#include <Python.h>
14
15#include "BLI_utildefines.hh"
16
18
19#include "bmesh.hh"
20
21#include "bmesh_py_ops_call.hh" /* own include */
22
23#include "bmesh_py_types.hh"
24
26
27namespace blender {
28
29BLI_STATIC_ASSERT(sizeof(PyC_FlagSet) == sizeof(BMO_FlagSet), "size mismatch");
30
32{
34 /* NOTE: we could have multiple errors. */
35 const char *errmsg;
36 if (BMO_error_get(bm, &errmsg, nullptr, nullptr)) {
37 PyErr_Format(PyExc_RuntimeError, "bmesh operator: %.200s", errmsg);
39 return -1;
40 }
41 }
42 return 0;
43}
44
54 BMesh *bm,
55 const char htype,
56 /* for error messages */
57 const char *opname,
58 const char *slot_name,
59 const char *descr)
60{
61 if (!BPy_BMElem_Check(value) || !(value->ele->head.htype & htype)) {
62 PyErr_Format(PyExc_TypeError,
63 "%.200s: keyword \"%.200s\" %.200s, expected a %.200s not %.200s",
64 opname,
65 slot_name,
66 descr,
68 Py_TYPE(value)->tp_name);
69 return -1;
70 }
71 if (value->bm == nullptr) {
72 PyErr_Format(PyExc_TypeError,
73 "%.200s: keyword \"%.200s\" %.200s invalidated element",
74 opname,
75 slot_name,
76 descr);
77 return -1;
78 }
79 if (value->bm != bm) { /* we may want to make this check optional by setting 'bm' to nullptr */
80 PyErr_Format(PyExc_TypeError,
81 "%.200s: keyword \"%.200s\" %.200s from another mesh",
82 opname,
83 slot_name,
84 descr);
85 return -1;
86 }
87 return 0;
88}
89
100 BMesh *bm,
101 const char htype_py,
102 const char htype_bmo,
103 /* for error messages */
104 const char *opname,
105 const char *slot_name,
106 const char *descr)
107{
108 if (value->bm == nullptr) {
109 PyErr_Format(PyExc_TypeError,
110 "%.200s: keyword \"%.200s\" %.200s, invalidated sequence",
111 opname,
112 slot_name,
113 descr);
114 return -1;
115 }
116 if (value->bm != bm) { /* we may want to make this check optional by setting 'bm' to nullptr */
117 PyErr_Format(PyExc_TypeError,
118 "%.200s: keyword \"%.200s\" %.200s, invalidated sequence",
119 opname,
120 slot_name,
121 descr);
122 return -1;
123 }
124 if ((htype_py & htype_bmo) == 0) {
125 char str_bmo[32];
126 char str_py[32];
127 PyErr_Format(PyExc_TypeError,
128 "%.200s: keyword \"%.200s\" %.200s, expected "
129 "a sequence of %.200s not %.200s",
130 opname,
131 slot_name,
132 descr,
133 BPy_BMElem_StringFromHType_ex(htype_bmo, str_bmo),
134 BPy_BMElem_StringFromHType_ex(htype_py, str_py));
135 return -1;
136 }
137
138 return 0;
139}
140
145 BMOperator *bmop,
146 BMOpSlot *slot,
147 PyObject *value,
148 /* the are just for exception messages */
149 const char *opname,
150 const char *slot_name)
151{
152 switch (slot->slot_type) {
153 case BMO_OP_SLOT_BOOL: {
154 const int param = PyC_Long_AsBool(value);
155
156 if (param == -1) {
157 PyErr_Format(PyExc_TypeError,
158 "%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s",
159 opname,
160 slot_name,
161 Py_TYPE(value)->tp_name);
162 return -1;
163 }
164
165 BMO_SLOT_AS_BOOL(slot) = param;
166
167 break;
168 }
169 case BMO_OP_SLOT_INT: {
171 int enum_val = -1;
172 PyC_FlagSet *items = reinterpret_cast<PyC_FlagSet *>(slot->data.enum_data.flags);
173 const char *enum_str = PyUnicode_AsUTF8(value);
174
175 if (enum_str == nullptr) {
176 PyErr_Format(PyExc_TypeError,
177 "%.200s: keyword \"%.200s\" expected a string, not %.200s",
178 opname,
179 slot_name,
180 Py_TYPE(value)->tp_name);
181 return -1;
182 }
183
184 if (PyC_FlagSet_ValueFromID(items, enum_str, &enum_val, slot_name) == -1) {
185 return -1;
186 }
187
188 BMO_SLOT_AS_INT(slot) = enum_val;
189 }
191 int flag = 0;
192 PyC_FlagSet *items = reinterpret_cast<PyC_FlagSet *>(slot->data.enum_data.flags);
193
194 if (PyC_FlagSet_ToBitfield(items, value, &flag, slot_name) == -1) {
195 return -1;
196 }
197
198 BMO_SLOT_AS_INT(slot) = flag;
199 }
200 else {
201 const int param = PyC_Long_AsI32(value);
202
203 if (param == -1 && PyErr_Occurred()) {
204 PyErr_Format(PyExc_TypeError,
205 "%.200s: keyword \"%.200s\" expected an int, not %.200s",
206 opname,
207 slot_name,
208 Py_TYPE(value)->tp_name);
209 return -1;
210 }
211
212 BMO_SLOT_AS_INT(slot) = param;
213 }
214 break;
215 }
216 case BMO_OP_SLOT_FLT: {
217 const float param = PyFloat_AsDouble(value);
218 if (param == -1 && PyErr_Occurred()) {
219 PyErr_Format(PyExc_TypeError,
220 "%.200s: keyword \"%.200s\" expected a float, not %.200s",
221 opname,
222 slot_name,
223 Py_TYPE(value)->tp_name);
224 return -1;
225 }
226
227 BMO_SLOT_AS_FLOAT(slot) = param;
228
229 break;
230 }
231 case BMO_OP_SLOT_MAT: {
232 /* XXX: BMesh operator design is crappy here, operator slot should define matrix size,
233 * not the caller! */
234 MatrixObject *pymat;
235 if (!Matrix_ParseAny(value, &pymat)) {
236 return -1;
237 }
238 const ushort size = pymat->col_num;
239 if ((size != pymat->row_num) || !ELEM(size, 3, 4)) {
240 PyErr_Format(PyExc_TypeError,
241 "%.200s: keyword \"%.200s\" expected a 3x3 or 4x4 matrix",
242 opname,
243 slot_name);
244 return -1;
245 }
246
247 BMO_slot_mat_set(bmop, bmop->slots_in, slot_name, pymat->matrix, size);
248 break;
249 }
250 case BMO_OP_SLOT_VEC: {
251 /* passing slot name here is a bit non-descriptive */
252 if (mathutils_array_parse(BMO_SLOT_AS_VECTOR(slot), 3, 3, value, slot_name) == -1) {
253 return -1;
254 }
255 break;
256 }
259 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(value),
260 bm,
262 opname,
263 slot_name,
264 "single element") == -1)
265 {
266 return -1; /* error is set in bpy_slot_from_py_elem_check() */
267 }
268
270 bmop, slot, &(reinterpret_cast<BPy_BMElem *>(value))->ele->head);
271 }
272 else {
273 /* there are many ways we could interpret arguments, for now...
274 * - verts/edges/faces from the mesh direct,
275 * this way the operator takes every item.
276 * - `TODO` a plain python sequence (list) of elements.
277 * - `TODO` an iterator. eg.
278 * face.verts
279 * - `TODO` (type, flag) pair, eg.
280 * ('VERT', {'TAG'})
281 */
282
283 if (BPy_BMVertSeq_Check(value)) {
284 if (bpy_slot_from_py_elemseq_check(reinterpret_cast<BPy_BMGeneric *>(value),
285 bm,
286 BM_VERT,
288 opname,
289 slot_name,
290 "element buffer") == -1)
291 {
292 return -1; /* error is set in bpy_slot_from_py_elem_check() */
293 }
294
295 BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_VERT);
296 }
297 else if (BPy_BMEdgeSeq_Check(value)) {
298 if (bpy_slot_from_py_elemseq_check(reinterpret_cast<BPy_BMGeneric *>(value),
299 bm,
300 BM_EDGE,
302 opname,
303 slot_name,
304 "element buffer") == -1)
305 {
306 return -1; /* error is set in bpy_slot_from_py_elem_check() */
307 }
308
309 BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_EDGE);
310 }
311 else if (BPy_BMFaceSeq_Check(value)) {
312 if (bpy_slot_from_py_elemseq_check(reinterpret_cast<BPy_BMGeneric *>(value),
313 bm,
314 BM_FACE,
316 opname,
317 slot_name,
318 "element buffer") == -1)
319 {
320 return -1; /* error is set in bpy_slot_from_py_elem_check() */
321 }
322 BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_FACE);
323 }
324
325 else if (BPy_BMElemSeq_Check(value)) {
326 BMIter iter;
327 BMHeader *ele;
328 int tot;
329 uint i;
330
332 reinterpret_cast<BPy_BMGeneric *>(value),
333 bm,
334 bm_iter_itype_htype_map[(reinterpret_cast<BPy_BMElemSeq *>(value))->itype],
336 opname,
337 slot_name,
338 "element buffer") == -1)
339 {
340 return -1; /* error is set in bpy_slot_from_py_elem_check() */
341 }
342
343 /* this will loop over all elements which is a shame but
344 * we need to know this before alloc */
345 /* calls bpy_bmelemseq_length() */
346 tot = Py_TYPE(value)->tp_as_sequence->sq_length(value);
347
348 BMO_slot_buffer_alloc(bmop, bmop->slots_in, slot_name, tot);
349
350 i = 0;
351 BM_ITER_BPY_BM_SEQ (ele, &iter, ((BPy_BMElemSeq *)value)) {
352 slot->data.buf[i] = ele;
353 i++;
354 }
355 }
356 /* keep this last */
357 else if (PySequence_Check(value)) {
358 BMElem **elem_array = nullptr;
359 Py_ssize_t elem_array_len;
360
361 elem_array = static_cast<BMElem **>(
363 value,
364 0,
365 PY_SSIZE_T_MAX,
366 &elem_array_len,
368 true,
369 true,
370 slot_name));
371
372 /* error is set above */
373 if (elem_array == nullptr) {
374 return -1;
375 }
376
377 BMO_slot_buffer_alloc(bmop, bmop->slots_in, slot_name, elem_array_len);
378 memcpy(slot->data.buf, elem_array, sizeof(void *) * elem_array_len);
379 PyMem_FREE(elem_array);
380 }
381 else {
382 PyErr_Format(PyExc_TypeError,
383 "%.200s: keyword \"%.200s\" expected "
384 "a bmesh sequence, list, (htype, flag) pair, not %.200s",
385 opname,
386 slot_name,
387 Py_TYPE(value)->tp_name);
388 return -1;
389 }
390 }
391 break;
392 }
393 case BMO_OP_SLOT_MAPPING: {
394 /* first check types */
396 if (!PyDict_Check(value)) {
397 PyErr_Format(PyExc_TypeError,
398 "%.200s: keyword \"%.200s\" expected "
399 "a dict, not %.200s",
400 opname,
401 slot_name,
402 Py_TYPE(value)->tp_name);
403 return -1;
404 }
405 }
406 else {
407 if (!PySet_Check(value)) {
408 PyErr_Format(PyExc_TypeError,
409 "%.200s: keyword \"%.200s\" expected "
410 "a set, not %.200s",
411 opname,
412 slot_name,
413 Py_TYPE(value)->tp_name);
414 return -1;
415 }
416 }
417
418 switch (slot->slot_subtype.map) {
420 if (PyDict_Size(value) > 0) {
421 PyObject *arg_key, *arg_value;
422 Py_ssize_t arg_pos = 0;
423 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
424 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_key),
425 bm,
427 opname,
428 slot_name,
429 "invalid key in dict") == -1)
430 {
431 return -1; /* error is set in bpy_slot_from_py_elem_check() */
432 }
433
434 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_value),
435 bm,
437 opname,
438 slot_name,
439 "invalid value in dict") == -1)
440 {
441 return -1; /* error is set in bpy_slot_from_py_elem_check() */
442 }
443
445 slot,
446 (reinterpret_cast<BPy_BMElem *>(arg_key))->ele,
447 (reinterpret_cast<BPy_BMElem *>(arg_value))->ele);
448 }
449 }
450 break;
451 }
453 if (PyDict_Size(value) > 0) {
454 PyObject *arg_key, *arg_value;
455 Py_ssize_t arg_pos = 0;
456 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
457 float value_f;
458
459 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_key),
460 bm,
462 opname,
463 slot_name,
464 "invalid key in dict") == -1)
465 {
466 return -1; /* error is set in bpy_slot_from_py_elem_check() */
467 }
468
469 value_f = PyFloat_AsDouble(arg_value);
470
471 if (value_f == -1.0f && PyErr_Occurred()) {
472 PyErr_Format(PyExc_TypeError,
473 "%.200s: keyword \"%.200s\" expected "
474 "a dict with float values, not %.200s",
475 opname,
476 slot_name,
477 Py_TYPE(arg_value)->tp_name);
478 return -1;
479 }
480
482 bmop, slot, (reinterpret_cast<BPy_BMElem *>(arg_key))->ele, value_f);
483 }
484 }
485 break;
486 }
488 if (PyDict_Size(value) > 0) {
489 PyObject *arg_key, *arg_value;
490 Py_ssize_t arg_pos = 0;
491 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
492 int value_i;
493
494 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_key),
495 bm,
497 opname,
498 slot_name,
499 "invalid key in dict") == -1)
500 {
501 return -1; /* error is set in bpy_slot_from_py_elem_check() */
502 }
503
504 value_i = PyC_Long_AsI32(arg_value);
505
506 if (value_i == -1 && PyErr_Occurred()) {
507 PyErr_Format(PyExc_TypeError,
508 "%.200s: keyword \"%.200s\" expected "
509 "a dict with int values, not %.200s",
510 opname,
511 slot_name,
512 Py_TYPE(arg_value)->tp_name);
513 return -1;
514 }
515
517 bmop, slot, (reinterpret_cast<BPy_BMElem *>(arg_key))->ele, value_i);
518 }
519 }
520 break;
521 }
523 if (PyDict_Size(value) > 0) {
524 PyObject *arg_key, *arg_value;
525 Py_ssize_t arg_pos = 0;
526 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
527 int value_i;
528
529 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_key),
530 bm,
532 opname,
533 slot_name,
534 "invalid key in dict") == -1)
535 {
536 return -1; /* error is set in bpy_slot_from_py_elem_check() */
537 }
538
539 value_i = PyC_Long_AsI32(arg_value);
540
541 if (value_i == -1 && PyErr_Occurred()) {
542 PyErr_Format(PyExc_TypeError,
543 "%.200s: keyword \"%.200s\" expected "
544 "a dict with bool values, not %.200s",
545 opname,
546 slot_name,
547 Py_TYPE(arg_value)->tp_name);
548 return -1;
549 }
550
552 bmop, slot, (reinterpret_cast<BPy_BMElem *>(arg_key))->ele, value_i != 0);
553 }
554 }
555 break;
556 }
558 if (PySet_GET_SIZE(value) > 0) {
559 PyObject *it = PyObject_GetIter(value);
560 PyObject *arg_key;
561 while ((arg_key = PyIter_Next(it))) {
562 /* Borrow from the set. */
563 Py_DECREF(arg_key);
564
565 if (bpy_slot_from_py_elem_check(reinterpret_cast<BPy_BMElem *>(arg_key),
566 bm,
568 opname,
569 slot_name,
570 "invalid key in set") == -1)
571 {
572 /* Error is set in #bpy_slot_from_py_elem_check(). */
573 break;
574 }
575
577 bmop, slot, (reinterpret_cast<BPy_BMElem *>(arg_key))->ele);
578 }
579 Py_DECREF(it);
580 if (arg_key) {
581 return -1;
582 }
583 }
584 break;
585 }
587 /* can't convert from these */
588 PyErr_Format(PyExc_NotImplementedError,
589 "This arguments mapping subtype %d is not supported",
590 slot->slot_subtype.map);
591 return -1;
592 }
593 }
594 break;
595 }
596 default:
597 /* TODO: many others. */
598 PyErr_Format(PyExc_NotImplementedError,
599 "%.200s: keyword \"%.200s\" type %d not working yet!",
600 opname,
601 slot_name,
602 slot->slot_type);
603 return -1;
604 }
605
606 /* all is well */
607 return 0;
608}
609
615static PyObject *bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
616{
617 PyObject *item = nullptr;
618
619 /* keep switch in same order as above */
620 switch (slot->slot_type) {
621 case BMO_OP_SLOT_BOOL:
622 item = PyBool_FromLong(BMO_SLOT_AS_BOOL(slot));
623 break;
624 case BMO_OP_SLOT_INT:
625 item = PyLong_FromLong(BMO_SLOT_AS_INT(slot));
626 break;
627 case BMO_OP_SLOT_FLT:
628 item = PyFloat_FromDouble(double(BMO_SLOT_AS_FLOAT(slot)));
629 break;
630 case BMO_OP_SLOT_MAT:
632 reinterpret_cast<float *> BMO_SLOT_AS_MATRIX(slot), 4, 4, nullptr);
633 break;
634 case BMO_OP_SLOT_VEC:
635 item = Vector_CreatePyObject(BMO_SLOT_AS_VECTOR(slot), slot->len, nullptr);
636 break;
637 case BMO_OP_SLOT_PTR:
638 BLI_assert(0); /* currently we don't have any pointer return values in use */
639 item = Py_NewRef(Py_None);
640 break;
643 BMHeader *ele = static_cast<BMHeader *>(BMO_slot_buffer_get_single(slot));
644 item = ele ? BPy_BMElem_CreatePyObject(bm, ele) : Py_NewRef(Py_None);
645 }
646 else {
647 const int size = slot->len;
648 void **buffer = BMO_SLOT_AS_BUFFER(slot);
649 int j;
650
651 item = PyList_New(size);
652 for (j = 0; j < size; j++) {
653 BMHeader *ele = static_cast<BMHeader *>(buffer[j]);
654 PyList_SET_ITEM(item, j, BPy_BMElem_CreatePyObject(bm, ele));
655 }
656 }
657 break;
658 }
659 case BMO_OP_SLOT_MAPPING: {
660 GHash *slot_hash = BMO_SLOT_AS_GHASH(slot);
661 GHashIterator hash_iter;
662
663 switch (slot->slot_subtype.map) {
665 item = _PyDict_NewPresized(slot_hash ? BLI_ghash_len(slot_hash) : 0);
666 if (slot_hash) {
667 GHASH_ITER (hash_iter, slot_hash) {
668 BMHeader *ele_key = static_cast<BMHeader *>(BLI_ghashIterator_getKey(&hash_iter));
669 void *ele_val = BLI_ghashIterator_getValue(&hash_iter);
670
671 PyObject *py_key = BPy_BMElem_CreatePyObject(bm, ele_key);
672 PyObject *py_val = BPy_BMElem_CreatePyObject(bm, static_cast<BMHeader *>(ele_val));
673
674 PyDict_SetItem(item, py_key, py_val);
675 Py_DECREF(py_key);
676 Py_DECREF(py_val);
677 }
678 }
679 break;
680 }
682 item = _PyDict_NewPresized(slot_hash ? BLI_ghash_len(slot_hash) : 0);
683 if (slot_hash) {
684 GHASH_ITER (hash_iter, slot_hash) {
685 BMHeader *ele_key = static_cast<BMHeader *>(BLI_ghashIterator_getKey(&hash_iter));
686 void *ele_val = BLI_ghashIterator_getValue(&hash_iter);
687
688 PyObject *py_key = BPy_BMElem_CreatePyObject(bm, ele_key);
689 PyObject *py_val = PyFloat_FromDouble(*reinterpret_cast<float *>(&ele_val));
690
691 PyDict_SetItem(item, py_key, py_val);
692 Py_DECREF(py_key);
693 Py_DECREF(py_val);
694 }
695 }
696 break;
697 }
699 item = _PyDict_NewPresized(slot_hash ? BLI_ghash_len(slot_hash) : 0);
700 if (slot_hash) {
701 GHASH_ITER (hash_iter, slot_hash) {
702 BMHeader *ele_key = static_cast<BMHeader *>(BLI_ghashIterator_getKey(&hash_iter));
703 void *ele_val = BLI_ghashIterator_getValue(&hash_iter);
704
705 PyObject *py_key = BPy_BMElem_CreatePyObject(bm, ele_key);
706 PyObject *py_val = PyLong_FromLong(*reinterpret_cast<int *>(&ele_val));
707
708 PyDict_SetItem(item, py_key, py_val);
709 Py_DECREF(py_key);
710 Py_DECREF(py_val);
711 }
712 }
713 break;
714 }
716 item = _PyDict_NewPresized(slot_hash ? BLI_ghash_len(slot_hash) : 0);
717 if (slot_hash) {
718 GHASH_ITER (hash_iter, slot_hash) {
719 BMHeader *ele_key = static_cast<BMHeader *>(BLI_ghashIterator_getKey(&hash_iter));
720 void *ele_val = BLI_ghashIterator_getValue(&hash_iter);
721
722 PyObject *py_key = BPy_BMElem_CreatePyObject(bm, ele_key);
723 PyObject *py_val = PyBool_FromLong(*reinterpret_cast<bool *>(&ele_val));
724
725 PyDict_SetItem(item, py_key, py_val);
726 Py_DECREF(py_key);
727 Py_DECREF(py_val);
728 }
729 }
730 break;
731 }
733 item = PySet_New(nullptr);
734 if (slot_hash) {
735 GHASH_ITER (hash_iter, slot_hash) {
736 BMHeader *ele_key = static_cast<BMHeader *>(BLI_ghashIterator_getKey(&hash_iter));
737
738 PyObject *py_key = BPy_BMElem_CreatePyObject(bm, ele_key);
739
740 PySet_Add(item, py_key);
741
742 Py_DECREF(py_key);
743 }
744 }
745 break;
746 }
748 /* can't convert from these */
749 item = Py_NewRef(Py_None);
750 break;
751 }
752 break;
753 }
754 }
755 BLI_assert(item != nullptr);
756
757 return item;
758}
759
760PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
761{
762 PyObject *ret;
763 BPy_BMesh *py_bm;
764 BMesh *bm;
765
766 BMOperator bmop;
767
768 if ((PyTuple_GET_SIZE(args) == 1) &&
769 (py_bm = reinterpret_cast<BPy_BMesh *> PyTuple_GET_ITEM(args, 0)) && BPy_BMesh_Check(py_bm))
770 {
771 BPY_BM_CHECK_OBJ(py_bm);
772 bm = py_bm->bm;
773
774 if (bm->use_toolflags == false) {
775 PyErr_SetString(PyExc_ValueError, "bmesh created with 'use_operators=False'");
776 return nullptr;
777 }
778
779 /* could complain about entering with exceptions... */
781 }
782 else {
783 PyErr_SetString(PyExc_TypeError,
784 "bmesh operators expect a single BMesh positional argument, all other args "
785 "must be keywords");
786 return nullptr;
787 }
788
789 /* TODO: error check this!, though we do the error check on attribute access. */
790 /* TODO: make flags optional. */
791 BMO_op_init(bm, &bmop, BMO_FLAG_DEFAULTS, self->opname);
792
793 if (kw && PyDict_Size(kw) > 0) {
794 /* Setup properties, see `bpy_rna.cc`: #pyrna_py_to_prop()
795 * which shares this logic for parsing properties. */
796
797 PyObject *key, *value;
798 Py_ssize_t pos = 0;
799 while (PyDict_Next(kw, &pos, &key, &value)) {
800 const char *slot_name = PyUnicode_AsUTF8(key);
801 BMOpSlot *slot;
802
803 if (!BMO_slot_exists(bmop.slots_in, slot_name)) {
804 PyErr_Format(PyExc_TypeError,
805 "%.200s: keyword \"%.200s\" is invalid for this operator",
806 self->opname,
807 slot_name);
808 BMO_op_finish(bm, &bmop);
809 return nullptr;
810 }
811
812 slot = BMO_slot_get(bmop.slots_in, slot_name);
813
814 /* now assign the value */
815 if (bpy_slot_from_py(bm, &bmop, slot, value, self->opname, slot_name) == -1) {
816 BMO_op_finish(bm, &bmop);
817 return nullptr;
818 }
819 }
820 }
821
822 BMO_op_exec(bm, &bmop);
823
824 /* from here until the end of the function, no returns, just set 'ret' */
825 if (bpy_bm_op_as_py_error(bm) == -1) [[unlikely]] {
826 ret = nullptr; /* exception raised above */
827 }
828 else if (bmop.slots_out[0].slot_name == nullptr) {
829 ret = Py_NewRef(Py_None);
830 }
831 else {
832 /* build return value */
833 int i;
834 ret = PyDict_New();
835
836 for (i = 0; bmop.slots_out[i].slot_name; i++) {
837 // BMOpDefine *op_def = opdefines[bmop.type];
838 // BMOSlotType *slot_type = op_def->slot_types_out[i];
839 BMOpSlot *slot = &bmop.slots_out[i];
840 PyObject *item;
841
842 /* this function doesn't throw exceptions */
843 item = bpy_slot_to_py(bm, slot);
844 if (item == nullptr) {
845 item = Py_NewRef(Py_None);
846 }
847
848#if 1
849 /* Temporary code, strip off `.out` while we keep this convention. */
850 {
851 char slot_name_strip[MAX_SLOTNAME];
852 const char *ch = strchr(slot->slot_name, '.'); /* can't fail! */
853 const int tot = ch - slot->slot_name;
854 BLI_assert(ch != nullptr);
855 memcpy(slot_name_strip, slot->slot_name, tot);
856 slot_name_strip[tot] = '\0';
857 PyDict_SetItemString(ret, slot_name_strip, item);
858 }
859#else
860 PyDict_SetItemString(ret, slot->slot_name, item);
861#endif
862 Py_DECREF(item);
863 }
864 }
865
866 BMO_op_finish(bm, &bmop);
867 return ret;
868}
869
870} // namespace blender
#define GHASH_ITER(gh_iter_, ghash_)
Definition BLI_ghash.hh:320
#define ELEM(...)
#define BM_ALL_NOLOOP
#define BMO_SLOT_AS_VECTOR(slot)
#define BMO_SLOT_AS_FLOAT(slot)
#define MAX_SLOTNAME
#define BMO_SLOT_AS_BUFFER(slot)
#define BMO_SLOT_AS_GHASH(slot)
#define BMO_SLOT_AS_BOOL(slot)
#define BMO_SLOT_AS_MATRIX(slot)
#define BMO_FLAG_DEFAULTS
#define BMO_SLOT_AS_INT(slot)
#define BPY_BM_CHECK_OBJ(obj)
#define BPy_BMVertSeq_Check(v)
#define BPy_BMFaceSeq_Check(v)
#define BPy_BMElemSeq_Check(v)
#define BPy_BMElem_Check(v)
#define BPy_BMesh_Check(v)
#define BPy_BMEdgeSeq_Check(v)
#define BM_ITER_BPY_BM_SEQ(ele, iter, bpy_bmelemseq)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool BMO_error_occurred_at_level(BMesh *bm, eBMOpErrorLevel level)
int PyC_FlagSet_ToBitfield(const PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix)
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.hh:297
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
@ BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL
@ BMO_OP_SLOT_SUBTYPE_MAP_EMPTY
unsigned int uint
Definition BKE_text.h:55
const char bm_iter_itype_htype_map[BM_ITYPE_MAX]
void BMO_error_clear(BMesh *bm)
unsigned short ushort
Definition BKE_text.h:56
static PyObject * bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
char * BPy_BMElem_StringFromHType(const char htype)
int PyC_FlagSet_ValueFromID(const PyC_FlagSet *item, const char *identifier, int *r_value, const char *error_prefix)
void BMO_slot_buffer_from_all(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype)
BMO_ALL_TO_SLOT.
int Matrix_ParseAny(PyObject *o, void *p)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
void * BMO_slot_buffer_get_single(BMOpSlot *slot)
BLI_INLINE void BMO_slot_map_bool_insert(BMOperator *op, BMOpSlot *slot, void *element, const bool val)
BLI_INLINE void BMO_slot_map_empty_insert(BMOperator *op, BMOpSlot *slot, const void *element)
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.hh:301
BMesh const char itype
PyObject * Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
void BMO_slot_mat_set(BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const float *mat, int size)
PyObject * Matrix_CreatePyObject(const float *mat, const ushort col_num, const ushort row_num, PyTypeObject *base_type)
BLI_INLINE void BMO_slot_map_float_insert(BMOperator *op, BMOpSlot *slot, void *element, const float val)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
static int bpy_slot_from_py(BMesh *bm, BMOperator *bmop, BMOpSlot *slot, PyObject *value, const char *opname, const char *slot_name)
PyObject * BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele)
@ BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE
unsigned int BLI_ghash_len(const GHash *gh) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:706
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
int PyC_Long_AsBool(PyObject *value)
static int bpy_slot_from_py_elem_check(BPy_BMElem *value, BMesh *bm, const char htype, const char *opname, const char *slot_name, const char *descr)
Utility function to check BMVert/BMEdge/BMFace's.
void * BMO_slot_buffer_alloc(BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, int len)
BLI_INLINE void BMO_slot_map_int_insert(BMOperator *op, BMOpSlot *slot, void *element, const int val)
static int bpy_slot_from_py_elemseq_check(BPy_BMGeneric *value, BMesh *bm, const char htype_py, const char htype_bmo, const char *opname, const char *slot_name, const char *descr)
Utility function to check BMVertSeq/BMEdgeSeq/BMFaceSeq's.
void BMO_slot_buffer_from_single(BMOperator *op, BMOpSlot *slot, BMHeader *ele)
char * BPy_BMElem_StringFromHType_ex(const char htype, char ret[32])
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
PyObject * BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
void * BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_ssize_t max, Py_ssize_t *r_seq_num, const char htype, const bool do_unique_check, const bool do_bm_check, const char *error_prefix)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
BLI_INLINE void BMO_slot_map_elem_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
@ BMO_ERROR_FATAL
void bool BMO_error_get(BMesh *bm, const char **r_msg, BMOperator **r_op, eBMOpErrorLevel *r_level)
bool BMO_slot_exists(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK HAS SLOT.
static int bpy_bm_op_as_py_error(BMesh *bm)
eBMOpSlotSubType_Union slot_subtype
union blender::BMOpSlot::@376352321363166111241172004263244043165250166277 data
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
PyObject_HEAD BMesh * bm
PyObject_HEAD BMesh * bm
PyObject_HEAD const char * opname
PyObject_HEAD BMesh * bm