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