Code cleanup: use 'const' for arrays (python)
[blender.git] / source / blender / python / bmesh / bmesh_py_ops_call.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/python/bmesh/bmesh_py_ops_call.c
27  *  \ingroup pybmesh
28  *
29  * This file provides __call__ aka BPy_BMO_call for
30  * the bmesh operatorand has been given its own file
31  * because argument conversion is involved.
32  */
33
34 #include <Python.h>
35
36 #include "BLI_utildefines.h"
37
38 #include "../mathutils/mathutils.h"
39
40 #include "bmesh.h"
41
42 #include "bmesh_py_ops.h"
43 #include "bmesh_py_ops_call.h"  /* own include */
44
45 #include "bmesh_py_types.h"
46 #include "bmesh_py_utils.h"
47
48 static int bpy_bm_op_as_py_error(BMesh *bm)
49 {
50         if (BMO_error_occurred(bm)) {
51                 /* note: we could have multiple errors */
52                 const char *errmsg;
53                 if (BMO_error_get(bm, &errmsg, NULL)) {
54                         PyErr_Format(PyExc_RuntimeError,
55                                      "bmesh operator: %.200s",
56                                      errmsg);
57                         BMO_error_clear(bm);
58                         return -1;
59                 }
60         }
61         return 0;
62 }
63
64 /**
65  * \brief Utility function to check BMVert/BMEdge/BMFace's
66  *
67  * \param value
68  * \param bm Check the \a value against this.
69  * \param htype Test \a value matches this type.
70  * \param descr Description text.
71  */
72 static int bpy_slot_from_py_elem_check(BPy_BMElem *value, BMesh *bm, const char htype,
73                                        /* for error messages */
74                                        const char *opname, const char *slot_name, const char *descr)
75 {
76         if (!BPy_BMElem_Check(value) ||
77             !(value->ele->head.htype & htype))
78         {
79                 PyErr_Format(PyExc_TypeError,
80                              "%.200s: keyword \"%.200s\" %.200s, expected a %.200s not *.200s",
81                              opname, slot_name, descr,
82                              BPy_BMElem_StringFromHType(htype),
83                              Py_TYPE(value)->tp_name);
84                 return -1;
85         }
86         else if (value->bm == NULL) {
87                 PyErr_Format(PyExc_TypeError,
88                              "%.200s: keyword \"%.200s\" %.200s invalidated element",
89                              opname, slot_name, descr);
90                 return -1;
91         }
92         else if (value->bm != bm) {  /* we may want to make this check optional by setting 'bm' to NULL */
93                 PyErr_Format(PyExc_TypeError,
94                              "%.200s: keyword \"%.200s\" %.200s invalidated element",
95                              opname, slot_name, descr);
96                 return -1;
97         }
98         return 0;
99 }
100
101 /**
102  * \brief Utility function to check BMVertSeq/BMEdgeSeq/BMFaceSeq's
103  *
104  * \param value Caller must check its a BMeshSeq
105  * \param bm Check the \a value against this.
106  * \param htype_py The type(s) of \a value.
107  * \param htype_bmo The type(s) supported by the target slot.
108  * \param descr Description text.
109  */
110 static int bpy_slot_from_py_elemseq_check(BPy_BMGeneric *value, BMesh *bm,
111                                           const char htype_py, const char htype_bmo,
112                                           /* for error messages */
113                                           const char *opname, const char *slot_name, const char *descr)
114 {
115         if (value->bm == NULL) {
116                 PyErr_Format(PyExc_TypeError,
117                              "%.200s: keyword \"%.200s\" %.200s, invalidated sequence",
118                              opname, slot_name, descr);
119                 return -1;
120         }
121         else if (value->bm != bm) {  /* we may want to make this check optional by setting 'bm' to NULL */
122                 PyErr_Format(PyExc_TypeError,
123                              "%.200s: keyword \"%.200s\" %.200s, invalidated sequence",
124                              opname, slot_name, descr);
125                 return -1;
126         }
127         else if ((htype_py & htype_bmo) == 0) {
128                 char str_bmo[32];
129                 char str_py[32];
130                 PyErr_Format(PyExc_TypeError,
131                              "%.200s: keyword \"%.200s\" %.200s, expected "
132                              "a sequence of %.200s not %.200s",
133                              opname, slot_name, descr,
134                              BPy_BMElem_StringFromHType_ex(htype_bmo, str_bmo),
135                              BPy_BMElem_StringFromHType_ex(htype_py,  str_py));
136                 return -1;
137         }
138
139         return 0;
140 }
141
142 /**
143  * Use for giving py args to an operator.
144  */
145 static int bpy_slot_from_py(BMesh *bm, BMOperator *bmop, BMOpSlot *slot, PyObject *value,
146                             /* the are just for exception messages */
147                             const char *opname, const char *slot_name)
148 {
149         switch (slot->slot_type) {
150                 case BMO_OP_SLOT_BOOL:
151                 {
152                         int param;
153
154                         param = PyLong_AsLong(value);
155
156                         if (param < 0) {
157                                 PyErr_Format(PyExc_TypeError,
158                                              "%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s",
159                                              opname, slot_name, Py_TYPE(value)->tp_name);
160                                 return -1;
161                         }
162                         else {
163                                 BMO_SLOT_AS_BOOL(slot) = param;
164                         }
165
166                         break;
167                 }
168                 case BMO_OP_SLOT_INT:
169                 {
170                         int overflow;
171                         long param = PyLong_AsLongAndOverflow(value, &overflow);
172                         if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
173                                 PyErr_Format(PyExc_ValueError,
174                                              "%.200s: keyword \"%.200s\" value not in 'int' range "
175                                              "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
176                                              opname, slot_name, Py_TYPE(value)->tp_name);
177                                 return -1;
178                         }
179                         else if (param == -1 && PyErr_Occurred()) {
180                                 PyErr_Format(PyExc_TypeError,
181                                              "%.200s: keyword \"%.200s\" expected an int, not %.200s",
182                                              opname, slot_name, Py_TYPE(value)->tp_name);
183                                 return -1;
184                         }
185                         else {
186                                 BMO_SLOT_AS_INT(slot) = (int)param;
187                         }
188                         break;
189                 }
190                 case BMO_OP_SLOT_FLT:
191                 {
192                         float param = PyFloat_AsDouble(value);
193                         if (param == -1 && PyErr_Occurred()) {
194                                 PyErr_Format(PyExc_TypeError,
195                                              "%.200s: keyword \"%.200s\" expected a float, not %.200s",
196                                              opname, slot_name, Py_TYPE(value)->tp_name);
197                                 return -1;
198                         }
199                         else {
200                                 BMO_SLOT_AS_FLOAT(slot) = param;
201                         }
202                         break;
203                 }
204                 case BMO_OP_SLOT_MAT:
205                 {
206                         /* XXX - BMesh operator design is crappy here, operator slot should define matrix size,
207                          * not the caller! */
208                         unsigned short size;
209                         if (!MatrixObject_Check(value)) {
210                                 PyErr_Format(PyExc_TypeError,
211                                              "%.200s: keyword \"%.200s\" expected a Matrix, not %.200s",
212                                              opname, slot_name, Py_TYPE(value)->tp_name);
213                                 return -1;
214                         }
215                         else if (BaseMath_ReadCallback((MatrixObject *)value) == -1) {
216                                 return -1;
217                         }
218                         else if (((size = ((MatrixObject *)value)->num_col) != ((MatrixObject *)value)->num_row) ||
219                                  (ELEM(size, 3, 4) == false))
220                         {
221                                 PyErr_Format(PyExc_TypeError,
222                                              "%.200s: keyword \"%.200s\" expected a 3x3 or 4x4 matrix Matrix",
223                                              opname, slot_name);
224                                 return -1;
225                         }
226
227                         BMO_slot_mat_set(bmop, bmop->slots_in, slot_name, ((MatrixObject *)value)->matrix, size);
228                         break;
229                 }
230                 case BMO_OP_SLOT_VEC:
231                 {
232                         /* passing slot name here is a bit non-descriptive */
233                         if (mathutils_array_parse(BMO_SLOT_AS_VECTOR(slot), 3, 3, value, slot_name) == -1) {
234                                 return -1;
235                         }
236                         break;
237                 }
238                 case BMO_OP_SLOT_ELEMENT_BUF:
239                 {
240                         if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
241                                 if (bpy_slot_from_py_elem_check((BPy_BMElem *)value, bm, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
242                                                                 opname, slot_name, "single element") == -1)
243                                 {
244                                         return -1;  /* error is set in bpy_slot_from_py_elem_check() */
245                                 }
246
247                                 BMO_slot_buffer_from_single(bmop, slot, &((BPy_BMElem *)value)->ele->head);
248                         }
249                         else {
250                                 /* there are many ways we could interpret arguments, for now...
251                                  * - verts/edges/faces from the mesh direct,
252                                  *   this way the operator takes every item.
253                                  * - `TODO` a plain python sequence (list) of elements.
254                                  * - `TODO`  an iterator. eg.
255                                  *   face.verts
256                                  * - `TODO`  (type, flag) pair, eg.
257                                  *   ('VERT', {'TAG'})
258                                  */
259
260                                 if (BPy_BMVertSeq_Check(value)) {
261                                         if (bpy_slot_from_py_elemseq_check((BPy_BMGeneric *)value, bm,
262                                                                            BM_VERT, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
263                                                                            opname, slot_name, "element buffer") == -1)
264                                         {
265                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
266                                         }
267
268                                         BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_VERT);
269                                 }
270                                 else if (BPy_BMEdgeSeq_Check(value)) {
271                                         if (bpy_slot_from_py_elemseq_check((BPy_BMGeneric *)value, bm,
272                                                                            BM_EDGE, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
273                                                                            opname, slot_name, "element buffer") == -1)
274                                         {
275                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
276                                         }
277
278                                         BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_EDGE);
279                                 }
280                                 else if (BPy_BMFaceSeq_Check(value)) {
281                                         if (bpy_slot_from_py_elemseq_check((BPy_BMGeneric *)value, bm,
282                                                                            BM_FACE, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
283                                                                            opname, slot_name, "element buffer") == -1)
284                                         {
285                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
286                                         }
287                                         BMO_slot_buffer_from_all(bm, bmop, bmop->slots_in, slot_name, BM_FACE);
288                                 }
289
290                                 else if (BPy_BMElemSeq_Check(value)) {
291                                         BMIter iter;
292                                         BMHeader *ele;
293                                         int tot;
294                                         unsigned int i;
295
296                                         if (bpy_slot_from_py_elemseq_check((BPy_BMGeneric *)value, bm,
297                                                                            bm_iter_itype_htype_map[((BPy_BMElemSeq *)value)->itype],
298                                                                            (slot->slot_subtype.elem & BM_ALL_NOLOOP),
299                                                                            opname, slot_name, "element buffer") == -1)
300                                         {
301                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
302                                         }
303
304                                         /* this will loop over all elements which is a shame but
305                                          * we need to know this before alloc */
306                                         /* calls bpy_bmelemseq_length() */
307                                         tot = Py_TYPE(value)->tp_as_sequence->sq_length(value);
308
309                                         BMO_slot_buffer_alloc(bmop, bmop->slots_in, slot_name, tot);
310
311                                         i = 0;
312                                         BM_ITER_BPY_BM_SEQ (ele, &iter, ((BPy_BMElemSeq *)value)) {
313                                                 slot->data.buf[i] = ele;
314                                                 i++;
315                                         }
316                                 }
317                                 /* keep this last */
318                                 else if (PySequence_Check(value)) {
319                                         BMElem **elem_array = NULL;
320                                         Py_ssize_t elem_array_len;
321
322                                         elem_array = BPy_BMElem_PySeq_As_Array(&bm, value, 0, PY_SSIZE_T_MAX,
323                                                                                &elem_array_len, (slot->slot_subtype.elem & BM_ALL_NOLOOP),
324                                                                                true, true, slot_name);
325
326                                         /* error is set above */
327                                         if (elem_array == NULL) {
328                                                 return -1;
329                                         }
330
331                                         BMO_slot_buffer_alloc(bmop, bmop->slots_in, slot_name, elem_array_len);
332                                         memcpy(slot->data.buf, elem_array, sizeof(void *) * elem_array_len);
333                                         PyMem_FREE(elem_array);
334                                 }
335                                 else {
336                                         PyErr_Format(PyExc_TypeError,
337                                                      "%.200s: keyword \"%.200s\" expected "
338                                                      "a bmesh sequence, list, (htype, flag) pair, not %.200s",
339                                                      opname, slot_name, Py_TYPE(value)->tp_name);
340                                         return -1;
341                                 }
342                         }
343                         break;
344                 }
345                 case BMO_OP_SLOT_MAPPING:
346                 {
347                         /* first check types */
348                         if (slot->slot_subtype.map != BMO_OP_SLOT_SUBTYPE_MAP_EMPTY) {
349                                 if (!PyDict_Check(value)) {
350                                         PyErr_Format(PyExc_TypeError,
351                                                      "%.200s: keyword \"%.200s\" expected "
352                                                      "a dict, not %.200s",
353                                                      opname, slot_name, Py_TYPE(value)->tp_name);
354                                         return -1;
355                                 }
356                         }
357                         else {
358                                 if (!PySet_Check(value)) {
359                                         PyErr_Format(PyExc_TypeError,
360                                                      "%.200s: keyword \"%.200s\" expected "
361                                                      "a set, not %.200s",
362                                                      opname, slot_name, Py_TYPE(value)->tp_name);
363                                         return -1;
364                                 }
365                         }
366
367                         switch (slot->slot_subtype.map) {
368                                 case BMO_OP_SLOT_SUBTYPE_MAP_ELEM:
369                                 {
370                                         if (PyDict_Size(value) > 0) {
371                                                 PyObject *arg_key, *arg_value;
372                                                 Py_ssize_t arg_pos = 0;
373                                                 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
374                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_key, bm, BM_ALL_NOLOOP,
375                                                                                         opname, slot_name, "invalid key in dict") == -1)
376                                                         {
377                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
378                                                         }
379
380                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_value, bm, BM_ALL_NOLOOP,
381                                                                                         opname, slot_name, "invalid value in dict") == -1)
382                                                         {
383                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
384                                                         }
385
386                                                         BMO_slot_map_elem_insert(bmop, slot,
387                                                                                  ((BPy_BMElem *)arg_key)->ele, ((BPy_BMElem *)arg_value)->ele);
388                                                 }
389                                         }
390                                         break;
391                                 }
392                                 case BMO_OP_SLOT_SUBTYPE_MAP_FLT:
393                                 {
394                                         if (PyDict_Size(value) > 0) {
395                                                 PyObject *arg_key, *arg_value;
396                                                 Py_ssize_t arg_pos = 0;
397                                                 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
398                                                         float value_f;
399
400                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_key, bm, BM_ALL_NOLOOP,
401                                                                                         opname, slot_name, "invalid key in dict") == -1)
402                                                         {
403                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
404                                                         }
405
406                                                         value_f = PyFloat_AsDouble(arg_value);
407
408                                                         if (value_f == -1.0f && PyErr_Occurred()) {
409                                                                 PyErr_Format(PyExc_TypeError,
410                                                                              "%.200s: keyword \"%.200s\" expected "
411                                                                              "a dict with float values, not %.200s",
412                                                                              opname, slot_name, Py_TYPE(arg_value)->tp_name);
413                                                                 return -1;
414                                                         }
415
416                                                         BMO_slot_map_float_insert(bmop, slot,
417                                                                                   ((BPy_BMElem *)arg_key)->ele, value_f);
418                                                 }
419                                         }
420                                         break;
421                                 }
422                                 case BMO_OP_SLOT_SUBTYPE_MAP_INT:
423                                 {
424                                         if (PyDict_Size(value) > 0) {
425                                                 PyObject *arg_key, *arg_value;
426                                                 Py_ssize_t arg_pos = 0;
427                                                 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
428                                                         int value_i;
429
430                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_key, bm, BM_ALL_NOLOOP,
431                                                                                         opname, slot_name, "invalid key in dict") == -1)
432                                                         {
433                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
434                                                         }
435
436                                                         value_i = PyLong_AsLong(arg_value);
437
438                                                         if (value_i == -1 && PyErr_Occurred()) {
439                                                                 PyErr_Format(PyExc_TypeError,
440                                                                              "%.200s: keyword \"%.200s\" expected "
441                                                                              "a dict with int values, not %.200s",
442                                                                              opname, slot_name, Py_TYPE(arg_value)->tp_name);
443                                                                 return -1;
444                                                         }
445
446                                                         BMO_slot_map_int_insert(bmop, slot,
447                                                                                 ((BPy_BMElem *)arg_key)->ele, value_i);
448                                                 }
449                                         }
450                                         break;
451                                 }
452                                 case BMO_OP_SLOT_SUBTYPE_MAP_BOOL:
453                                 {
454                                         if (PyDict_Size(value) > 0) {
455                                                 PyObject *arg_key, *arg_value;
456                                                 Py_ssize_t arg_pos = 0;
457                                                 while (PyDict_Next(value, &arg_pos, &arg_key, &arg_value)) {
458                                                         int value_i;
459
460                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_key, bm, BM_ALL_NOLOOP,
461                                                                                         opname, slot_name, "invalid key in dict") == -1)
462                                                         {
463                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
464                                                         }
465
466                                                         value_i = PyLong_AsLong(arg_value);
467
468                                                         if (value_i == -1 && PyErr_Occurred()) {
469                                                                 PyErr_Format(PyExc_TypeError,
470                                                                              "%.200s: keyword \"%.200s\" expected "
471                                                                              "a dict with bool values, not %.200s",
472                                                                              opname, slot_name, Py_TYPE(arg_value)->tp_name);
473                                                                 return -1;
474                                                         }
475
476                                                         BMO_slot_map_bool_insert(bmop, slot,
477                                                                                  ((BPy_BMElem *)arg_key)->ele, value_i != 0);
478                                                 }
479                                         }
480                                         break;
481                                 }
482                                 case BMO_OP_SLOT_SUBTYPE_MAP_EMPTY:
483                                 {
484                                         if (PySet_Size(value) > 0) {
485                                                 PyObject *arg_key;
486                                                 Py_ssize_t arg_pos = 0;
487                                                 Py_ssize_t arg_hash = 0;
488                                                 while (_PySet_NextEntry(value, &arg_pos, &arg_key, &arg_hash)) {
489
490                                                         if (bpy_slot_from_py_elem_check((BPy_BMElem *)arg_key, bm, BM_ALL_NOLOOP,
491                                                                                         opname, slot_name, "invalid key in set") == -1)
492                                                         {
493                                                                 return -1;  /* error is set in bpy_slot_from_py_elem_check() */
494                                                         }
495
496                                                         BMO_slot_map_empty_insert(bmop, slot,
497                                                                                  ((BPy_BMElem *)arg_key)->ele);
498                                                 }
499                                         }
500                                         break;
501                                 }
502                                 case BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL:
503                                 {
504                                         /* can't convert from these */
505                                         PyErr_Format(PyExc_NotImplementedError,
506                                                      "This arguments mapping subtype %d is not supported", slot->slot_subtype.map);
507                                         return -1;
508                                 }
509                         }
510                         /* fall-through */
511                 }
512                 default:
513                         /* TODO --- many others */
514                         PyErr_Format(PyExc_NotImplementedError,
515                                      "%.200s: keyword \"%.200s\" type %d not working yet!",
516                                      opname, slot_name, slot->slot_type);
517                         return -1;
518         }
519
520         /* all is well */
521         return 0;
522 }
523
524 /**
525  * Use for getting return values from an operator thats already executed.
526  *
527  * \note Don't throw any exceptions and should always return a valid (PyObject *).
528  */
529 static PyObject *bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
530 {
531         PyObject *item = NULL;
532
533         /* keep switch in same order as above */
534         switch (slot->slot_type) {
535                 case BMO_OP_SLOT_BOOL:
536                         item = PyBool_FromLong((BMO_SLOT_AS_BOOL(slot)));
537                         break;
538                 case BMO_OP_SLOT_INT:
539                         item = PyLong_FromLong(BMO_SLOT_AS_INT(slot));
540                         break;
541                 case BMO_OP_SLOT_FLT:
542                         item = PyFloat_FromDouble((double)BMO_SLOT_AS_FLOAT(slot));
543                         break;
544                 case BMO_OP_SLOT_MAT:
545                         item = Matrix_CreatePyObject((float *)BMO_SLOT_AS_MATRIX(slot), 4, 4, Py_NEW, NULL);
546                         break;
547                 case BMO_OP_SLOT_VEC:
548                         item = Vector_CreatePyObject(BMO_SLOT_AS_VECTOR(slot), slot->len, Py_NEW, NULL);
549                         break;
550                 case BMO_OP_SLOT_PTR:
551                         BLI_assert(0);  /* currently we don't have any pointer return values in use */
552                         item = (Py_INCREF(Py_None), Py_None);
553                         break;
554                 case BMO_OP_SLOT_ELEMENT_BUF:
555                 {
556                         if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
557                                 BMHeader *ele = BMO_slot_buffer_get_single(slot);
558                                 item = ele ? BPy_BMElem_CreatePyObject(bm, ele) : (Py_INCREF(Py_None), Py_None);
559                         }
560                         else {
561                                 const int size = slot->len;
562                                 void **buffer = BMO_SLOT_AS_BUFFER(slot);
563                                 int j;
564
565                                 item = PyList_New(size);
566                                 for (j = 0; j < size; j++) {
567                                         BMHeader *ele = buffer[j];
568                                         PyList_SET_ITEM(item, j, BPy_BMElem_CreatePyObject(bm, ele));
569                                 }
570                         }
571                         break;
572                 }
573                 case BMO_OP_SLOT_MAPPING:
574                 {
575                         GHash *slot_hash = BMO_SLOT_AS_GHASH(slot);
576                         GHashIterator hash_iter;
577
578                         switch (slot->slot_subtype.map) {
579                                 case BMO_OP_SLOT_SUBTYPE_MAP_ELEM:
580                                 {
581                                         item = PyDict_New();
582                                         if (slot_hash) {
583                                                 GHASH_ITER (hash_iter, slot_hash) {
584                                                         BMHeader *ele_key = BLI_ghashIterator_getKey(&hash_iter);
585                                                         void     *ele_val = BLI_ghashIterator_getValue(&hash_iter);
586
587                                                         PyObject *py_key =  BPy_BMElem_CreatePyObject(bm, ele_key);
588                                                         PyObject *py_val =  BPy_BMElem_CreatePyObject(bm, ele_val);
589
590                                                         PyDict_SetItem(item, py_key, py_val);
591                                                         Py_DECREF(py_key);
592                                                         Py_DECREF(py_val);
593                                                 }
594                                         }
595                                         break;
596                                 }
597                                 case BMO_OP_SLOT_SUBTYPE_MAP_FLT:
598                                 {
599                                         item = PyDict_New();
600                                         if (slot_hash) {
601                                                 GHASH_ITER (hash_iter, slot_hash) {
602                                                         BMHeader *ele_key = BLI_ghashIterator_getKey(&hash_iter);
603                                                         void     *ele_val = BLI_ghashIterator_getValue(&hash_iter);
604
605                                                         PyObject *py_key =  BPy_BMElem_CreatePyObject(bm,  ele_key);
606                                                         PyObject *py_val =  PyFloat_FromDouble(*(float *)&ele_val);
607
608                                                         PyDict_SetItem(item, py_key, py_val);
609                                                         Py_DECREF(py_key);
610                                                         Py_DECREF(py_val);
611                                                 }
612                                         }
613                                         break;
614                                 }
615                                 case BMO_OP_SLOT_SUBTYPE_MAP_INT:
616                                 {
617                                         item = PyDict_New();
618                                         if (slot_hash) {
619                                                 GHASH_ITER (hash_iter, slot_hash) {
620                                                         BMHeader *ele_key = BLI_ghashIterator_getKey(&hash_iter);
621                                                         void     *ele_val = BLI_ghashIterator_getValue(&hash_iter);
622
623                                                         PyObject *py_key =  BPy_BMElem_CreatePyObject(bm,  ele_key);
624                                                         PyObject *py_val =  PyLong_FromLong(*(int *)&ele_val);
625
626                                                         PyDict_SetItem(item, py_key, py_val);
627                                                         Py_DECREF(py_key);
628                                                         Py_DECREF(py_val);
629                                                 }
630                                         }
631                                         break;
632                                 }
633                                 case BMO_OP_SLOT_SUBTYPE_MAP_BOOL:
634                                 {
635                                         item = PyDict_New();
636                                         if (slot_hash) {
637                                                 GHASH_ITER (hash_iter, slot_hash) {
638                                                         BMHeader *ele_key = BLI_ghashIterator_getKey(&hash_iter);
639                                                         void     *ele_val = BLI_ghashIterator_getValue(&hash_iter);
640
641                                                         PyObject *py_key =  BPy_BMElem_CreatePyObject(bm,  ele_key);
642                                                         PyObject *py_val =  PyBool_FromLong(*(bool *)&ele_val);
643
644                                                         PyDict_SetItem(item, py_key, py_val);
645                                                         Py_DECREF(py_key);
646                                                         Py_DECREF(py_val);
647                                                 }
648                                         }
649                                         break;
650                                 }
651                                 case BMO_OP_SLOT_SUBTYPE_MAP_EMPTY:
652                                 {
653                                         item = PySet_New(NULL);
654                                         if (slot_hash) {
655                                                 GHASH_ITER (hash_iter, slot_hash) {
656                                                         BMHeader       *ele_key = BLI_ghashIterator_getKey(&hash_iter);
657
658                                                         PyObject *py_key =  BPy_BMElem_CreatePyObject(bm,  ele_key);
659
660                                                         PySet_Add(item, py_key);
661
662                                                         Py_DECREF(py_key);
663                                                 }
664                                         }
665                                         break;
666                                 }
667                                 case BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL:
668                                         /* can't convert from these */
669                                         item = (Py_INCREF(Py_None), Py_None);
670                                         break;
671                         }
672                         break;
673                 }
674         }
675         BLI_assert(item != NULL);
676
677         return item;
678 }
679
680 /**
681  * This is the __call__ for bmesh.ops.xxx()
682  */
683 PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
684 {
685         PyObject *ret;
686         BPy_BMesh *py_bm;
687         BMesh *bm;
688
689         BMOperator bmop;
690
691         if ((PyTuple_GET_SIZE(args) == 1) &&
692             (py_bm = (BPy_BMesh *)PyTuple_GET_ITEM(args, 0)) &&
693             (BPy_BMesh_Check(py_bm)))
694         {
695                 BPY_BM_CHECK_OBJ(py_bm);
696                 bm = py_bm->bm;
697
698                 /* could complain about entering with exceptions... */
699                 BMO_error_clear(bm);
700         }
701         else {
702                 PyErr_SetString(PyExc_TypeError,
703                                 "bmesh operators expect a single BMesh positional argument, all other args must be keywords");
704                 return NULL;
705         }
706
707         /* TODO - error check this!, though we do the error check on attribute access */
708         /* TODO - make flags optional */
709         BMO_op_init(bm, &bmop, BMO_FLAG_DEFAULTS, self->opname);
710
711         if (kw && PyDict_Size(kw) > 0) {
712                 /* setup properties, see bpy_rna.c: pyrna_py_to_prop()
713                  * which shares this logic for parsing properties */
714
715                 PyObject *key, *value;
716                 Py_ssize_t pos = 0;
717                 while (PyDict_Next(kw, &pos, &key, &value)) {
718                         const char *slot_name = _PyUnicode_AsString(key);
719                         BMOpSlot *slot;
720
721                         if (!BMO_slot_exists(bmop.slots_in, slot_name)) {
722                                 PyErr_Format(PyExc_TypeError,
723                                              "%.200s: keyword \"%.200s\" is invalid for this operator",
724                                              self->opname, slot_name);
725                                 BMO_op_finish(bm, &bmop);
726                                 return NULL;
727                         }
728
729                         slot = BMO_slot_get(bmop.slots_in, slot_name);
730
731                         /* now assign the value */
732                         if (bpy_slot_from_py(bm, &bmop, slot, value,
733                                              self->opname, slot_name) == -1)
734                         {
735                                 BMO_op_finish(bm, &bmop);
736                                 return NULL;
737                         }
738                 }
739         }
740
741         BMO_op_exec(bm, &bmop);
742
743         /* from here until the end of the function, no returns, just set 'ret' */
744         if (UNLIKELY(bpy_bm_op_as_py_error(bm) == -1)) {
745                 ret = NULL;  /* exception raised above */
746         }
747         else if (bmop.slots_out[0].slot_name == NULL) {
748                 ret = (Py_INCREF(Py_None), Py_None);
749         }
750         else {
751                 /* build return value */
752                 int i;
753                 ret = PyDict_New();
754
755                 for (i = 0; bmop.slots_out[i].slot_name; i++) {
756                         // BMOpDefine *op_def = opdefines[bmop.type];
757                         // BMOSlotType *slot_type = op_def->slot_types_out[i];
758                         BMOpSlot *slot = &bmop.slots_out[i];
759                         PyObject *item;
760
761                         /* this function doesn't throw exceptions */
762                         item = bpy_slot_to_py(bm, slot);
763                         if (item == NULL) {
764                                 item = (Py_INCREF(Py_None), Py_None);
765                         }
766
767 #if 1
768                         /* temp code, strip off '.out' while we keep this convention */
769                         {
770                                 char slot_name_strip[MAX_SLOTNAME];
771                                 const char *ch = strchr(slot->slot_name, '.');  /* can't fail! */
772                                 int tot = ch - slot->slot_name;
773                                 BLI_assert(ch != NULL);
774                                 memcpy(slot_name_strip, slot->slot_name, tot);
775                                 slot_name_strip[tot] = '\0';
776                                 PyDict_SetItemString(ret, slot_name_strip, item);
777                         }
778 #else
779                         PyDict_SetItemString(ret, slot->slot_name, item);
780 #endif
781                         Py_DECREF(item);
782                 }
783         }
784
785         BMO_op_finish(bm, &bmop);
786         return ret;
787 }