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