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