- lazy subtype initialization rna, was initializing every type in bpy.types at startu...
[blender.git] / source / blender / python / intern / bpy_rna.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "bpy_rna.h"
26 #include "bpy_compat.h"
27 //#include "blendef.h"
28 #include "BLI_dynstr.h"
29 #include "BLI_listbase.h"
30 #include "float.h" /* FLT_MIN/MAX */
31
32 #include "RNA_define.h" /* for defining our own rna */
33
34 #include "MEM_guardedalloc.h"
35 #include "BKE_context.h"
36 #include "BKE_global.h" /* evil G.* */
37
38 static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
39 {
40         return (a->ptr.data==b->ptr.data) ? 0 : -1;
41 }
42
43 static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b )
44 {
45         return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1;
46 }
47
48
49 /* For some reason python3 needs these :/ */
50 static PyObject *pyrna_struct_richcmp(BPy_StructRNA * a, BPy_StructRNA * b, int op)
51 {
52         int cmp_result= -1; /* assume false */
53         if (BPy_StructRNA_Check(a) && BPy_StructRNA_Check(b)) {
54                 cmp_result= pyrna_struct_compare(a, b);
55         }
56
57         return Py_CmpToRich(op, cmp_result);
58 }
59
60 static PyObject *pyrna_prop_richcmp(BPy_PropertyRNA * a, BPy_PropertyRNA * b, int op)
61 {
62         int cmp_result= -1; /* assume false */
63         if (BPy_PropertyRNA_Check(a) && BPy_PropertyRNA_Check(b)) {
64                 cmp_result= pyrna_prop_compare(a, b);
65         }
66
67         return Py_CmpToRich(op, cmp_result);
68 }
69
70 /*----------------------repr--------------------------------------------*/
71 static PyObject *pyrna_struct_repr( BPy_StructRNA * self )
72 {
73         char str[512];
74         RNA_string_get(&self->ptr, "identifier", str);
75         return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(&self->ptr), str);
76 }
77
78 static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
79 {
80         char str[512];
81         RNA_string_get(&self->ptr, "identifier", str);
82         return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" -> \"%s\" ]", RNA_struct_identifier(&self->ptr), str, RNA_property_identifier(&self->ptr, self->prop) );
83 }
84
85 static long pyrna_struct_hash( BPy_StructRNA * self )
86 {
87         return (long)self->ptr.data;
88 }
89
90 /* use our own dealloc so we can free a property if we use one */
91 static void pyrna_struct_dealloc( BPy_StructRNA * self )
92 {
93         /* Note!! for some weired reason calling PyObject_DEL() directly crashes blender! */
94         if (self->freeptr && self->ptr.data) {
95                 IDP_FreeProperty(self->ptr.data);
96                 MEM_freeN(self->ptr.data);
97                 self->ptr.data= NULL;
98         }
99
100         Py_TYPE(self)->tp_free(self);
101         return;
102 }
103
104 static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
105 {
106         const EnumPropertyItem *item;
107         int totitem, i;
108
109         DynStr *dynstr= BLI_dynstr_new();
110         char *cstring;
111
112         RNA_property_enum_items(ptr, prop, &item, &totitem);
113
114         for (i=0; i<totitem; i++) {
115                 
116                 BLI_dynstr_appendf(dynstr, i?", '%s'":"'%s'", item[i].identifier);
117         }
118         
119         cstring = BLI_dynstr_get_cstring(dynstr);
120         BLI_dynstr_free(dynstr);
121         return cstring;
122 }
123
124 PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
125 {
126         PyObject *ret;
127         int type = RNA_property_type(ptr, prop);
128         int len = RNA_property_array_length(ptr, prop);
129
130         if (len > 0) {
131                 /* resolve the array from a new pytype */
132                 return pyrna_prop_CreatePyObject(ptr, prop);
133         }
134         
135         /* see if we can coorce into a python type - PropertyType */
136         switch (type) {
137         case PROP_BOOLEAN:
138                 ret = PyBool_FromLong( RNA_property_boolean_get(ptr, prop) );
139                 break;
140         case PROP_INT:
141                 ret = PyLong_FromSize_t( (size_t)RNA_property_int_get(ptr, prop) );
142                 break;
143         case PROP_FLOAT:
144                 ret = PyFloat_FromDouble( RNA_property_float_get(ptr, prop) );
145                 break;
146         case PROP_STRING:
147         {
148                 char *buf;
149                 buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1);
150                 ret = PyUnicode_FromString( buf );
151                 MEM_freeN(buf);
152                 break;
153         }
154         case PROP_ENUM:
155         {
156                 const char *identifier;
157                 int val = RNA_property_enum_get(ptr, prop);
158                 
159                 if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) {
160                         ret = PyUnicode_FromString( identifier );
161                 } else {
162                         PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
163                         ret = NULL;
164                 }
165
166                 break;
167         }
168         case PROP_POINTER:
169         {
170                 PointerRNA newptr;
171                 newptr= RNA_property_pointer_get(ptr, prop);
172                 if (newptr.data) {
173                         ret = pyrna_struct_CreatePyObject(&newptr);
174                 } else {
175                         ret = Py_None;
176                         Py_INCREF(ret);
177                 }
178                 break;
179         }
180         case PROP_COLLECTION:
181                 ret = pyrna_prop_CreatePyObject(ptr, prop);
182                 break;
183         default:
184                 PyErr_Format(PyExc_AttributeError, "RNA Error: unknown type \"%d\" (pyrna_prop_to_py)", type);
185                 ret = NULL;
186                 break;
187         }
188         
189         return ret;
190 }
191
192
193 int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
194 {
195         int type = RNA_property_type(ptr, prop);
196         int len = RNA_property_array_length(ptr, prop);
197         
198         if (len > 0) {
199                 PyObject *item;
200                 int i;
201                 
202                 if (!PySequence_Check(value)) {
203                         PyErr_SetString(PyExc_TypeError, "expected a python sequence type assigned to an RNA array.");
204                         return -1;
205                 }
206                 
207                 if ((int)PySequence_Length(value) != len) {
208                         PyErr_SetString(PyExc_AttributeError, "python sequence length did not match the RNA array.");
209                         return -1;
210                 }
211                 
212                 /* for arrays we have a limited number of types */
213                 switch (type) {
214                 case PROP_BOOLEAN:
215                 {
216                         int *param_arr = MEM_mallocN(sizeof(char) * len, "pyrna bool array");
217                         
218                         /* collect the variables before assigning, incase one of them is incorrect */
219                         for (i=0; i<len; i++) {
220                                 item = PySequence_GetItem(value, i);
221                                 param_arr[i] = PyObject_IsTrue( item );
222                                 Py_DECREF(item);
223                                 
224                                 if (param_arr[i] < 0) {
225                                         MEM_freeN(param_arr);
226                                         PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence is not a boolean");
227                                         return -1;
228                                 }
229                         }
230                         
231                         RNA_property_boolean_set_array(ptr, prop, param_arr);
232                         
233                         MEM_freeN(param_arr);
234                         break;
235                 }
236                 case PROP_INT:
237                 {
238                         int *param_arr = MEM_mallocN(sizeof(int) * len, "pyrna int array");
239                         
240                         /* collect the variables before assigning, incase one of them is incorrect */
241                         for (i=0; i<len; i++) {
242                                 item = PySequence_GetItem(value, i);
243                                 param_arr[i] = (int)PyLong_AsSsize_t(item); /* deal with any errors later */
244                                 Py_DECREF(item);
245                         }
246                         
247                         if (PyErr_Occurred()) {
248                                 MEM_freeN(param_arr);
249                                 PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as an int");
250                                 return -1;
251                         }
252                         
253                         RNA_property_int_set_array(ptr, prop, param_arr);
254                         
255                         MEM_freeN(param_arr);
256                         break;
257                 }
258                 case PROP_FLOAT:
259                 {
260                         float *param_arr = MEM_mallocN(sizeof(float) * len, "pyrna float array");
261                         
262                         /* collect the variables before assigning, incase one of them is incorrect */
263                         for (i=0; i<len; i++) {
264                                 item = PySequence_GetItem(value, i);
265                                 param_arr[i] = (float)PyFloat_AsDouble(item); /* deal with any errors later */
266                                 Py_DECREF(item);
267                         }
268                         
269                         if (PyErr_Occurred()) {
270                                 MEM_freeN(param_arr);
271                                 PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as a float");
272                                 return -1;
273                         }
274                         
275                         RNA_property_float_set_array(ptr, prop, param_arr);
276                         
277                         MEM_freeN(param_arr);
278                         break;
279                 }
280                 }
281         } else {
282                 /* Normal Property (not an array) */
283                 
284                 /* see if we can coorce into a python type - PropertyType */
285                 switch (type) {
286                 case PROP_BOOLEAN:
287                 {
288                         int param = PyObject_IsTrue( value );
289                         
290                         if( param < 0 ) {
291                                 PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
292                                 return -1;
293                         } else {
294                                 RNA_property_boolean_set(ptr, prop, param);
295                         }
296                         break;
297                 }
298                 case PROP_INT:
299                 {
300                         int param = PyLong_AsSsize_t(value);
301                         if (PyErr_Occurred()) {
302                                 PyErr_SetString(PyExc_TypeError, "expected an int type");
303                                 return -1;
304                         } else {
305                                 RNA_property_int_set(ptr, prop, param);
306                         }
307                         break;
308                 }
309                 case PROP_FLOAT:
310                 {
311                         float param = PyFloat_AsDouble(value);
312                         if (PyErr_Occurred()) {
313                                 PyErr_SetString(PyExc_TypeError, "expected a float type");
314                                 return -1;
315                         } else {
316                                 RNA_property_float_set(ptr, prop, param);
317                         }
318                         break;
319                 }
320                 case PROP_STRING:
321                 {
322                         char *param = _PyUnicode_AsString(value);
323                         
324                         if (param==NULL) {
325                                 PyErr_SetString(PyExc_TypeError, "expected a string type");
326                                 return -1;
327                         } else {
328                                 RNA_property_string_set(ptr, prop, param);
329                         }
330                         break;
331                 }
332                 case PROP_ENUM:
333                 {
334                         char *param = _PyUnicode_AsString(value);
335                         
336                         if (param==NULL) {
337                                 char *enum_str= pyrna_enum_as_string(ptr, prop);
338                                 PyErr_Format(PyExc_TypeError, "expected a string enum type in (%s)", enum_str);
339                                 MEM_freeN(enum_str);
340                                 return -1;
341                         } else {
342                                 int val;
343                                 if (RNA_property_enum_value(ptr, prop, param, &val)) {
344                                         RNA_property_enum_set(ptr, prop, val);
345                                 } else {
346                                         char *enum_str= pyrna_enum_as_string(ptr, prop);
347                                         PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found in (%s)", param, enum_str);
348                                         MEM_freeN(enum_str);
349                                         return -1;
350                                 }
351                         }
352                         
353                         break;
354                 }
355                 case PROP_POINTER:
356                 {
357                         PyErr_SetString(PyExc_AttributeError, "cant assign pointers yet");
358                         return -1;
359                         break;
360                 }
361                 case PROP_COLLECTION:
362                         PyErr_SetString(PyExc_AttributeError, "cant assign to collections");
363                         return -1;
364                         break;
365                 default:
366                         PyErr_SetString(PyExc_AttributeError, "unknown property type (pyrna_py_to_prop)");
367                         return -1;
368                         break;
369                 }
370         }
371         
372         return 0;
373 }
374
375
376 static PyObject * pyrna_prop_to_py_index(PointerRNA *ptr, PropertyRNA *prop, int index)
377 {
378         PyObject *ret;
379         int type = RNA_property_type(ptr, prop);
380         
381         /* see if we can coorce into a python type - PropertyType */
382         switch (type) {
383         case PROP_BOOLEAN:
384                 ret = PyBool_FromLong( RNA_property_boolean_get_index(ptr, prop, index) );
385                 break;
386         case PROP_INT:
387                 ret = PyLong_FromSize_t( (size_t)RNA_property_int_get_index(ptr, prop, index) );
388                 break;
389         case PROP_FLOAT:
390                 ret = PyFloat_FromDouble( RNA_property_float_get_index(ptr, prop, index) );
391                 break;
392         default:
393                 PyErr_SetString(PyExc_AttributeError, "not an array type");
394                 ret = NULL;
395                 break;
396         }
397         
398         return ret;
399 }
400
401 static int pyrna_py_to_prop_index(PointerRNA *ptr, PropertyRNA *prop, int index, PyObject *value)
402 {
403         int ret = 0;
404         int type = RNA_property_type(ptr, prop);
405         
406         /* see if we can coorce into a python type - PropertyType */
407         switch (type) {
408         case PROP_BOOLEAN:
409         {
410                 int param = PyObject_IsTrue( value );
411                 
412                 if( param < 0 ) {
413                         PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
414                         ret = -1;
415                 } else {
416                         RNA_property_boolean_set_index(ptr, prop, index, param);
417                 }
418                 break;
419         }
420         case PROP_INT:
421         {
422                 int param = PyLong_AsSsize_t(value);
423                 if (PyErr_Occurred()) {
424                         PyErr_SetString(PyExc_TypeError, "expected an int type");
425                         ret = -1;
426                 } else {
427                         RNA_property_int_set_index(ptr, prop, index, param);
428                 }
429                 break;
430         }
431         case PROP_FLOAT:
432         {
433                 float param = PyFloat_AsDouble(value);
434                 if (PyErr_Occurred()) {
435                         PyErr_SetString(PyExc_TypeError, "expected a float type");
436                         ret = -1;
437                 } else {
438                         RNA_property_float_set_index(ptr, prop, index, param);
439                 }
440                 break;
441         }
442         default:
443                 PyErr_SetString(PyExc_AttributeError, "not an array type");
444                 ret = -1;
445                 break;
446         }
447         
448         return ret;
449 }
450
451 //---------------sequence-------------------------------------------
452 static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA * self )
453 {
454         Py_ssize_t len;
455         
456         if (RNA_property_type(&self->ptr, self->prop) == PROP_COLLECTION) {
457                 len = RNA_property_collection_length(&self->ptr, self->prop);
458         } else {
459                 len = RNA_property_array_length(&self->ptr, self->prop);
460                 
461                 if (len==0) { /* not an array*/
462                         PyErr_SetString(PyExc_AttributeError, "len() only available for collection RNA types");
463                         return -1;
464                 }
465         }
466         
467         return len;
468 }
469
470 static PyObject *pyrna_prop_subscript( BPy_PropertyRNA * self, PyObject *key )
471 {
472         PyObject *ret;
473         PointerRNA newptr;
474         int keynum = 0;
475         char *keyname = NULL;
476         
477         if (PyUnicode_Check(key)) {
478                 keyname = _PyUnicode_AsString(key);
479         } else if (PyLong_Check(key)) {
480                 keynum = PyLong_AsSsize_t(key);
481         } else {
482                 PyErr_SetString(PyExc_AttributeError, "invalid key, key must be a string or an int");
483                 return NULL;
484         }
485         
486         if (RNA_property_type(&self->ptr, self->prop) == PROP_COLLECTION) {
487                 int ok;
488                 if (keyname)    ok = RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr);
489                 else                    ok = RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr);
490                 
491                 if (ok) {
492                         ret = pyrna_struct_CreatePyObject(&newptr);
493                 } else {
494                         PyErr_SetString(PyExc_AttributeError, "out of range");
495                         ret = NULL;
496                 }
497                 
498         } else if (keyname) {
499                 PyErr_SetString(PyExc_AttributeError, "string keys are only supported for collections");
500                 ret = NULL;
501         } else {
502                 int len = RNA_property_array_length(&self->ptr, self->prop);
503                 
504                 if (len==0) { /* not an array*/
505                         PyErr_Format(PyExc_AttributeError, "not an array or collection %d", keynum);
506                         ret = NULL;
507                 }
508                 
509                 if (keynum >= len){
510                         PyErr_SetString(PyExc_AttributeError, "index out of range");
511                         ret = NULL;
512                 } else { /* not an array*/
513                         ret = pyrna_prop_to_py_index(&self->ptr, self->prop, keynum);
514                 }
515         }
516         
517         return ret;
518 }
519
520
521 static int pyrna_prop_assign_subscript( BPy_PropertyRNA * self, PyObject *key, PyObject *value )
522 {
523         int ret = 0;
524         int keynum = 0;
525         char *keyname = NULL;
526         
527         if (!RNA_property_editable(&self->ptr, self->prop)) {
528                 PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(&self->ptr, self->prop), RNA_struct_identifier(&self->ptr) );
529                 return -1;
530         }
531         
532         if (PyUnicode_Check(key)) {
533                 keyname = _PyUnicode_AsString(key);
534         } else if (PyLong_Check(key)) {
535                 keynum = PyLong_AsSsize_t(key);
536         } else {
537                 PyErr_SetString(PyExc_AttributeError, "PropertyRNA - invalid key, key must be a string or an int");
538                 return -1;
539         }
540         
541         if (RNA_property_type(&self->ptr, self->prop) == PROP_COLLECTION) {
542                 PyErr_SetString(PyExc_AttributeError, "PropertyRNA - assignment is not supported for collections (yet)");
543                 ret = -1;
544         } else if (keyname) {
545                 PyErr_SetString(PyExc_AttributeError, "PropertyRNA - string keys are only supported for collections");
546                 ret = -1;
547         } else {
548                 int len = RNA_property_array_length(&self->ptr, self->prop);
549                 
550                 if (len==0) { /* not an array*/
551                         PyErr_Format(PyExc_AttributeError, "PropertyRNA - not an array or collection %d", keynum);
552                         ret = -1;
553                 }
554                 
555                 if (keynum >= len){
556                         PyErr_SetString(PyExc_AttributeError, "PropertyRNA - index out of range");
557                         ret = -1;
558                 } else {
559                         ret = pyrna_py_to_prop_index(&self->ptr, self->prop, keynum, value);
560                 }
561         }
562         
563         return ret;
564 }
565
566
567
568 static PyMappingMethods pyrna_prop_as_mapping = {
569         ( inquiry ) pyrna_prop_len,     /* mp_length */
570         ( binaryfunc ) pyrna_prop_subscript,    /* mp_subscript */
571         ( objobjargproc ) pyrna_prop_assign_subscript,  /* mp_ass_subscript */
572 };
573
574 static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
575 {
576         PyObject *ret, *dict;
577         PyObject *pystring;
578         
579         /* Include this incase this instance is a subtype of a python class
580          * In these instances we may want to return a function or variable provided by the subtype
581          * */
582
583         if (BPy_StructRNA_CheckExact(self)) {
584                 ret = PyList_New(0);
585         } else {
586                 pystring = PyUnicode_FromString("__dict__");
587                 dict = PyObject_GenericGetAttr((PyObject *)self, pystring);
588                 Py_DECREF(pystring);
589
590                 if (dict==NULL) {
591                         PyErr_Clear();
592                         ret = PyList_New(0);
593                 }
594                 else {
595                         ret = PyDict_Keys(dict);
596                         Py_DECREF(dict);
597                 }
598         }
599         
600         /* Collect RNA items*/
601         {
602                 PropertyRNA *iterprop, *nameprop;
603                 CollectionPropertyIterator iter;
604                 char name[256], *nameptr;
605
606                 iterprop= RNA_struct_iterator_property(&self->ptr);
607                 RNA_property_collection_begin(&self->ptr, iterprop, &iter);
608
609                 for(; iter.valid; RNA_property_collection_next(&iter)) {
610                         if(iter.ptr.data && (nameprop = RNA_struct_name_property(&iter.ptr))) {
611                                 nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name));
612                                 
613                                 pystring = PyUnicode_FromString(nameptr);
614                                 PyList_Append(ret, pystring);
615                                 Py_DECREF(pystring);
616                                 
617                                 if ((char *)&name != nameptr)
618                                         MEM_freeN(nameptr);
619                         }
620                 }
621                 
622                 RNA_property_collection_end(&iter);
623         }
624         
625         return ret;
626 }
627
628
629 //---------------getattr--------------------------------------------
630 static PyObject *pyrna_struct_getattro( BPy_StructRNA * self, PyObject *pyname )
631 {
632         char *name = _PyUnicode_AsString(pyname);
633         PyObject *ret;
634         PropertyRNA *prop;
635         
636         /* Include this incase this instance is a subtype of a python class
637          * In these instances we may want to return a function or variable provided by the subtype
638          * 
639          * Also needed to return methods when its not a subtype
640          * */
641         ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
642         if (ret)        return ret;
643         else            PyErr_Clear();
644         /* done with subtypes */
645
646         prop = RNA_struct_find_property(&self->ptr, name);
647         
648         if (prop) {
649                 ret = pyrna_prop_to_py(&self->ptr, prop);
650         }
651         else if (/*self->ptr.type == &RNA_Context*/0) {
652                 PointerRNA newptr;
653                 ListBase newlb;
654
655                 CTX_data_get(self->ptr.data, name, &newptr, &newlb);
656
657         if (newptr.data) {
658             ret = pyrna_struct_CreatePyObject(&newptr);
659                 }
660                 else if (newlb.first) {
661                         CollectionPointerLink *link;
662                         PyObject *linkptr;
663
664                         ret = PyList_New(0);
665
666                         for(link=newlb.first; link; link=link->next) {
667                                 linkptr= pyrna_struct_CreatePyObject(&link->ptr);
668                                 PyList_Append(ret, linkptr);
669                                 Py_DECREF(linkptr);
670                         }
671                 }
672         else {
673             ret = Py_None;
674             Py_INCREF(ret);
675         }
676
677                 BLI_freelistN(&newlb);
678         }
679         else {
680                 PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
681                 ret = NULL;
682         }
683         
684         return ret;
685 }
686
687 //--------------- setattr-------------------------------------------
688 static int pyrna_struct_setattro( BPy_StructRNA * self, PyObject *pyname, PyObject * value )
689 {
690         char *name = _PyUnicode_AsString(pyname);
691         PropertyRNA *prop = RNA_struct_find_property(&self->ptr, name);
692         
693         if (prop==NULL) {
694                 if (!BPy_StructRNA_CheckExact(self) && PyObject_GenericSetAttr((PyObject *)self, pyname, value) >= 0) {
695                         return 0;
696                 }
697                 else {
698                         PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
699                         return -1;
700                 }
701         }               
702         
703         if (!RNA_property_editable(&self->ptr, prop)) {
704                 PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(&self->ptr, prop), RNA_struct_identifier(&self->ptr) );
705                 return -1;
706         }
707                 
708         /* pyrna_py_to_prop sets its own exceptions */
709         return pyrna_py_to_prop(&self->ptr, prop, value);
710 }
711
712 PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
713 {
714         PyObject *ret;
715         if (RNA_property_type(&self->ptr, self->prop) != PROP_COLLECTION) {
716                 PyErr_SetString( PyExc_TypeError, "keys() is only valid for collection types" );
717                 ret = NULL;
718         } else {
719                 PyObject *item;
720                 CollectionPropertyIterator iter;
721                 PropertyRNA *nameprop;
722                 char name[256], *nameptr;
723
724                 ret = PyList_New(0);
725                 
726                 RNA_property_collection_begin(&self->ptr, self->prop, &iter);
727                 for(; iter.valid; RNA_property_collection_next(&iter)) {
728                         if(iter.ptr.data && (nameprop = RNA_struct_name_property(&iter.ptr))) {
729                                 nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name));                                
730                                 
731                                 /* add to python list */
732                                 item = PyUnicode_FromString( nameptr );
733                                 PyList_Append(ret, item);
734                                 Py_DECREF(item);
735                                 /* done */
736                                 
737                                 if ((char *)&name != nameptr)
738                                         MEM_freeN(nameptr);
739                         }
740                 }
741                 RNA_property_collection_end(&iter);
742         }
743         
744         return ret;
745 }
746
747 PyObject *pyrna_prop_items(BPy_PropertyRNA *self)
748 {
749         PyObject *ret;
750         if (RNA_property_type(&self->ptr, self->prop) != PROP_COLLECTION) {
751                 PyErr_SetString( PyExc_TypeError, "items() is only valid for collection types" );
752                 ret = NULL;
753         } else {
754                 PyObject *item;
755                 CollectionPropertyIterator iter;
756                 PropertyRNA *nameprop;
757                 char name[256], *nameptr;
758
759                 ret = PyList_New(0);
760                 
761                 RNA_property_collection_begin(&self->ptr, self->prop, &iter);
762                 for(; iter.valid; RNA_property_collection_next(&iter)) {
763                         if(iter.ptr.data && (nameprop = RNA_struct_name_property(&iter.ptr))) {
764                                 nameptr= RNA_property_string_get_alloc(&iter.ptr, nameprop, name, sizeof(name));
765                                 
766                                 /* add to python list */
767                                 item = Py_BuildValue("(NN)", PyUnicode_FromString( nameptr ), pyrna_struct_CreatePyObject(&iter.ptr));
768                                 PyList_Append(ret, item);
769                                 Py_DECREF(item);
770                                 /* done */
771                                 
772                                 if ((char *)&name != nameptr)
773                                         MEM_freeN(nameptr);
774                         }
775                 }
776                 RNA_property_collection_end(&iter);
777         }
778         
779         return ret;
780 }
781
782
783 PyObject *pyrna_prop_values(BPy_PropertyRNA *self)
784 {
785         PyObject *ret;
786         if (RNA_property_type(&self->ptr, self->prop) != PROP_COLLECTION) {
787                 PyErr_SetString( PyExc_TypeError, "values() is only valid for collection types" );
788                 ret = NULL;
789         } else {
790                 PyObject *item;
791                 CollectionPropertyIterator iter;
792                 PropertyRNA *nameprop;
793                 
794                 ret = PyList_New(0);
795                 
796                 RNA_property_collection_begin(&self->ptr, self->prop, &iter);
797                 for(; iter.valid; RNA_property_collection_next(&iter)) {
798                         if(iter.ptr.data && (nameprop = RNA_struct_name_property(&iter.ptr))) {
799                                 item = pyrna_struct_CreatePyObject(&iter.ptr);
800                                 PyList_Append(ret, item);
801                                 Py_DECREF(item);
802                         }
803                 }
804                 RNA_property_collection_end(&iter);
805         }
806         
807         return ret;
808 }
809
810 /* A bit of a kludge, make a list out of a collection or array,
811  * then return the lists iter function, not especially fast but convenient for now */
812 PyObject *pyrna_prop_iter(BPy_PropertyRNA *self)
813 {
814         /* Try get values from a collection */
815         PyObject *ret = pyrna_prop_values(self);
816         
817         if (ret==NULL) {
818                 /* collection did not work, try array */
819                 int len = RNA_property_array_length(&self->ptr, self->prop);
820                 
821                 if (len) {
822                         int i;
823                         PyErr_Clear();
824                         ret = PyList_New(len);
825                         
826                         for (i=0; i < len; i++) {
827                                 PyList_SET_ITEM(ret, i, pyrna_prop_to_py_index(&self->ptr, self->prop, i));
828                         }
829                 }
830         }
831         
832         if (ret) {
833                 /* we know this is a list so no need to PyIter_Check */
834                 PyObject *iter = PyObject_GetIter(ret); 
835                 Py_DECREF(ret);
836                 return iter;
837         }
838         
839         PyErr_SetString( PyExc_TypeError, "this BPy_PropertyRNA object is not iterable" );
840         return NULL;
841 }
842
843 static struct PyMethodDef pyrna_struct_methods[] = {
844         {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, ""},
845         {NULL, NULL, 0, NULL}
846 };
847
848 static struct PyMethodDef pyrna_prop_methods[] = {
849         {"keys", (PyCFunction)pyrna_prop_keys, METH_NOARGS, ""},
850         {"items", (PyCFunction)pyrna_prop_items, METH_NOARGS, ""},
851         {"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, ""},
852         {NULL, NULL, 0, NULL}
853 };
854
855 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
856  * todo - also accept useful args */
857 static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
858
859         BPy_StructRNA *base = NULL;
860         
861         if (!PyArg_ParseTuple(args, "O!:Base BPy_StructRNA", &pyrna_struct_Type, &base))
862                 return NULL;
863         
864         if (type == &pyrna_struct_Type) {
865                 return pyrna_struct_CreatePyObject(&base->ptr);
866         } else {
867                 BPy_StructRNA *ret = (BPy_StructRNA *) type->tp_alloc(type, 0);
868                 ret->ptr = base->ptr;
869                 return (PyObject *)ret;
870         }
871 }
872
873 /* only needed for subtyping, so a new class gets a valid BPy_StructRNA
874  * todo - also accept useful args */
875 static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
876
877         BPy_PropertyRNA *base = NULL;
878         
879         if (!PyArg_ParseTuple(args, "O!:Base BPy_PropertyRNA", &pyrna_prop_Type, &base))
880                 return NULL;
881         
882         if (type == &pyrna_prop_Type) {
883                 return pyrna_prop_CreatePyObject(&base->ptr, base->prop);
884         } else {
885                 BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
886                 ret->ptr = base->ptr;
887                 ret->prop = base->prop;
888                 return (PyObject *)ret;
889         }
890 }
891
892 /*-----------------------BPy_StructRNA method def------------------------------*/
893 PyTypeObject pyrna_struct_Type = {
894 #if (PY_VERSION_HEX >= 0x02060000)
895         PyVarObject_HEAD_INIT(NULL, 0)
896 #else
897         /* python 2.5 and below */
898         PyObject_HEAD_INIT( NULL )  /* required py macro */
899         0,                          /* ob_size */
900 #endif
901         "StructRNA",                    /* tp_name */
902         sizeof( BPy_StructRNA ),        /* tp_basicsize */
903         0,                      /* tp_itemsize */
904         /* methods */
905         ( destructor ) pyrna_struct_dealloc,/* tp_dealloc */
906         NULL,                       /* printfunc tp_print; */
907         NULL,                                           /* getattrfunc tp_getattr; */
908         NULL,                                           /* setattrfunc tp_setattr; */
909         NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
910         ( reprfunc ) pyrna_struct_repr, /* tp_repr */
911
912         /* Method suites for standard classes */
913
914         NULL,                       /* PyNumberMethods *tp_as_number; */
915         NULL,                                           /* PySequenceMethods *tp_as_sequence; */
916         NULL,                                           /* PyMappingMethods *tp_as_mapping; */
917
918         /* More standard operations (here for binary compatibility) */
919
920         ( hashfunc )pyrna_struct_hash,  /* hashfunc tp_hash; */
921         NULL,                                           /* ternaryfunc tp_call; */
922         NULL,                       /* reprfunc tp_str; */
923         ( getattrofunc ) pyrna_struct_getattro, /* getattrofunc tp_getattro; */
924         ( setattrofunc ) pyrna_struct_setattro, /* setattrofunc tp_setattro; */
925
926         /* Functions to access object as input/output buffer */
927         NULL,                       /* PyBufferProcs *tp_as_buffer; */
928
929   /*** Flags to define presence of optional/expanded features ***/
930         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
931
932         NULL,                                           /*  char *tp_doc;  Documentation string */
933   /*** Assigned meaning in release 2.0 ***/
934         /* call function for all accessible objects */
935         NULL,                       /* traverseproc tp_traverse; */
936
937         /* delete references to contained objects */
938         NULL,                       /* inquiry tp_clear; */
939
940   /***  Assigned meaning in release 2.1 ***/
941   /*** rich comparisons ***/
942         (richcmpfunc)pyrna_struct_richcmp,      /* richcmpfunc tp_richcompare; */
943
944   /***  weak reference enabler ***/
945         0,                          /* long tp_weaklistoffset; */
946
947   /*** Added in release 2.2 ***/
948         /*   Iterators */
949         NULL,                       /* getiterfunc tp_iter; */
950         NULL,                       /* iternextfunc tp_iternext; */
951
952   /*** Attribute descriptor and subclassing stuff ***/
953         pyrna_struct_methods,                   /* struct PyMethodDef *tp_methods; */
954         NULL,                       /* struct PyMemberDef *tp_members; */
955         NULL,                                           /* struct PyGetSetDef *tp_getset; */
956         NULL,                       /* struct _typeobject *tp_base; */
957         NULL,                       /* PyObject *tp_dict; */
958         NULL,                       /* descrgetfunc tp_descr_get; */
959         NULL,                       /* descrsetfunc tp_descr_set; */
960         0,                          /* long tp_dictoffset; */
961         NULL,                       /* initproc tp_init; */
962         NULL,                       /* allocfunc tp_alloc; */
963         pyrna_struct_new,                       /* newfunc tp_new; */
964         /*  Low-level free-memory routine */
965         NULL,                       /* freefunc tp_free;  */
966         /* For PyObject_IS_GC */
967         NULL,                       /* inquiry tp_is_gc;  */
968         NULL,                       /* PyObject *tp_bases; */
969         /* method resolution order */
970         NULL,                       /* PyObject *tp_mro;  */
971         NULL,                       /* PyObject *tp_cache; */
972         NULL,                       /* PyObject *tp_subclasses; */
973         NULL,                       /* PyObject *tp_weaklist; */
974         NULL
975 };
976
977 /*-----------------------BPy_PropertyRNA method def------------------------------*/
978 PyTypeObject pyrna_prop_Type = {
979 #if (PY_VERSION_HEX >= 0x02060000)
980         PyVarObject_HEAD_INIT(NULL, 0)
981 #else
982         /* python 2.5 and below */
983         PyObject_HEAD_INIT( NULL )  /* required py macro */
984         0,                          /* ob_size */
985 #endif
986         
987         "PropertyRNA",          /* tp_name */
988         sizeof( BPy_PropertyRNA ),                      /* tp_basicsize */
989         0,                      /* tp_itemsize */
990         /* methods */
991         NULL,                                           /* tp_dealloc */
992         NULL,                       /* printfunc tp_print; */
993         NULL,                                           /* getattrfunc tp_getattr; */
994         NULL,                       /* setattrfunc tp_setattr; */
995         NULL,                                           /* tp_compare */ /* DEPRECATED in python 3.0! */
996         ( reprfunc ) pyrna_prop_repr,   /* tp_repr */
997
998         /* Method suites for standard classes */
999
1000         NULL,                       /* PyNumberMethods *tp_as_number; */
1001         NULL,                                           /* PySequenceMethods *tp_as_sequence; */
1002         &pyrna_prop_as_mapping,         /* PyMappingMethods *tp_as_mapping; */
1003
1004         /* More standard operations (here for binary compatibility) */
1005
1006         NULL,                                           /* hashfunc tp_hash; */
1007         NULL,                       /* ternaryfunc tp_call; */
1008         NULL,                       /* reprfunc tp_str; */
1009         NULL, /*PyObject_GenericGetAttr - MINGW Complains, assign later */      /* getattrofunc tp_getattro; */ /* will only use these if this is a subtype of a py class */
1010         NULL, /*PyObject_GenericSetAttr - MINGW Complains, assign later */      /* setattrofunc tp_setattro; */
1011
1012         /* Functions to access object as input/output buffer */
1013         NULL,                       /* PyBufferProcs *tp_as_buffer; */
1014
1015   /*** Flags to define presence of optional/expanded features ***/
1016         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
1017
1018         NULL,                                           /*  char *tp_doc;  Documentation string */
1019   /*** Assigned meaning in release 2.0 ***/
1020         /* call function for all accessible objects */
1021         NULL,                       /* traverseproc tp_traverse; */
1022
1023         /* delete references to contained objects */
1024         NULL,                       /* inquiry tp_clear; */
1025
1026   /***  Assigned meaning in release 2.1 ***/
1027   /*** rich comparisons ***/
1028         (richcmpfunc)pyrna_prop_richcmp,        /* richcmpfunc tp_richcompare; */
1029
1030   /***  weak reference enabler ***/
1031         0,                          /* long tp_weaklistoffset; */
1032
1033   /*** Added in release 2.2 ***/
1034         /*   Iterators */
1035         (getiterfunc)pyrna_prop_iter,   /* getiterfunc tp_iter; */
1036         NULL,                       /* iternextfunc tp_iternext; */
1037
1038   /*** Attribute descriptor and subclassing stuff ***/
1039         pyrna_prop_methods,                     /* struct PyMethodDef *tp_methods; */
1040         NULL,                       /* struct PyMemberDef *tp_members; */
1041         NULL,                                           /* struct PyGetSetDef *tp_getset; */
1042         NULL,                       /* struct _typeobject *tp_base; */
1043         NULL,                       /* PyObject *tp_dict; */
1044         NULL,                       /* descrgetfunc tp_descr_get; */
1045         NULL,                       /* descrsetfunc tp_descr_set; */
1046         0,                          /* long tp_dictoffset; */
1047         NULL,                       /* initproc tp_init; */
1048         NULL,                       /* allocfunc tp_alloc; */
1049         pyrna_prop_new,                         /* newfunc tp_new; */
1050         /*  Low-level free-memory routine */
1051         NULL,                       /* freefunc tp_free;  */
1052         /* For PyObject_IS_GC */
1053         NULL,                       /* inquiry tp_is_gc;  */
1054         NULL,                       /* PyObject *tp_bases; */
1055         /* method resolution order */
1056         NULL,                       /* PyObject *tp_mro;  */
1057         NULL,                       /* PyObject *tp_cache; */
1058         NULL,                       /* PyObject *tp_subclasses; */
1059         NULL,                       /* PyObject *tp_weaklist; */
1060         NULL
1061 };
1062
1063 PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
1064 {
1065         PyObject *newclass = NULL;
1066         PropertyRNA *nameprop;
1067
1068         if (ptr->type==NULL) {
1069                 newclass= NULL; /* Nothing to do */
1070         } else if ((newclass= BPy_RNA_PYTYPE(ptr->data))) {
1071                 Py_INCREF(newclass);
1072         } else if ((nameprop = RNA_struct_name_property(ptr))) {
1073                 /* for now, return the base RNA type rather then a real module */
1074                 
1075                 /* Assume BPy_RNA_PYTYPE(ptr->data) was alredy checked */
1076                 
1077                 /* subclass equivelents
1078                 - class myClass(myBase):
1079                         some='value' # or ...
1080                 - myClass = type(name='myClass', bases=(myBase,), dict={'some':'value'})
1081                 */
1082                 char name[256], *nameptr;
1083
1084                 PyObject *args = PyTuple_New(3);
1085                 PyObject *bases = PyTuple_New(1);
1086                 PyObject *dict = PyDict_New();
1087                 
1088                 nameptr= RNA_property_string_get_alloc(ptr, nameprop, name, sizeof(name));
1089                 
1090                 // arg 1
1091                 //PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(tp_name));
1092                 PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(nameptr));
1093                 
1094                 // arg 2
1095                 PyTuple_SET_ITEM(bases, 0, (PyObject *)&pyrna_struct_Type);
1096                 Py_INCREF(&pyrna_struct_Type);
1097
1098                 PyTuple_SET_ITEM(args, 1, bases);
1099                 
1100                 // arg 3 - add an instance of the rna 
1101                 PyTuple_SET_ITEM(args, 2, dict); // fill with useful subclass things!
1102                 
1103                 if (PyErr_Occurred()) {
1104                         PyErr_Print();
1105                         PyErr_Clear();
1106                 }
1107                 
1108                 newclass = PyObject_CallObject((PyObject *)&PyType_Type, args);
1109                 // Set this later
1110                 
1111
1112                 if (newclass) {
1113                         PyObject *rna;
1114                         BPy_RNA_PYTYPE(ptr->data) = (void *)newclass; /* Store for later use */
1115
1116                         /* Not 100% needed but useful,
1117                          * having an instance within a type looks wrong however this instance IS an rna type */
1118                         rna = pyrna_struct_CreatePyObject(ptr);
1119                         PyDict_SetItemString(((PyTypeObject *)newclass)->tp_dict, "__rna__", rna);
1120                         Py_DECREF(rna);
1121                         /* done with rna instance */
1122                 }
1123                 
1124                 Py_DECREF(args);
1125                 
1126                 if ((char *)&name != nameptr)
1127                         MEM_freeN(nameptr);
1128                 
1129         }
1130         
1131         return newclass;
1132 }
1133
1134 /*-----------------------CreatePyObject---------------------------------*/
1135 PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
1136 {
1137         BPy_StructRNA *pyrna= NULL;
1138         int tp_init= 0;
1139         
1140         if (ptr->data==NULL && ptr->type==NULL) { /* Operator RNA has NULL data */
1141                 Py_RETURN_NONE;
1142         }
1143         
1144         if (ptr->type == &RNA_Struct) { /* always return a python subtype from rna struct types */
1145                 PyTypeObject *tp = pyrna_struct_Subtype(ptr);
1146                 
1147                 if (tp) {
1148                         pyrna = (BPy_StructRNA *) tp->tp_alloc(tp, 0);
1149                 }
1150                 else {
1151                         fprintf(stderr, "Could not make type\n");
1152                         pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
1153                 }
1154         }
1155         else {
1156                 pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
1157         }
1158         
1159         if( !pyrna ) {
1160                 PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_StructRNA object" );
1161                 return NULL;
1162         }
1163         
1164         pyrna->ptr= *ptr;
1165         pyrna->freeptr= 0;
1166         return ( PyObject * ) pyrna;
1167 }
1168
1169 PyObject *pyrna_prop_CreatePyObject( PointerRNA *ptr, PropertyRNA *prop )
1170 {
1171         BPy_PropertyRNA *pyrna;
1172
1173         pyrna = ( BPy_PropertyRNA * ) PyObject_NEW( BPy_PropertyRNA, &pyrna_prop_Type );
1174
1175         if( !pyrna ) {
1176                 PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_rna object" );
1177                 return NULL;
1178         }
1179         
1180         pyrna->ptr = *ptr;
1181         pyrna->prop = prop;
1182                 
1183         return ( PyObject * ) pyrna;
1184 }
1185
1186
1187 PyObject *BPY_rna_module( void )
1188 {
1189         PointerRNA ptr;
1190         
1191         /* This can't be set in the pytype struct because some compilers complain */
1192         pyrna_prop_Type.tp_getattro = PyObject_GenericGetAttr; 
1193         pyrna_prop_Type.tp_setattro = PyObject_GenericSetAttr; 
1194         
1195         if( PyType_Ready( &pyrna_struct_Type ) < 0 )
1196                 return NULL;
1197         
1198         if( PyType_Ready( &pyrna_prop_Type ) < 0 )
1199                 return NULL;
1200
1201         
1202         /* for now, return the base RNA type rather then a real module */
1203         RNA_main_pointer_create(G.main, &ptr);
1204         
1205         return pyrna_struct_CreatePyObject(&ptr);
1206 }
1207
1208 #if 0
1209 /* This is a way we can access docstrings for RNA types
1210  * without having the datatypes in blender */
1211 PyObject *BPY_rna_doc( void )
1212 {
1213         PointerRNA ptr;
1214         
1215         /* for now, return the base RNA type rather then a real module */
1216         RNA_blender_rna_pointer_create(&ptr);
1217         
1218         return pyrna_struct_CreatePyObject(&ptr);
1219 }
1220 #endif
1221
1222
1223 /* pyrna_basetype_* - BPy_BaseTypeRNA is just a BPy_PropertyRNA struct with a differnt type
1224  * the self->ptr and self->prop are always set to the "structs" collection */
1225 //---------------getattr--------------------------------------------
1226 static PyObject *pyrna_basetype_getattro( BPy_BaseTypeRNA * self, PyObject *pyname )
1227 {
1228         PointerRNA newptr;
1229         PyObject *ret;
1230         
1231         ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
1232         if (ret)        return ret;
1233         else            PyErr_Clear();
1234         
1235         if (RNA_property_collection_lookup_string(&self->ptr, self->prop, _PyUnicode_AsString(pyname), &newptr)) {
1236                 return pyrna_struct_Subtype(&newptr);
1237         }
1238         else { /* Override the error */
1239                 PyErr_Format(PyExc_AttributeError, "bpy.types.%s not a valid RNA_Struct", _PyUnicode_AsString(pyname));
1240                 return NULL;
1241         }
1242 }
1243
1244 static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self);
1245 static struct PyMethodDef pyrna_basetype_methods[] = {
1246         {"__dir__", (PyCFunction)pyrna_basetype_dir, METH_NOARGS, ""},
1247         {NULL, NULL, 0, NULL}
1248 };
1249
1250 static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
1251 {
1252         PyObject *list, *name;
1253         PyMethodDef *meth;
1254         
1255         list= pyrna_prop_keys(self); /* like calling structs.keys(), avoids looping here */
1256
1257         for(meth=pyrna_basetype_methods; meth->ml_name; meth++) {
1258                 name = PyUnicode_FromString(meth->ml_name);
1259                 PyList_Append(list, name);
1260                 Py_DECREF(name);
1261         }
1262         
1263         return list;
1264 }
1265
1266 PyTypeObject pyrna_basetype_Type = {NULL};
1267
1268 PyObject *BPY_rna_types(void)
1269 {
1270         BPy_BaseTypeRNA *self;
1271         pyrna_basetype_Type.tp_name = "RNA_Types";
1272         pyrna_basetype_Type.tp_basicsize = sizeof( BPy_BaseTypeRNA );
1273         pyrna_basetype_Type.tp_getattro = ( getattrofunc )pyrna_basetype_getattro;
1274         pyrna_basetype_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1275         pyrna_basetype_Type.tp_methods = pyrna_basetype_methods;
1276         
1277         if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
1278                 return NULL;
1279         
1280         self= (BPy_BaseTypeRNA *)PyObject_NEW( BPy_BaseTypeRNA, &pyrna_basetype_Type );
1281         
1282         /* avoid doing this lookup for every getattr */
1283         RNA_blender_rna_pointer_create(&self->ptr);
1284         self->prop = RNA_struct_find_property(&self->ptr, "structs");
1285         
1286         return (PyObject *)self;
1287 }
1288
1289
1290
1291 /* Orphan functions, not sure where they should go */
1292
1293 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
1294  * This isnt incorrect since its a python object - but be careful */
1295 PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
1296 {
1297         static char *kwlist[] = {"attr", "name", "description", "min", "max", "soft_min", "soft_max", "default", NULL};
1298         char *id, *name="", *description="";
1299         float min=FLT_MIN, max=FLT_MAX, soft_min=FLT_MIN, soft_max=FLT_MAX, def=0.0f;
1300         
1301         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssfffff:FloatProperty", kwlist, &id, &name, &description, &min, &max, &soft_min, &soft_max, &def))
1302                 return NULL;
1303         
1304         if (PyTuple_Size(args) > 0) {
1305                 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
1306                 return NULL;
1307         }
1308         
1309         if (self) {
1310                 StructRNA *srna = PyCObject_AsVoidPtr(self);
1311                 RNA_def_float(srna, id, def, min, max, name, description, soft_min, soft_max);
1312                 Py_RETURN_NONE;
1313         } else {
1314                 PyObject *ret = PyTuple_New(2);
1315                 PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_FloatProperty, NULL));
1316                 PyTuple_SET_ITEM(ret, 1, kw);
1317                 Py_INCREF(kw);
1318                 return ret;
1319         }
1320 }
1321
1322 PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
1323 {
1324         static char *kwlist[] = {"attr", "name", "description", "min", "max", "soft_min", "soft_max", "default", NULL};
1325         char *id, *name="", *description="";
1326         int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, def=0;
1327         
1328         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiii:IntProperty", kwlist, &id, &name, &description, &min, &max, &soft_min, &soft_max, &def))
1329                 return NULL;
1330         
1331         if (PyTuple_Size(args) > 0) {
1332                 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
1333                 return NULL;
1334         }
1335         
1336         if (self) {
1337                 StructRNA *srna = PyCObject_AsVoidPtr(self);
1338                 RNA_def_int(srna, id, def, min, max, name, description, soft_min, soft_max);
1339                 Py_RETURN_NONE;
1340         } else {
1341                 PyObject *ret = PyTuple_New(2);
1342                 PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_IntProperty, NULL));
1343                 PyTuple_SET_ITEM(ret, 1, kw);
1344                 Py_INCREF(kw);
1345                 return ret;
1346         }
1347 }
1348
1349 PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
1350 {
1351         static char *kwlist[] = {"attr", "name", "description", "default", NULL};
1352         char *id, *name="", *description="";
1353         int def=0;
1354         
1355         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssi:IntProperty", kwlist, &id, &name, &description, &def))
1356                 return NULL;
1357         
1358         if (PyTuple_Size(args) > 0) {
1359                 PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
1360                 return NULL;
1361         }
1362         
1363         if (self) {
1364                 StructRNA *srna = PyCObject_AsVoidPtr(self);
1365                 RNA_def_boolean(srna, id, def, name, description);
1366                 Py_RETURN_NONE;
1367         } else {
1368                 PyObject *ret = PyTuple_New(2);
1369                 PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_IntProperty, NULL));
1370                 PyTuple_SET_ITEM(ret, 1, kw);
1371                 Py_INCREF(kw);
1372                 return ret;
1373         }
1374 }