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