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