49de1b3e944e76938ca929f15ae025221c10d5ab
[blender.git] / source / blender / python / intern / bpy_rna.c
1
2 /**
3  * $Id$
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include "bpy_rna.h"
27 #include "bpy_props.h"
28 #include "bpy_util.h"
29 //#include "blendef.h"
30 #include "BLI_dynstr.h"
31 #include "BLI_listbase.h"
32 #include "BLI_string.h"
33 #include "float.h" /* FLT_MIN/MAX */
34
35 #include "RNA_access.h"
36 #include "RNA_define.h" /* for defining our own rna */
37
38 #include "MEM_guardedalloc.h"
39 #include "BKE_utildefines.h"
40 #include "BKE_context.h"
41 #include "BKE_global.h" /* evil G.* */
42 #include "BKE_report.h"
43
44 #include "BKE_animsys.h"
45 #include "BKE_fcurve.h"
46
47 /* only for keyframing */
48 #include "DNA_scene_types.h"
49 #include "DNA_anim_types.h"
50 #include "ED_keyframing.h"
51
52 #define USE_MATHUTILS
53
54 #ifdef USE_MATHUTILS
55 #include "../generic/Mathutils.h" /* so we can have mathutils callbacks */
56 #include "../generic/IDProp.h" /* for IDprop lookups */
57
58 static PyObject *prop_subscript_array_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
59
60 /* bpyrna vector/euler/quat callbacks */
61 static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
62
63 static int mathutils_rna_generic_check(BPy_PropertyRNA *self)
64 {
65         return self->prop?1:0;
66 }
67
68 static int mathutils_rna_vector_get(BPy_PropertyRNA *self, int subtype, float *vec_from)
69 {
70         if(self->prop==NULL)
71                 return 0;
72         
73         RNA_property_float_get_array(&self->ptr, self->prop, vec_from);
74         return 1;
75 }
76
77 static int mathutils_rna_vector_set(BPy_PropertyRNA *self, int subtype, float *vec_to)
78 {
79         if(self->prop==NULL)
80                 return 0;
81
82         RNA_property_float_set_array(&self->ptr, self->prop, vec_to);
83         RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
84         return 1;
85 }
86
87 static int mathutils_rna_vector_get_index(BPy_PropertyRNA *self, int subtype, float *vec_from, int index)
88 {
89         if(self->prop==NULL)
90                 return 0;
91         
92         vec_from[index]= RNA_property_float_get_index(&self->ptr, self->prop, index);
93         return 1;
94 }
95
96 static int mathutils_rna_vector_set_index(BPy_PropertyRNA *self, int subtype, float *vec_to, int index)
97 {
98         if(self->prop==NULL)
99                 return 0;
100
101         RNA_property_float_set_index(&self->ptr, self->prop, index, vec_to[index]);
102         RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
103         return 1;
104 }
105
106 Mathutils_Callback mathutils_rna_array_cb = {
107         (BaseMathCheckFunc)             mathutils_rna_generic_check,
108         (BaseMathGetFunc)               mathutils_rna_vector_get,
109         (BaseMathSetFunc)               mathutils_rna_vector_set,
110         (BaseMathGetIndexFunc)  mathutils_rna_vector_get_index,
111         (BaseMathSetIndexFunc)  mathutils_rna_vector_set_index
112 };
113
114
115 /* bpyrna matrix callbacks */
116 static int mathutils_rna_matrix_cb_index= -1; /* index for our callbacks */
117
118 static int mathutils_rna_matrix_get(BPy_PropertyRNA *self, int subtype, float *mat_from)
119 {
120         if(self->prop==NULL)
121                 return 0;
122
123         RNA_property_float_get_array(&self->ptr, self->prop, mat_from);
124         return 1;
125 }
126
127 static int mathutils_rna_matrix_set(BPy_PropertyRNA *self, int subtype, float *mat_to)
128 {
129         if(self->prop==NULL)
130                 return 0;
131
132         RNA_property_float_set_array(&self->ptr, self->prop, mat_to);
133         RNA_property_update(BPy_GetContext(), &self->ptr, self->prop);
134         return 1;
135 }
136
137 Mathutils_Callback mathutils_rna_matrix_cb = {
138         (BaseMathCheckFunc)             mathutils_rna_generic_check,
139         (BaseMathGetFunc)               mathutils_rna_matrix_get,
140         (BaseMathSetFunc)               mathutils_rna_matrix_set,
141         (BaseMathGetIndexFunc)  NULL,
142         (BaseMathSetIndexFunc)  NULL
143 };
144
145 #define PROP_ALL_VECTOR_SUBTYPES PROP_TRANSLATION: case PROP_DIRECTION: case PROP_VELOCITY: case PROP_ACCELERATION: case PROP_XYZ: case PROP_XYZ|PROP_UNIT_LENGTH
146
147 PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
148 {
149         PyObject *ret= NULL;
150
151 #ifdef USE_MATHUTILS
152         int subtype, totdim;
153         int len;
154         int is_thick;
155         int flag= RNA_property_flag(prop);
156
157         /* disallow dynamic sized arrays to be wrapped since the size could change
158          * to a size mathutils does not support */
159         if ((RNA_property_type(prop) != PROP_FLOAT) || (flag & PROP_DYNAMIC))
160                 return NULL;
161
162         len= RNA_property_array_length(ptr, prop);
163         subtype= RNA_property_subtype(prop);
164         totdim= RNA_property_array_dimension(ptr, prop, NULL);
165         is_thick = (flag & PROP_THICK_WRAP);
166
167         if (totdim == 1 || (totdim == 2 && subtype == PROP_MATRIX)) {
168                 if(!is_thick)
169                         ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
170
171                 switch(RNA_property_subtype(prop)) {
172                 case PROP_ALL_VECTOR_SUBTYPES:
173                         if(len>=2 && len <= 4) {
174                                 if(is_thick) {
175                                         ret= newVectorObject(NULL, len, Py_NEW, NULL);
176                                         RNA_property_float_get_array(ptr, prop, ((VectorObject *)ret)->vec);
177                                 }
178                                 else {
179                                         PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, FALSE);
180                                         Py_DECREF(ret); /* the vector owns now */
181                                         ret= vec_cb; /* return the vector instead */
182                                 }
183                         }
184                         break;
185                 case PROP_MATRIX:
186                         if(len==16) {
187                                 if(is_thick) {
188                                         ret= newMatrixObject(NULL, 4, 4, Py_NEW, NULL);
189                                         RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
190                                 }
191                                 else {
192                                         PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, FALSE);
193                                         Py_DECREF(ret); /* the matrix owns now */
194                                         ret= mat_cb; /* return the matrix instead */
195                                 }
196                         }
197                         else if (len==9) {
198                                 if(is_thick) {
199                                         ret= newMatrixObject(NULL, 3, 3, Py_NEW, NULL);
200                                         RNA_property_float_get_array(ptr, prop, ((MatrixObject *)ret)->contigPtr);
201                                 }
202                                 else {
203                                         PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, FALSE);
204                                         Py_DECREF(ret); /* the matrix owns now */
205                                         ret= mat_cb; /* return the matrix instead */
206                                 }
207                         }
208                         break;
209                 case PROP_EULER:
210                 case PROP_QUATERNION:
211                         if(len==3) { /* euler */
212                                 if(is_thick) {
213                                         ret= newEulerObject(NULL, Py_NEW, NULL);
214                                         RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
215                                 }
216                                 else {
217                                         PyObject *eul_cb= newEulerObject_cb(ret, mathutils_rna_array_cb_index, FALSE);
218                                         Py_DECREF(ret); /* the matrix owns now */
219                                         ret= eul_cb; /* return the matrix instead */
220                                 }
221                         }
222                         else if (len==4) {
223                                 if(is_thick) {
224                                         ret= newQuaternionObject(NULL, Py_NEW, NULL);
225                                         RNA_property_float_get_array(ptr, prop, ((QuaternionObject *)ret)->quat);
226                                 }
227                                 else {
228                                         PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, FALSE);
229                                         Py_DECREF(ret); /* the matrix owns now */
230                                         ret= quat_cb; /* return the matrix instead */
231                                 }
232                         }
233                         break;
234                 default:
235                         break;
236                 }
237         }
238
239         if(ret==NULL) {
240                 if(is_thick) {
241                         /* this is an array we cant reference (since its not thin wrappable)
242                          * and cannot be coerced into a mathutils type, so return as a list */
243                         ret = prop_subscript_array_slice(NULL, ptr, prop, 0, len, len);
244                 } else {
245                         ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
246                 }
247         }
248
249 #endif
250
251         return ret;
252 }
253
254 #endif
255
256 static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
257 {
258         return (a->ptr.data==b->ptr.data) ? 0 : -1;
259 }
260
261 static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b )
262 {
263         return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1;
264 }
265
266 static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
267 {
268         PyObject *res;
269         int ok= -1; /* zero is true */
270
271         if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b))
272                 ok= pyrna_struct_compare((BPy_StructRNA *)a, (BPy_StructRNA *)b);
273
274         switch (op) {
275         case Py_NE:
276                 ok = !ok; /* pass through */
277         case Py_EQ:
278                 res = ok ? Py_False : Py_True;
279                 break;
280
281         case Py_LT:
282         case Py_LE:
283         case Py_GT:
284         case Py_GE:
285                 res = Py_NotImplemented;
286                 break;
287         default:
288                 PyErr_BadArgument();
289                 return NULL;
290         }
291
292         Py_INCREF(res);
293         return res;
294 }
295
296 static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
297 {
298         PyObject *res;
299         int ok= -1; /* zero is true */
300
301         if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b))
302                 ok= pyrna_prop_compare((BPy_PropertyRNA *)a, (BPy_PropertyRNA *)b);
303
304         switch (op) {
305         case Py_NE:
306                 ok = !ok; /* pass through */
307         case Py_EQ:
308                 res = ok ? Py_False : Py_True;
309                 break;
310
311         case Py_LT:
312         case Py_LE:
313         case Py_GT:
314         case Py_GE:
315                 res = Py_NotImplemented;
316                 break;
317         default:
318                 PyErr_BadArgument();
319                 return NULL;
320         }
321
322         Py_INCREF(res);
323         return res;
324 }
325
326 /*----------------------repr--------------------------------------------*/
327 static PyObject *pyrna_struct_repr( BPy_StructRNA *self )
328 {
329         PyObject *pyob;
330         char *name;
331
332         /* print name if available */
333         name= RNA_struct_name_get_alloc(&self->ptr, NULL, FALSE);
334         if(name) {
335                 pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), name);
336                 MEM_freeN(name);
337                 return pyob;
338         }
339
340         return PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\"]", RNA_struct_identifier(self->ptr.type));
341 }
342
343 static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
344 {
345         PyObject *pyob;
346         PointerRNA ptr;
347         char *name;
348
349         /* if a pointer, try to print name of pointer target too */
350         if(RNA_property_type(self->prop) == PROP_POINTER) {
351                 ptr= RNA_property_pointer_get(&self->ptr, self->prop);
352                 name= RNA_struct_name_get_alloc(&ptr, NULL, FALSE);
353
354                 if(name) {
355                         pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\" -> \"%.200s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
356                         MEM_freeN(name);
357                         return pyob;
358                 }
359         }
360
361         return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
362 }
363
364 static long pyrna_struct_hash( BPy_StructRNA *self )
365 {
366         return (long)self->ptr.data;
367 }
368
369 /* use our own dealloc so we can free a property if we use one */
370 static void pyrna_struct_dealloc( BPy_StructRNA *self )
371 {
372         if (self->freeptr && self->ptr.data) {
373                 IDP_FreeProperty(self->ptr.data);
374                 if (self->ptr.type != &RNA_Context)
375                 {
376                         MEM_freeN(self->ptr.data);
377                         self->ptr.data= NULL;
378                 }
379         }
380
381         /* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
382         Py_TYPE(self)->tp_free(self);
383         return;
384 }
385
386 static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
387 {
388         EnumPropertyItem *item;
389         char *result;
390         int free= FALSE;
391         
392         RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
393         if(item) {
394                 result= (char*)BPy_enum_as_string(item);
395         }
396         else {
397                 result= "";
398         }
399         
400         if(free)
401                 MEM_freeN(item);
402         
403         return result;
404 }
405
406 static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *val, const char *error_prefix)
407 {
408         char *param= _PyUnicode_AsString(item);
409
410         if (param==NULL) {
411                 char *enum_str= pyrna_enum_as_string(ptr, prop);
412                 PyErr_Format(PyExc_TypeError, "%.200s expected a string enum type in (%.200s)", error_prefix, enum_str);
413                 MEM_freeN(enum_str);
414                 return 0;
415         } else {
416                 if (!RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, val)) {
417                         char *enum_str= pyrna_enum_as_string(ptr, prop);
418                         PyErr_Format(PyExc_TypeError, "%.200s enum \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str);
419                         MEM_freeN(enum_str);
420                         return 0;
421                 }
422         }
423
424         return 1;
425 }
426
427 PyObject *pyrna_enum_bitfield_to_py(EnumPropertyItem *items, int value)
428 {
429         PyObject *ret= PySet_New(NULL);
430         const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
431
432         if(RNA_enum_bitflag_identifiers(items, value, identifier)) {
433                 PyObject *item;
434                 int index;
435                 for(index=0; identifier[index]; index++) {
436                         item= PyUnicode_FromString(identifier[index]);
437                         PySet_Add(ret, item);
438                         Py_DECREF(item);
439                 }
440         }
441
442         return ret;
443 }
444
445 static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val)
446 {
447         PyObject *item, *ret= NULL;
448
449         if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
450                 const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1];
451
452                 ret= PySet_New(NULL);
453
454                 if (RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier)) {
455                         int index;
456
457                         for(index=0; identifier[index]; index++) {
458                                 item= PyUnicode_FromString(identifier[index]);
459                                 PySet_Add(ret, item);
460                                 Py_DECREF(item);
461                         }
462
463                 }
464         }
465         else {
466                 const char *identifier;
467                 if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
468                         ret = PyUnicode_FromString(identifier);
469                 } else {
470                         EnumPropertyItem *item;
471                         int free= FALSE;
472
473                         /* don't throw error here, can't trust blender 100% to give the
474                          * right values, python code should not generate error for that */
475                         RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
476                         if(item && item->identifier) {
477                                 ret= PyUnicode_FromString(item->identifier);
478                         }
479                         else {
480                                 char *ptr_name= RNA_struct_name_get_alloc(ptr, NULL, FALSE);
481
482                                 /* prefer not fail silently incase of api errors, maybe disable it later */
483                                 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));
484
485 #if 0           // gives python decoding errors while generating docs :(
486                                 char error_str[256];
487                                 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));
488                                 PyErr_Warn(PyExc_RuntimeWarning, error_str);
489 #endif
490
491                                 if(ptr_name)
492                                         MEM_freeN(ptr_name);
493
494                                 ret = PyUnicode_FromString( "" );
495                         }
496
497                         if(free)
498                                 MEM_freeN(item);
499
500                         /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
501                         ret = NULL;*/
502                 }
503         }
504
505         return ret;
506 }
507
508 PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
509 {
510         PyObject *ret;
511         int type = RNA_property_type(prop);
512
513         if (RNA_property_array_check(ptr, prop)) {
514                 return pyrna_py_from_array(ptr, prop);
515         }
516         
517         /* see if we can coorce into a python type - PropertyType */
518         switch (type) {
519         case PROP_BOOLEAN:
520                 ret = PyBool_FromLong( RNA_property_boolean_get(ptr, prop) );
521                 break;
522         case PROP_INT:
523                 ret = PyLong_FromSsize_t( (Py_ssize_t)RNA_property_int_get(ptr, prop) );
524                 break;
525         case PROP_FLOAT:
526                 ret = PyFloat_FromDouble( RNA_property_float_get(ptr, prop) );
527                 break;
528         case PROP_STRING:
529         {
530                 char *buf;
531                 buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1);
532                 ret = PyUnicode_FromString( buf );
533                 MEM_freeN(buf);
534                 break;
535         }
536         case PROP_ENUM:
537         {
538                 ret= pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop));
539                 break;
540         }
541         case PROP_POINTER:
542         {
543                 PointerRNA newptr;
544                 newptr= RNA_property_pointer_get(ptr, prop);
545                 if (newptr.data) {
546                         ret = pyrna_struct_CreatePyObject(&newptr);
547                 } else {
548                         ret = Py_None;
549                         Py_INCREF(ret);
550                 }
551                 break;
552         }
553         case PROP_COLLECTION:
554                 ret = pyrna_prop_CreatePyObject(ptr, prop);
555                 break;
556         default:
557                 PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_prop_to_py)", type);
558                 ret = NULL;
559                 break;
560         }
561         
562         return ret;
563 }
564
565 /* This function is used by operators and converting dicts into collections.
566  * Its takes keyword args and fills them with property values */
567 int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const char *error_prefix)
568 {
569         int error_val = 0;
570         int totkw;
571         const char *arg_name= NULL;
572         PyObject *item;
573
574         totkw = kw ? PyDict_Size(kw):0;
575
576         RNA_STRUCT_BEGIN(ptr, prop) {
577                 arg_name= RNA_property_identifier(prop);
578
579                 if (strcmp(arg_name, "rna_type")==0) continue;
580
581                 if (kw==NULL) {
582                         PyErr_Format( PyExc_TypeError, "%.200s: no keywords, expected \"%.200s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
583                         error_val= -1;
584                         break;
585                 }
586
587                 item= PyDict_GetItemString(kw, arg_name); /* wont set an error */
588
589                 if (item == NULL) {
590                         if(all_args) {
591                                 PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
592                                 error_val = -1; /* pyrna_py_to_prop sets the error */
593                                 break;
594                         }
595                 } else {
596                         if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
597                                 error_val= -1;
598                                 break;
599                         }
600                         totkw--;
601                 }
602         }
603         RNA_STRUCT_END;
604
605         if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
606                 PyObject *key, *value;
607                 Py_ssize_t pos = 0;
608
609                 while (PyDict_Next(kw, &pos, &key, &value)) {
610                         arg_name= _PyUnicode_AsString(key);
611                         if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
612                         arg_name= NULL;
613                 }
614
615                 PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
616                 error_val = -1;
617         }
618
619         return error_val;
620 }
621
622 static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw);
623
624 static PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func)
625 {
626         static PyMethodDef func_meth = {"<generic rna function>", (PyCFunction)pyrna_func_call, METH_VARARGS|METH_KEYWORDS, "python rna function"};
627         PyObject *self;
628         PyObject *ret;
629         
630         if(func==NULL) {
631                 PyErr_Format( PyExc_RuntimeError, "%.200s: type attempted to get NULL function", RNA_struct_identifier(pyrna->ptr.type));
632                 return NULL;
633         }
634
635         self= PyTuple_New(2);
636         
637         PyTuple_SET_ITEM(self, 0, (PyObject *)pyrna);
638         Py_INCREF(pyrna);
639
640         PyTuple_SET_ITEM(self, 1, PyCObject_FromVoidPtr((void *)func, NULL));
641         
642         ret= PyCFunction_New(&func_meth, self);
643         Py_DECREF(self);
644         
645         return ret;
646 }
647
648
649
650 int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
651 {
652         /* XXX hard limits should be checked here */
653         int type = RNA_property_type(prop);
654         
655
656         if (RNA_property_array_check(ptr, prop)) {
657
658                 /* char error_str[512]; */
659                 int ok= 1;
660
661 #ifdef USE_MATHUTILS
662                 if(MatrixObject_Check(value)) {
663                         MatrixObject *mat = (MatrixObject*)value;
664                         if(!BaseMath_ReadCallback(mat))
665                                 return -1;
666                 } else /* continue... */
667 #endif
668                 if (!PySequence_Check(value)) {
669                         PyErr_Format(PyExc_TypeError, "%.200s RNA array assignment expected a sequence instead of %.200s instance.", error_prefix, Py_TYPE(value)->tp_name);
670                         return -1;
671                 }
672                 /* done getting the length */
673                 ok= pyrna_py_to_array(ptr, prop, data, value, error_prefix);
674
675                 if (!ok) {
676                         /* PyErr_Format(PyExc_AttributeError, "%.200s %s", error_prefix, error_str); */
677                         return -1;
678                 }
679         }
680         else {
681                 /* Normal Property (not an array) */
682                 
683                 /* see if we can coorce into a python type - PropertyType */
684                 switch (type) {
685                 case PROP_BOOLEAN:
686                 {
687                         int param;
688                         /* prefer not to have an exception here
689                          * however so many poll functions return None or a valid Object.
690                          * its a hassle to convert these into a bool before returning, */
691                         if(RNA_property_flag(prop) & PROP_RETURN)
692                                 param = PyObject_IsTrue( value );
693                         else
694                                 param = PyLong_AsSsize_t( value );
695                         
696                         if( param < 0 || param > 1) {
697                                 PyErr_Format(PyExc_TypeError, "%.200s expected True/False or 0/1", error_prefix);
698                                 return -1;
699                         } else {
700                                 if(data)        *((int*)data)= param;
701                                 else            RNA_property_boolean_set(ptr, prop, param);
702                         }
703                         break;
704                 }
705                 case PROP_INT:
706                 {
707                         int param = PyLong_AsSsize_t(value);
708                         if (param==-1 && PyErr_Occurred()) {
709                                 PyErr_Format(PyExc_TypeError, "%.200s expected an int type", error_prefix);
710                                 return -1;
711                         } else {
712                                 if(data)        *((int*)data)= param;
713                                 else            RNA_property_int_set(ptr, prop, param);
714                         }
715                         break;
716                 }
717                 case PROP_FLOAT:
718                 {
719                         float param = PyFloat_AsDouble(value);
720                         if (PyErr_Occurred()) {
721                                 PyErr_Format(PyExc_TypeError, "%.200s expected a float type", error_prefix);
722                                 return -1;
723                         } else {
724                                 if(data)        *((float*)data)= param;
725                                 else            RNA_property_float_set(ptr, prop, param);
726                         }
727                         break;
728                 }
729                 case PROP_STRING:
730                 {
731                         char *param = _PyUnicode_AsString(value);
732                         
733                         if (param==NULL) {
734                                 PyErr_Format(PyExc_TypeError, "%.200s expected a string type", error_prefix);
735                                 return -1;
736                         } else {
737                                 if(data)        *((char**)data)= param;
738                                 else            RNA_property_string_set(ptr, prop, param);
739                         }
740                         break;
741                 }
742                 case PROP_ENUM:
743                 {
744                         int val= 0, tmpval;
745
746                         if (PyUnicode_Check(value)) {
747                                 if (!pyrna_string_to_enum(value, ptr, prop, &val, error_prefix))
748                                         return -1;
749                         }
750                         else if (PyAnySet_Check(value)) {
751                                 if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
752                                         /* set of enum items, concatenate all values with OR */
753
754                                         /* set looping */
755                                         Py_ssize_t pos = 0;
756                                         PyObject *key;
757                                         long hash;
758
759                                         while (_PySet_NextEntry(value, &pos, &key, &hash)) {
760                                                 if (!pyrna_string_to_enum(key, ptr, prop, &tmpval, error_prefix))
761                                                         return -1;
762
763                                                 val |= tmpval;
764                                         }
765                                 }
766                                 else {
767                                         PyErr_Format(PyExc_TypeError, "%.200s, %.200s.%.200s is not a bitflag enum type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
768                                         return -1;
769                                 }
770                         }
771                         else {
772                                 char *enum_str= pyrna_enum_as_string(ptr, prop);
773                                 PyErr_Format(PyExc_TypeError, "%.200s expected a string enum or a set of strings in (%.200s)", error_prefix, enum_str);
774                                 MEM_freeN(enum_str);
775                                 return -1;
776                         }
777
778                         if(data)        *((int*)data)= val;
779                         else            RNA_property_enum_set(ptr, prop, val);
780                         
781                         break;
782                 }
783                 case PROP_POINTER:
784                 {
785                         StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
786                         int flag = RNA_property_flag(prop);
787
788                         if(!BPy_StructRNA_Check(value) && value != Py_None) {
789                                 PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(ptype));
790                                 return -1;
791                         } else if((flag & PROP_NEVER_NULL) && value == Py_None) {
792                                 PyErr_Format(PyExc_TypeError, "%.200s does not suppory a 'None' assignment %.200s type", error_prefix, RNA_struct_identifier(ptype));
793                                 return -1;
794                         } else {
795                                 BPy_StructRNA *param= (BPy_StructRNA*)value;
796                                 int raise_error= FALSE;
797                                 if(data) {
798
799                                         if(flag & PROP_RNAPTR) {
800                                                 if(value == Py_None)
801                                                         memset(data, 0, sizeof(PointerRNA));
802                                                 else
803                                                         *((PointerRNA*)data)= param->ptr;
804                                         }
805                                         else if(value == Py_None) {
806                                                 *((void**)data)= NULL;
807                                         }
808                                         else if(RNA_struct_is_a(param->ptr.type, ptype)) {
809                                                 *((void**)data)= param->ptr.data;
810                                         }
811                                         else {
812                                                 raise_error= TRUE;
813                                         }
814                                 }
815                                 else {
816                                         /* data==NULL, assign to RNA */
817                                         if(value == Py_None) {
818                                                 PointerRNA valueptr;
819                                                 memset(&valueptr, 0, sizeof(valueptr));
820                                                 RNA_property_pointer_set(ptr, prop, valueptr);
821                                         }
822                                         else if(RNA_struct_is_a(param->ptr.type, ptype)) {
823                                                 RNA_property_pointer_set(ptr, prop, param->ptr);
824                                         }
825                                         else {
826                                                 PointerRNA tmp;
827                                                 RNA_pointer_create(NULL, ptype, NULL, &tmp);
828                                                 PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(tmp.type));
829                                                 return -1;
830                                         }
831                                 }
832                                 
833                                 if(raise_error) {
834                                         PointerRNA tmp;
835                                         RNA_pointer_create(NULL, ptype, NULL, &tmp);
836                                         PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(tmp.type));
837                                         return -1;
838                                 }
839                         }
840                         break;
841                 }
842                 case PROP_COLLECTION:
843                 {
844                         int seq_len, i;
845                         PyObject *item;
846                         PointerRNA itemptr;
847                         ListBase *lb;
848                         CollectionPointerLink *link;
849
850                         lb= (data)? (ListBase*)data: NULL;
851                         
852                         /* convert a sequence of dict's into a collection */
853                         if(!PySequence_Check(value)) {
854                                 PyErr_Format(PyExc_TypeError, "%.200s expected a sequence of dicts for an RNA collection", error_prefix);
855                                 return -1;
856                         }
857                         
858                         seq_len = PySequence_Length(value);
859                         for(i=0; i<seq_len; i++) {
860                                 item= PySequence_GetItem(value, i);
861                                 if(item==NULL || PyDict_Check(item)==0) {
862                                         PyErr_Format(PyExc_TypeError, "%.200s expected a sequence of dicts for an RNA collection", error_prefix);
863                                         Py_XDECREF(item);
864                                         return -1;
865                                 }
866
867                                 if(lb) {
868                                         link= MEM_callocN(sizeof(CollectionPointerLink), "PyCollectionPointerLink");
869                                         link->ptr= itemptr;
870                                         BLI_addtail(lb, link);
871                                 }
872                                 else
873                                         RNA_property_collection_add(ptr, prop, &itemptr);
874
875                                 if(pyrna_pydict_to_props(&itemptr, item, 1, "Converting a python list to an RNA collection")==-1) {
876                                         Py_DECREF(item);
877                                         return -1;
878                                 }
879                                 Py_DECREF(item);
880                         }
881                         
882                         break;
883                 }
884                 default:
885                         PyErr_Format(PyExc_AttributeError, "%.200s unknown property type (pyrna_py_to_prop)", error_prefix);
886                         return -1;
887                         break;
888                 }
889         }
890
891         /* Run rna property functions */
892         RNA_property_update(BPy_GetContext(), ptr, prop);
893
894         return 0;
895 }
896
897 static PyObject * pyrna_prop_to_py_index(BPy_PropertyRNA *self, int index)
898 {
899         return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
900 }
901
902 static int pyrna_py_to_prop_index(BPy_PropertyRNA *self, int index, PyObject *value)
903 {
904         int ret = 0;
905         int totdim;
906         PointerRNA *ptr= &self->ptr;
907         PropertyRNA *prop= self->prop;
908         int type = RNA_property_type(prop);
909
910         totdim= RNA_property_array_dimension(ptr, prop, NULL);
911
912         if (totdim > 1) {
913                 /* char error_str[512]; */
914                 if (!pyrna_py_to_array_index(&self->ptr, self->prop, self->arraydim, self->arrayoffset, index, value, "")) {
915                         /* PyErr_SetString(PyExc_AttributeError, error_str); */
916                         ret= -1;
917                 }
918         }
919         else {
920                 /* see if we can coorce into a python type - PropertyType */
921                 switch (type) {
922                 case PROP_BOOLEAN:
923                         {
924                                 int param = PyLong_AsSsize_t( value );
925                 
926                                 if( param < 0 || param > 1) {
927                                         PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
928                                         ret = -1;
929                                 } else {
930                                         RNA_property_boolean_set_index(ptr, prop, index, param);
931                                 }
932                                 break;
933                         }
934                 case PROP_INT:
935                         {
936                                 int param = PyLong_AsSsize_t(value);
937                                 if (param==-1 && PyErr_Occurred()) {
938                                         PyErr_SetString(PyExc_TypeError, "expected an int type");
939                                         ret = -1;
940                                 } else {
941                                         RNA_property_int_set_index(ptr, prop, index, param);
942                                 }
943                                 break;
944                         }
945                 case PROP_FLOAT:
946                         {
947                                 float param = PyFloat_AsDouble(value);
948                                 if (PyErr_Occurred()) {
949                                         PyErr_SetString(PyExc_TypeError, "expected a float type");
950                                         ret = -1;
951                                 } else {
952                                         RNA_property_float_set_index(ptr, prop, index, param);
953                                 }
954                                 break;
955                         }
956                 default:
957                         PyErr_SetString(PyExc_AttributeError, "not an array type");
958                         ret = -1;
959                         break;
960                 }
961         }
962         
963         return ret;
964 }
965
966 //---------------sequence-------------------------------------------
967 static int pyrna_prop_array_length(BPy_PropertyRNA *self)
968 {
969         if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
970                 return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
971         else
972                 return RNA_property_array_length(&self->ptr, self->prop);
973 }
974
975 static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA *self )
976 {
977         Py_ssize_t len;
978         
979         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
980                 len = RNA_property_collection_length(&self->ptr, self->prop);
981         } else if (RNA_property_array_check(&self->ptr, self->prop)) {
982                 len = pyrna_prop_array_length(self);
983         } else {
984                 PyErr_SetString(PyExc_AttributeError, "len() only available for collection and array RNA types");
985                 len = -1; /* error value */
986         }
987         
988         return len;
989 }
990
991 /* internal use only */
992 static PyObject *prop_subscript_collection_int(BPy_PropertyRNA *self, int keynum)
993 {
994         PointerRNA newptr;
995
996         if(keynum < 0) keynum += RNA_property_collection_length(&self->ptr, self->prop);
997
998         if(RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr))
999                 return pyrna_struct_CreatePyObject(&newptr);
1000
1001         PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
1002         return NULL;
1003 }
1004
1005 static PyObject *prop_subscript_array_int(BPy_PropertyRNA *self, int keynum)
1006 {
1007         int len= pyrna_prop_array_length(self);
1008
1009         if(keynum < 0) keynum += len;
1010
1011         if(keynum >= 0 && keynum < len)
1012                 return pyrna_prop_to_py_index(self, keynum);
1013
1014         PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
1015         return NULL;
1016 }
1017
1018 static PyObject *prop_subscript_collection_str(BPy_PropertyRNA *self, char *keyname)
1019 {
1020         PointerRNA newptr;
1021         if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1022                 return pyrna_struct_CreatePyObject(&newptr);
1023
1024         PyErr_Format(PyExc_KeyError, "key \"%.200s\" not found", keyname);
1025         return NULL;
1026 }
1027 /* static PyObject *prop_subscript_array_str(BPy_PropertyRNA *self, char *keyname) */
1028
1029 static PyObject *prop_subscript_collection_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
1030 {
1031         PointerRNA newptr;
1032         PyObject *list = PyList_New(stop - start);
1033         int count;
1034
1035         start = MIN2(start,stop); /* values are clamped from  */
1036
1037         for(count = start; count < stop; count++) {
1038                 if(RNA_property_collection_lookup_int(ptr, prop, count - start, &newptr)) {
1039                         PyList_SET_ITEM(list, count - start, pyrna_struct_CreatePyObject(&newptr));
1040                 }
1041                 else {
1042                         Py_DECREF(list);
1043
1044                         PyErr_SetString(PyExc_RuntimeError, "error getting an rna struct from a collection");
1045                         return NULL;
1046                 }
1047         }
1048
1049         return list;
1050 }
1051
1052 /* TODO - dimensions
1053  * note: could also use pyrna_prop_to_py_index(self, count) in a loop but its a lot slower
1054  * since at the moment it reads (and even allocates) the entire array for each index.
1055  */
1056 static PyObject *prop_subscript_array_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
1057 {
1058         int count, totdim;
1059
1060         PyObject *list = PyList_New(stop - start);
1061
1062         totdim = RNA_property_array_dimension(ptr, prop, NULL);
1063
1064         if (totdim > 1) {
1065                 for (count = start; count < stop; count++)
1066                         PyList_SET_ITEM(list, count - start, pyrna_prop_to_py_index(self, count));
1067         }
1068         else {
1069                 switch (RNA_property_type(prop)) {
1070                 case PROP_FLOAT:
1071                         {
1072                                 float values_stack[PYRNA_STACK_ARRAY];
1073                                 float *values;
1074                                 if(length > PYRNA_STACK_ARRAY)  {       values= PyMem_MALLOC(sizeof(float) * length); }
1075                                 else                                                    {       values= values_stack; }
1076                                 RNA_property_float_get_array(ptr, prop, values);
1077                         
1078                                 for(count=start; count<stop; count++)
1079                                         PyList_SET_ITEM(list, count-start, PyFloat_FromDouble(values[count]));
1080
1081                                 if(values != values_stack) {
1082                                         PyMem_FREE(values);
1083                                 }
1084                                 break;
1085                         }
1086                 case PROP_BOOLEAN:
1087                         {
1088                                 int values_stack[PYRNA_STACK_ARRAY];
1089                                 int *values;
1090                                 if(length > PYRNA_STACK_ARRAY)  {       values= PyMem_MALLOC(sizeof(int) * length); }
1091                                 else                                                    {       values= values_stack; }
1092
1093                                 RNA_property_boolean_get_array(ptr, prop, values);
1094                                 for(count=start; count<stop; count++)
1095                                         PyList_SET_ITEM(list, count-start, PyBool_FromLong(values[count]));
1096
1097                                 if(values != values_stack) {
1098                                         PyMem_FREE(values);
1099                                 }
1100                                 break;
1101                         }
1102                 case PROP_INT:
1103                         {
1104                                 int values_stack[PYRNA_STACK_ARRAY];
1105                                 int *values;
1106                                 if(length > PYRNA_STACK_ARRAY)  {       values= PyMem_MALLOC(sizeof(int) * length); }
1107                                 else                                                    {       values= values_stack; }
1108
1109                                 RNA_property_int_get_array(ptr, prop, values);
1110                                 for(count=start; count<stop; count++)
1111                                         PyList_SET_ITEM(list, count-start, PyLong_FromSsize_t(values[count]));
1112
1113                                 if(values != values_stack) {
1114                                         PyMem_FREE(values);
1115                                 }
1116                                 break;
1117                         }
1118                 default:
1119                         /* probably will never happen */
1120                         PyErr_SetString(PyExc_TypeError, "not an array type");
1121                         Py_DECREF(list);
1122                         list= NULL;
1123                 }
1124         }
1125         return list;
1126 }
1127
1128 static PyObject *prop_subscript_collection(BPy_PropertyRNA *self, PyObject *key)
1129 {
1130         if (PyUnicode_Check(key)) {
1131                 return prop_subscript_collection_str(self, _PyUnicode_AsString(key));
1132         }
1133         else if (PyIndex_Check(key)) {
1134                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1135                 if (i == -1 && PyErr_Occurred())
1136                         return NULL;
1137
1138                 return prop_subscript_collection_int(self, i);
1139         }
1140         else if (PySlice_Check(key)) {
1141                 int len= RNA_property_collection_length(&self->ptr, self->prop);
1142                 Py_ssize_t start, stop, step, slicelength;
1143
1144                 if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1145                         return NULL;
1146
1147                 if (slicelength <= 0) {
1148                         return PyList_New(0);
1149                 }
1150                 else if (step == 1) {
1151                         return prop_subscript_collection_slice(&self->ptr, self->prop, start, stop, len);
1152                 }
1153                 else {
1154                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
1155                         return NULL;
1156                 }
1157         }
1158         else {
1159                 PyErr_Format(PyExc_TypeError, "invalid rna key, key must be a string or an int instead of %.200s instance.", Py_TYPE(key)->tp_name);
1160                 return NULL;
1161         }
1162 }
1163
1164 static PyObject *prop_subscript_array(BPy_PropertyRNA *self, PyObject *key)
1165 {
1166         /*if (PyUnicode_Check(key)) {
1167                 return prop_subscript_array_str(self, _PyUnicode_AsString(key));
1168         } else*/
1169         if (PyIndex_Check(key)) {
1170                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1171                 if (i == -1 && PyErr_Occurred())
1172                         return NULL;
1173                 return prop_subscript_array_int(self, PyLong_AsSsize_t(key));
1174         }
1175         else if (PySlice_Check(key)) {
1176                 Py_ssize_t start, stop, step, slicelength;
1177                 int len = pyrna_prop_array_length(self);
1178
1179                 if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1180                         return NULL;
1181
1182                 if (slicelength <= 0) {
1183                         return PyList_New(0);
1184                 }
1185                 else if (step == 1) {
1186                         return prop_subscript_array_slice(self, &self->ptr, self->prop, start, stop, len);
1187                 }
1188                 else {
1189                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
1190                         return NULL;
1191                 }
1192         }
1193         else {
1194                 PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
1195                 return NULL;
1196         }
1197 }
1198
1199 static PyObject *pyrna_prop_subscript( BPy_PropertyRNA *self, PyObject *key )
1200 {
1201         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
1202                 return prop_subscript_collection(self, key);
1203         } else if (RNA_property_array_check(&self->ptr, self->prop)) {
1204                 return prop_subscript_array(self, key);
1205         } 
1206
1207         PyErr_SetString(PyExc_TypeError, "rna type is not an array or a collection");
1208         return NULL;
1209 }
1210
1211 /* could call (pyrna_py_to_prop_index(self, i, value) in a loop but it is slow */
1212 static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length, PyObject *value_orig)
1213 {
1214         PyObject *value;
1215         int count;
1216         void *values_alloc= NULL;
1217         int ret= 0;
1218
1219         if(value_orig == NULL) {
1220                 PyErr_SetString(PyExc_TypeError, "invalid slice assignment, deleting with list types is not supported by StructRNA.");
1221                 return -1;
1222         }
1223
1224         if(!(value=PySequence_Fast(value_orig, "invalid slice assignment, type is not a sequence"))) {
1225                 return -1;
1226         }
1227
1228         if(PySequence_Fast_GET_SIZE(value) != stop-start) {
1229                 Py_DECREF(value);
1230                 PyErr_SetString(PyExc_TypeError, "invalid slice assignment, resizing StructRNA arrays isn't supported.");
1231                 return -1;
1232         }
1233
1234         switch (RNA_property_type(prop)) {
1235                 case PROP_FLOAT:
1236                 {
1237                         float values_stack[PYRNA_STACK_ARRAY];
1238                         float *values;
1239                         if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(float) * length); }
1240                         else                                                    {       values= values_stack; }
1241                         if(start != 0 || stop != length) /* partial assignment? - need to get the array */
1242                                 RNA_property_float_get_array(ptr, prop, values);
1243                         
1244                         for(count=start; count<stop; count++)
1245                                 values[count] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, count-start));
1246
1247                         if(PyErr_Occurred())    ret= -1;
1248                         else                                    RNA_property_float_set_array(ptr, prop, values);
1249                         break;
1250                 }
1251                 case PROP_BOOLEAN:
1252                 {
1253                         int values_stack[PYRNA_STACK_ARRAY];
1254                         int *values;
1255                         if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(int) * length); }
1256                         else                                                    {       values= values_stack; }
1257
1258                         if(start != 0 || stop != length) /* partial assignment? - need to get the array */
1259                                 RNA_property_boolean_get_array(ptr, prop, values);
1260         
1261                         for(count=start; count<stop; count++)
1262                                 values[count] = PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, count-start));
1263
1264                         if(PyErr_Occurred())    ret= -1;
1265                         else                                    RNA_property_boolean_set_array(ptr, prop, values);
1266                         break;
1267                 }
1268                 case PROP_INT:
1269                 {
1270                         int values_stack[PYRNA_STACK_ARRAY];
1271                         int *values;
1272                         if(length > PYRNA_STACK_ARRAY)  {       values= values_alloc= PyMem_MALLOC(sizeof(int) * length); }
1273                         else                                                    {       values= values_stack; }
1274
1275                         if(start != 0 || stop != length) /* partial assignment? - need to get the array */
1276                                 RNA_property_int_get_array(ptr, prop, values);
1277
1278                         for(count=start; count<stop; count++)
1279                                 values[count] = PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, count-start));
1280
1281                         if(PyErr_Occurred())    ret= -1;
1282                         else                                    RNA_property_int_set_array(ptr, prop, values);
1283                         break;
1284                 }
1285                 default:
1286                         PyErr_SetString(PyExc_TypeError, "not an array type");
1287                         ret= -1;
1288         }
1289
1290         Py_DECREF(value);
1291         
1292         if(values_alloc) {
1293                 PyMem_FREE(values_alloc);
1294         }
1295         
1296         return ret;
1297
1298 }
1299
1300 static int prop_subscript_ass_array_int(BPy_PropertyRNA *self, int keynum, PyObject *value)
1301 {
1302         int len= pyrna_prop_array_length(self);
1303
1304         if(keynum < 0) keynum += len;
1305
1306         if(keynum >= 0 && keynum < len)
1307                 return pyrna_py_to_prop_index(self, keynum, value);
1308
1309         PyErr_SetString(PyExc_IndexError, "out of range");
1310         return -1;
1311 }
1312
1313 static int pyrna_prop_ass_subscript( BPy_PropertyRNA *self, PyObject *key, PyObject *value )
1314 {
1315         /* char *keyname = NULL; */ /* not supported yet */
1316         
1317         if (!RNA_property_editable(&self->ptr, self->prop)) {
1318                 PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
1319                 return -1;
1320         }
1321         
1322         /* maybe one day we can support this... */
1323         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
1324                 PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is a collection, assignment not supported", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
1325                 return -1;
1326         }
1327
1328         if (PyIndex_Check(key)) {
1329                 Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1330                 if (i == -1 && PyErr_Occurred())
1331                         return -1;
1332
1333                 return prop_subscript_ass_array_int(self, i, value);
1334         }
1335         else if (PySlice_Check(key)) {
1336                 int len= RNA_property_array_length(&self->ptr, self->prop);
1337                 Py_ssize_t start, stop, step, slicelength;
1338
1339                 if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
1340                         return -1;
1341
1342                 if (slicelength <= 0) {
1343                         return 0;
1344                 }
1345                 else if (step == 1) {
1346                         return prop_subscript_ass_array_slice(&self->ptr, self->prop, start, stop, len, value);
1347                 }
1348                 else {
1349                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
1350                         return -1;
1351                 }
1352         }
1353         else {
1354                 PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
1355                 return -1;
1356         }
1357 }
1358
1359
1360 static PyMappingMethods pyrna_prop_as_mapping = {
1361         ( lenfunc ) pyrna_prop_len,     /* mp_length */
1362         ( binaryfunc ) pyrna_prop_subscript,    /* mp_subscript */
1363         ( objobjargproc ) pyrna_prop_ass_subscript,     /* mp_ass_subscript */
1364 };
1365
1366 static int pyrna_prop_contains(BPy_PropertyRNA *self, PyObject *value)
1367 {
1368         PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */
1369
1370         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
1371                 /* key in dict style check */
1372                 char *keyname = _PyUnicode_AsString(value);
1373
1374                 if(keyname==NULL) {
1375                         PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, key must be a string type");
1376                         return -1;
1377                 }
1378
1379
1380                 if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
1381                         return 1;
1382         }
1383         else if (RNA_property_array_check(&self->ptr, self->prop)) {
1384                 /* value in list style check */
1385                 return pyrna_array_contains_py(&self->ptr, self->prop, value);
1386         }
1387         else {
1388                 PyErr_SetString(PyExc_TypeError, "PropertyRNA - type is not an array or a collection");
1389                 return -1;
1390         }
1391
1392         return 0;
1393 }
1394
1395 static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
1396 {
1397         IDProperty *group;
1398         char *name = _PyUnicode_AsString(value);
1399
1400         if (!name) {
1401                 PyErr_SetString( PyExc_TypeError, "expected a string");
1402                 return -1;
1403         }
1404
1405         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1406                 PyErr_SetString( PyExc_TypeError, "this type doesnt support IDProperties");
1407                 return -1;
1408         }
1409
1410         group= RNA_struct_idproperties(&self->ptr, 0);
1411         
1412         if(!group)
1413                 return 0;
1414         
1415         return IDP_GetPropertyFromGroup(group, name) ? 1:0;
1416 }
1417
1418 static PyObject *pyrna_prop_item(BPy_PropertyRNA *self, Py_ssize_t index)
1419 {
1420         /* reuse subscript functions */
1421         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
1422                 return prop_subscript_collection_int(self, index);
1423         } else if (RNA_property_array_check(&self->ptr, self->prop)) {
1424                 return prop_subscript_array_int(self, index);
1425         }
1426
1427         PyErr_SetString(PyExc_TypeError, "rna type is not an array or a collection");
1428         return NULL;
1429 }
1430
1431 static PySequenceMethods pyrna_prop_as_sequence = {
1432         NULL,           /* Cant set the len otherwise it can evaluate as false */
1433         NULL,           /* sq_concat */
1434         NULL,           /* sq_repeat */
1435         (ssizeargfunc)pyrna_prop_item, /* sq_item */ /* Only set this so PySequence_Check() returns True */
1436         NULL,           /* sq_slice */
1437         NULL,           /* sq_ass_item */
1438         NULL,           /* sq_ass_slice */
1439         (objobjproc)pyrna_prop_contains,        /* sq_contains */
1440 };
1441
1442 static PySequenceMethods pyrna_struct_as_sequence = {
1443         NULL,           /* Cant set the len otherwise it can evaluate as false */
1444         NULL,           /* sq_concat */
1445         NULL,           /* sq_repeat */
1446         NULL,           /* sq_item */ /* Only set this so PySequence_Check() returns True */
1447         NULL,           /* sq_slice */
1448         NULL,           /* sq_ass_item */
1449         NULL,           /* sq_ass_slice */
1450         (objobjproc)pyrna_struct_contains,      /* sq_contains */
1451 };
1452
1453 static PyObject *pyrna_struct_subscript( BPy_StructRNA *self, PyObject *key )
1454 {
1455         /* mostly copied from BPy_IDGroup_Map_GetItem */
1456         IDProperty *group, *idprop;
1457         char *name= _PyUnicode_AsString(key);
1458
1459         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1460                 PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
1461                 return NULL;
1462         }
1463
1464         if(name==NULL) {
1465                 PyErr_SetString( PyExc_TypeError, "only strings are allowed as keys of ID properties");
1466                 return NULL;
1467         }
1468
1469         group= RNA_struct_idproperties(&self->ptr, 0);
1470
1471         if(group==NULL) {
1472                 PyErr_Format( PyExc_KeyError, "key \"%s\" not found", name);
1473                 return NULL;
1474         }
1475
1476         idprop= IDP_GetPropertyFromGroup(group, name);
1477
1478         if(idprop==NULL) {
1479                 PyErr_Format( PyExc_KeyError, "key \"%s\" not found", name);
1480                 return NULL;
1481         }
1482
1483         return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
1484 }
1485
1486 static int pyrna_struct_ass_subscript( BPy_StructRNA *self, PyObject *key, PyObject *value )
1487 {
1488         IDProperty *group= RNA_struct_idproperties(&self->ptr, 1);
1489
1490         if(group==NULL) {
1491                 PyErr_SetString(PyExc_TypeError, "id properties not supported for this type");
1492                 return -1;
1493         }
1494
1495         return BPy_Wrap_SetMapItem(group, key, value);
1496 }
1497
1498 static PyMappingMethods pyrna_struct_as_mapping = {
1499         ( lenfunc ) NULL,       /* mp_length */
1500         ( binaryfunc ) pyrna_struct_subscript,  /* mp_subscript */
1501         ( objobjargproc ) pyrna_struct_ass_subscript,   /* mp_ass_subscript */
1502 };
1503
1504 static PyObject *pyrna_struct_keys(BPy_PropertyRNA *self)
1505 {
1506         IDProperty *group;
1507
1508         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1509                 PyErr_SetString( PyExc_TypeError, "this type doesnt support IDProperties");
1510                 return NULL;
1511         }
1512
1513         group= RNA_struct_idproperties(&self->ptr, 0);
1514
1515         if(group==NULL)
1516                 return PyList_New(0);
1517
1518         return BPy_Wrap_GetKeys(group);
1519 }
1520
1521 static PyObject *pyrna_struct_items(BPy_PropertyRNA *self)
1522 {
1523         IDProperty *group;
1524
1525         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1526                 PyErr_SetString( PyExc_TypeError, "this type doesnt support IDProperties");
1527                 return NULL;
1528         }
1529
1530         group= RNA_struct_idproperties(&self->ptr, 0);
1531
1532         if(group==NULL)
1533                 return PyList_New(0);
1534
1535         return BPy_Wrap_GetItems(self->ptr.id.data, group);
1536 }
1537
1538
1539 static PyObject *pyrna_struct_values(BPy_PropertyRNA *self)
1540 {
1541         IDProperty *group;
1542
1543         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
1544                 PyErr_SetString( PyExc_TypeError, "this type doesnt support IDProperties");
1545                 return NULL;
1546         }
1547
1548         group= RNA_struct_idproperties(&self->ptr, 0);
1549
1550         if(group==NULL)
1551                 return PyList_New(0);
1552
1553         return BPy_Wrap_GetValues(self->ptr.id.data, group);
1554 }
1555
1556 static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args)
1557 {
1558         char *path, *path_full;
1559         int index= -1; /* default to all */
1560         float cfra = CTX_data_scene(BPy_GetContext())->r.cfra;
1561         PropertyRNA *prop;
1562         PyObject *result;
1563
1564         if (!PyArg_ParseTuple(args, "s|if:keyframe_insert", &path, &index, &cfra))
1565                 return NULL;
1566
1567         if (self->ptr.data==NULL) {
1568                 PyErr_Format( PyExc_TypeError, "keyframe_insert, this struct has no data, cant be animated", path);
1569                 return NULL;
1570         }
1571
1572         prop = RNA_struct_find_property(&self->ptr, path);
1573
1574         if (prop==NULL) {
1575                 PyErr_Format( PyExc_TypeError, "keyframe_insert, property \"%s\" not found", path);
1576                 return NULL;
1577         }
1578
1579         if (!RNA_property_animateable(&self->ptr, prop)) {
1580                 PyErr_Format( PyExc_TypeError, "keyframe_insert, property \"%s\" not animatable", path);
1581                 return NULL;
1582         }
1583
1584         path_full= RNA_path_from_ID_to_property(&self->ptr, prop);
1585
1586         if (path_full==NULL) {
1587                 PyErr_Format( PyExc_TypeError, "keyframe_insert, could not make path to \"%s\"", path);
1588                 return NULL;
1589         }
1590
1591         result= PyBool_FromLong( insert_keyframe((ID *)self->ptr.id.data, NULL, NULL, path_full, index, cfra, 0));
1592         MEM_freeN(path_full);
1593
1594         return result;
1595 }
1596
1597
1598 static PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
1599 {
1600         char *path, *path_full;
1601         int index= -1; /* default to all */
1602         PropertyRNA *prop;
1603         PyObject *ret;
1604
1605         if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index))
1606                 return NULL;
1607
1608         if (self->ptr.data==NULL) {
1609                 PyErr_Format( PyExc_TypeError, "driver_add, this struct has no data, cant be animated", path);
1610                 return NULL;
1611         }
1612
1613         prop = RNA_struct_find_property(&self->ptr, path);
1614
1615         if (prop==NULL) {
1616                 PyErr_Format( PyExc_TypeError, "driver_add, property \"%s\" not found", path);
1617                 return NULL;
1618         }
1619
1620         if (!RNA_property_animateable(&self->ptr, prop)) {
1621                 PyErr_Format( PyExc_TypeError, "driver_add, property \"%s\" not animatable", path);
1622                 return NULL;
1623         }
1624
1625         path_full= RNA_path_from_ID_to_property(&self->ptr, prop);
1626
1627         if (path_full==NULL) {
1628                 PyErr_Format( PyExc_TypeError, "driver_add, could not make path to \"%s\"", path);
1629                 return NULL;
1630         }
1631
1632         if(ANIM_add_driver((ID *)self->ptr.id.data, path_full, index, 0, DRIVER_TYPE_PYTHON)) {
1633                 ID *id= self->ptr.id.data;
1634                 AnimData *adt= BKE_animdata_from_id(id);
1635                 FCurve *fcu;
1636
1637                 PointerRNA tptr;
1638                 PyObject *item;
1639
1640                 if(index == -1) { /* all, use a list */
1641                         int i= 0;
1642                         ret= PyList_New(0);
1643                         while((fcu= list_find_fcurve(&adt->drivers, path_full, i++))) {
1644                                 RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
1645                                 item= pyrna_struct_CreatePyObject(&tptr);
1646                                 PyList_Append(ret, item);
1647                                 Py_DECREF(item);
1648                         }
1649                 }
1650                 else {
1651                         fcu= list_find_fcurve(&adt->drivers, path_full, index);
1652                         RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
1653                         ret= pyrna_struct_CreatePyObject(&tptr);
1654                 }
1655         }
1656         else {
1657                 ret= Py_None;
1658                 Py_INCREF(ret);
1659         }
1660
1661         MEM_freeN(path_full);
1662
1663         return ret;
1664 }
1665
1666 static PyObject *pyrna_struct_is_property_set(BPy_StructRNA *self, PyObject *args)
1667 {
1668         char *name;
1669
1670         if (!PyArg_ParseTuple(args, "s:is_property_set", &name))
1671                 return NULL;
1672
1673         return PyBool_FromLong(RNA_property_is_set(&self->ptr, name));
1674 }
1675
1676 static PyObject *pyrna_struct_is_property_hidden(BPy_StructRNA *self, PyObject *args)
1677 {
1678         PropertyRNA *prop;
1679         char *name;
1680         int hidden;
1681
1682         if (!PyArg_ParseTuple(args, "s:is_property_hidden", &name))
1683                 return NULL;
1684         
1685         prop= RNA_struct_find_property(&self->ptr, name);
1686         hidden= (prop)? (RNA_property_flag(prop) & PROP_HIDDEN): 1;
1687
1688         return PyBool_FromLong(hidden);
1689 }
1690
1691 static PyObject *pyrna_struct_path_resolve(BPy_StructRNA *self, PyObject *value)
1692 {
1693         char *path= _PyUnicode_AsString(value);
1694         PointerRNA r_ptr;
1695         PropertyRNA *r_prop;
1696
1697         if(path==NULL) {
1698                 PyErr_SetString( PyExc_TypeError, "items() is only valid for collection types" );
1699                 return NULL;
1700         }
1701
1702         if (RNA_path_resolve(&self->ptr, path, &r_ptr, &r_prop))
1703                 return pyrna_prop_CreatePyObject(&r_ptr, r_prop);
1704
1705         Py_RETURN_NONE;
1706 }
1707
1708 static PyObject *pyrna_struct_path_to_id(BPy_StructRNA *self, PyObject *args)
1709 {
1710         char *name= NULL;
1711         char *path;
1712         PropertyRNA *prop;
1713         PyObject *ret;
1714
1715         if (!PyArg_ParseTuple(args, "|s:path_to_id", &name))
1716                 return NULL;
1717
1718         if(name) {
1719                 prop= RNA_struct_find_property(&self->ptr, name);
1720                 if(prop==NULL) {
1721                         PyErr_Format(PyExc_TypeError, "path_to_id(\"%.200s\") not found", name);
1722                         return NULL;
1723                 }
1724
1725                 path= RNA_path_from_ID_to_property(&self->ptr, prop);
1726         }
1727         else {
1728                 path= RNA_path_from_ID_to_struct(&self->ptr);
1729         }
1730
1731         if(path==NULL) {
1732                 if(name)        PyErr_Format(PyExc_TypeError, "%.200s.path_to_id(\"%s\") found but does not support path creation", RNA_struct_identifier(self->ptr.type), name);
1733                 else            PyErr_Format(PyExc_TypeError, "%.200s.path_to_id() does not support path creation for this type", name);
1734                 return NULL;
1735         }
1736
1737         ret= PyUnicode_FromString(path);
1738         MEM_freeN(path);
1739
1740         return ret;
1741 }
1742
1743 static PyObject *pyrna_struct_recast_type(BPy_StructRNA *self, PyObject *args)
1744 {
1745         PointerRNA r_ptr;
1746         RNA_pointer_recast(&self->ptr, &r_ptr);
1747         return pyrna_struct_CreatePyObject(&r_ptr);
1748 }
1749
1750 static PyObject *pyrna_prop_path_to_id(BPy_PropertyRNA *self)
1751 {
1752         char *path;
1753         PropertyRNA *prop = self->prop;
1754         PyObject *ret;
1755
1756         path= RNA_path_from_ID_to_property(&self->ptr, self->prop);
1757
1758         if(path==NULL) {
1759                 PyErr_Format(PyExc_TypeError, "%.200s.%.200s.path_to_id() does not support path creation for this type", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(prop));
1760                 return NULL;
1761         }
1762
1763         ret= PyUnicode_FromString(path);
1764         MEM_freeN(path);
1765
1766         return ret;
1767 }
1768
1769 static void pyrna_dir_members_py(PyObject *list, PyObject *self)
1770 {
1771         PyObject *dict;
1772         PyObject **dict_ptr;
1773         PyObject *list_tmp;
1774
1775         dict_ptr= _PyObject_GetDictPtr((PyObject *)self);
1776
1777         if(dict_ptr && (dict=*dict_ptr)) {
1778                 list_tmp = PyDict_Keys(dict);
1779                 PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
1780                 Py_DECREF(list_tmp);
1781         }
1782
1783         dict= ((PyTypeObject *)Py_TYPE(self))->tp_dict;
1784         if(dict) {
1785                 list_tmp = PyDict_Keys(dict);
1786                 PyList_SetSlice(list, INT_MAX, INT_MAX, list_tmp);
1787                 Py_DECREF(list_tmp);
1788         }
1789 }
1790
1791 static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
1792 {
1793         PyObject *pystring;
1794         const char *idname;
1795
1796         /* for looping over attrs and funcs */
1797         PointerRNA tptr;
1798         PropertyRNA *iterprop;
1799
1800         {
1801                 RNA_pointer_create(NULL, &RNA_Struct, ptr->type, &tptr);
1802                 iterprop= RNA_struct_find_property(&tptr, "functions");
1803
1804                 RNA_PROP_BEGIN(&tptr, itemptr, iterprop) {
1805                         idname= RNA_function_identifier(itemptr.data);
1806
1807                         pystring = PyUnicode_FromString(idname);
1808                         PyList_Append(list, pystring);
1809                         Py_DECREF(pystring);
1810                 }
1811                 RNA_PROP_END;
1812         }
1813
1814         {
1815                 /*
1816                  * Collect RNA attributes
1817                  */
1818                 char name[256], *nameptr;
1819
1820                 iterprop= RNA_struct_iterator_property(ptr->type);
1821
1822                 RNA_PROP_BEGIN(ptr, itemptr, iterprop) {
1823                         nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
1824
1825                         if(nameptr) {
1826                                 pystring = PyUnicode_FromString(nameptr);
1827                                 PyList_Append(list, pystring);
1828                                 Py_DECREF(pystring);
1829
1830                                 if(name != nameptr)
1831                                         MEM_freeN(nameptr);
1832                         }
1833                 }
1834                 RNA_PROP_END;
1835         }
1836 }
1837
1838
1839 static PyObject *pyrna_struct_dir(BPy_StructRNA *self)
1840 {
1841         PyObject *ret;
1842         PyObject *pystring;
1843
1844         /* Include this incase this instance is a subtype of a python class
1845          * In these instances we may want to return a function or variable provided by the subtype
1846          * */
1847         ret = PyList_New(0);
1848
1849         if (!BPy_StructRNA_CheckExact(self))
1850                 pyrna_dir_members_py(ret, (PyObject *)self);
1851
1852         pyrna_dir_members_rna(ret, &self->ptr);
1853
1854         if(self->ptr.type == &RNA_Context) {
1855                 ListBase lb = CTX_data_dir_get(self->ptr.data);
1856                 LinkData *link;
1857
1858                 for(link=lb.first; link; link=link->next) {
1859                         pystring = PyUnicode_FromString(link->data);
1860                         PyList_Append(ret, pystring);
1861                         Py_DECREF(pystring);
1862                 }
1863
1864                 BLI_freelistN(&lb);
1865         }
1866         return ret;
1867 }
1868
1869 //---------------getattr--------------------------------------------
1870 static PyObject *pyrna_struct_getattro( BPy_StructRNA *self, PyObject *pyname )
1871 {
1872         char *name = _PyUnicode_AsString(pyname);
1873         PyObject *ret;
1874         PropertyRNA *prop;
1875         FunctionRNA *func;
1876         
1877         if(name[0]=='_') { // rna can't start with a "_", so for __dict__ and similar we can skip using rna lookups
1878                 /* annoying exception, maybe we need to have different types for this... */
1879                 if((strcmp(name, "__getitem__")==0 || strcmp(name, "__setitem__")==0) && !RNA_struct_idproperties_check(self->ptr.type)) {
1880                         PyErr_SetString(PyExc_AttributeError, "StructRNA - no __getitem__ support for this type");
1881                         ret = NULL;
1882                 }
1883                 else {
1884                         ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
1885                 }
1886         }
1887         else if ((prop = RNA_struct_find_property(&self->ptr, name))) {
1888                 ret = pyrna_prop_to_py(&self->ptr, prop);
1889         }
1890         /* RNA function only if callback is declared (no optional functions) */
1891         else if ((func = RNA_struct_find_function(&self->ptr, name)) && RNA_function_defined(func)) {
1892                 ret = pyrna_func_to_py((BPy_DummyPointerRNA *)self, func);
1893         }
1894         else if (self->ptr.type == &RNA_Context) {
1895                 PointerRNA newptr;
1896                 ListBase newlb;
1897                 int done;
1898
1899                 done= CTX_data_get(self->ptr.data, name, &newptr, &newlb);
1900
1901                 if(done==1) { /* found */
1902                         if (newptr.data) {
1903                                 ret = pyrna_struct_CreatePyObject(&newptr);
1904                         }
1905                         else if (newlb.first) {
1906                                 CollectionPointerLink *link;
1907                                 PyObject *linkptr;
1908
1909                                 ret = PyList_New(0);
1910
1911                                 for(link=newlb.first; link; link=link->next) {
1912                                         linkptr= pyrna_struct_CreatePyObject(&link->ptr);
1913                                         PyList_Append(ret, linkptr);
1914                                         Py_DECREF(linkptr);
1915                                 }
1916                         }
1917                         else {
1918                                 ret = Py_None;
1919                                 Py_INCREF(ret);
1920                         }
1921                 }
1922                 else if (done==-1) { /* found but not set */
1923                         ret = Py_None;
1924                         Py_INCREF(ret);
1925                 }
1926         else { /* not found in the context */
1927                 /* lookup the subclass. raise an error if its not found */
1928                 ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
1929         }
1930
1931                 BLI_freelistN(&newlb);
1932         }
1933         else {
1934 #if 0
1935                 PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
1936                 ret = NULL;
1937 #endif
1938                 /* Include this incase this instance is a subtype of a python class
1939                  * In these instances we may want to return a function or variable provided by the subtype
1940                  *
1941                  * Also needed to return methods when its not a subtype
1942                  * */
1943
1944                 /* The error raised here will be displayed */
1945                 ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
1946         }
1947         
1948         return ret;
1949 }
1950
1951 #if 0
1952 static int pyrna_struct_pydict_contains(PyObject *self, PyObject *pyname)
1953 {
1954          PyObject *dict= *(_PyObject_GetDictPtr((PyObject *)self));
1955          if (dict==NULL) /* unlikely */
1956                  return 0;
1957
1958         return PyDict_Contains(dict, pyname);
1959 }
1960 #endif
1961
1962 //--------------- setattr-------------------------------------------
1963 static int pyrna_struct_setattro( BPy_StructRNA *self, PyObject *pyname, PyObject *value )
1964 {
1965         char *name = _PyUnicode_AsString(pyname);
1966         PropertyRNA *prop = RNA_struct_find_property(&self->ptr, name);
1967         
1968         if (prop==NULL) {
1969                 return PyObject_GenericSetAttr((PyObject *)self, pyname, value);
1970 #if 0
1971                 // XXX - This currently allows anything to be assigned to an rna prop, need to see how this should be used
1972                 // but for now it makes porting scripts confusing since it fails silently.
1973                 // edit: allowing this for setting classes internal attributes.
1974                 // edit: allow this for any attribute that alredy exists as a python attr
1975                 if (    (name[0]=='_' /* || pyrna_struct_pydict_contains(self, pyname) */ ) &&
1976                                 !BPy_StructRNA_CheckExact(self) &&
1977
1978                         return 0;
1979                 } else
1980                 {
1981                         PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
1982                         return -1;
1983                 }
1984 #endif
1985         }               
1986         
1987         if (!RNA_property_editable(&self->ptr, prop)) {
1988                 PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
1989                 return -1;
1990         }
1991                 
1992         /* pyrna_py_to_prop sets its own exceptions */
1993         return pyrna_py_to_prop(&self->ptr, prop, NULL, value, "StructRNA - item.attr = val:");
1994 }
1995
1996 static PyObject *pyrna_prop_dir(BPy_PropertyRNA *self)
1997 {
1998         PyObject *ret;
1999         PointerRNA r_ptr;
2000
2001         /* Include this incase this instance is a subtype of a python class
2002          * In these instances we may want to return a function or variable provided by the subtype
2003          * */
2004         ret = PyList_New(0);
2005
2006         if (!BPy_PropertyRNA_CheckExact(self))
2007                 pyrna_dir_members_py(ret, (PyObject *)self);
2008
2009         if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr))
2010                 pyrna_dir_members_rna(ret, &r_ptr);
2011
2012         return ret;
2013 }
2014
2015
2016 static PyObject *pyrna_prop_getattro( BPy_PropertyRNA *self, PyObject *pyname )
2017 {
2018         char *name = _PyUnicode_AsString(pyname);
2019
2020         if(name[0] != '_') {
2021                 if (RNA_property_type(self->prop) == PROP_COLLECTION) {
2022                         PyObject *ret;
2023                         PropertyRNA *prop;
2024                         FunctionRNA *func;
2025
2026                         PointerRNA r_ptr;
2027                         if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
2028                                 if ((prop = RNA_struct_find_property(&r_ptr, name))) {
2029                                         ret = pyrna_prop_to_py(&r_ptr, prop);
2030
2031                                         return ret;
2032                                 }
2033                                 else if ((func = RNA_struct_find_function(&r_ptr, name))) {
2034                                         PyObject *self_collection= pyrna_struct_CreatePyObject(&r_ptr);
2035                                         ret = pyrna_func_to_py((BPy_DummyPointerRNA *)self_collection, func);
2036                                         Py_DECREF(self_collection);
2037
2038                                         return ret;
2039                                 }
2040                         }
2041                 }
2042         }
2043         else {
2044                 /* annoying exception, maybe we need to have different types for this... */
2045                 if((strcmp(name, "__getitem__")==0 || strcmp(name, "__setitem__")==0) &&
2046                                 (RNA_property_type(self->prop) != PROP_COLLECTION) &&
2047                                 RNA_property_array_check(&self->ptr, self->prop)==0
2048                 ) {
2049                         PyErr_SetString(PyExc_AttributeError, "PropertyRNA - no __getitem__ support for this type");
2050                         return NULL;
2051                 }
2052         }
2053
2054         /* The error raised here will be displayed */
2055         return PyObject_GenericGetAttr((PyObject *)self, pyname);
2056 }
2057
2058 //--------------- setattr-------------------------------------------
2059 static int pyrna_prop_setattro( BPy_PropertyRNA *self, PyObject *pyname, PyObject *value )
2060 {
2061         char *name = _PyUnicode_AsString(pyname);
2062         PropertyRNA *prop;
2063
2064         if (RNA_property_type(self->prop) == PROP_COLLECTION) {
2065                 PointerRNA r_ptr;
2066                 if(RNA_property_collection_type_get(&self->ptr, self->prop, &r_ptr)) {
2067                         if ((prop = RNA_struct_find_property(&r_ptr, name))) {
2068                                 /* pyrna_py_to_prop sets its own exceptions */
2069                                 return pyrna_py_to_prop(&r_ptr, prop, NULL, value, "BPy_PropertyRNA - Attribute (setattr):");
2070                         }
2071                 }
2072         }
2073
2074         PyErr_Format( PyExc_AttributeError, "BPy_PropertyRNA - Attribute \"%.200s\" not found", name);
2075         return -1;
2076 }
2077
2078 /* odd case, we need to be able return a python method from a tp_getset */
2079 static PyObject *pyrna_prop_add(BPy_PropertyRNA *self)
2080 {
2081         PointerRNA r_ptr;
2082
2083         RNA_property_collection_add(&self->ptr, self->prop, &r_ptr);
2084         if(!r_ptr.data) {
2085                 PyErr_SetString( PyExc_TypeError, "add() not supported for this collection");
2086                 return NULL;
2087         }
2088         else {
2089                 return pyrna_struct_CreatePyObject(&r_ptr);
2090         }
2091 }
2092
2093 static PyObject *pyrna_prop_remove(BPy_PropertyRNA *self, PyObject *value)
2094 {
2095         PyObject *ret;
2096         int key= PyLong_AsSsize_t(value);
2097
2098         if (key==-1 && PyErr_Occurred()) {
2099                 PyErr_SetString( PyExc_TypeError, "remove() expected one int argument");
2100                 return NULL;
2101         }
2102
2103         if(!RNA_property_collection_remove(&self->ptr, self->prop, key)) {
2104                 PyErr_SetString( PyExc_TypeError, "remove() not supported for this collection");
2105                 return NULL;
2106         }
2107
2108         ret = Py_None;
2109         Py_INCREF(ret);
2110
2111         return ret;
2112 }
2113
2114 static PyObject *pyrna_struct_get_id_data(BPy_StructRNA *self)
2115 {
2116         if(self->ptr.id.data) {
2117                 PointerRNA id_ptr;
2118                 RNA_id_pointer_create((ID *)self->ptr.id.data, &id_ptr);
2119                 return pyrna_struct_CreatePyObject(&id_ptr);
2120         }
2121
2122         Py_RETURN_NONE;
2123 }
2124
2125 /*****************************************************************************/
2126 /* Python attributes get/set structure:                                      */
2127 /*****************************************************************************/
2128 #if 0
2129 static PyGetSetDef pyrna_prop_getseters[] = {
2130         {"active", (getter)pyrna_prop_get_active, (setter)pyrna_prop_set_active, "", NULL},
2131         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
2132 };
2133 #endif
2134
2135 static PyGetSetDef pyrna_struct_getseters[] = {
2136         {"id_data", (getter)pyrna_struct_get_id_data, (setter)NULL, "The ID data this datablock is from, (not available for all data)", NULL},
2137         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
2138 };
2139
2140 static PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
2141 {
2142         PyObject *ret;
2143         if (RNA_property_type(self->prop) != PROP_COLLECTION) {
2144                 PyErr_SetString( PyExc_TypeError, "keys() is only valid for collection types" );
2145                 ret = NULL;
2146         } else {
2147                 PyObject *item;
2148                 char name[256], *nameptr;
2149
2150                 ret = PyList_New(0);
2151                 
2152                 RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2153                         nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
2154
2155                         if(nameptr) {
2156                                 /* add to python list */
2157                                 item = PyUnicode_FromString( nameptr );
2158                                 PyList_Append(ret, item);
2159                                 Py_DECREF(item);
2160                                 /* done */
2161                                 
2162                                 if(name != nameptr)
2163                                         MEM_freeN(nameptr);
2164                         }
2165                 }
2166                 RNA_PROP_END;
2167         }
2168         
2169         return ret;
2170 }
2171
2172 static PyObject *pyrna_prop_items(BPy_PropertyRNA *self)
2173 {
2174         PyObject *ret;
2175         if (RNA_property_type(self->prop) != PROP_COLLECTION) {
2176                 PyErr_SetString( PyExc_TypeError, "items() is only valid for collection types" );
2177                 ret = NULL;
2178         } else {
2179                 PyObject *item;
2180                 char name[256], *nameptr;
2181                 int i= 0;
2182
2183                 ret = PyList_New(0);
2184                 
2185                 RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2186                         if(itemptr.data) {
2187                                 /* add to python list */
2188                                 item= PyTuple_New(2);
2189                                 nameptr= RNA_struct_name_get_alloc(&itemptr, name, sizeof(name));
2190                                 if(nameptr) {
2191                                         PyTuple_SET_ITEM(item, 0, PyUnicode_FromString( nameptr ));
2192                                         if(name != nameptr)
2193                                                 MEM_freeN(nameptr);
2194                                 }
2195                                 else {
2196                                         PyTuple_SET_ITEM(item, 0, PyLong_FromSsize_t(i)); /* a bit strange but better then returning an empty list */
2197                                 }
2198                                 PyTuple_SET_ITEM(item, 1, pyrna_struct_CreatePyObject(&itemptr));
2199                                 
2200                                 PyList_Append(ret, item);
2201                                 Py_DECREF(item);
2202                                 
2203                                 i++;
2204                         }
2205                 }
2206                 RNA_PROP_END;
2207         }
2208         
2209         return ret;
2210 }
2211
2212
2213 static PyObject *pyrna_prop_values(BPy_PropertyRNA *self)
2214 {
2215         PyObject *ret;
2216         
2217         if (RNA_property_type(self->prop) != PROP_COLLECTION) {
2218                 PyErr_SetString( PyExc_TypeError, "values() is only valid for collection types" );
2219                 ret = NULL;
2220         } else {
2221                 PyObject *item;
2222                 ret = PyList_New(0);
2223                 
2224                 RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2225                         item = pyrna_struct_CreatePyObject(&itemptr);
2226                         PyList_Append(ret, item);
2227                         Py_DECREF(item);
2228                 }
2229                 RNA_PROP_END;
2230         }
2231         
2232         return ret;
2233 }
2234
2235 static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
2236 {
2237         IDProperty *group, *idprop;
2238
2239         char *key;
2240         PyObject* def = Py_None;
2241
2242         if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
2243                 return NULL;
2244
2245         /* mostly copied from BPy_IDGroup_Map_GetItem */
2246         if(RNA_struct_idproperties_check(self->ptr.type)==0) {
2247                 PyErr_SetString( PyExc_TypeError, "this type doesn't support IDProperties");
2248                 return NULL;
2249         }
2250
2251         group= RNA_struct_idproperties(&self->ptr, 0);
2252         if(group) {
2253                 idprop= IDP_GetPropertyFromGroup(group, key);
2254
2255                 if(idprop)
2256                         return BPy_IDGroup_WrapData(self->ptr.id.data, idprop);
2257         }
2258
2259         Py_INCREF(def);
2260         return def;
2261 }
2262
2263 static PyObject *pyrna_prop_get(BPy_PropertyRNA *self, PyObject *args)
2264 {
2265         PointerRNA newptr;
2266         
2267         char *key;
2268         PyObject* def = Py_None;
2269
2270         if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
2271                 return NULL;
2272         
2273         if(RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
2274                 return pyrna_struct_CreatePyObject(&newptr);
2275         
2276         Py_INCREF(def);
2277         return def;
2278 }
2279
2280 static void foreach_attr_type(  BPy_PropertyRNA *self, char *attr,
2281                                                                         /* values to assign */
2282                                                                         RawPropertyType *raw_type, int *attr_tot, int *attr_signed )
2283 {
2284         PropertyRNA *prop;
2285         *raw_type= PROP_RAW_UNSET;
2286         *attr_tot= 0;
2287         *attr_signed= FALSE;
2288
2289         /* note: this is fail with zero length lists, so dont let this get caled in that case */
2290         RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
2291                 prop = RNA_struct_find_property(&itemptr, attr);
2292                 *raw_type= RNA_property_raw_type(prop);
2293                 *attr_tot = RNA_property_array_length(&itemptr, prop);
2294                 *attr_signed= (RNA_property_subtype(prop)==PROP_UNSIGNED) ? FALSE:TRUE;
2295                 break;
2296         }
2297         RNA_PROP_END;
2298 }
2299
2300 /* pyrna_prop_foreach_get/set both use this */
2301 static int foreach_parse_args(
2302                 BPy_PropertyRNA *self, PyObject *args,
2303
2304                 /*values to assign */
2305                 char **attr, PyObject **seq, int *tot, int *size, RawPropertyType *raw_type, int *attr_tot, int *attr_signed)
2306 {
2307 #if 0
2308         int array_tot;
2309         int target_tot;
2310 #endif
2311
2312         *size= *attr_tot= *attr_signed= FALSE;
2313         *raw_type= PROP_RAW_UNSET;
2314
2315         if(!PyArg_ParseTuple(args, "sO", attr, seq) || (!PySequence_Check(*seq) && PyObject_CheckBuffer(*seq))) {
2316                 PyErr_SetString( PyExc_TypeError, "foreach_get(attr, sequence) expects a string and a sequence" );
2317                 return -1;
2318         }
2319
2320         *tot= PySequence_Length(*seq); // TODO - buffer may not be a sequence! array.array() is tho.
2321
2322         if(*tot>0) {
2323                 foreach_attr_type(self, *attr, raw_type, attr_tot, attr_signed);
2324                 *size= RNA_raw_type_sizeof(*raw_type);
2325
2326 #if 0   // works fine but not strictly needed, we could allow RNA_property_collection_raw_* to do the checks
2327                 if((*attr_tot) < 1)
2328                         *attr_tot= 1;
2329
2330                 if (RNA_property_type(self->prop) == PROP_COLLECTION)
2331                         array_tot = RNA_property_collection_length(&self->ptr, self->prop);
2332                 else
2333                         array_tot = RNA_property_array_length(&self->ptr, self->prop);
2334
2335
2336                 target_tot= array_tot * (*attr_tot);
2337
2338                 /* rna_access.c - rna_raw_access(...) uses this same method */
2339                 if(target_tot != (*tot)) {
2340                         PyErr_Format( PyExc_TypeError, "foreach_get(attr, sequence) sequence length mismatch given %d, needed %d", *tot, target_tot);
2341                         return -1;
2342                 }
2343 #endif
2344         }
2345
2346         /* check 'attr_tot' otherwise we dont know if any values were set
2347          * this isnt ideal because it means running on an empty list may fail silently when its not compatible. */
2348         if (*size == 0 && *attr_tot != 0) {
2349                 PyErr_SetString( PyExc_AttributeError, "attribute does not support foreach method" );
2350                 return -1;
2351         }
2352         return 0;
2353 }
2354
2355 static int foreach_compat_buffer(RawPropertyType raw_type, int attr_signed, const char *format)
2356 {
2357         char f = format ? *format:'B'; /* B is assumed when not set */
2358
2359         switch(raw_type) {
2360         case PROP_RAW_CHAR:
2361                 if (attr_signed)        return (f=='b') ? 1:0;
2362                 else                            return (f=='B') ? 1:0;
2363         case PROP_RAW_SHORT:
2364                 if (attr_signed)        return (f=='h') ? 1:0;
2365                 else                            return (f=='H') ? 1:0;
2366         case PROP_RAW_INT:
2367                 if (attr_signed)        return (f=='i') ? 1:0;
2368                 else                            return (f=='I') ? 1:0;
2369         case PROP_RAW_FLOAT:
2370                 return (f=='f') ? 1:0;
2371         case PROP_RAW_DOUBLE:
2372                 return (f=='d') ? 1:0;
2373         case PROP_RAW_UNSET:
2374                 return 0;
2375         }
2376
2377         return 0;
2378 }
2379
2380 static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
2381 {
2382         PyObject *item = NULL;
2383         int i=0, ok=0, buffer_is_compat;
2384         void *array= NULL;
2385
2386         /* get/set both take the same args currently */
2387         char *attr;
2388         PyObject *seq;
2389         int tot, size, attr_tot, attr_signed;
2390         RawPropertyType raw_type;
2391
2392         if(foreach_parse_args(self, args,    &attr, &seq, &tot, &size, &raw_type, &attr_tot, &attr_signed) < 0)
2393                 return NULL;
2394
2395         if(tot==0)
2396                 Py_RETURN_NONE;
2397
2398
2399
2400         if(set) { /* get the array from python */
2401                 buffer_is_compat = FALSE;
2402                 if(PyObject_CheckBuffer(seq)) {
2403                         Py_buffer buf;
2404                         PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
2405
2406                         /* check if the buffer matches */
2407
2408                         buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
2409
2410                         if(buffer_is_compat) {
2411                                 ok = RNA_property_collection_raw_set(NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
2412                         }
2413
2414                         PyBuffer_Release(&buf);
2415                 }
2416
2417                 /* could not use the buffer, fallback to sequence */
2418                 if(!buffer_is_compat) {
2419                         array= PyMem_Malloc(size * tot);
2420
2421                         for( ; i<tot; i++) {
2422                                 item= PySequence_GetItem(seq, i);
2423                                 switch(raw_type) {
2424                                 case PROP_RAW_CHAR:
2425                                         ((char *)array)[i]= (char)PyLong_AsSsize_t(item);
2426                                         break;
2427                                 case PROP_RAW_SHORT:
2428                                         ((short *)array)[i]= (short)PyLong_AsSsize_t(item);
2429                                         break;
2430                                 case PROP_RAW_INT:
2431                                         ((int *)array)[i]= (int)PyLong_AsSsize_t(item);
2432                                         break;
2433                                 case PROP_RAW_FLOAT:
2434                                         ((float *)array)[i]= (float)PyFloat_AsDouble(item);
2435                                         break;
2436                                 case PROP_RAW_DOUBLE:
2437                                         ((double *)array)[i]= (double)PyFloat_AsDouble(item);
2438                                         break;
2439                                 case PROP_RAW_UNSET:
2440                                         /* should never happen */
2441                                         break;
2442                                 }
2443
2444                                 Py_DECREF(item);
2445                         }
2446
2447                         ok = RNA_property_collection_raw_set(NULL, &self->ptr, self->prop, attr, array, raw_type, tot);
2448                 }
2449         }
2450         else {
2451                 buffer_is_compat = FALSE;
2452                 if(PyObject_CheckBuffer(seq)) {
2453                         Py_buffer buf;
2454                         PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
2455
2456                         /* check if the buffer matches, TODO - signed/unsigned types */
2457
2458                         buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
2459
2460                         if(buffer_is_compat) {
2461                                 ok = RNA_property_collection_raw_get(NULL, &self->ptr, self->prop, attr, buf.buf, raw_type, tot);
2462                         }
2463
2464                         PyBuffer_Release(&buf);
2465                 }
2466
2467                 /* could not use the buffer, fallback to sequence */
2468                 if(!buffer_is_compat) {
2469                         array= PyMem_Malloc(size * tot);
2470
2471                         ok = RNA_property_collection_raw_get(NULL, &self->ptr, self->prop, attr, array, raw_type, tot);
2472
2473                         if(!ok) i= tot; /* skip the loop */
2474
2475                         for( ; i<tot; i++) {
2476
2477                                 switch(raw_type) {
2478                                 case PROP_RAW_CHAR:
2479                                         item= PyLong_FromSsize_t(  (Py_ssize_t) ((char *)array)[i]  );
2480                                         break;
2481                                 case PROP_RAW_SHORT:
2482                                         item= PyLong_FromSsize_t(  (Py_ssize_t) ((short *)array)[i]  );
2483                                         break;
2484                                 case PROP_RAW_INT:
2485                                         item= PyLong_FromSsize_t(  (Py_ssize_t) ((int *)array)[i]  );
2486                                         break;
2487                                 case PROP_RAW_FLOAT:
2488                                         item= PyFloat_FromDouble(  (double) ((float *)array)[i]  );
2489                                         break;
2490                                 case PROP_RAW_DOUBLE:
2491                                         item= PyFloat_FromDouble(  (double) ((double *)array)[i]  );
2492                                         break;
2493                                 case PROP_RAW_UNSET:
2494                                         /* should never happen */
2495                                         break;
2496                                 }
2497
2498                                 PySequence_SetItem(seq, i, item);
2499                                 Py_DECREF(item);
2500                         }
2501                 }
2502         }
2503
2504         if(array)
2505                 PyMem_Free(array);
2506
2507         if(PyErr_Occurred()) {
2508                 /* Maybe we could make our own error */
2509                 PyErr_Print();
2510                 PyErr_SetString(PyExc_SystemError, "could not access the py sequence");
2511                 return NULL;
2512         }
2513         if (!ok) {
2514                 PyErr_SetString(PyExc_SystemError, "internal error setting the array");
2515                 return NULL;
2516         }
2517
2518         Py_RETURN_NONE;
2519 }
2520
2521 static PyObject *pyrna_prop_foreach_get(BPy_PropertyRNA *self, PyObject *args)
2522 {
2523         return foreach_getset(self, args, 0);
2524 }
2525
2526 static  PyObject *pyrna_prop_foreach_set(BPy_PropertyRNA *self, PyObject *args)
2527 {
2528         return foreach_getset(self, args, 1);
2529 }
2530
2531 /* A bit of a kludge, make a list out of a collection or array,
2532  * then return the lists iter function, not especially fast but convenient for now */
2533 PyObject *pyrna_prop_iter(BPy_PropertyRNA *self)
2534 {
2535         /* Try get values from a collection */
2536         PyObject *ret;
2537         PyObject *iter;
2538         
2539         if(RNA_property_array_check(&self->ptr, self->prop)) {
2540                 int len= pyrna_prop_array_length(self);
2541                 ret = prop_subscript_array_slice(self, &self->ptr, self->prop, 0, len, len);
2542         }
2543         else if ((ret = pyrna_prop_values(self))) {
2544                 /* do nothing */
2545         }
2546         else {
2547                 PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not iterable" );
2548                 return NULL;
2549         }
2550         
2551         
2552         /* we know this is a list so no need to PyIter_Check */
2553         iter = PyObject_GetIter(ret);
2554         Py_DECREF(ret);
2555         return iter;
2556 }
2557
2558 static struct PyMethodDef pyrna_struct_methods[] = {
2559
2560         /* only for PointerRNA's with ID'props */
2561         {"keys", (PyCFunction)pyrna_struct_keys, METH_NOARGS, NULL},
2562         {"values", (PyCFunction)pyrna_struct_values, METH_NOARGS, NULL},
2563         {"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, NULL},
2564
2565         {"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, NULL},
2566
2567         /* maybe this become and ID function */
2568         {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL},
2569         {"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, NULL},
2570         {"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, NULL},
2571         {"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, NULL},
2572         {"path_resolve", (PyCFunction)pyrna_struct_path_resolve, METH_O, NULL},
2573         {"path_to_id", (PyCFunction)pyrna_struct_path_to_id, METH_VARARGS, NULL},
2574         {"recast_type", (PyCFunction)pyrna_struct_recast_type, METH_NOARGS, NULL},
2575         {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
2576         {NULL, NULL, 0, NULL}
2577 };
2578
2579 static struct PyMethodDef pyrna_prop_methods[] = {
2580         {"keys", (PyCFunction)pyrna_prop_keys, METH_NOARGS, NULL},
2581         {"items", (PyCFunction)pyrna_prop_items, METH_NOARGS,NULL},
2582         {"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, NULL},
2583         
2584         {"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
2585
2586         /* moved into a getset */
2587         {"add", (PyCFunction)pyrna_prop_add, METH_NOARGS, NULL},
2588         {"remove", (PyCFunction)pyrna_prop_remove, METH_O, NULL},
2589
2590         /* almost the same as the srna function */
2591         {"path_to_id", (PyCFunction)pyrna_prop_path_to_id, METH_NOARGS, NULL},
2592
2593         /* array accessor function */
2594         {"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
2595         {"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},
2596         {"__dir__", (PyCFunction)pyrna_prop_dir, METH_NOARGS, NULL},
2597         {NULL, NULL, 0, NULL}
2598 };
2599
2600 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
2601  * todo - also accept useful args */
2602 static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
2603
2604         BPy_StructRNA *base = NULL;
2605         
2606         if (!PyArg_ParseTuple(args, "O!:Base BPy_StructRNA", &pyrna_struct_Type, &base))
2607                 return NULL;
2608         
2609         if (type == &pyrna_struct_Type) {
2610                 return pyrna_struct_CreatePyObject(&base->ptr);
2611         } else {
2612                 BPy_StructRNA *ret = (BPy_StructRNA *) type->tp_alloc(type, 0);
2613                 ret->ptr = base->ptr;
2614                 return (PyObject *)ret;
2615         }
2616 }
2617
2618 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
2619  * todo - also accept useful args */
2620 static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
2621
2622         BPy_PropertyRNA *base = NULL;
2623         
2624         if (!PyArg_ParseTuple(args, "O!:Base BPy_PropertyRNA", &pyrna_prop_Type, &base))
2625                 return NULL;
2626         
2627         if (type == &pyrna_prop_Type) {
2628                 return pyrna_prop_CreatePyObject(&base->ptr, base->prop);
2629         } else {
2630                 BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
2631                 ret->ptr = base->ptr;
2632                 ret->prop = base->prop;
2633                 return (PyObject *)ret;
2634         }
2635 }
2636
2637 PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
2638 {
2639         PyObject *ret;
2640         int type = RNA_property_type(prop);
2641         int flag = RNA_property_flag(prop);
2642         int a;
2643
2644         if(RNA_property_array_check(ptr, prop)) {
2645                 int len = RNA_property_array_length(ptr, prop);
2646
2647                 /* resolve the array from a new pytype */
2648
2649                 /* kazanbas: TODO make multidim sequences here */
2650
2651                 switch (type) {
2652                 case PROP_BOOLEAN:
2653                         ret = PyTuple_New(len);
2654                         for(a=0; a<len; a++)
2655                                 PyTuple_SET_ITEM(ret, a, PyBool_FromLong( ((int*)data)[a] ));
2656                         break;
2657                 case PROP_INT:
2658                         ret = PyTuple_New(len);
2659                         for(a=0; a<len; a++)
2660                                 PyTuple_SET_ITEM(ret, a, PyLong_FromSsize_t( (Py_ssize_t)((int*)data)[a] ));
2661                         break;
2662                 case PROP_FLOAT:
2663                         switch(RNA_property_subtype(prop)) {
2664                                 case PROP_ALL_VECTOR_SUBTYPES:
2665                                         ret= newVectorObject(data, len, Py_NEW, NULL);
2666                                         break;
2667                                 case PROP_MATRIX:
2668                                         if(len==16) {
2669                                                 ret= newMatrixObject(data, 4, 4, Py_NEW, NULL);
2670                                                 break;
2671                                         }
2672                                         else if (len==9) {
2673                                                 ret= newMatrixObject(data, 3, 3, Py_NEW, NULL);
2674                                                 break;
2675                                         }
2676                                         /* pass through */
2677                                 default:
2678                                         ret = PyTuple_New(len);
2679                                         for(a=0; a<len; a++)
2680                                                 PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble( ((float*)data)[a] ));
2681
2682                         }
2683                         break;
2684                 default:
2685                         PyErr_Format(PyExc_TypeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
2686                         ret = NULL;
2687                         break;
2688                 }
2689         }
2690         else {
2691                 /* see if we can coorce into a python type - PropertyType */
2692                 switch (type) {
2693                 case PROP_BOOLEAN:
2694                         ret = PyBool_FromLong( *(int*)data );
2695                         break;
2696                 case PROP_INT:
2697                         ret = PyLong_FromSsize_t( (Py_ssize_t)*(int*)data );
2698                         break;
2699                 case PROP_FLOAT:
2700                         ret = PyFloat_FromDouble( *(float*)data );
2701                         break;
2702                 case PROP_STRING:
2703                 {
2704                         if(flag & PROP_THICK_WRAP)
2705                                 ret = PyUnicode_FromString( (char*)data );
2706                         else
2707                                 ret = PyUnicode_FromString( *(char**)data );
2708                         break;
2709                 }
2710                 case PROP_ENUM:
2711                 {
2712                         ret= pyrna_enum_to_py(ptr, prop, *(int*)data);
2713                         break;
2714                 }
2715                 case PROP_POINTER:
2716                 {
2717                         PointerRNA newptr;
2718                         StructRNA *type= RNA_property_pointer_type(ptr, prop);
2719
2720                         if(flag & PROP_RNAPTR) {
2721                                 /* in this case we get the full ptr */
2722                                 newptr= *(PointerRNA*)data;
2723                         }
2724                         else {
2725                                 if(RNA_struct_is_ID(type)) {
2726                                         RNA_id_pointer_create(*(void**)data, &newptr);
2727                                 } else {
2728                                         /* note: this is taken from the function's ID pointer
2729                                          * and will break if a function returns a pointer from
2730                                          * another ID block, watch this! - it should at least be
2731                                          * easy to debug since they are all ID's */
2732                                         RNA_pointer_create(ptr->id.data, type, *(void**)data, &newptr);
2733                                 }
2734                         }
2735
2736                         if (newptr.data) {
2737                                 ret = pyrna_struct_CreatePyObject(&newptr);
2738                         } else {
2739                                 ret = Py_None;
2740                                 Py_INCREF(ret);
2741                         }
2742                         break;
2743                 }
2744                 case PROP_COLLECTION:
2745                 {
2746                         ListBase *lb= (ListBase*)data;
2747                         CollectionPointerLink *link;
2748                         PyObject *linkptr;
2749
2750                         ret = PyList_New(0);
2751
2752                         for(link=lb->first; link; link=link->next) {
2753                                 linkptr= pyrna_struct_CreatePyObject(&link->ptr);
2754                                 PyList_Append(ret, linkptr);
2755                                 Py_DECREF(linkptr);
2756                         }
2757
2758                         break;
2759                 }
2760                 default:
2761                         PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
2762                         ret = NULL;
2763                         break;
2764                 }
2765         }
2766
2767         return ret;
2768 }
2769
2770 static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw)
2771 {
2772         /* Note, both BPy_StructRNA and BPy_PropertyRNA can be used here */
2773         PointerRNA *self_ptr= &(((BPy_DummyPointerRNA *)PyTuple_GET_ITEM(self, 0))->ptr);
2774         FunctionRNA *self_func=  PyCObject_AsVoidPtr(PyTuple_GET_ITEM(self, 1));
2775
2776         PointerRNA funcptr;
2777         ParameterList parms;
2778         ParameterIterator iter;
2779         PropertyRNA *parm;
2780         PyObject *ret, *item;
2781         int i, args_len, parms_len, ret_len, flag, err= 0, kw_tot= 0, kw_arg;
2782         const char *parm_id;
2783
2784         PropertyRNA *pret_single= NULL;
2785         void *retdata_single= NULL;
2786
2787         /* Should never happen but it does in rare cases */
2788         if(self_ptr==NULL) {
2789                 PyErr_SetString(PyExc_RuntimeError, "rna functions internal rna pointer is NULL, this is a bug. aborting");
2790                 return NULL;
2791         }
2792         
2793         if(self_func==NULL) {
2794                 PyErr_Format(PyExc_RuntimeError, "%.200s.<unknown>(): rna function internal function is NULL, this is a bug. aborting", RNA_struct_identifier(self_ptr->type));
2795                 return NULL;
2796         }
2797         
2798         /* include the ID pointer for pyrna_param_to_py() so we can include the
2799          * ID pointer on return values, this only works when returned values have
2800          * the same ID as the functions. */
2801         RNA_pointer_create(self_ptr->id.data, &RNA_Function, self_func, &funcptr);
2802
2803         args_len= PyTuple_GET_SIZE(args);
2804
2805         RNA_parameter_list_create(&parms, self_ptr, self_func);
2806         RNA_parameter_list_begin(&parms, &iter);
2807         parms_len= RNA_parameter_list_size(&parms);
2808         ret_len= 0;
2809
2810         if(args_len + (kw ? PyDict_Size(kw):0) > parms_len) {
2811                 RNA_parameter_list_end(&iter);
2812                 PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): takes at most %d arguments, got %d", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parms_len, args_len);
2813                 err= -1;
2814         }
2815
2816         /* parse function parameters */
2817         for (i= 0; iter.valid && err==0; RNA_parameter_list_next(&iter)) {
2818                 parm= iter.parm;
2819                 flag= RNA_property_flag(parm);
2820
2821                 /* only useful for single argument returns, we'll need another list loop for multiple */
2822                 if (flag & PROP_RETURN) {
2823                         ret_len++;
2824                         if (pret_single==NULL) {
2825                                 pret_single= parm;
2826                                 retdata_single= iter.data;
2827                         }
2828
2829                         continue;
2830                 }
2831
2832                 parm_id= RNA_property_identifier(parm);
2833                 item= NULL;
2834
2835                 if ((i < args_len) && (flag & PROP_REQUIRED)) {
2836                         item= PyTuple_GET_ITEM(args, i);
2837                         i++;
2838
2839                         kw_arg= FALSE;
2840                 }
2841                 else if (kw != NULL) {
2842                         item= PyDict_GetItemString(kw, parm_id);  /* borrow ref */
2843                         if(item)
2844                                 kw_tot++; /* make sure invalid keywords are not given */
2845
2846                         kw_arg= TRUE;
2847                 }
2848
2849                 if (item==NULL) {
2850                         if(flag & PROP_REQUIRED) {
2851                                 PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): required parameter \"%.200s\" not specified", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
2852                                 err= -1;
2853                                 break;
2854                         }
2855                         else /* PyDict_GetItemString wont raise an error */
2856                                 continue;
2857                 }
2858
2859                 err= pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
2860
2861                 if(err!=0) {
2862                         /* the error generated isnt that useful, so generate it again with a useful prefix
2863                          * could also write a function to prepend to error messages */
2864                         char error_prefix[512];
2865                         PyErr_Clear(); /* re-raise */
2866
2867                         if(kw_arg==TRUE)
2868                                 snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with keyword argument \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
2869                         else
2870                                 snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with argument %d, \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), i, parm_id);
2871
2872                         pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
2873
2874                         break;
2875                 }
2876         }
2877         
2878         RNA_parameter_list_end(&iter);
2879
2880
2881         /* Check if we gave args that dont exist in the function
2882          * printing the error is slow but it should only happen when developing.
2883          * the if below is quick, checking if it passed less keyword args then we gave.
2884          * (Dont overwrite the error if we have one, otherwise can skip important messages and confuse with args)
2885          */
2886         if(err == 0 && kw && (PyDict_Size(kw) > kw_tot)) {
2887                 PyObject *key, *value;
2888                 Py_ssize_t pos = 0;
2889
2890                 DynStr *bad_args= BLI_dynstr_new();
2891                 DynStr *good_args= BLI_dynstr_new();
2892
2893                 char *arg_name, *bad_args_str, *good_args_str;
2894                 int found= FALSE, first= TRUE;
2895
2896                 while (PyDict_Next(kw, &pos, &key, &value)) {
2897
2898                         arg_name= _PyUnicode_AsString(key);
2899                         found= FALSE;
2900
2901                         if(arg_name==NULL) { /* unlikely the argname is not a string but ignore if it is*/
2902                                 PyErr_Clear();
2903                         }
2904                         else {
2905                                 /* Search for arg_name */
2906                                 RNA_parameter_list_begin(&parms, &iter);
2907                                 for(; iter.valid; RNA_parameter_list_next(&iter)) {
2908                                         parm= iter.parm;
2909                                         if (strcmp(arg_name, RNA_property_identifier(parm))==0) {
2910                                                 found= TRUE;
2911                                                 break;
2912                                         }
2913                                 }
2914
2915                                 RNA_parameter_list_end(&iter);
2916
2917                                 if(found==FALSE) {
2918                                         BLI_dynstr_appendf(bad_args, first ? "%s" : ", %s", arg_name);
2919                                         first= FALSE;
2920                                 }
2921                         }
2922                 }
2923
2924                 /* list good args */
2925                 first= TRUE;
2926
2927                 RNA_parameter_list_begin(&parms, &iter);
2928                 for(; iter.valid; RNA_parameter_list_next(&iter)) {
2929                         parm= iter.parm;
2930                         if(RNA_property_flag(parm) & PROP_RETURN)
2931                                 continue;
2932
2933                         BLI_dynstr_appendf(good_args, first ? "%s" : ", %s", RNA_property_identifier(parm));
2934                         first= FALSE;
2935                 }
2936                 RNA_parameter_list_end(&iter);
2937
2938
2939                 bad_args_str= BLI_dynstr_get_cstring(bad_args);
2940                 good_args_str= BLI_dynstr_get_cstring(good_args);
2941
2942                 PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): was called with invalid keyword arguments(s) (%s), expected (%s)", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), bad_args_str, good_args_str);
2943
2944                 BLI_dynstr_free(bad_args);
2945                 BLI_dynstr_free(good_args);
2946                 MEM_freeN(bad_args_str);
2947                 MEM_freeN(good_args_str);
2948
2949                 err= -1;
2950         }
2951
2952         ret= NULL;
2953         if (err==0) {
2954                 /* call function */
2955                 ReportList reports;
2956                 bContext *C= BPy_GetContext();
2957
2958                 BKE_reports_init(&reports, RPT_STORE);
2959                 RNA_function_call(C, &reports, self_ptr, self_func, &parms);
2960
2961                 err= (BPy_reports_to_error(&reports))? -1: 0;
2962                 BKE_reports_clear(&reports);
2963
2964                 /* return value */
2965                 if(err==0) {
2966                         if (ret_len > 0) {
2967                                 if (ret_len > 1) {
2968                                         ret= PyTuple_New(ret_len);
2969                                         i= 0; /* arg index */
2970
2971                                         RNA_parameter_list_begin(&parms, &iter);
2972
2973                                         for(; iter.valid; RNA_parameter_list_next(&iter)) {
2974                                                 parm= iter.parm;
2975                                                 flag= RNA_property_flag(parm);
2976
2977                                                 if (flag & PROP_RETURN)
2978                                                         PyTuple_SET_ITEM(ret, i++, pyrna_param_to_py(&funcptr, parm, iter.data));
2979                                         }
2980
2981                                         RNA_parameter_list_end(&iter);
2982                                 }
2983                                 else
2984                                         ret= pyrna_param_to_py(&funcptr, pret_single, retdata_single);
2985
2986                                 /* possible there is an error in conversion */
2987                                 if(ret==NULL)
2988                                         err= -1;
2989                         }
2990                 }
2991         }
2992
2993         /* cleanup */
2994         RNA_parameter_list_end(&iter);
2995         RNA_parameter_list_free(&parms);
2996
2997         if (ret)
2998                 return ret;
2999
3000         if (err==-1)
3001                 return NULL;
3002
3003         Py_RETURN_NONE;
3004 }
3005
3006 /*-----------------------BPy_StructRNA method def------------------------------*/
3007 PyTypeObject pyrna_struct_Type = {
3008         PyVarObject_HEAD_INIT(NULL, 0)
3009         "StructRNA",                    /* tp_name */
3010         sizeof( BPy_StructRNA ),        /* tp_basicsize */
3011         0,                      /* tp_itemsize */
3012         /* methods */
3013         ( destructor ) pyrna_struct_dealloc,/* tp_dealloc */
3014         NULL,                       /* printfunc tp_print; */
3015         NULL,                                           /* getattrfunc tp_getattr; */
3016         NULL,                                           /* setattrfunc tp_setattr; */
3017         NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
3018         ( reprfunc ) pyrna_struct_repr, /* tp_repr */
3019
3020         /* Method suites for standard classes */
3021
3022         NULL,                       /* PyNumberMethods *tp_as_number; */
3023         &pyrna_struct_as_sequence,      /* PySequenceMethods *tp_as_sequence; */
3024         &pyrna_struct_as_mapping,       /* PyMappingMethods *tp_as_mapping; */
3025
3026         /* More standard operations (here for binary compatibility) */
3027
3028         ( hashfunc )pyrna_struct_hash,  /* hashfunc tp_hash; */
3029         NULL,                                           /* ternaryfunc tp_call; */
3030         NULL,                       /* reprfunc tp_str; */
3031         ( getattrofunc ) pyrna_struct_getattro, /* getattrofunc tp_getattro; */
3032         ( setattrofunc ) pyrna_struct_setattro, /* setattrofunc tp_setattro; */
3033
3034         /* Functions to access object as input/output buffer */
3035         NULL,                       /* PyBufferProcs *tp_as_buffer; */
3036
3037   /*** Flags to define presence of optional/expanded features ***/
3038         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
3039
3040         NULL,                                           /*  char *tp_doc;  Documentation string */
3041   /*** Assigned meaning in release 2.0 ***/
3042         /* call function for all accessible objects */
3043         NULL,                       /* traverseproc tp_traverse; */
3044
3045         /* delete references to contained objects */
3046         NULL,                       /* inquiry tp_clear; */
3047
3048   /***  Assigned meaning in release 2.1 ***/
3049   /*** rich comparisons ***/
3050         (richcmpfunc)pyrna_struct_richcmp,      /* richcmpfunc tp_richcompare; */
3051
3052   /***  weak reference enabler ***/
3053         0,                          /* long tp_weaklistoffset; */
3054
3055   /*** Added in release 2.2 ***/
3056         /*   Iterators */
3057         NULL,                       /* getiterfunc tp_iter; */
3058         NULL,                       /* iternextfunc tp_iternext; */
3059
3060   /*** Attribute descriptor and subclassing stuff ***/
3061         pyrna_struct_methods,                   /* struct PyMethodDef *tp_methods; */
3062         NULL,                       /* struct PyMemberDef *tp_members; */
3063         pyrna_struct_getseters,         /* struct PyGetSetDef *tp_getset; */
3064         NULL,                       /* struct _typeobject *tp_base; */
3065         NULL,                       /* PyObject *tp_dict; */
3066         NULL,                       /* descrgetfunc tp_descr_get; */
3067         NULL,                       /* descrsetfunc tp_descr_set; */
3068         0,                          /* long tp_dictoffset; */
3069         NULL,                       /* initproc tp_init; */
3070         NULL,                       /* allocfunc tp_alloc; */
3071         pyrna_struct_new,                       /* newfunc tp_new; */
3072         /*  Low-level free-memory routine */
3073         NULL,                       /* freefunc tp_free;  */
3074         /* For PyObject_IS_GC */
3075         NULL,                       /* inquiry tp_is_gc;  */
3076         NULL,                       /* PyObject *tp_bases; */
3077         /* method resolution order */
3078         NULL,                       /* PyObject *tp_mro;  */
3079         NULL,                       /* PyObject *tp_cache; */
3080         NULL,                       /* PyObject *tp_subclasses; */
3081         NULL,                       /* PyObject *tp_weaklist; */
3082         NULL
3083 };
3084
3085 /*-----------------------BPy_PropertyRNA method def------------------------------*/
3086 PyTypeObject pyrna_prop_Type = {
3087         PyVarObject_HEAD_INIT(NULL, 0)
3088         "PropertyRNA",          /* tp_name */
3089         sizeof( BPy_PropertyRNA ),                      /* tp_basicsize */
3090         0,                      /* tp_itemsize */
3091         /* methods */
3092         NULL,                                           /* tp_dealloc */
3093         NULL,                       /* printfunc tp_print; */
3094         NULL,                                           /* getattrfunc tp_getattr; */
3095         NULL,                       /* setattrfunc tp_setattr; */
3096         NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
3097         ( reprfunc ) pyrna_prop_repr,   /* tp_repr */
3098
3099         /* Method suites for standard classes */
3100
3101         NULL,                       /* PyNumberMethods *tp_as_number; */
3102         &pyrna_prop_as_sequence,        /* PySequenceMethods *tp_as_sequence; */
3103         &pyrna_prop_as_mapping,         /* PyMappingMethods *tp_as_mapping; */
3104
3105         /* More standard operations (here for binary compatibility) */
3106
3107         NULL,                                           /* hashfunc tp_hash; */
3108         NULL,                       /* ternaryfunc tp_call; */
3109         NULL,                       /* reprfunc tp_str; */
3110
3111         /* will only use these if this is a subtype of a py class */
3112         ( getattrofunc ) pyrna_prop_getattro,   /* getattrofunc tp_getattro; */
3113         ( setattrofunc ) pyrna_prop_setattro,   /* setattrofunc tp_setattro; */
3114
3115         /* Functions to access object as input/output buffer */
3116         NULL,                       /* PyBufferProcs *tp_as_buffer; */