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