PyRNA structs and properties can now be subtyped to add functionality in python.
authorCampbell Barton <ideasman42@gmail.com>
Mon, 1 Dec 2008 16:59:18 +0000 (16:59 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 1 Dec 2008 16:59:18 +0000 (16:59 +0000)
rna_actuator.c was missing an enum

source/blender/makesrna/intern/rna_actuator.c
source/blender/python/intern/bpy_interface.c
source/blender/python/intern/bpy_rna.c
source/blender/python/intern/bpy_rna.h

index e044da24943d7ce8c9b12335b2e99c530a358760..b851612345214ab3309a840dfc081c0fb6426af1 100644 (file)
@@ -52,6 +52,7 @@ void RNA_def_actuator(BlenderRNA *brna)
                {ACT_SCENE, "SCENE", "Scene", ""},
                {ACT_RANDOM, "RANDOM", "Random", ""},
                {ACT_MESSAGE, "MESSAGE", "Message", ""},
+               {ACT_ACTION, "ACTION", "Action", ""},
                {ACT_CD, "CD", "CD", ""},
                {ACT_GAME, "GAME", "Game", ""},
                {ACT_VISIBILITY, "VISIBILITY", "Visibility", ""},
index 6cce80c2e75b500c6b557e86b8ca4e16b9eefeb7..8ae7dcb5c88a3593be7be089ea196b0fb0f29a3c 100644 (file)
@@ -22,7 +22,7 @@ static PyObject *CreateGlobalDictionary( void )
        
        /* Add Modules */
        item = BPY_rna_module();
-       PyDict_SetItemString( dict, "rna", item );
+       PyDict_SetItemString( dict, "bpy", item );
        Py_DECREF(item);
        
        
index 45a0aeaa490fb3432455ea6bafb38e31e13d7cd7..d6df4645be0a1c55b80e96c7af1620de5b9318dd 100644 (file)
@@ -30,6 +30,8 @@
 #include "MEM_guardedalloc.h"
 #include "BKE_global.h" /* evil G.* */
 
+/* floats bigger then this are displayed as inf in the docstrings */
+#define MAXFLOAT_DOC 10000000
 
 static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
 { 
@@ -100,7 +102,7 @@ static PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
                if (i<totitem) {
                        ret = PyUnicode_FromString( item[i].identifier );
                } else {
-                       PyErr_SetString(PyExc_AttributeError, "enum not found");
+                       PyErr_Format(PyExc_AttributeError, "enum \"%d\" not found", val);
                        ret = NULL;
                }
                
@@ -597,11 +599,11 @@ PyObject *pyrna_struct_to_docstring(BPy_StructRNA *self)
                                
                                BLI_dynstr_appendf(dynstr, "@ivar %s: %s in (", identifier, desc);
                                
-                               if (hardmin < -9999999) BLI_dynstr_append(dynstr, "-inf, ");
-                               else                                    BLI_dynstr_appendf(dynstr, "%.3f, ", hardmin);
+                               if (hardmin < -MAXFLOAT_DOC)BLI_dynstr_append(dynstr, "-inf, ");
+                               else                                            BLI_dynstr_appendf(dynstr, "%.3f, ", hardmin);
                                                
-                               if (hardmax >  9999999) BLI_dynstr_append(dynstr, "inf");
-                               else                                    BLI_dynstr_appendf(dynstr, "%.3f", hardmax);
+                               if (hardmax >  MAXFLOAT_DOC)BLI_dynstr_append(dynstr, "inf");
+                               else                                            BLI_dynstr_appendf(dynstr, "%.3f", hardmax);
                                
                                BLI_dynstr_appendf(dynstr, ")%s\n", readonly);
                                                
@@ -676,11 +678,20 @@ PyObject *pyrna_struct_to_docstring(BPy_StructRNA *self)
 
 
 //---------------getattr--------------------------------------------
-static PyObject *pyrna_struct_getattr( BPy_StructRNA * self, char *name )
+static PyObject *pyrna_struct_getattro( BPy_StructRNA * self, PyObject *pyname )
 {
+       char *name = _PyUnicode_AsString(pyname);
        PyObject *ret;
        PropertyRNA *prop;
        
+       /* Include this incase this instance is a subtype of a python class
+        * In these instances we may want to return a function or variable provided by the subtype
+        * */
+       ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
+       if (ret)        return ret;
+       else            PyErr_Clear();
+       /* done with subtypes */
+       
        if( strcmp( name, "__members__" ) == 0 ) {
                PyObject *item;
                
@@ -718,8 +729,9 @@ static PyObject *pyrna_struct_getattr( BPy_StructRNA * self, char *name )
 }
 
 //--------------- setattr-------------------------------------------
-static int pyrna_struct_setattr( BPy_StructRNA * self, char *name, PyObject * value )
+static int pyrna_struct_setattro( BPy_StructRNA * self, PyObject *pyname, PyObject * value )
 {
+       char *name = _PyUnicode_AsString(pyname);
        PropertyRNA *prop = RNA_struct_find_property(&self->ptr, name);
        
        if (prop==NULL) {
@@ -849,6 +861,43 @@ static struct PyMethodDef pyrna_prop_methods[] = {
        {NULL, NULL, 0, NULL}
 };
 
+/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
+ * todo - also accept useful args */
+static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+
+       BPy_StructRNA *base = NULL;
+       
+       if (!PyArg_ParseTuple(args, "O!:Base BPy_StructRNA", &pyrna_struct_Type, &base))
+               return NULL;
+       
+       if (type == &pyrna_struct_Type) {
+               return pyrna_struct_CreatePyObject(&base->ptr);
+       } else {
+               BPy_StructRNA *ret = (BPy_StructRNA *) type->tp_alloc(type, 0);
+               ret->ptr = base->ptr;
+               return (PyObject *)ret;
+       }
+}
+
+/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
+ * todo - also accept useful args */
+static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+
+       BPy_PropertyRNA *base = NULL;
+       
+       if (!PyArg_ParseTuple(args, "O!:Base BPy_PropertyRNA", &pyrna_prop_Type, &base))
+               return NULL;
+       
+       if (type == &pyrna_prop_Type) {
+               return pyrna_prop_CreatePyObject(&base->ptr, base->prop);
+       } else {
+               BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
+               ret->ptr = base->ptr;
+               ret->prop = base->prop;
+               return (PyObject *)ret;
+       }
+}
+
 /*-----------------------BPy_StructRNA method def------------------------------*/
 PyTypeObject pyrna_struct_Type = {
 #if (PY_VERSION_HEX >= 0x02060000)
@@ -864,8 +913,8 @@ PyTypeObject pyrna_struct_Type = {
        /* methods */
        NULL,                                           /* tp_dealloc */
        NULL,                       /* printfunc tp_print; */
-       ( getattrfunc ) pyrna_struct_getattr,           /* getattrfunc tp_getattr; */
-       ( setattrfunc ) pyrna_struct_setattr,           /* setattrfunc tp_setattr; */
+       NULL,                                           /* getattrfunc tp_getattr; */
+       NULL,                                           /* setattrfunc tp_setattr; */
        ( cmpfunc ) pyrna_struct_compare,       /* tp_compare */
        ( reprfunc ) pyrna_struct_repr, /* tp_repr */
 
@@ -878,16 +927,16 @@ PyTypeObject pyrna_struct_Type = {
        /* More standard operations (here for binary compatibility) */
 
        NULL,                                           /* hashfunc tp_hash; */
-       NULL,                       /* ternaryfunc tp_call; */
+       NULL,                                           /* ternaryfunc tp_call; */
        NULL,                       /* reprfunc tp_str; */
-       NULL,                       /* getattrofunc tp_getattro; */
-       NULL,                       /* setattrofunc tp_setattro; */
+       ( getattrofunc ) pyrna_struct_getattro, /* getattrofunc tp_getattro; */
+       ( setattrofunc ) pyrna_struct_setattro, /* setattrofunc tp_setattro; */
 
        /* Functions to access object as input/output buffer */
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
   /*** Flags to define presence of optional/expanded features ***/
-       Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
        NULL,                                           /*  char *tp_doc;  Documentation string */
   /*** Assigned meaning in release 2.0 ***/
@@ -920,7 +969,7 @@ PyTypeObject pyrna_struct_Type = {
        0,                          /* long tp_dictoffset; */
        NULL,                       /* initproc tp_init; */
        NULL,                       /* allocfunc tp_alloc; */
-       NULL,                       /* newfunc tp_new; */
+       pyrna_struct_new,                       /* newfunc tp_new; */
        /*  Low-level free-memory routine */
        NULL,                       /* freefunc tp_free;  */
        /* For PyObject_IS_GC */
@@ -966,14 +1015,14 @@ PyTypeObject pyrna_prop_Type = {
        NULL,                                           /* hashfunc tp_hash; */
        NULL,                       /* ternaryfunc tp_call; */
        NULL,                       /* reprfunc tp_str; */
-       NULL,                       /* getattrofunc tp_getattro; */
-       NULL,                       /* setattrofunc tp_setattro; */
+       PyObject_GenericGetAttr,        /* getattrofunc tp_getattro; */ /* will only use these if this is a subtype of a py class */
+       PyObject_GenericSetAttr,        /* setattrofunc tp_setattro; */
 
        /* Functions to access object as input/output buffer */
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
   /*** Flags to define presence of optional/expanded features ***/
-       Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
 
        NULL,                                           /*  char *tp_doc;  Documentation string */
   /*** Assigned meaning in release 2.0 ***/
@@ -1006,7 +1055,7 @@ PyTypeObject pyrna_prop_Type = {
        0,                          /* long tp_dictoffset; */
        NULL,                       /* initproc tp_init; */
        NULL,                       /* allocfunc tp_alloc; */
-       NULL,                       /* newfunc tp_new; */
+       pyrna_prop_new,                         /* newfunc tp_new; */
        /*  Low-level free-memory routine */
        NULL,                       /* freefunc tp_free;  */
        /* For PyObject_IS_GC */
index f5fa5dffb67b8751a66fa6ea9acb78004b058e42..6ceda34268f7f235edc83432cdc612e343897db4 100644 (file)
@@ -29,6 +29,9 @@
 #include "RNA_access.h"
 #include "RNA_types.h"
 
+extern PyTypeObject pyrna_struct_Type;
+extern PyTypeObject pyrna_prop_Type;
+
 typedef struct {
        PyObject_VAR_HEAD /* required python macro   */
        PointerRNA ptr;