[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17665] branches/blender2.5/blender/source /blender: PyRNA structs and properties can now be subtyped to add functionality in python .

Campbell Barton ideasman42 at gmail.com
Mon Dec 1 17:59:18 CET 2008


Revision: 17665
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17665
Author:   campbellbarton
Date:     2008-12-01 17:59:18 +0100 (Mon, 01 Dec 2008)

Log Message:
-----------
PyRNA structs and properties can now be subtyped to add functionality in python.
rna_actuator.c was missing an enum

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_actuator.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_actuator.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_actuator.c	2008-12-01 14:36:14 UTC (rev 17664)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_actuator.c	2008-12-01 16:59:18 UTC (rev 17665)
@@ -52,6 +52,7 @@
 		{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", ""},

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2008-12-01 14:36:14 UTC (rev 17664)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2008-12-01 16:59:18 UTC (rev 17665)
@@ -22,7 +22,7 @@
 	
 	/* Add Modules */
 	item = BPY_rna_module();
-	PyDict_SetItemString( dict, "rna", item );
+	PyDict_SetItemString( dict, "bpy", item );
 	Py_DECREF(item);
 	
 	

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2008-12-01 14:36:14 UTC (rev 17664)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2008-12-01 16:59:18 UTC (rev 17665)
@@ -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 @@
 		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 @@
 				
 				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 @@
 
 
 //---------------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 @@
 }
 
 //--------------- 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 @@
 	{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 @@
 	/* 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 @@
 	/* 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 @@
 	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 @@
 	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 @@
 	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 */

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h	2008-12-01 14:36:14 UTC (rev 17664)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h	2008-12-01 16:59:18 UTC (rev 17665)
@@ -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;





More information about the Bf-blender-cvs mailing list