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