Remove MinGW support
[blender-staging.git] / source / blender / python / intern / bpy_rna.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  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_rna.c
24  *  \ingroup pythonintern
25  *
26  * This file is the main interface between python and blenders data api (RNA),
27  * exposing RNA to python so blender data can be accessed in a python like way.
28  *
29  * The two main types are 'BPy_StructRNA' and 'BPy_PropertyRNA' - the base
30  * classes for most of the data python accesses in blender.
31  */
32
33 #include <Python.h>
34
35 #include <stddef.h>
36 #include <float.h> /* FLT_MIN/MAX */
37
38 #include "RNA_types.h"
39
40 #include "BLI_bitmap.h"
41 #include "BLI_dynstr.h"
42 #include "BLI_string.h"
43 #include "BLI_listbase.h"
44 #include "BLI_math_rotation.h"
45 #include "BLI_utildefines.h"
46
47 #include "BPY_extern.h"
48
49 #include "bpy_rna.h"
50 #include "bpy_rna_anim.h"
51 #include "bpy_props.h"
52 #include "bpy_util.h"
53 #include "bpy_rna_callback.h"
54 #include "bpy_intern_string.h"
55
56 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
57 #  include "BLI_ghash.h"
58 #endif
59
60 #include "RNA_enum_types.h"
61 #include "RNA_define.h" /* RNA_def_property_free_identifier */
62 #include "RNA_access.h"
63
64 #include "MEM_guardedalloc.h"
65
66 #include "BKE_main.h"
67 #include "BKE_idcode.h"
68 #include "BKE_context.h"
69 #include "BKE_global.h" /* evil G.* */
70 #include "BKE_report.h"
71 #include "BKE_idprop.h"
72
73
74 #include "../generic/idprop_py_api.h" /* for IDprop lookups */
75 #include "../generic/py_capi_utils.h"
76 #include "../generic/python_utildefines.h"
77
78 #define USE_PEDANTIC_WRITE
79 #define USE_MATHUTILS
80 #define USE_STRING_COERCE
81
82 static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
83 static PyObject *pyrna_prop_collection_values(BPy_PropertyRNA *self);
84
85 #define BPY_DOC_ID_PROP_TYPE_NOTE                                             \
86 "   .. note::\n"                                                              \
87 "\n"                                                                          \
88 "      Only :class:`bpy.types.ID`, :class:`bpy.types.Bone` and \n"            \
89 "      :class:`bpy.types.PoseBone` classes support custom properties.\n"
90
91
92 int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
93 {
94         if (pysrna->ptr.type) {
95                 return 0;
96         }
97         PyErr_Format(PyExc_ReferenceError,
98                      "StructRNA of type %.200s has been removed",
99                      Py_TYPE(pysrna)->tp_name);
100         return -1;
101 }
102
103 int pyrna_prop_validity_check(BPy_PropertyRNA *self)
104 {
105         if (self->ptr.type) {
106                 return 0;
107         }
108         PyErr_Format(PyExc_ReferenceError,
109                      "PropertyRNA of type %.200s.%.200s has been removed",
110                      Py_TYPE(self)->tp_name, RNA_property_identifier(self->prop));
111         return -1;
112 }
113
114 void pyrna_invalidate(BPy_DummyPointerRNA *self)
115 {
116         RNA_POINTER_INVALIDATE(&self->ptr);
117 }
118
119 #ifdef USE_PYRNA_INVALIDATE_GC
120 #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g) + 1))
121
122 /* only for sizeof() */
123 struct gc_generation {
124         PyGC_Head head;
125         int threshold;
126         int count;
127 } gc_generation;
128
129 static void id_release_gc(struct ID *id)
130 {
131         unsigned int j;
132         // unsigned int i = 0;
133         for (j = 0; j < 3; j++) {
134                 /* hack below to get the 2 other lists from _PyGC_generation0 that are normally not exposed */
135                 PyGC_Head *gen = (PyGC_Head *)(((char *)_PyGC_generation0) + (sizeof(gc_generation) * j));
136                 PyGC_Head *g = gen->gc.gc_next;
137                 while ((g = g->gc.gc_next) != gen) {
138                         PyObject *ob = FROM_GC(g);
139                         if (PyType_IsSubtype(Py_TYPE(ob), &pyrna_struct_Type) ||
140                             PyType_IsSubtype(Py_TYPE(ob), &pyrna_prop_Type))
141                         {
142                                 BPy_DummyPointerRNA *ob_ptr = (BPy_DummyPointerRNA *)ob;
143                                 if (ob_ptr->ptr.id.data == id) {
144                                         pyrna_invalidate(ob_ptr);
145                                         // printf("freeing: %p %s, %.200s\n", (void *)ob, id->name, Py_TYPE(ob)->tp_name);
146                                         // i++;
147                                 }
148                         }
149                 }
150         }
151         // printf("id_release_gc freed '%s': %d\n", id->name, i);
152 }
153 #endif
154
155 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
156 //#define DEBUG_RNA_WEAKREF
157
158 struct GHash *id_weakref_pool = NULL;
159 static PyObject *id_free_weakref_cb(PyObject *weakinfo_pair, PyObject *weakref);
160 static PyMethodDef id_free_weakref_cb_def = {"id_free_weakref_cb", (PyCFunction)id_free_weakref_cb, METH_O, NULL};
161
162 /* adds a reference to the list, remember to decref */
163 static GHash *id_weakref_pool_get(ID *id)
164 {
165         GHash *weakinfo_hash = NULL;
166
167         if (id_weakref_pool) {
168                 weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
169         }
170         else {
171                 /* first time, allocate pool */
172                 id_weakref_pool = BLI_ghash_ptr_new("rna_global_pool");
173                 weakinfo_hash = NULL;
174         }
175
176         if (weakinfo_hash == NULL) {
177                 /* we're using a ghash as a set, could use libHX's HXMAP_SINGULAR but would be an extra dep. */
178                 weakinfo_hash = BLI_ghash_ptr_new("rna_id");
179                 BLI_ghash_insert(id_weakref_pool, id, weakinfo_hash);
180         }
181
182         return weakinfo_hash;
183 }
184
185 /* called from pyrna_struct_CreatePyObject() and pyrna_prop_CreatePyObject() */
186 static void id_weakref_pool_add(ID *id, BPy_DummyPointerRNA *pyrna)
187 {
188         PyObject *weakref;
189         PyObject *weakref_capsule;
190         PyObject *weakref_cb_py;
191
192         /* create a new function instance and insert the list as 'self' so we can remove ourself from it */
193         GHash *weakinfo_hash = id_weakref_pool_get(id); /* new or existing */
194
195         weakref_capsule = PyCapsule_New(weakinfo_hash, NULL, NULL);
196         weakref_cb_py = PyCFunction_New(&id_free_weakref_cb_def, weakref_capsule);
197         Py_DECREF(weakref_capsule);
198
199         /* add weakref to weakinfo_hash list */
200         weakref = PyWeakref_NewRef((PyObject *)pyrna, weakref_cb_py);
201
202         Py_DECREF(weakref_cb_py); /* function owned by the weakref now */
203
204         /* important to add at the end, since first removal looks at the end */
205         BLI_ghash_insert(weakinfo_hash, weakref, id); /* using a hash table as a set, all 'id's are the same */
206         /* weakinfo_hash owns the weakref */
207
208 }
209
210 /* workaround to get the last id without a lookup */
211 static ID *_id_tmp_ptr;
212 static void value_id_set(void *id)
213 {
214         _id_tmp_ptr = (ID *)id;
215 }
216
217 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash);
218 static PyObject *id_free_weakref_cb(PyObject *weakinfo_capsule, PyObject *weakref)
219 {
220         /* important to search backwards */
221         GHash *weakinfo_hash = PyCapsule_GetPointer(weakinfo_capsule, NULL);
222
223
224         if (BLI_ghash_size(weakinfo_hash) > 1) {
225                 BLI_ghash_remove(weakinfo_hash, weakref, NULL, NULL);
226         }
227         else { /* get the last id and free it */
228                 BLI_ghash_remove(weakinfo_hash, weakref, NULL, value_id_set);
229                 id_release_weakref_list(_id_tmp_ptr, weakinfo_hash);
230         }
231
232         Py_DECREF(weakref);
233
234         Py_RETURN_NONE;
235 }
236
237 static void id_release_weakref_list(struct ID *id, GHash *weakinfo_hash)
238 {
239         GHashIterator weakinfo_hash_iter;
240
241         BLI_ghashIterator_init(&weakinfo_hash_iter, weakinfo_hash);
242
243 #ifdef DEBUG_RNA_WEAKREF
244         fprintf(stdout, "id_release_weakref: '%s', %d items\n", id->name, BLI_ghash_size(weakinfo_hash));
245 #endif
246
247         while (!BLI_ghashIterator_done(&weakinfo_hash_iter)) {
248                 PyObject *weakref = (PyObject *)BLI_ghashIterator_getKey(&weakinfo_hash_iter);
249                 PyObject *item = PyWeakref_GET_OBJECT(weakref);
250                 if (item != Py_None) {
251
252 #ifdef DEBUG_RNA_WEAKREF
253                         PyC_ObSpit("id_release_weakref item ", item);
254 #endif
255
256                         pyrna_invalidate((BPy_DummyPointerRNA *)item);
257                 }
258
259                 Py_DECREF(weakref);
260
261                 BLI_ghashIterator_step(&weakinfo_hash_iter);
262         }
263
264         BLI_ghash_remove(id_weakref_pool, (void *)id, NULL, NULL);
265         BLI_ghash_free(weakinfo_hash, NULL, NULL);
266
267         if (BLI_ghash_size(id_weakref_pool) == 0) {
268                 BLI_ghash_free(id_weakref_pool, NULL, NULL);
269                 id_weakref_pool = NULL;
270 #ifdef DEBUG_RNA_WEAKREF
271                 printf("id_release_weakref freeing pool\n");
272 #endif
273         }
274 }
275
276 static void id_release_weakref(struct ID *id)
277 {
278         GHash *weakinfo_hash = BLI_ghash_lookup(id_weakref_pool, (void *)id);
279         if (weakinfo_hash) {
280                 id_release_weakref_list(id, weakinfo_hash);
281         }
282 }
283
284 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
285
286 void BPY_id_release(struct ID *id)
287 {
288 #ifdef USE_PYRNA_INVALIDATE_GC
289         id_release_gc(id);
290 #endif
291
292 #ifdef USE_PYRNA_INVALIDATE_WEAKREF
293         if (id_weakref_pool) {
294                 PyGILState_STATE gilstate = PyGILState_Ensure();
295
296                 id_release_weakref(id);
297
298                 PyGILState_Release(gilstate);
299         }
300 #endif /* USE_PYRNA_INVALIDATE_WEAKREF */
301
302         (void)id;
303 }
304
305 #ifdef USE_PEDANTIC_WRITE
306 static bool rna_disallow_writes = false;
307
308 static bool rna_id_write_error(PointerRNA *ptr, PyObject *key)
309 {
310         ID *id = ptr->id.data;
311         if (id) {
312                 const short idcode = GS(id->name);
313                 if (!ELEM(idcode, ID_WM, ID_SCR)) { /* may need more added here */
314                         const char *idtype = BKE_idcode_to_name(idcode);
315                         const char *pyname;
316                         if (key && PyUnicode_Check(key)) pyname = _PyUnicode_AsString(key);
317                         else                             pyname = "<UNKNOWN>";
318
319                         /* make a nice string error */
320                         BLI_assert(idtype != NULL);
321                         PyErr_Format(PyExc_AttributeError,
322                                      "Writing to ID classes in this context is not allowed: "
323                                      "%.200s, %.200s datablock, error setting %.200s.%.200s",
324                                      id->name + 2, idtype, RNA_struct_identifier(ptr->type), pyname);
325
326                         return true;
327                 }
328         }
329         return false;
330 }
331 #endif  /* USE_PEDANTIC_WRITE */
332
333
334 #ifdef USE_PEDANTIC_WRITE
335 bool pyrna_write_check(void)
336 {
337         return !rna_disallow_writes;
338 }
339
340 void pyrna_write_set(bool val)
341 {
342         rna_disallow_writes = !val;
343 }
344 #else  /* USE_PEDANTIC_WRITE */
345 bool pyrna_write_check(void)
346 {
347         return true;
348 }
349 void pyrna_write_set(bool UNUSED(val))
350 {
351         /* nothing */
352 }
353 #endif  /* USE_PEDANTIC_WRITE */
354
355 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
356 static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self);
357 static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix);
358 static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item);
359
360 #ifdef USE_MATHUTILS
361 #include "../mathutils/mathutils.h" /* so we can have mathutils callbacks */
362
363 static PyObject *pyrna_prop_array_subscript_slice(
364         BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
365         Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length);
366 static short pyrna_rotation_euler_order_get(
367         PointerRNA *ptr, const short order_fallback,
368         PropertyRNA **r_prop_eul_order);
369
370 /* bpyrna vector/euler/quat callbacks */
371 static unsigned char mathutils_rna_array_cb_index = -1; /* index for our callbacks */
372
373 /* subtype not used much yet */
374 #define MATHUTILS_CB_SUBTYPE_EUL 0
375 #define MATHUTILS_CB_SUBTYPE_VEC 1
376 #define MATHUTILS_CB_SUBTYPE_QUAT 2
377 #define MATHUTILS_CB_SUBTYPE_COLOR 3
378
379 static int mathutils_rna_generic_check(BaseMathObject *bmo)
380 {
381         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
382
383         PYRNA_PROP_CHECK_INT(self);
384
385         return self->prop ? 0 : -1;
386 }
387
388 static int mathutils_rna_vector_get(BaseMathObject *bmo, int subtype)
389 {
390         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
391
392         PYRNA_PROP_CHECK_INT(self);
393
394         if (self->prop == NULL)
395                 return -1;
396
397         RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
398
399         /* Euler order exception */
400         if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
401                 EulerObject *eul = (EulerObject *)bmo;
402                 PropertyRNA *prop_eul_order = NULL;
403                 eul->order = pyrna_rotation_euler_order_get(&self->ptr, eul->order, &prop_eul_order);
404         }
405
406         return 0;
407 }
408
409 static int mathutils_rna_vector_set(BaseMathObject *bmo, int subtype)
410 {
411         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
412         float min, max;
413
414         PYRNA_PROP_CHECK_INT(self);
415
416         if (self->prop == NULL)
417                 return -1;
418
419 #ifdef USE_PEDANTIC_WRITE
420         if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
421                 return -1;
422         }
423 #endif  /* USE_PEDANTIC_WRITE */
424
425         if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
426                 PyErr_Format(PyExc_AttributeError,
427                              "bpy_prop \"%.200s.%.200s\" is read-only",
428                              RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
429                 return -1;
430         }
431
432         RNA_property_float_range(&self->ptr, self->prop, &min, &max);
433
434         if (min != -FLT_MAX || max != FLT_MAX) {
435                 int i, len = RNA_property_array_length(&self->ptr, self->prop);
436                 for (i = 0; i < len; i++) {
437                         CLAMP(bmo->data[i], min, max);
438                 }
439         }
440
441         RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
442         if (RNA_property_update_check(self->prop)) {
443                 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
444         }
445
446         /* Euler order exception */
447         if (subtype == MATHUTILS_CB_SUBTYPE_EUL) {
448                 EulerObject *eul = (EulerObject *)bmo;
449                 PropertyRNA *prop_eul_order = NULL;
450                 short order = pyrna_rotation_euler_order_get(&self->ptr, eul->order, &prop_eul_order);
451                 if (order != eul->order) {
452                         RNA_property_enum_set(&self->ptr, prop_eul_order, eul->order);
453                         if (RNA_property_update_check(prop_eul_order)) {
454                                 RNA_property_update(BPy_GetContext(), &self->ptr, prop_eul_order);
455                         }
456                 }
457         }
458         return 0;
459 }
460
461 static int mathutils_rna_vector_get_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
462 {
463         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
464
465         PYRNA_PROP_CHECK_INT(self);
466
467         if (self->prop == NULL)
468                 return -1;
469
470         bmo->data[index] = RNA_property_float_get_index(&self->ptr, self->prop, index);
471         return 0;
472 }
473
474 static int mathutils_rna_vector_set_index(BaseMathObject *bmo, int UNUSED(subtype), int index)
475 {
476         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
477
478         PYRNA_PROP_CHECK_INT(self);
479
480         if (self->prop == NULL)
481                 return -1;
482
483 #ifdef USE_PEDANTIC_WRITE
484         if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
485                 return -1;
486         }
487 #endif  /* USE_PEDANTIC_WRITE */
488
489         if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
490                 PyErr_Format(PyExc_AttributeError,
491                              "bpy_prop \"%.200s.%.200s\" is read-only",
492                              RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
493                 return -1;
494         }
495
496         RNA_property_float_clamp(&self->ptr, self->prop, &bmo->data[index]);
497         RNA_property_float_set_index(&self->ptr, self->prop, index, bmo->data[index]);
498
499         if (RNA_property_update_check(self->prop)) {
500                 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
501         }
502
503         return 0;
504 }
505
506 static Mathutils_Callback mathutils_rna_array_cb = {
507         (BaseMathCheckFunc)     mathutils_rna_generic_check,
508         (BaseMathGetFunc)       mathutils_rna_vector_get,
509         (BaseMathSetFunc)       mathutils_rna_vector_set,
510         (BaseMathGetIndexFunc)  mathutils_rna_vector_get_index,
511         (BaseMathSetIndexFunc)  mathutils_rna_vector_set_index
512 };
513
514
515 /* bpyrna matrix callbacks */
516 static unsigned char mathutils_rna_matrix_cb_index = -1; /* index for our callbacks */
517
518 static int mathutils_rna_matrix_get(BaseMathObject *bmo, int UNUSED(subtype))
519 {
520         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
521
522         PYRNA_PROP_CHECK_INT(self);
523
524         if (self->prop == NULL)
525                 return -1;
526
527         RNA_property_float_get_array(&self->ptr, self->prop, bmo->data);
528         return 0;
529 }
530
531 static int mathutils_rna_matrix_set(BaseMathObject *bmo, int UNUSED(subtype))
532 {
533         BPy_PropertyRNA *self = (BPy_PropertyRNA *)bmo->cb_user;
534
535         PYRNA_PROP_CHECK_INT(self);
536
537         if (self->prop == NULL)
538                 return -1;
539
540 #ifdef USE_PEDANTIC_WRITE
541         if (rna_disallow_writes && rna_id_write_error(&self->ptr, NULL)) {
542                 return -1;
543         }
544 #endif  /* USE_PEDANTIC_WRITE */
545
546         if (!RNA_property_editable_flag(&self->ptr, self->prop)) {
547                 PyErr_Format(PyExc_AttributeError,
548                              "bpy_prop \"%.200s.%.200s\" is read-only",
549                              RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
550                 return -1;
551         }
552
553         /* can ignore clamping here */
554         RNA_property_float_set_array(&self->ptr, self->prop, bmo->data);
555
556         if (RNA_property_update_check(self->prop)) {
557                 RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
558         }
559         return 0;
560 }
561
562 static Mathutils_Callback mathutils_rna_matrix_cb = {
563         mathutils_rna_generic_check,
564         mathutils_rna_matrix_get,
565         mathutils_rna_matrix_set,
566         NULL,
567         NULL
568 };
569
570 static short pyrna_rotation_euler_order_get(
571         PointerRNA *ptr, const short order_fallback,
572         PropertyRNA **r_prop_eul_order)
573 {
574         /* attempt to get order */
575         if (*r_prop_eul_order == NULL) {
576                 *r_prop_eul_order = RNA_struct_find_property(ptr, "rotation_mode");
577         }
578
579         if (*r_prop_eul_order) {
580                 short order = RNA_property_enum_get(ptr, *r_prop_eul_order);
581                 /* could be quat or axisangle */
582                 if (order >= EULER_ORDER_XYZ && order <= EULER_ORDER_ZYX) {
583                         return order;
584                 }
585         }
586
587         return order_fallback;
588 }
589
590 #endif  /* USE_MATHUTILS */
591
592 /* note that PROP_NONE is included as a vector subtype. this is because its handy to
593  * have x/y access to fcurve keyframes and other fixed size float arrays of length 2-4. */
594 #define PROP_ALL_VECTOR_SUBTYPES                                              \
595              PROP_COORDS:                                                         \
596         case PROP_TRANSLATION:                                                    \
597         case PROP_DIRECTION:                                                      \
598         case PROP_VELOCITY:                                                       \
599         case PROP_ACCELERATION:                                                   \
600         case PROP_XYZ:                                                            \
601         case PROP_XYZ_LENGTH                                                      \
602
603
604 PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
605 {
606         PyObject *ret = NULL;
607
608 #ifdef USE_MATHUTILS
609         int subtype, totdim;
610         int len;
611         const int flag = RNA_property_flag(prop);
612         const int type = RNA_property_type(prop);
613         const bool is_thick = (flag & PROP_THICK_WRAP) != 0;
614
615         /* disallow dynamic sized arrays to be wrapped since the size could change
616          * to a size mathutils does not support */
617         if (flag & PROP_DYNAMIC) {
618                 return NULL;
619         }
620
621         len = RNA_property_array_length(ptr, prop);
622         if (type == PROP_FLOAT) {
623                 /* pass */
624         }
625         else if (type == PROP_INT) {
626                 if (is_thick) {
627                         goto thick_wrap_slice;
628                 }
629                 else {
630                         return NULL;
631                 }
632         }
633         else {
634                 return NULL;
635         }
636
637         subtype = RNA_property_subtype(prop);
638         totdim = RNA_property_array_dimension(ptr, prop, NULL);
639
640         if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
641                 if (!is_thick)
642                         ret = pyrna_prop_CreatePyObject(ptr, prop);  /* owned by the mathutils PyObject */
643
644                 switch (subtype) {
645                         case PROP_ALL_VECTOR_SUBTYPES:
646                                 if (len >= 2 && len <= 4) {
647                                         if (is_thick) {
648                                                 ret = Vector_CreatePyObject(NULL, len, NULL);
649                                                 RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
650                                         }
651                                         else {
652                                                 PyObject *vec_cb = Vector_CreatePyObject_cb(
653                                                         ret, len, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_VEC);
654                                                 Py_DECREF(ret); /* the vector owns now */
655                                                 ret = vec_cb; /* return the vector instead */
656                                         }
657                                 }
658                                 break;
659                         case PROP_MATRIX:
660                                 if (len == 16) {
661                                         if (is_thick) {
662                                                 ret = Matrix_CreatePyObject(NULL, 4, 4, NULL);
663                                                 RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
664                                         }
665                                         else {
666                                                 PyObject *mat_cb = Matrix_CreatePyObject_cb(
667                                                         ret, 4, 4, mathutils_rna_matrix_cb_index, 0);
668                                                 Py_DECREF(ret); /* the matrix owns now */
669                                                 ret = mat_cb; /* return the matrix instead */
670                                         }
671                                 }
672                                 else if (len == 9) {
673                                         if (is_thick) {
674                                                 ret = Matrix_CreatePyObject(NULL, 3, 3, NULL);
675                                                 RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->matrix);
676                                         }
677                                         else {
678                                                 PyObject *mat_cb = Matrix_CreatePyObject_cb(
679                                                         ret, 3, 3, mathutils_rna_matrix_cb_index, 0);
680                                                 Py_DECREF(ret); /* the matrix owns now */
681                                                 ret = mat_cb; /* return the matrix instead */
682                                         }
683                                 }
684                                 break;
685                         case PROP_EULER:
686                         case PROP_QUATERNION:
687                                 if (len == 3) { /* euler */
688                                         if (is_thick) {
689                                                 /* attempt to get order, only needed for thick types since wrapped with update via callbacks */
690                                                 PropertyRNA *prop_eul_order = NULL;
691                                                 short order = pyrna_rotation_euler_order_get(ptr, EULER_ORDER_XYZ, &prop_eul_order);
692
693                                                 ret = Euler_CreatePyObject(NULL, order, NULL);  /* TODO, get order from RNA */
694                                                 RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
695                                         }
696                                         else {
697                                                 /* order will be updated from callback on use */
698                                                 // TODO, get order from RNA
699                                                 PyObject *eul_cb = Euler_CreatePyObject_cb(
700                                                         ret, EULER_ORDER_XYZ, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL);
701                                                 Py_DECREF(ret); /* the euler owns now */
702                                                 ret = eul_cb; /* return the euler instead */
703                                         }
704                                 }
705                                 else if (len == 4) {
706                                         if (is_thick) {
707                                                 ret = Quaternion_CreatePyObject(NULL, NULL);
708                                                 RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
709                                         }
710                                         else {
711                                                 PyObject *quat_cb = Quaternion_CreatePyObject_cb(
712                                                         ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_QUAT);
713                                                 Py_DECREF(ret); /* the quat owns now */
714                                                 ret = quat_cb; /* return the quat instead */
715                                         }
716                                 }
717                                 break;
718                         case PROP_COLOR:
719                         case PROP_COLOR_GAMMA:
720                                 if (len == 3) { /* color */
721                                         if (is_thick) {
722                                                 ret = Color_CreatePyObject(NULL, NULL);
723                                                 RNA_property_float_get_array(ptr, prop, ((ColorObject *)ret)->col);
724                                         }
725                                         else {
726                                                 PyObject *col_cb = Color_CreatePyObject_cb(
727                                                         ret, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_COLOR);
728                                                 Py_DECREF(ret); /* the color owns now */
729                                                 ret = col_cb; /* return the color instead */
730                                         }
731                                 }
732                                 break;
733                         default:
734                                 break;
735                 }
736         }
737
738         if (ret == NULL) {
739                 if (is_thick) {
740                         /* this is an array we cant reference (since its not thin wrappable)
741                          * and cannot be coerced into a mathutils type, so return as a list */
742 thick_wrap_slice:
743                         ret = pyrna_prop_array_subscript_slice(NULL, ptr, prop, 0, len, len);
744                 }
745                 else {
746                         ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the mathutils PyObject */
747                 }
748         }
749 #else  /* USE_MATHUTILS */
750         (void)ptr;
751         (void)prop;
752 #endif  /* USE_MATHUTILS */
753
754         return ret;
755 }
756
757 /* same as RNA_enum_value_from_id but raises an exception */
758 int pyrna_enum_value_from_id(
759         EnumPropertyItem *item, const char *identifier, int *r_value,
760         const char *error_prefix)
761 {
762         if (RNA_enum_value_from_id(item, identifier, r_value) == 0) {
763                 const char *enum_str = BPy_enum_as_string(item);
764                 PyErr_Format(PyExc_ValueError,
765                              "%s: '%.200s' not found in (%s)",
766                              error_prefix, identifier, enum_str);
767                 MEM_freeN((void *)enum_str);
768                 return -1;
769         }
770
771         return 0;
772 }
773
774 /* note on __cmp__:
775  * checking the 'ptr->data' matches works in almost all cases,
776  * however there are a few RNA properties that are fake sub-structs and
777  * share the pointer with the parent, in those cases this happens 'a.b == a'
778  * see: r43352 for example.
779  *
780  * So compare the 'ptr->type' as well to avoid this problem.
781  * It's highly unlikely this would happen that 'ptr->data' and 'ptr->prop' would match,
782  * but _not_ 'ptr->type' but include this check for completeness.
783  * - campbell */
784
785 static int pyrna_struct_compare(BPy_StructRNA *a, BPy_StructRNA *b)
786 {
787         return (((a->ptr.data == b->ptr.data) &&
788                  (a->ptr.type == b->ptr.type)) ? 0 : -1);
789 }
790
791 static int pyrna_prop_compare(BPy_PropertyRNA *a, BPy_PropertyRNA *b)
792 {
793         return (((a->prop == b->prop) &&
794                  (a->ptr.data == b->ptr.data) &&
795                  (a->ptr.type == b->ptr.type)) ? 0 : -1);
796 }
797
798 static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
799 {
800         PyObject *res;
801         int ok = -1; /* zero is true */
802
803         if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b))
804                 ok = pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b);
805
806         switch (op) {
807                 case Py_NE:
808                         ok = !ok;
809                         ATTR_FALLTHROUGH;
810                 case Py_EQ:
811                         res = ok ? Py_False : Py_True;
812                         break;
813
814                 case Py_LT:
815                 case Py_LE:
816                 case Py_GT:
817                 case Py_GE:
818                         res = Py_NotImplemented;
819                         break;
820                 default:
821                         PyErr_BadArgument();
822                         return NULL;
823         }
824
825         return Py_INCREF_RET(res);
826 }
827
828 static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
829 {
830         PyObject *res;
831         int ok = -1; /* zero is true */
832
833         if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b))
834                 ok = pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b);
835
836         switch (op) {
837                 case Py_NE:
838                         ok = !ok;
839                         ATTR_FALLTHROUGH;
840                 case Py_EQ:
841                         res = ok ? Py_False : Py_True;
842                         break;
843
844                 case Py_LT:
845                 case Py_LE:
846                 case Py_GT:
847                 case Py_GE:
848                         res = Py_NotImplemented;
849                         break;
850                 default:
851                         PyErr_BadArgument();
852                         return NULL;
853         }
854
855         return Py_INCREF_RET(res);
856 }
857
858 /*----------------------repr--------------------------------------------*/
859 static PyObject *pyrna_struct_str(BPy_StructRNA *self)
860 {
861         PyObject *ret;
862         const char *name;
863
864         if (!PYRNA_STRUCT_IS_VALID(self)) {
865                 return PyUnicode_FromFormat("<bpy_struct, %.200s invalid>",
866                                             Py_TYPE(self)->tp_name);
867         }
868
869         /* print name if available */
870         name = RNA_struct_name_get_alloc(&self->ptr, NULL, 0, NULL);
871         if (name) {
872                 ret = PyUnicode_FromFormat("<bpy_struct, %.200s(\"%.200s\")>",
873                                            RNA_struct_identifier(self->ptr.type),
874                                            name);
875                 MEM_freeN((void *)name);
876                 return ret;
877         }
878
879         return PyUnicode_FromFormat("<bpy_struct, %.200s at %p>",
880                                     RNA_struct_identifier(self->ptr.type),
881                                     self->ptr.data);
882 }
883
884 static PyObject *pyrna_struct_repr(BPy_StructRNA *self)
885 {
886         ID *id = self->ptr.id.data;
887         PyObject *tmp_str;
888         PyObject *ret;
889
890         if (id == NULL || !PYRNA_STRUCT_IS_VALID(self))
891                 return pyrna_struct_str(self);  /* fallback */
892
893         tmp_str = PyUnicode_FromString(id->name + 2);
894
895         if (RNA_struct_is_ID(self->ptr.type)) {
896                 ret = PyUnicode_FromFormat("bpy.data.%s[%R]",
897                                            BKE_idcode_to_name_plural(GS(id->name)),
898                                            tmp_str);
899         }
900         else {
901                 const char *path;
902                 path = RNA_path_from_ID_to_struct(&self->ptr);
903                 if (path) {
904                         if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
905                                 ret = PyUnicode_FromFormat("bpy.data...%s",
906                                                            path);
907                         }
908                         else {
909                                 ret = PyUnicode_FromFormat("bpy.data.%s[%R].%s",
910                                                            BKE_idcode_to_name_plural(GS(id->name)),
911                                                            tmp_str,
912                                                            path);
913                         }
914
915                         MEM_freeN((void *)path);
916                 }
917                 else { /* cant find, print something sane */
918                         ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
919                                                    BKE_idcode_to_name_plural(GS(id->name)),
920                                                    tmp_str,
921                                                    RNA_struct_identifier(self->ptr.type));
922                 }
923         }
924
925         Py_DECREF(tmp_str);
926
927         return ret;
928 }
929
930 static PyObject *pyrna_prop_str(BPy_PropertyRNA *self)
931 {
932         PyObject *ret;
933         PointerRNA ptr;
934         const char *name;
935         const char *type_id = NULL;
936         char type_fmt[64] = "";
937         int type;
938
939         PYRNA_PROP_CHECK_OBJ(self);
940
941         type = RNA_property_type(self->prop);
942
943         if (RNA_enum_id_from_value(rna_enum_property_type_items, type, &type_id) == 0) {
944                 PyErr_SetString(PyExc_RuntimeError, "could not use property type, internal error"); /* should never happen */
945                 return NULL;
946         }
947         else {
948                 /* this should never fail */
949                 int len = -1;
950                 char *c = type_fmt;
951
952                 while ((*c++ = tolower(*type_id++))) {}
953
954                 if (type == PROP_COLLECTION) {
955                         len = pyrna_prop_collection_length(self);
956                 }
957                 else if (RNA_property_array_check(self->prop)) {
958                         len = pyrna_prop_array_length((BPy_PropertyArrayRNA *)self);
959                 }
960
961                 if (len != -1)
962                         sprintf(--c, "[%d]", len);
963         }
964
965         /* if a pointer, try to print name of pointer target too */
966         if (type == PROP_POINTER) {
967                 ptr = RNA_property_pointer_get(&self->ptr, self->prop);
968                 name = RNA_struct_name_get_alloc(&ptr, NULL, 0, NULL);
969
970                 if (name) {
971                         ret = PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s(\"%.200s\")>",
972                                                    type_fmt,
973                                                    RNA_struct_identifier(self->ptr.type),
974                                                    RNA_property_identifier(self->prop),
975                                                    name);
976                         MEM_freeN((void *)name);
977                         return ret;
978                 }
979         }
980         if (type == PROP_COLLECTION) {
981                 PointerRNA r_ptr;
982                 if (RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
983                         return PyUnicode_FromFormat("<bpy_%.200s, %.200s>",
984                                                     type_fmt,
985                                                     RNA_struct_identifier(r_ptr.type));
986                 }
987         }
988
989         return PyUnicode_FromFormat("<bpy_%.200s, %.200s.%.200s>",
990                                     type_fmt,
991                                     RNA_struct_identifier(self->ptr.type),
992                                     RNA_property_identifier(self->prop));
993 }
994
995 static PyObject *pyrna_prop_repr_ex(
996         BPy_PropertyRNA *self,
997         const int index_dim, const int index)
998 {
999         ID *id = self->ptr.id.data;
1000         PyObject *tmp_str;
1001         PyObject *ret;
1002         const char *path;
1003
1004         PYRNA_PROP_CHECK_OBJ(self);
1005
1006         if (id == NULL)
1007                 return pyrna_prop_str(self);  /* fallback */
1008
1009         tmp_str = PyUnicode_FromString(id->name + 2);
1010
1011         path = RNA_path_from_ID_to_property_index(&self->ptr, self->prop, index_dim, index);
1012
1013         if (path) {
1014                 const char *data_delim = (path[0] == '[') ? "" : ".";
1015                 if (GS(id->name) == ID_NT) { /* nodetree paths are not accurate */
1016                         ret = PyUnicode_FromFormat("bpy.data...%s",
1017                                                    path);
1018                 }
1019                 else {
1020                         ret = PyUnicode_FromFormat("bpy.data.%s[%R]%s%s",
1021                                                    BKE_idcode_to_name_plural(GS(id->name)),
1022                                                    tmp_str,
1023                                                    data_delim, path);
1024                 }
1025
1026                 MEM_freeN((void *)path);
1027         }
1028         else { /* cant find, print something sane */
1029                 ret = PyUnicode_FromFormat("bpy.data.%s[%R]...%s",
1030                                            BKE_idcode_to_name_plural(GS(id->name)),
1031                                            tmp_str,
1032                                            RNA_property_identifier(self->prop));
1033         }
1034
1035         Py_DECREF(tmp_str);
1036
1037         return ret;
1038 }
1039
1040 static PyObject *pyrna_prop_repr(BPy_PropertyRNA *self)
1041 {
1042         return pyrna_prop_repr_ex(self, 0, -1);
1043 }
1044
1045 static PyObject *pyrna_prop_array_repr(BPy_PropertyArrayRNA *self)
1046 {
1047         return pyrna_prop_repr_ex((BPy_PropertyRNA *)self, self->arraydim, self->arrayoffset);
1048 }
1049
1050 static PyObject *pyrna_func_repr(BPy_FunctionRNA *self)
1051 {
1052         return PyUnicode_FromFormat("<%.200s %.200s.%.200s()>",
1053                                     Py_TYPE(self)->tp_name,
1054                                     RNA_struct_identifier(self->ptr.type),
1055                                     RNA_function_identifier(self->func));
1056 }
1057
1058
1059 static Py_hash_t pyrna_struct_hash(BPy_StructRNA *self)
1060 {
1061         return _Py_HashPointer(self->ptr.data);
1062 }
1063
1064 /* from python's meth_hash v3.1.2 */
1065 static long pyrna_prop_hash(BPy_PropertyRNA *self)
1066 {
1067         long x, y;
1068         if (self->ptr.data == NULL)
1069                 x = 0;
1070         else {
1071                 x = _Py_HashPointer(self->ptr.data);
1072                 if (x == -1)
1073                         return -1;
1074         }
1075         y = _Py_HashPointer((void *)(self->prop));
1076         if (y == -1)
1077                 return -1;
1078         x ^= y;
1079         if (x == -1)
1080                 x = -2;
1081         return x;
1082 }
1083
1084 #ifdef USE_PYRNA_STRUCT_REFERENCE
1085 static int pyrna_struct_traverse(BPy_StructRNA *self, visitproc visit, void *arg)
1086 {
1087         Py_VISIT(self->reference);
1088         return 0;
1089 }
1090
1091 static int pyrna_struct_clear(BPy_StructRNA *self)
1092 {
1093         Py_CLEAR(self->reference);
1094         return 0;
1095 }
1096 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1097
1098 /* use our own dealloc so we can free a property if we use one */
1099 static void pyrna_struct_dealloc(BPy_StructRNA *self)
1100 {
1101 #ifdef PYRNA_FREE_SUPPORT
1102         if (self->freeptr && self->ptr.data) {
1103                 IDP_FreeProperty(self->ptr.data);
1104                 MEM_freeN(self->ptr.data);
1105                 self->ptr.data = NULL;
1106         }
1107 #endif /* PYRNA_FREE_SUPPORT */
1108
1109 #ifdef USE_WEAKREFS
1110         if (self->in_weakreflist != NULL) {
1111                 PyObject_ClearWeakRefs((PyObject *)self);
1112         }
1113 #endif
1114
1115 #ifdef USE_PYRNA_STRUCT_REFERENCE
1116         if (self->reference) {
1117                 PyObject_GC_UnTrack(self);
1118                 pyrna_struct_clear(self);
1119         }
1120 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1121
1122         /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1123         Py_TYPE(self)->tp_free(self);
1124 }
1125
1126 #ifdef USE_PYRNA_STRUCT_REFERENCE
1127 static void pyrna_struct_reference_set(BPy_StructRNA *self, PyObject *reference)
1128 {
1129         if (self->reference) {
1130 //              PyObject_GC_UnTrack(self); /* INITIALIZED TRACKED ? */
1131                 pyrna_struct_clear(self);
1132         }
1133         /* reference is now NULL */
1134
1135         if (reference) {
1136                 self->reference = reference;
1137                 Py_INCREF(reference);
1138 //              PyObject_GC_Track(self);  /* INITIALIZED TRACKED ? */
1139         }
1140 }
1141 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
1142
1143 /* use our own dealloc so we can free a property if we use one */
1144 static void pyrna_prop_dealloc(BPy_PropertyRNA *self)
1145 {
1146 #ifdef USE_WEAKREFS
1147         if (self->in_weakreflist != NULL) {
1148                 PyObject_ClearWeakRefs((PyObject *)self);
1149         }
1150 #endif
1151         /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1152         Py_TYPE(self)->tp_free(self);
1153 }
1154
1155 static void pyrna_prop_array_dealloc(BPy_PropertyRNA *self)
1156 {
1157 #ifdef USE_WEAKREFS
1158         if (self->in_weakreflist != NULL) {
1159                 PyObject_ClearWeakRefs((PyObject *)self);
1160         }
1161 #endif
1162         /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
1163         Py_TYPE(self)->tp_free(self);
1164 }
1165
1166 static const char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
1167 {
1168         EnumPropertyItem *item;
1169         const char *result;
1170         bool free = false;
1171
1172         RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
1173         if (item) {
1174                 result = BPy_enum_as_string(item);
1175         }
1176         else {
1177                 result = "";
1178         }
1179
1180         if (free)
1181                 MEM_freeN(item);
1182
1183         return result;
1184 }
1185
1186
1187 static int pyrna_string_to_enum(
1188         PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *r_value,
1189         const char *error_prefix)
1190 {
1191         const char *param = _PyUnicode_AsString(item);
1192
1193         if (param == NULL) {
1194                 PyErr_Format(PyExc_TypeError,
1195                              "%.200s expected a string enum, not %.200s",
1196                              error_prefix, Py_TYPE(item)->tp_name);
1197                 return -1;
1198         }
1199         else {
1200                 if (!RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, r_value)) {
1201                         const char *enum_str = pyrna_enum_as_string(ptr, prop);
1202                         PyErr_Format(PyExc_TypeError,
1203                                      "%.200s enum \"%.200s\" not found in (%.200s)",
1204                                      error_prefix, param, enum_str);
1205                         MEM_freeN((void *)enum_str);
1206                         return -1;
1207                 }
1208         }
1209
1210         return 0;
1211 }
1212
1213 /**
1214  * Takes a set of strings and map it to and array of booleans.
1215  *
1216  * Useful when the values aren't flags.
1217  *
1218  * \param type_convert_sign: Maps signed to unsigned range,
1219  * needed when we want to use the full range of a signed short/char.
1220  */
1221 BLI_bitmap *pyrna_set_to_enum_bitmap(
1222         EnumPropertyItem *items, PyObject *value,
1223         int type_size, bool type_convert_sign,
1224         int bitmap_size,
1225         const char *error_prefix)
1226 {
1227         /* set looping */
1228         Py_ssize_t pos = 0;
1229         Py_ssize_t hash = 0;
1230         PyObject *key;
1231
1232         BLI_bitmap *bitmap = BLI_BITMAP_NEW(bitmap_size, __func__);
1233
1234         while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1235                 const char *param = _PyUnicode_AsString(key);
1236                 if (param == NULL) {
1237                         PyErr_Format(PyExc_TypeError,
1238                                      "%.200s expected a string, not %.200s",
1239                                      error_prefix, Py_TYPE(key)->tp_name);
1240                         goto error;
1241                 }
1242
1243                 int ret;
1244                 if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
1245                         goto error;
1246                 }
1247
1248                 int index = ret;
1249
1250                 if (type_convert_sign) {
1251                         if (type_size == 2) {
1252                                 union { signed short as_signed; unsigned short as_unsigned; } ret_convert;
1253                                 ret_convert.as_signed = (signed short)ret;
1254                                 index = (int)ret_convert.as_unsigned;
1255                         }
1256                         else if (type_size == 1) {
1257                                 union { signed char as_signed; unsigned char as_unsigned; } ret_convert;
1258                                 ret_convert.as_signed = (signed char)ret;
1259                                 index = (int)ret_convert.as_unsigned;
1260                         }
1261                         else {
1262                                 BLI_assert(0);
1263                         }
1264                 }
1265                 BLI_assert(index < bitmap_size);
1266                 BLI_BITMAP_ENABLE(bitmap, index);
1267         }
1268
1269         return bitmap;
1270
1271 error:
1272         MEM_freeN(bitmap);
1273         return NULL;
1274 }
1275
1276 /* 'value' _must_ be a set type, error check before calling */
1277 int pyrna_set_to_enum_bitfield(
1278         EnumPropertyItem *items, PyObject *value, int *r_value,
1279         const char *error_prefix)
1280 {
1281         /* set of enum items, concatenate all values with OR */
1282         int ret, flag = 0;
1283
1284         /* set looping */
1285         Py_ssize_t pos = 0;
1286         Py_ssize_t hash = 0;
1287         PyObject *key;
1288
1289         *r_value = 0;
1290
1291         while (_PySet_NextEntry(value, &pos, &key, &hash)) {
1292                 const char *param = _PyUnicode_AsString(key);
1293
1294                 if (param == NULL) {
1295                         PyErr_Format(PyExc_TypeError,
1296                                      "%.200s expected a string, not %.200s",
1297                                      error_prefix, Py_TYPE(key)->tp_name);
1298                         return -1;
1299                 }
1300
1301                 if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) {
1302                         return -1;
1303                 }
1304
1305                 flag |= ret;
1306         }
1307
1308         *r_value = flag;
1309         return 0;
1310 }
1311
1312 static int pyrna_prop_to_enum_bitfield(
1313         PointerRNA *ptr, PropertyRNA *prop, PyObject *value, int *r_value,
1314         const char *error_prefix)
1315 {
1316         EnumPropertyItem *item;
1317         int ret;
1318         bool free = false;
1319
1320         *r_value = 0;
1321
1322         if (!PyAnySet_Check(value)) {
1323                 PyErr_Format(PyExc_TypeError,
1324                              "%.200s, %.200s.%.200s expected a set, not a %.200s",
1325                              error_prefix, RNA_struct_identifier(ptr->type),
1326                              RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1327                 return -1;
1328         }
1329
1330         RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
1331
1332         if (item) {
1333                 ret = pyrna_set_to_enum_bitfield(item, value, r_value, error_prefix);
1334         }
1335         else {
1336                 if (PySet_GET_SIZE(value)) {
1337                         PyErr_Format(PyExc_TypeError,
1338                                      "%.200s: empty enum \"%.200s\" could not have any values assigned",
1339                                      error_prefix, RNA_property_identifier(prop));
1340                         ret = -1;
1341                 }
1342                 else {
1343                         ret = 0;
1344                 }
1345         }
1346
1347         if (free)
1348                 MEM_freeN(item);
1349
1350         return ret;
1351 }
1352
1353 PyObject *pyrna_enum_bitfield_to_py(EnumPropertyItem *items, int value)
1354 {
1355         PyObject *ret = PySet_New(NULL);
1356         const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1357
1358         if (RNA_enum_bitflag_identifiers(items, value, identifier)) {
1359                 PyObject *item;
1360                 int index;
1361                 for (index = 0; identifier[index]; index++) {
1362                         item = PyUnicode_FromString(identifier[index]);
1363                         PySet_Add(ret, item);
1364                         Py_DECREF(item);
1365                 }
1366         }
1367
1368         return ret;
1369 }
1370
1371 static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
1372 {
1373         PyObject *item, *ret = NULL;
1374
1375         if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1376                 const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
1377
1378                 ret = PySet_New(NULL);
1379
1380                 if (RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier)) {
1381                         int index;
1382
1383                         for (index = 0; identifier[index]; index++) {
1384                                 item = PyUnicode_FromString(identifier[index]);
1385                                 PySet_Add(ret, item);
1386                                 Py_DECREF(item);
1387                         }
1388
1389                 }
1390         }
1391         else {
1392                 const char *identifier;
1393                 if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
1394                         ret = PyUnicode_FromString(identifier);
1395                 }
1396                 else {
1397                         EnumPropertyItem *enum_item;
1398                         bool free = false;
1399
1400                         /* don't throw error here, can't trust blender 100% to give the
1401                          * right values, python code should not generate error for that */
1402                         RNA_property_enum_items(BPy_GetContext(), ptr, prop, &enum_item, NULL, &free);
1403                         if (enum_item && enum_item->identifier) {
1404                                 ret = PyUnicode_FromString(enum_item->identifier);
1405                         }
1406                         else {
1407                                 RNA_property_enum_items(NULL, ptr, prop, &enum_item, NULL, &free);
1408
1409                                 /* Do not print warning in case of DummyRNA_NULL_items, this one will never match any value... */
1410                                 if (enum_item != DummyRNA_NULL_items) {
1411                                         const char *ptr_name = RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
1412
1413                                         /* prefer not fail silently in case of api errors, maybe disable it later */
1414                                         printf("RNA Warning: Current value \"%d\" "
1415                                                    "matches no enum in '%s', '%s', '%s'\n",
1416                                                    val, RNA_struct_identifier(ptr->type),
1417                                                    ptr_name, RNA_property_identifier(prop));
1418
1419 #if 0                           /* gives python decoding errors while generating docs :( */
1420                                         char error_str[256];
1421                                         BLI_snprintf(error_str, sizeof(error_str),
1422                                                      "RNA Warning: Current value \"%d\" "
1423                                                      "matches no enum in '%s', '%s', '%s'",
1424                                                      val, RNA_struct_identifier(ptr->type),
1425                                                      ptr_name, RNA_property_identifier(prop));
1426
1427                                         PyErr_Warn(PyExc_RuntimeWarning, error_str);
1428 #endif
1429
1430                                         if (ptr_name)
1431                                                 MEM_freeN((void *)ptr_name);
1432                                 }
1433
1434                                 ret = PyUnicode_FromString("");
1435                         }
1436
1437                         if (free)
1438                                 MEM_freeN(enum_item);
1439 #if 0
1440                         PyErr_Format(PyExc_AttributeError,
1441                                      "RNA Error: Current value \"%d\" matches no enum", val);
1442                         ret = NULL;
1443 #endif
1444                 }
1445         }
1446
1447         return ret;
1448 }
1449
1450 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
1451 {
1452         PyObject *ret;
1453         const int type = RNA_property_type(prop);
1454
1455         if (RNA_property_array_check(prop)) {
1456                 return pyrna_py_from_array(ptr, prop);
1457         }
1458
1459         /* see if we can coerce into a python type - PropertyType */
1460         switch (type) {
1461                 case PROP_BOOLEAN:
1462                         ret = PyBool_FromLong(RNA_property_boolean_get(ptr, prop));
1463                         break;
1464                 case PROP_INT:
1465                         ret = PyLong_FromLong(RNA_property_int_get(ptr, prop));
1466                         break;
1467                 case PROP_FLOAT:
1468                         ret = PyFloat_FromDouble(RNA_property_float_get(ptr, prop));
1469                         break;
1470                 case PROP_STRING:
1471                 {
1472                         const int subtype = RNA_property_subtype(prop);
1473                         const char *buf;
1474                         int buf_len;
1475                         char buf_fixed[32];
1476
1477                         buf = RNA_property_string_get_alloc(ptr, prop, buf_fixed, sizeof(buf_fixed), &buf_len);
1478 #ifdef USE_STRING_COERCE
1479                         /* only file paths get special treatment, they may contain non utf-8 chars */
1480                         if (subtype == PROP_BYTESTRING) {
1481                                 ret = PyBytes_FromStringAndSize(buf, buf_len);
1482                         }
1483                         else if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1484                                 ret = PyC_UnicodeFromByteAndSize(buf, buf_len);
1485                         }
1486                         else {
1487                                 ret = PyUnicode_FromStringAndSize(buf, buf_len);
1488                         }
1489 #else  /* USE_STRING_COERCE */
1490                         if (subtype == PROP_BYTESTRING) {
1491                                 ret = PyBytes_FromStringAndSize(buf, buf_len);
1492                         }
1493                         else {
1494                                 ret = PyUnicode_FromStringAndSize(buf, buf_len);
1495                         }
1496 #endif  /* USE_STRING_COERCE */
1497                         if (buf_fixed != buf) {
1498                                 MEM_freeN((void *)buf);
1499                         }
1500                         break;
1501                 }
1502                 case PROP_ENUM:
1503                 {
1504                         ret = pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop));
1505                         break;
1506                 }
1507                 case PROP_POINTER:
1508                 {
1509                         PointerRNA newptr;
1510                         newptr = RNA_property_pointer_get(ptr, prop);
1511                         if (newptr.data) {
1512                                 ret = pyrna_struct_CreatePyObject(&newptr);
1513                         }
1514                         else {
1515                                 ret = Py_None;
1516                                 Py_INCREF(ret);
1517                         }
1518                         break;
1519                 }
1520                 case PROP_COLLECTION:
1521                         ret = pyrna_prop_CreatePyObject(ptr, prop);
1522                         break;
1523                 default:
1524                         PyErr_Format(PyExc_TypeError,
1525                                      "bpy_struct internal error: unknown type '%d' (pyrna_prop_to_py)", type);
1526                         ret = NULL;
1527                         break;
1528         }
1529
1530         return ret;
1531 }
1532
1533 /**
1534  * This function is used by operators and converting dicts into collections.
1535  * Its takes keyword args and fills them with property values
1536  */
1537 int pyrna_pydict_to_props(
1538         PointerRNA *ptr, PyObject *kw, const bool all_args,
1539         const char *error_prefix)
1540 {
1541         int error_val = 0;
1542         int totkw;
1543         const char *arg_name = NULL;
1544         PyObject *item;
1545
1546         totkw = kw ? PyDict_Size(kw) : 0;
1547
1548         RNA_STRUCT_BEGIN (ptr, prop)
1549         {
1550                 arg_name = RNA_property_identifier(prop);
1551
1552                 if (STREQ(arg_name, "rna_type")) {
1553                         continue;
1554                 }
1555
1556                 if (kw == NULL) {
1557                         PyErr_Format(PyExc_TypeError,
1558                                      "%.200s: no keywords, expected \"%.200s\"",
1559                                      error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1560                         error_val = -1;
1561                         break;
1562                 }
1563
1564                 item = PyDict_GetItemString(kw, arg_name); /* wont set an error */
1565
1566                 if (item == NULL) {
1567                         if (all_args) {
1568                                 PyErr_Format(PyExc_TypeError,
1569                                              "%.200s: keyword \"%.200s\" missing",
1570                                              error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1571                                 error_val = -1; /* pyrna_py_to_prop sets the error */
1572                                 break;
1573                         }
1574                 }
1575                 else {
1576                         if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
1577                                 error_val = -1;
1578                                 break;
1579                         }
1580                         totkw--;
1581                 }
1582         }
1583         RNA_STRUCT_END;
1584
1585         if (error_val == 0 && totkw > 0) { /* some keywords were given that were not used :/ */
1586                 PyObject *key, *value;
1587                 Py_ssize_t pos = 0;
1588
1589                 while (PyDict_Next(kw, &pos, &key, &value)) {
1590                         arg_name = _PyUnicode_AsString(key);
1591                         if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
1592                         arg_name = NULL;
1593                 }
1594
1595                 PyErr_Format(PyExc_TypeError,
1596                              "%.200s: keyword \"%.200s\" unrecognized",
1597                              error_prefix, arg_name ? arg_name : "<UNKNOWN>");
1598                 error_val = -1;
1599         }
1600
1601         return error_val;
1602 }
1603
1604
1605 static PyObject *pyrna_func_to_py(const PointerRNA *ptr, FunctionRNA *func)
1606 {
1607         BPy_FunctionRNA *pyfunc = (BPy_FunctionRNA *) PyObject_NEW(BPy_FunctionRNA, &pyrna_func_Type);
1608         pyfunc->ptr = *ptr;
1609         pyfunc->func = func;
1610         return (PyObject *)pyfunc;
1611 }
1612
1613
1614 static int pyrna_py_to_prop(
1615         PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value,
1616         const char *error_prefix)
1617 {
1618         /* XXX hard limits should be checked here */
1619         const int type = RNA_property_type(prop);
1620
1621
1622         if (RNA_property_array_check(prop)) {
1623                 /* done getting the length */
1624                 if (pyrna_py_to_array(ptr, prop, data, value, error_prefix) == -1) {
1625                         return -1;
1626                 }
1627         }
1628         else {
1629                 /* Normal Property (not an array) */
1630
1631                 /* see if we can coerce into a python type - PropertyType */
1632                 switch (type) {
1633                         case PROP_BOOLEAN:
1634                         {
1635                                 int param;
1636                                 /* prefer not to have an exception here
1637                                  * however so many poll functions return None or a valid Object.
1638                                  * its a hassle to convert these into a bool before returning, */
1639                                 if (RNA_parameter_flag(prop) & PARM_OUTPUT) {
1640                                         param = PyObject_IsTrue(value);
1641                                 }
1642                                 else {
1643                                         param = PyLong_AsLong(value);
1644
1645                                         if (UNLIKELY(param & ~1)) {  /* only accept 0/1 */
1646                                                 param = -1;              /* error out below */
1647                                         }
1648                                 }
1649
1650                                 if (param == -1) {
1651                                         PyErr_Format(PyExc_TypeError,
1652                                                      "%.200s %.200s.%.200s expected True/False or 0/1, not %.200s",
1653                                                      error_prefix, RNA_struct_identifier(ptr->type),
1654                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1655                                         return -1;
1656                                 }
1657                                 else {
1658                                         if (data) *((int *)data) = param;
1659                                         else RNA_property_boolean_set(ptr, prop, param);
1660                                 }
1661                                 break;
1662                         }
1663                         case PROP_INT:
1664                         {
1665                                 int overflow;
1666                                 long param = PyLong_AsLongAndOverflow(value, &overflow);
1667                                 if (overflow || (param > INT_MAX) || (param < INT_MIN)) {
1668                                         PyErr_Format(PyExc_ValueError,
1669                                                      "%.200s %.200s.%.200s value not in 'int' range "
1670                                                      "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")",
1671                                                      error_prefix, RNA_struct_identifier(ptr->type),
1672                                                      RNA_property_identifier(prop));
1673                                         return -1;
1674                                 }
1675                                 else if (param == -1 && PyErr_Occurred()) {
1676                                         PyErr_Format(PyExc_TypeError,
1677                                                      "%.200s %.200s.%.200s expected an int type, not %.200s",
1678                                                      error_prefix, RNA_struct_identifier(ptr->type),
1679                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1680                                         return -1;
1681                                 }
1682                                 else {
1683                                         int param_i = (int)param;
1684                                         if (data) {
1685                                                 RNA_property_int_clamp(ptr, prop, &param_i);
1686                                                 *((int *)data) = param_i;
1687                                         }
1688                                         else {
1689                                                 RNA_property_int_set(ptr, prop, param_i);
1690                                         }
1691                                 }
1692                                 break;
1693                         }
1694                         case PROP_FLOAT:
1695                         {
1696                                 float param = PyFloat_AsDouble(value);
1697                                 if (PyErr_Occurred()) {
1698                                         PyErr_Format(PyExc_TypeError,
1699                                                      "%.200s %.200s.%.200s expected a float type, not %.200s",
1700                                                      error_prefix, RNA_struct_identifier(ptr->type),
1701                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1702                                         return -1;
1703                                 }
1704                                 else {
1705                                         if (data) {
1706                                                 RNA_property_float_clamp(ptr, prop, (float *)&param);
1707                                                 *((float *)data) = param;
1708                                         }
1709                                         else {
1710                                                 RNA_property_float_set(ptr, prop, param);
1711                                         }
1712                                 }
1713                                 break;
1714                         }
1715                         case PROP_STRING:
1716                         {
1717                                 const int subtype = RNA_property_subtype(prop);
1718                                 const char *param;
1719
1720                                 if (subtype == PROP_BYTESTRING) {
1721
1722                                         /* Byte String */
1723
1724                                         param = PyBytes_AsString(value);
1725
1726                                         if (param == NULL) {
1727                                                 if (PyBytes_Check(value)) {
1728                                                         /* there was an error assigning a string type,
1729                                                          * rather than setting a new error, prefix the existing one
1730                                                          */
1731                                                         PyC_Err_Format_Prefix(PyExc_TypeError,
1732                                                                               "%.200s %.200s.%.200s error assigning bytes",
1733                                                                               error_prefix, RNA_struct_identifier(ptr->type),
1734                                                                               RNA_property_identifier(prop));
1735                                                 }
1736                                                 else {
1737                                                         PyErr_Format(PyExc_TypeError,
1738                                                                      "%.200s %.200s.%.200s expected a bytes type, not %.200s",
1739                                                                      error_prefix, RNA_struct_identifier(ptr->type),
1740                                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1741                                                 }
1742
1743                                                 return -1;
1744                                         }
1745                                         else {
1746                                                 /* same as unicode */
1747                                                 /* XXX, this is suspect but needed for function calls, need to see if theres a better way */
1748                                                 if (data) *((char **)data) = (char *)param;
1749                                                 else RNA_property_string_set(ptr, prop, param);
1750                                         }
1751                                 }
1752                                 else {
1753                                         /* Unicode String */
1754 #ifdef USE_STRING_COERCE
1755                                         PyObject *value_coerce = NULL;
1756                                         if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
1757                                                 /* TODO, get size */
1758                                                 param = PyC_UnicodeAsByte(value, &value_coerce);
1759                                         }
1760                                         else {
1761                                                 param = _PyUnicode_AsString(value);
1762                                         }
1763 #else  /* USE_STRING_COERCE */
1764                                         param = _PyUnicode_AsString(value);
1765 #endif  /* USE_STRING_COERCE */
1766
1767                                         if (param == NULL) {
1768                                                 if (PyUnicode_Check(value)) {
1769                                                         /* there was an error assigning a string type,
1770                                                          * rather than setting a new error, prefix the existing one
1771                                                          */
1772                                                         PyC_Err_Format_Prefix(PyExc_TypeError,
1773                                                                               "%.200s %.200s.%.200s error assigning string",
1774                                                                               error_prefix, RNA_struct_identifier(ptr->type),
1775                                                                               RNA_property_identifier(prop));
1776                                                 }
1777                                                 else {
1778                                                         PyErr_Format(PyExc_TypeError,
1779                                                                      "%.200s %.200s.%.200s expected a string type, not %.200s",
1780                                                                      error_prefix, RNA_struct_identifier(ptr->type),
1781                                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1782                                                 }
1783
1784                                                 return -1;
1785                                         }
1786                                         else {
1787                                                 /* same as bytes */
1788                                                 /* XXX, this is suspect but needed for function calls, need to see if theres a better way */
1789                                                 if (data) *((char **)data) = (char *)param;
1790                                                 else RNA_property_string_set(ptr, prop, param);
1791                                         }
1792 #ifdef USE_STRING_COERCE
1793                                         Py_XDECREF(value_coerce);
1794 #endif  /* USE_STRING_COERCE */
1795                                 }
1796                                 break;
1797                         }
1798                         case PROP_ENUM:
1799                         {
1800                                 int val = 0;
1801
1802                                 /* type checkins is done by each function */
1803                                 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1804                                         /* set of enum items, concatenate all values with OR */
1805                                         if (pyrna_prop_to_enum_bitfield(ptr, prop, value, &val, error_prefix) == -1) {
1806                                                 return -1;
1807                                         }
1808                                 }
1809                                 else {
1810                                         /* simple enum string */
1811                                         if (pyrna_string_to_enum(value, ptr, prop, &val, error_prefix) == -1) {
1812                                                 return -1;
1813                                         }
1814                                 }
1815
1816                                 if (data) *((int *)data) = val;
1817                                 else RNA_property_enum_set(ptr, prop, val);
1818
1819                                 break;
1820                         }
1821                         case PROP_POINTER:
1822                         {
1823                                 PyObject *value_new = NULL;
1824
1825                                 StructRNA *ptr_type = RNA_property_pointer_type(ptr, prop);
1826                                 int flag = RNA_property_flag(prop);
1827                                 int flag_parameter = RNA_parameter_flag(prop);
1828
1829                                 /* this is really nasty!, so we can fake the operator having direct properties eg:
1830                                  * layout.prop(self, "filepath")
1831                                  * ... which in fact should be
1832                                  * layout.prop(self.properties, "filepath")
1833                                  *
1834                                  * we need to do this trick.
1835                                  * if the prop is not an operator type and the pyobject is an operator,
1836                                  * use its properties in place of its self.
1837                                  *
1838                                  * this is so bad that its almost a good reason to do away with fake 'self.properties -> self'
1839                                  * class mixing if this causes problems in the future it should be removed.
1840                                  */
1841                                 if ((ptr_type == &RNA_AnyType) &&
1842                                     (BPy_StructRNA_Check(value)) &&
1843                                     (RNA_struct_is_a(((BPy_StructRNA *)value)->ptr.type, &RNA_Operator)))
1844                                 {
1845                                         value = PyObject_GetAttrString(value, "properties");
1846                                         value_new = value;
1847                                 }
1848
1849
1850                                 /* if property is an OperatorProperties pointer and value is a map,
1851                                  * forward back to pyrna_pydict_to_props */
1852                                 if (RNA_struct_is_a(ptr_type, &RNA_OperatorProperties) && PyDict_Check(value)) {
1853                                         PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
1854                                         return pyrna_pydict_to_props(&opptr, value, false, error_prefix);
1855                                 }
1856
1857                                 /* another exception, allow to pass a collection as an RNA property */
1858                                 if (Py_TYPE(value) == &pyrna_prop_collection_Type) { /* ok to ignore idprop collections */
1859                                         PointerRNA c_ptr;
1860                                         BPy_PropertyRNA *value_prop = (BPy_PropertyRNA *)value;
1861                                         if (RNA_property_collection_type_get(&value_prop->ptr, value_prop->prop, &c_ptr)) {
1862                                                 value = pyrna_struct_CreatePyObject(&c_ptr);
1863                                                 value_new = value;
1864                                         }
1865                                         else {
1866                                                 PyErr_Format(PyExc_TypeError,
1867                                                              "%.200s %.200s.%.200s collection has no type, "
1868                                                              "cant be used as a %.200s type",
1869                                                              error_prefix, RNA_struct_identifier(ptr->type),
1870                                                              RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1871                                                 return -1;
1872                                         }
1873                                 }
1874
1875                                 if (!BPy_StructRNA_Check(value) && value != Py_None) {
1876                                         PyErr_Format(PyExc_TypeError,
1877                                                      "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1878                                                      error_prefix, RNA_struct_identifier(ptr->type),
1879                                                      RNA_property_identifier(prop), RNA_struct_identifier(ptr_type),
1880                                                      Py_TYPE(value)->tp_name);
1881                                         Py_XDECREF(value_new); return -1;
1882                                 }
1883                                 else if ((flag & PROP_NEVER_NULL) && value == Py_None) {
1884                                         PyErr_Format(PyExc_TypeError,
1885                                                      "%.200s %.200s.%.200s does not support a 'None' assignment %.200s type",
1886                                                      error_prefix, RNA_struct_identifier(ptr->type),
1887                                                      RNA_property_identifier(prop), RNA_struct_identifier(ptr_type));
1888                                         Py_XDECREF(value_new); return -1;
1889                                 }
1890                                 else if ((value != Py_None) &&
1891                                          ((flag & PROP_ID_SELF_CHECK) && ptr->id.data == ((BPy_StructRNA *)value)->ptr.id.data))
1892                                 {
1893                                         PyErr_Format(PyExc_TypeError,
1894                                                      "%.200s %.200s.%.200s ID type does not support assignment to its self",
1895                                                      error_prefix, RNA_struct_identifier(ptr->type),
1896                                                      RNA_property_identifier(prop));
1897                                         Py_XDECREF(value_new); return -1;
1898                                 }
1899                                 else {
1900                                         BPy_StructRNA *param = (BPy_StructRNA *)value;
1901                                         bool raise_error = false;
1902                                         if (data) {
1903
1904                                                 if (flag_parameter & PARM_RNAPTR) {
1905                                                         if (flag & PROP_THICK_WRAP) {
1906                                                                 if (value == Py_None)
1907                                                                         memset(data, 0, sizeof(PointerRNA));
1908                                                                 else if (RNA_struct_is_a(param->ptr.type, ptr_type))
1909                                                                         *((PointerRNA *)data) = param->ptr;
1910                                                                 else
1911                                                                         raise_error = true;
1912                                                         }
1913                                                         else {
1914                                                                 /* for function calls, we sometimes want to pass the 'ptr' directly,
1915                                                                  * watch out that it remains valid!, possibly we could support this later if needed */
1916                                                                 BLI_assert(value_new == NULL);
1917                                                                 if (value == Py_None)
1918                                                                         *((void **)data) = NULL;
1919                                                                 else if (RNA_struct_is_a(param->ptr.type, ptr_type))
1920                                                                         *((PointerRNA **)data) = &param->ptr;
1921                                                                 else
1922                                                                         raise_error = true;
1923                                                         }
1924                                                 }
1925                                                 else if (value == Py_None) {
1926                                                         *((void **)data) = NULL;
1927                                                 }
1928                                                 else if (RNA_struct_is_a(param->ptr.type, ptr_type)) {
1929                                                         *((void **)data) = param->ptr.data;
1930                                                 }
1931                                                 else {
1932                                                         raise_error = true;
1933                                                 }
1934                                         }
1935                                         else {
1936                                                 /* data == NULL, assign to RNA */
1937                                                 if (value == Py_None || RNA_struct_is_a(param->ptr.type, ptr_type))
1938                                                         RNA_property_pointer_set(ptr, prop, value == Py_None ? PointerRNA_NULL : param->ptr);
1939                                                 else
1940                                                         raise_error = true;
1941                                         }
1942
1943                                         if (raise_error) {
1944                                                 if (pyrna_struct_validity_check(param) == -1) {
1945                                                         /* error set */
1946                                                 }
1947                                                 else {
1948                                                         PointerRNA tmp;
1949                                                         RNA_pointer_create(NULL, ptr_type, NULL, &tmp);
1950                                                         PyErr_Format(PyExc_TypeError,
1951                                                                      "%.200s %.200s.%.200s expected a %.200s type, not %.200s",
1952                                                                      error_prefix, RNA_struct_identifier(ptr->type),
1953                                                                      RNA_property_identifier(prop), RNA_struct_identifier(tmp.type),
1954                                                                      RNA_struct_identifier(param->ptr.type));
1955                                                 }
1956                                                 Py_XDECREF(value_new); return -1;
1957                                         }
1958                                 }
1959
1960                                 Py_XDECREF(value_new);
1961
1962                                 break;
1963                         }
1964                         case PROP_COLLECTION:
1965                         {
1966                                 Py_ssize_t seq_len, i;
1967                                 PyObject *item;
1968                                 PointerRNA itemptr;
1969                                 ListBase *lb;
1970                                 CollectionPointerLink *link;
1971
1972                                 lb = (data) ? (ListBase *)data : NULL;
1973
1974                                 /* convert a sequence of dict's into a collection */
1975                                 if (!PySequence_Check(value)) {
1976                                         PyErr_Format(PyExc_TypeError,
1977                                                      "%.200s %.200s.%.200s expected a sequence for an RNA collection, not %.200s",
1978                                                      error_prefix, RNA_struct_identifier(ptr->type),
1979                                                      RNA_property_identifier(prop), Py_TYPE(value)->tp_name);
1980                                         return -1;
1981                                 }
1982
1983                                 seq_len = PySequence_Size(value);
1984                                 for (i = 0; i < seq_len; i++) {
1985                                         item = PySequence_GetItem(value, i);
1986
1987                                         if (item == NULL) {
1988                                                 PyErr_Format(PyExc_TypeError,
1989                                                              "%.200s %.200s.%.200s failed to get sequence index '%d' for an RNA collection",
1990                                                              error_prefix, RNA_struct_identifier(ptr->type),
1991                                                              RNA_property_identifier(prop), i);
1992                                                 Py_XDECREF(item);
1993                                                 return -1;
1994                                         }
1995
1996                                         if (PyDict_Check(item) == 0) {
1997                                                 PyErr_Format(PyExc_TypeError,
1998                                                              "%.200s %.200s.%.200s expected a each sequence "
1999                                                              "member to be a dict for an RNA collection, not %.200s",
2000                                                              error_prefix, RNA_struct_identifier(ptr->type),
2001                                                              RNA_property_identifier(prop), Py_TYPE(item)->tp_name);
2002                                                 Py_XDECREF(item);
2003                                                 return -1;
2004                                         }
2005
2006                                         if (lb) {
2007                                                 link = MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
2008                                                 link->ptr = itemptr;
2009                                                 BLI_addtail(lb, link);
2010                                         }
2011                                         else
2012                                                 RNA_property_collection_add(ptr, prop, &itemptr);
2013
2014                                         if (pyrna_pydict_to_props(
2015                                                 &itemptr, item, true,
2016                                                 "Converting a python list to an RNA collection") == -1)
2017                                         {
2018                                                 PyObject *msg = PyC_ExceptionBuffer();
2019                                                 const char *msg_char = _PyUnicode_AsString(msg);
2020
2021                                                 PyErr_Format(PyExc_TypeError,
2022                                                              "%.200s %.200s.%.200s error converting a member of a collection "
2023                                                              "from a dicts into an RNA collection, failed with: %s",
2024                                                              error_prefix, RNA_struct_identifier(ptr->type),
2025                                                              RNA_property_identifier(prop), msg_char);
2026
2027                                                 Py_DECREF(item);
2028                                                 Py_DECREF(msg);
2029                                                 return -1;
2030                                         }
2031                                         Py_DECREF(item);
2032                                 }
2033
2034                                 break;
2035                         }
2036                         default:
2037                                 PyErr_Format(PyExc_AttributeError,
2038                                              "%.200s %.200s.%.200s unknown property type (pyrna_py_to_prop)",
2039                                              error_prefix, RNA_struct_identifier(ptr->type),
2040                                              RNA_property_identifier(prop));
2041                                 return -1;
2042                 }
2043         }
2044
2045         /* Run rna property functions */
2046         if (RNA_property_update_check(prop)) {
2047                 RNA_property_update(BPy_GetContext(), ptr, prop);
2048         }
2049
2050         return 0;
2051 }
2052
2053 static PyObject *pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
2054 {
2055         PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2056         return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
2057 }
2058
2059 static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
2060 {
2061         int ret = 0;
2062         PointerRNA *ptr = &self->ptr;
2063         PropertyRNA *prop = self->prop;
2064
2065         const int totdim = RNA_property_array_dimension(ptr, prop, NULL);
2066
2067         if (totdim > 1) {
2068                 /* char error_str[512]; */
2069                 if (pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "") == -1) {
2070                         /* error is set */
2071                         ret = -1;
2072                 }
2073         }
2074         else {
2075                 /* see if we can coerce into a python type - PropertyType */
2076                 switch (RNA_property_type(prop)) {
2077                         case PROP_BOOLEAN:
2078                         {
2079                                 int param = PyLong_AsLong(value);
2080
2081                                 if (param < 0 || param > 1) {
2082                                         PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
2083                                         ret = -1;
2084                                 }
2085                                 else {
2086                                         RNA_property_boolean_set_index(ptr, prop, index, param);
2087                                 }
2088                                 break;
2089                         }
2090                         case PROP_INT:
2091                         {
2092                                 int param = PyLong_AsLong(value);
2093                                 if (param == -1 && PyErr_Occurred()) {
2094                                         PyErr_SetString(PyExc_TypeError, "expected an int type");
2095                                         ret = -1;
2096                                 }
2097                                 else {
2098                                         RNA_property_int_clamp(ptr, prop, &param);
2099                                         RNA_property_int_set_index(ptr, prop, index, param);
2100                                 }
2101                                 break;
2102                         }
2103                         case PROP_FLOAT:
2104                         {
2105                                 float param = PyFloat_AsDouble(value);
2106                                 if (PyErr_Occurred()) {
2107                                         PyErr_SetString(PyExc_TypeError, "expected a float type");
2108                                         ret = -1;
2109                                 }
2110                                 else {
2111                                         RNA_property_float_clamp(ptr, prop, &param);
2112                                         RNA_property_float_set_index(ptr, prop, index, param);
2113                                 }
2114                                 break;
2115                         }
2116                         default:
2117                                 PyErr_SetString(PyExc_AttributeError, "not an array type");
2118                                 ret = -1;
2119                                 break;
2120                 }
2121         }
2122
2123         /* Run rna property functions */
2124         if (RNA_property_update_check(prop)) {
2125                 RNA_property_update(BPy_GetContext(), ptr, prop);
2126         }
2127
2128         return ret;
2129 }
2130
2131 /* ---------------sequence------------------------------------------- */
2132 static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self)
2133 {
2134         PYRNA_PROP_CHECK_INT((BPy_PropertyRNA *)self);
2135
2136         if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
2137                 return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
2138         else
2139                 return RNA_property_array_length(&self->ptr, self->prop);
2140 }
2141
2142 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self)
2143 {
2144         PYRNA_PROP_CHECK_INT(self);
2145
2146         return RNA_property_collection_length(&self->ptr, self->prop);
2147 }
2148
2149 /* bool functions are for speed, so we can avoid getting the length
2150  * of 1000's of items in a linked list for eg. */
2151 static int pyrna_prop_array_bool(BPy_PropertyRNA *self)
2152 {
2153         PYRNA_PROP_CHECK_INT(self);
2154
2155         return RNA_property_array_length(&self->ptr, self->prop) ? 1 : 0;
2156 }
2157
2158 static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
2159 {
2160         /* no callback defined, just iterate and find the nth item */
2161         CollectionPropertyIterator iter;
2162         int test;
2163
2164         PYRNA_PROP_CHECK_INT(self);
2165
2166         RNA_property_collection_begin(&self->ptr, self->prop, &iter);
2167         test = iter.valid;
2168         RNA_property_collection_end(&iter);
2169         return test;
2170 }
2171
2172
2173 /* notice getting the length of the collection is avoided unless negative
2174  * index is used or to detect internal error with a valid index.
2175  * This is done for faster lookups. */
2176 #define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err)                              \
2177         if (keynum < 0) {                                                         \
2178             keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
2179             if (keynum_abs < 0) {                                                 \
2180                 PyErr_Format(PyExc_IndexError,                                    \
2181                              "bpy_prop_collection[%d]: out of range.", keynum);   \
2182                 return ret_err;                                                   \
2183             }                                                                     \
2184         } (void)0
2185
2186
2187 /* internal use only */
2188 static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
2189 {
2190         PointerRNA newptr;
2191         Py_ssize_t keynum_abs = keynum;
2192
2193         PYRNA_PROP_CHECK_OBJ(self);
2194
2195         PYRNA_PROP_COLLECTION_ABS_INDEX(NULL);
2196
2197         if (RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum_abs, &newptr)) {
2198                 return pyrna_struct_CreatePyObject(&newptr);
2199         }
2200         else {
2201                 const int len = RNA_property_collection_length(&self->ptr, self->prop);
2202                 if (keynum_abs >= len) {
2203                         PyErr_Format(PyExc_IndexError,
2204                                      "bpy_prop_collection[index]: "
2205                                      "index %d out of range, size %d", keynum, len);
2206                 }
2207                 else {
2208                         PyErr_Format(PyExc_RuntimeError,
2209                                      "bpy_prop_collection[index]: internal error, "
2210                                      "valid index %d given in %d sized collection but value not found",
2211                                      keynum_abs, len);
2212                 }
2213
2214                 return NULL;
2215         }
2216 }
2217
2218 /* values type must have been already checked */
2219 static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
2220 {
2221         Py_ssize_t keynum_abs = keynum;
2222         const PointerRNA *ptr = (value == Py_None) ? (&PointerRNA_NULL) : &((BPy_StructRNA *)value)->ptr;
2223
2224         PYRNA_PROP_CHECK_INT(self);
2225
2226         PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
2227
2228         if (RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
2229                 const int len = RNA_property_collection_length(&self->ptr, self->prop);
2230                 if (keynum_abs >= len) {
2231                         PyErr_Format(PyExc_IndexError,
2232                                      "bpy_prop_collection[index] = value: "
2233                                      "index %d out of range, size %d", keynum, len);
2234                 }
2235                 else {
2236
2237                         PyErr_Format(PyExc_IndexError,
2238                                      "bpy_prop_collection[index] = value: "
2239                                      "failed assignment (unknown reason)", keynum);
2240                 }
2241                 return -1;
2242         }
2243
2244         return 0;
2245 }
2246
2247 static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
2248 {
2249         int len;
2250
2251         PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2252
2253         len = pyrna_prop_array_length(self);
2254
2255         if (keynum < 0) keynum += len;
2256
2257         if (keynum >= 0 && keynum < len)
2258                 return pyrna_prop_array_to_py_index(self, keynum);
2259
2260         PyErr_Format(PyExc_IndexError,
2261                      "bpy_prop_array[index]: index %d out of range", keynum);
2262         return NULL;
2263 }
2264
2265 static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, const char *keyname)
2266 {
2267         PointerRNA newptr;
2268
2269         PYRNA_PROP_CHECK_OBJ(self);
2270
2271         if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
2272                 return pyrna_struct_CreatePyObject(&newptr);
2273
2274         PyErr_Format(PyExc_KeyError, "bpy_prop_collection[key]: key \"%.200s\" not found", keyname);
2275         return NULL;
2276 }
2277 /* static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname) */
2278
2279 /**
2280  * Special case: `bpy.data.objects["some_id_name", "//some_lib_name.blend"]`
2281  * also for:     `bpy.data.objects.get(("some_id_name", "//some_lib_name.blend"), fallback)`
2282  *
2283  * \note
2284  * error codes since this is not to be called directly from python,
2285  * this matches pythons `__contains__` values capi.
2286  * - -1: exception set
2287  * -  0: not found
2288  * -  1: found
2289  */
2290 static int pyrna_prop_collection_subscript_str_lib_pair_ptr(
2291         BPy_PropertyRNA *self, PyObject *key,
2292         const char *err_prefix, const short err_not_found,
2293         PointerRNA *r_ptr)
2294 {
2295         const char *keyname;
2296
2297         /* first validate the args, all we know is that they are a tuple */
2298         if (PyTuple_GET_SIZE(key) != 2) {
2299                 PyErr_Format(PyExc_KeyError,
2300                              "%s: tuple key must be a pair, not size %d",
2301                              err_prefix, PyTuple_GET_SIZE(key));
2302                 return -1;
2303         }
2304         else if (self->ptr.type != &RNA_BlendData) {
2305                 PyErr_Format(PyExc_KeyError,
2306                              "%s: is only valid for bpy.data collections, not %.200s",
2307                              err_prefix, RNA_struct_identifier(self->ptr.type));
2308                 return -1;
2309         }
2310         else if ((keyname = _PyUnicode_AsString(PyTuple_GET_ITEM(key, 0))) == NULL) {
2311                 PyErr_Format(PyExc_KeyError,
2312                              "%s: id must be a string, not %.200s",
2313                              err_prefix, Py_TYPE(PyTuple_GET_ITEM(key, 0))->tp_name);
2314                 return -1;
2315         }
2316         else {
2317                 PyObject *keylib = PyTuple_GET_ITEM(key, 1);
2318                 Library *lib;
2319                 bool found = false;
2320
2321                 if (keylib == Py_None) {
2322                         lib = NULL;
2323                 }
2324                 else if (PyUnicode_Check(keylib)) {
2325                         Main *bmain = self->ptr.data;
2326                         const char *keylib_str = _PyUnicode_AsString(keylib);
2327                         lib = BLI_findstring(&bmain->library, keylib_str, offsetof(Library, name));
2328                         if (lib == NULL) {
2329                                 if (err_not_found) {
2330                                         PyErr_Format(PyExc_KeyError,
2331                                                      "%s: lib name '%.240s' "
2332                                                      "does not reference a valid library",
2333                                                      err_prefix, keylib_str);
2334                                         return -1;
2335                                 }
2336                                 else {
2337                                         return 0;
2338                                 }
2339
2340                         }
2341                 }
2342                 else {
2343                         PyErr_Format(PyExc_KeyError,
2344                                      "%s: lib must be a sting or None, not %.200s",
2345                                      err_prefix, Py_TYPE(keylib)->tp_name);
2346                         return -1;
2347                 }
2348
2349                 /* lib is either a valid pointer or NULL,
2350                  * either way can do direct comparison with id.lib */
2351
2352                 RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop)
2353                 {
2354                         ID *id = itemptr.data; /* always an ID */
2355                         if (id->lib == lib && (STREQLEN(keyname, id->name + 2, sizeof(id->name) - 2))) {
2356                                 found = true;
2357                                 if (r_ptr) {
2358                                         *r_ptr = itemptr;
2359                                 }
2360                                 break;
2361                         }
2362                 }
2363                 RNA_PROP_END;
2364
2365                 /* we may want to fail silently as with collection.get() */
2366                 if ((found == false) && err_not_found) {
2367                         /* only runs for getitem access so use fixed string */
2368                         PyErr_SetString(PyExc_KeyError,
2369                                         "bpy_prop_collection[key, lib]: not found");
2370                         return -1;
2371                 }
2372                 else {
2373                         return found; /* 1 / 0, no exception */
2374                 }
2375         }
2376 }
2377
2378 static PyObject *pyrna_prop_collection_subscript_str_lib_pair(
2379         BPy_PropertyRNA *self, PyObject *key,
2380         const char *err_prefix, const bool err_not_found)
2381 {
2382         PointerRNA ptr;
2383         const int contains = pyrna_prop_collection_subscript_str_lib_pair_ptr(self, key, err_prefix, err_not_found, &ptr);
2384
2385         if (contains == 1) {
2386                 return pyrna_struct_CreatePyObject(&ptr);
2387         }
2388         else {
2389                 return NULL;
2390         }
2391 }
2392
2393
2394 static PyObject *pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py_ssize_t start, Py_ssize_t stop)
2395 {
2396         CollectionPropertyIterator rna_macro_iter;
2397         int count;
2398
2399         PyObject *list;
2400         PyObject *item;
2401
2402         PYRNA_PROP_CHECK_OBJ(self);
2403
2404         list = PyList_New(0);
2405
2406         /* skip to start */
2407         RNA_property_collection_begin(&self->ptr, self->prop, &rna_macro_iter);
2408         RNA_property_collection_skip(&rna_macro_iter, start);
2409
2410         /* add items until stop */
2411         for (count = start; rna_macro_iter.valid;
2412              RNA_property_collection_next(&rna_macro_iter))
2413         {
2414                 item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
2415                 PyList_APPEND(list, item);
2416
2417                 count++;
2418                 if (count == stop) {
2419                         break;
2420                 }
2421         }
2422
2423         RNA_property_collection_end(&rna_macro_iter);
2424
2425         return list;
2426 }
2427
2428 /* TODO - dimensions
2429  * note: could also use pyrna_prop_array_to_py_index(self, count) in a loop but its a lot slower
2430  * since at the moment it reads (and even allocates) the entire array for each index.
2431  */
2432 static PyObject *pyrna_prop_array_subscript_slice(
2433         BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop,
2434         Py_ssize_t start, Py_ssize_t stop, Py_ssize_t length)
2435 {
2436         int count, totdim;
2437         PyObject *tuple;
2438
2439         /* isn't needed, internal use only */
2440         // PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2441
2442         tuple = PyTuple_New(stop - start);
2443
2444         totdim = RNA_property_array_dimension(ptr, prop, NULL);
2445
2446         if (totdim > 1) {
2447                 for (count = start; count < stop; count++)
2448                         PyTuple_SET_ITEM(tuple, count - start, pyrna_prop_array_to_py_index(self, count));
2449         }
2450         else {
2451                 switch (RNA_property_type(prop)) {
2452                         case PROP_FLOAT:
2453                         {
2454                                 float values_stack[PYRNA_STACK_ARRAY];
2455                                 float *values;
2456                                 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(float) * length); }
2457                                 else                            { values = values_stack; }
2458                                 RNA_property_float_get_array(ptr, prop, values);
2459
2460                                 for (count = start; count < stop; count++)
2461                                         PyTuple_SET_ITEM(tuple, count - start, PyFloat_FromDouble(values[count]));
2462
2463                                 if (values != values_stack) {
2464                                         PyMem_FREE(values);
2465                                 }
2466                                 break;
2467                         }
2468                         case PROP_BOOLEAN:
2469                         {
2470                                 int values_stack[PYRNA_STACK_ARRAY];
2471                                 int *values;
2472                                 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2473                                 else                            { values = values_stack; }
2474
2475                                 RNA_property_boolean_get_array(ptr, prop, values);
2476                                 for (count = start; count < stop; count++)
2477                                         PyTuple_SET_ITEM(tuple, count - start, PyBool_FromLong(values[count]));
2478
2479                                 if (values != values_stack) {
2480                                         PyMem_FREE(values);
2481                                 }
2482                                 break;
2483                         }
2484                         case PROP_INT:
2485                         {
2486                                 int values_stack[PYRNA_STACK_ARRAY];
2487                                 int *values;
2488                                 if (length > PYRNA_STACK_ARRAY) { values = PyMem_MALLOC(sizeof(int) * length); }
2489                                 else                            { values = values_stack; }
2490
2491                                 RNA_property_int_get_array(ptr, prop, values);
2492                                 for (count = start; count < stop; count++)
2493                                         PyTuple_SET_ITEM(tuple, count - start, PyLong_FromLong(values[count]));
2494
2495                                 if (values != values_stack) {
2496                                         PyMem_FREE(values);
2497                                 }
2498                                 break;
2499                         }
2500                         default:
2501                                 BLI_assert(!"Invalid array type");
2502
2503                                 PyErr_SetString(PyExc_TypeError, "not an array type");
2504                                 Py_DECREF(tuple);
2505                                 tuple = NULL;
2506                                 break;
2507                 }
2508         }
2509         return tuple;
2510 }
2511
2512 static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
2513 {
2514         PYRNA_PROP_CHECK_OBJ(self);
2515
2516         if (PyUnicode_Check(key)) {
2517                 return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
2518         }
2519         else if (PyIndex_Check(key)) {
2520                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2521                 if (i == -1 && PyErr_Occurred())
2522                         return NULL;
2523
2524                 return pyrna_prop_collection_subscript_int(self, i);
2525         }
2526         else if (PySlice_Check(key)) {
2527                 PySliceObject *key_slice = (PySliceObject *)key;
2528                 Py_ssize_t step = 1;
2529
2530                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2531                         return NULL;
2532                 }
2533                 else if (step != 1) {
2534                         PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2535                         return NULL;
2536                 }
2537                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2538                         return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2539                 }
2540                 else {
2541                         Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2542
2543                         /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2544                         if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2545                         if (key_slice->stop  != Py_None && !_PyEval_SliceIndex(key_slice->stop,  &stop))  return NULL;
2546
2547                         if (start < 0 || stop < 0) {
2548                                 /* only get the length for negative values */
2549                                 Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2550                                 if (start < 0) start += len;
2551                                 if (stop  < 0) stop  += len;
2552                         }
2553
2554                         if (stop - start <= 0) {
2555                                 return PyList_New(0);
2556                         }
2557                         else {
2558                                 return pyrna_prop_collection_subscript_slice(self, start, stop);
2559                         }
2560                 }
2561         }
2562         else if (PyTuple_Check(key)) {
2563                 /* special case, for ID datablocks we */
2564                 return pyrna_prop_collection_subscript_str_lib_pair(self, key,
2565                                                                     "bpy_prop_collection[id, lib]", true);
2566         }
2567         else {
2568                 PyErr_Format(PyExc_TypeError,
2569                              "bpy_prop_collection[key]: invalid key, "
2570                              "must be a string or an int, not %.200s",
2571                              Py_TYPE(key)->tp_name);
2572                 return NULL;
2573         }
2574 }
2575
2576 /* generic check to see if a PyObject is compatible with a collection
2577  * -1 on failure, 0 on success, sets the error */
2578 static int pyrna_prop_collection_type_check(BPy_PropertyRNA *self, PyObject *value)
2579 {
2580         StructRNA *prop_srna;
2581
2582         if (value == Py_None) {
2583                 if (RNA_property_flag(self->prop) & PROP_NEVER_NULL) {
2584                         PyErr_Format(PyExc_TypeError,
2585                                      "bpy_prop_collection[key] = value: invalid, "
2586                                      "this collection doesn't support None assignment");
2587                         return -1;
2588                 }
2589                 else {
2590                         return 0; /* None is OK */
2591                 }
2592         }
2593         else if (BPy_StructRNA_Check(value) == 0) {
2594                 PyErr_Format(PyExc_TypeError,
2595                              "bpy_prop_collection[key] = value: invalid, "
2596                              "expected a StructRNA type or None, not a %.200s",
2597                              Py_TYPE(value)->tp_name);
2598                 return -1;
2599         }
2600         else if ((prop_srna = RNA_property_pointer_type(&self->ptr, self->prop))) {
2601                 StructRNA *value_srna = ((BPy_StructRNA *)value)->ptr.type;
2602                 if (RNA_struct_is_a(value_srna, prop_srna) == 0) {
2603                         PyErr_Format(PyExc_TypeError,
2604                                      "bpy_prop_collection[key] = value: invalid, "
2605                                      "expected a '%.200s' type or None, not a '%.200s'",
2606                                      RNA_struct_identifier(prop_srna),
2607                                      RNA_struct_identifier(value_srna)
2608                                      );
2609                         return -1;
2610                 }
2611                 else {
2612                         return 0; /* OK, this is the correct type!*/
2613                 }
2614         }
2615
2616         PyErr_Format(PyExc_TypeError,
2617                      "bpy_prop_collection[key] = value: internal error, "
2618                      "failed to get the collection type");
2619         return -1;
2620 }
2621
2622 /* note: currently this is a copy of 'pyrna_prop_collection_subscript' with
2623  * large blocks commented, we may support slice/key indices later */
2624 static int pyrna_prop_collection_ass_subscript(BPy_PropertyRNA *self, PyObject *key, PyObject *value)
2625 {
2626         PYRNA_PROP_CHECK_INT(self);
2627
2628         /* validate the assigned value */
2629         if (value == NULL) {
2630                 PyErr_SetString(PyExc_TypeError,
2631                                 "del bpy_prop_collection[key]: not supported");
2632                 return -1;
2633         }
2634         else if (pyrna_prop_collection_type_check(self, value) == -1) {
2635                 return -1; /* exception is set */
2636         }
2637
2638 #if 0
2639         if (PyUnicode_Check(key)) {
2640                 return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
2641         }
2642         else
2643 #endif
2644         if (PyIndex_Check(key)) {
2645                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2646                 if (i == -1 && PyErr_Occurred())
2647                         return -1;
2648
2649                 return pyrna_prop_collection_ass_subscript_int(self, i, value);
2650         }
2651 #if 0 /* TODO, fake slice assignment */
2652         else if (PySlice_Check(key)) {
2653                 PySliceObject *key_slice = (PySliceObject *)key;
2654                 Py_ssize_t step = 1;
2655
2656                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2657                         return NULL;
2658                 }
2659                 else if (step != 1) {
2660                         PyErr_SetString(PyExc_TypeError, "bpy_prop_collection[slice]: slice steps not supported");
2661                         return NULL;
2662                 }
2663                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2664                         return pyrna_prop_collection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
2665                 }
2666                 else {
2667                         Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
2668
2669                         /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
2670                         if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) return NULL;
2671                         if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop))    return NULL;
2672
2673                         if (start < 0 || stop < 0) {
2674                                 /* only get the length for negative values */
2675                                 Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
2676                                 if (start < 0) start += len;
2677                                 if (stop  < 0) stop  += len;
2678                         }
2679
2680                         if (stop - start <= 0) {
2681                                 return PyList_New(0);
2682                         }
2683                         else {
2684                                 return pyrna_prop_collection_subscript_slice(self, start, stop);
2685                         }
2686                 }
2687         }
2688 #endif
2689         else {
2690                 PyErr_Format(PyExc_TypeError,
2691                              "bpy_prop_collection[key]: invalid key, "
2692                              "must be a string or an int, not %.200s",
2693                              Py_TYPE(key)->tp_name);
2694                 return -1;
2695         }
2696 }
2697
2698 static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
2699 {
2700         PYRNA_PROP_CHECK_OBJ((BPy_PropertyRNA *)self);
2701
2702 #if 0
2703         if (PyUnicode_Check(key)) {
2704                 return pyrna_prop_array_subscript_str(self, _PyUnicode_AsString(key));
2705         }
2706         else
2707 #endif
2708         if (PyIndex_Check(key)) {
2709                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2710                 if (i == -1 && PyErr_Occurred())
2711                         return NULL;
2712                 return pyrna_prop_array_subscript_int(self, PyLong_AsLong(key));
2713         }
2714         else if (PySlice_Check(key)) {
2715                 Py_ssize_t step = 1;
2716                 PySliceObject *key_slice = (PySliceObject *)key;
2717
2718                 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key, &step)) {
2719                         return NULL;
2720                 }
2721                 else if (step != 1) {
2722                         PyErr_SetString(PyExc_TypeError, "bpy_prop_array[slice]: slice steps not supported");
2723                         return NULL;
2724                 }
2725                 else if (key_slice->start == Py_None && key_slice->stop == Py_None) {
2726                         /* note, no significant advantage with optimizing [:] slice as with collections
2727                          * but include here for consistency with collection slice func */
2728                         Py_ssize_t len = (Py_ssize_t)pyrna_prop_array_length(self);
2729                         return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len);
2730                 }
2731                 else {
2732                         int len = pyrna_prop_array_length(self);
2733                         Py_ssize_t start, stop, slicelength;
2734
2735                         if (PySlice_GetIndicesEx(key, len, &start, &stop, &step, &slicelength) < 0)
2736                                 return NULL;
2737
2738                         if (slicelength <= 0) {
2739                                 return PyTuple_New(0);
2740                         }
2741                         else {
2742                                 return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, start, stop, len);
2743                         }
2744                 }
2745         }
2746         else {
2747                 PyErr_SetString(PyExc_AttributeError, "bpy_prop_array[key]: invalid key, key must be an int");
2748                 return NULL;
2749         }
2750 }
2751
2752 /**
2753  * Helpers for #prop_subscript_ass_array_slice
2754  */
2755
2756 static PyObject *prop_subscript_ass_array_slice__as_seq_fast(PyObject *value, int length)
2757 {
2758         PyObject *value_fast;
2759         if (!(value_fast = PySequence_Fast(
2760                   value,
2761                   "bpy_prop_array[slice] = value: "
2762                   "element in assignment is not a sequence type")))
2763         {
2764                 return NULL;
2765         }
2766         else if (PySequence_Fast_GET_SIZE(value_fast) != length) {
2767                 Py_DECREF(value_fast);
2768                 PyErr_SetString(
2769                         PyExc_ValueError,
2770                         "bpy_prop_array[slice] = value: "
2771                         "re-sizing bpy_struct element in arrays isn't supported");
2772
2773                 return NULL;
2774         }
2775         else {
2776                 return value_fast;
2777         }
2778 }
2779
2780 static int prop_subscript_ass_array_slice__float_recursive(
2781         PyObject **value_items, float *value,
2782         int totdim, const int dimsize[],
2783         const float range[2])
2784 {
2785         const int length = dimsize[0];
2786         if (totdim > 1) {
2787                 int index = 0;
2788                 int i;
2789                 for (i = 0; i != length; i++) {
2790                         PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
2791                         if (UNLIKELY(subvalue == NULL)) {
2792                                 return 0;
2793                         }
2794
2795                         index += prop_subscript_ass_array_slice__float_recursive(
2796                                 PySequence_Fast_ITEMS(subvalue), &value[index],
2797                                 totdim - 1, &dimsize[1], range);
2798
2799                         Py_DECREF(subvalue);
2800                 }
2801                 return index;
2802         }
2803         else {
2804                 BLI_assert(totdim == 1);
2805                 const float min = range[0], max = range[1];
2806                 int i;
2807                 for (i = 0; i != length; i++) {
2808                         float v = PyFloat_AsDouble(value_items[i]);
2809                         CLAMP(v, min, max);
2810                         value[i] = v;
2811                 }
2812                 return i;
2813         }
2814 }
2815
2816 static int prop_subscript_ass_array_slice__int_recursive(
2817         PyObject **value_items, int *value,
2818         int totdim, const int dimsize[],
2819         const int range[2])
2820 {
2821         const int length = dimsize[0];
2822         if (totdim > 1) {
2823                 int index = 0;
2824                 int i;
2825                 for (i = 0; i != length; i++) {
2826                         PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
2827                         if (UNLIKELY(subvalue == NULL)) {
2828                                 return 0;
2829                         }
2830
2831                         index += prop_subscript_ass_array_slice__int_recursive(
2832                                 PySequence_Fast_ITEMS(subvalue), &value[index],
2833                                 totdim - 1, &dimsize[1], range);
2834
2835                         Py_DECREF(subvalue);
2836                 }
2837                 return index;
2838         }
2839         else {
2840                 BLI_assert(totdim == 1);
2841                 const int min = range[0], max = range[1];
2842                 int i;
2843                 for (i = 0; i != length; i++) {
2844                         int v = PyLong_AsLong(value_items[i]);
2845                         CLAMP(v, min, max);
2846                         value[i] = v;
2847                 }
2848                 return i;
2849         }
2850 }
2851
2852 static int prop_subscript_ass_array_slice__bool_recursive(
2853         PyObject **value_items, int *value,
2854         int totdim, const int dimsize[])
2855 {
2856         const int length = dimsize[0];
2857         if (totdim > 1) {
2858                 int index = 0;
2859                 int i;
2860                 for (i = 0; i != length; i++) {
2861                         PyObject *subvalue = prop_subscript_ass_array_slice__as_seq_fast(value_items[i], dimsize[1]);
2862                         if (UNLIKELY(subvalue == NULL)) {
2863                                 return 0;
2864                         }
2865
2866                         index += prop_subscript_ass_array_slice__bool_recursive(
2867                                 PySequence_Fast_ITEMS(subvalue), &value[index],
2868                                 totdim - 1, &dimsize[1]);
2869
2870                         Py_DECREF(subvalue);
2871                 }
2872                 return index;
2873         }
2874         else {
2875                 BLI_assert(totdim == 1);
2876                 int i;
2877                 for (i = 0; i != length; i++) {
2878                         int v = PyLong_AsLong(value_items[i]);
2879                         value[i] = v;
2880                 }
2881                 return i;
2882         }
2883 }
2884
2885 /* could call (pyrna_py_to_prop_array_index(self, i, value) in a loop but it is slow */
2886 static int prop_subscript_ass_array_slice(
2887         PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset,
2888         int start, int stop, int length, PyObject *value_orig)
2889 {
2890         const int length_flat = RNA_property_array_length(ptr, prop);
2891         PyObject *value;
2892         PyObject **value_items;
2893         void *values_alloc = NULL;
2894         int ret = 0;
2895
2896         if (value_orig == NULL) {
2897                 PyErr_SetString(PyExc_TypeError,
2898                                 "bpy_prop_array[slice] = value: deleting with list types is not supported by bpy_struct");
2899                 return -1;
2900         }