[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11228] branches/pyapi_devel/source: PyAPI :

Campbell Barton cbarton at metavr.com
Wed Jul 11 12:32:37 CEST 2007


Revision: 11228
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11228
Author:   campbellbarton
Date:     2007-07-11 12:32:37 +0200 (Wed, 11 Jul 2007)

Log Message:
-----------
PyAPI:
made some material settings return a subtyped float that have 2 additions
keyframe() and curve

 material.alpha.keyframe() # adds a keyframe to alpha
 print material.alpha.curve # prints the IPO Curve for this attribute or None.

other attributes may be added later.

its been suggested that it would be better to reimpliment the PyNumber protocol however subtyping has its advantages
- bpy_float.c is only ~130 lines sans header and many parts of our own api dont support PyNumber.

Modified Paths:
--------------
    branches/pyapi_devel/source/blender/python/api2_2x/Blender.c
    branches/pyapi_devel/source/blender/python/api2_2x/Lamp.c
    branches/pyapi_devel/source/blender/python/api2_2x/Material.c
    branches/pyapi_devel/source/blender/python/api2_2x/bpy_list.c
    branches/pyapi_devel/source/blender/python/api2_2x/layer_set.c
    branches/pyapi_devel/source/creator/creator.c

Added Paths:
-----------
    branches/pyapi_devel/source/blender/python/api2_2x/bpy_float.c
    branches/pyapi_devel/source/blender/python/api2_2x/bpy_float.h

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Blender.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Blender.c	2007-07-11 10:29:18 UTC (rev 11227)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Blender.c	2007-07-11 10:32:37 UTC (rev 11228)
@@ -96,6 +96,7 @@
 #include "bpy_list.h"
 #include "layer_set.h"
 #include "color.h"
+#include "bpy_float.h"
 
 /**********************************************************/
 /* Python API function prototypes for the Blender module.	*/
@@ -982,4 +983,5 @@
 	BPyList_Init();
 	LayerSet_Init();
 	Color_Init();
+	BPyFloat_Init();
 }

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Lamp.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Lamp.c	2007-07-11 10:29:18 UTC (rev 11227)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Lamp.c	2007-07-11 10:32:37 UTC (rev 11228)
@@ -187,7 +187,7 @@
 static PyObject *Lamp_getHaloInt( BPy_Lamp * self );
 static PyObject *Lamp_getQuad1( BPy_Lamp * self );
 static PyObject *Lamp_getQuad2( BPy_Lamp * self );
-static BPy_Color *Lamp_getCol( BPy_Lamp * self );
+static PyObject *Lamp_getCol( BPy_Lamp * self );
 static PyObject *Lamp_getIpo( BPy_Lamp * self );
 static PyObject *Lamp_getComponent( BPy_Lamp * self, void * closure );
 static PyObject *Lamp_insertIpoKey( BPy_Lamp * self, PyObject * args );
@@ -803,12 +803,12 @@
 					"couldn't get Lamp.quad2 attribute" ) );
 }
 
-static BPy_Color *Lamp_getCol( BPy_Lamp * self )
+static PyObject *Lamp_getCol( BPy_Lamp * self )
 {
 	if (!self->color)
 		self->color = (BPy_Color *)newColorObject(NULL, 3, 0, 0, (PyObject *)self);
 	
-	return self->color;
+	return (PyObject *)self->color;
 }
 
 static int Lamp_setType( BPy_Lamp * self, PyObject * value )

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Material.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Material.c	2007-07-11 10:29:18 UTC (rev 11227)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Material.c	2007-07-11 10:32:37 UTC (rev 11228)
@@ -56,6 +56,7 @@
 #include "gen_library.h"
 #include "IDProp.h"
 #include "bpy_list.h"
+#include "bpy_float.h"
 /* closure values for getColorComponent()/setColorComponent() */
 
 #define IPOKEY_RGB          0
@@ -844,7 +845,7 @@
 /* Description: Create a new BPy_Material from an  existing */
 /*		 Blender material structure.	 */
 /*****************************************************************************/
-PyObject *Material_CreatePyObject( struct Material *mat )
+PyObject *Material_CreatePyObject( Material *mat )
 {
 	BPy_Material *pymat;
 
@@ -1057,35 +1058,41 @@
 static PyObject *getFloatAttr( BPy_Material *self, void *type )
 {
 	float param;
-	struct Material *mat= self->material;
-
+	Material *mat= self->material;
+	int ipocurve= -1; /* means no ipo */
+	
 	switch( (int)type ) {
 	case EXPP_MAT_ATTR_ADD:
 		param = mat->add;
+		ipocurve = MA_ADD;
 		break;
 	case EXPP_MAT_ATTR_ALPHA:
 		param = mat->alpha;
+		ipocurve = MA_ALPHA;
 		break;
 	case EXPP_MAT_ATTR_SHADOWALPHA:
 		param = mat->shad_alpha;
 		break;
 	case EXPP_MAT_ATTR_AMB:
 		param = mat->amb;
+		ipocurve = MA_AMB;
 		break;
 	case EXPP_MAT_ATTR_EMIT:
 		param = mat->emit;
+		ipocurve = MA_EMIT;
 		break;
 	case EXPP_MAT_ATTR_REF:
 		param = mat->ref;
+		ipocurve = MA_REF;
 		break;
 	case EXPP_MAT_ATTR_SPEC:
 		param = mat->spec;
+		ipocurve = MA_SPEC;
 		break;
 	case EXPP_MAT_ATTR_SPECTRA: 
 		param = mat->spectra;
+		ipocurve = MA_SPTR;
 		break;
-	
-	
 	case EXPP_MAT_ATTR_ROUGHNESS: 
 		param = mat->roughness;
 		break;
@@ -1113,31 +1120,36 @@
 	
 	case EXPP_MAT_ATTR_HALOSIZE:
 		param = mat->hasize;
+		ipocurve = MA_HASIZE;
 		break;
 	case EXPP_MAT_ATTR_FLARESIZE: 
 		param = mat->flaresize;
 		break;
-	case EXPP_MAT_ATTR_FLAREBOOST: 
+	case EXPP_MAT_ATTR_FLAREBOOST:
 		param = mat->flareboost;
 		break;
-	case EXPP_MAT_ATTR_SUBSIZE: 
+	case EXPP_MAT_ATTR_SUBSIZE:
 		param = mat->subsize;
 		break;
 		
-	case EXPP_MAT_ATTR_RAYMIRR: 
+	case EXPP_MAT_ATTR_RAYMIRR:
 		param = mat->ray_mirror;
+		ipocurve = MA_RAYM;
 		break;
 	case EXPP_MAT_ATTR_FRESNELMIRR: 
 		param = mat->fresnel_mir;
+		ipocurve = MA_FRESMIR;
 		break;
 	case EXPP_MAT_ATTR_FRESNELMIRRFAC: 
 		param = mat->fresnel_mir_i;
+		ipocurve = MA_FRESMIRI;
 		break;
 	case EXPP_MAT_ATTR_FILTER: 
 		param = mat->filter;
 		break;
 	case EXPP_MAT_ATTR_TRANSLUCENCY: 
 		param = mat->translucency;
+		ipocurve = MA_TRANSLU;
 		break;
 	case EXPP_MAT_ATTR_ZOFFS:
 		param = mat->zoffs;
@@ -1147,9 +1159,11 @@
 		break;
 	case EXPP_MAT_ATTR_FRESNELTRANS: 
 		param = mat->fresnel_tra;
+		ipocurve = MA_FRESTRA;
 		break;
 	case EXPP_MAT_ATTR_FRESNELTRANSFAC: 
 		param = mat->fresnel_tra_i;
+		ipocurve = MA_FRESTRAI;
 		break;
 	case EXPP_MAT_ATTR_SPECTRANS: 
 		param = mat->spectra;
@@ -1195,8 +1209,12 @@
 		return EXPP_ReturnPyObjError( PyExc_RuntimeError, 
 				"undefined type in getFloatAttr" );
 	}
-
-	return PyFloat_FromDouble( param );
+	
+	if (ipocurve != -1) {
+		return BPyFloat_CreatePyObject( param, (PyObject *)self, ipocurve );
+	} else {
+		return PyFloat_FromDouble( param );
+	}
 }
 
 /*
@@ -1206,7 +1224,7 @@
 static int setFloatAttrClamp( BPy_Material *self, PyObject *value, void *type )
 {
 	float *param;
-	struct Material *mat = self->material;
+	Material *mat = self->material;
 	float min, max;
 	
 	switch( (int)type ) {
@@ -1503,7 +1521,7 @@
 static PyObject *getIntAttr( BPy_Material *self, void *type )
 {
 	int param; 
-	struct Material *mat = self->material;
+	Material *mat = self->material;
 
 	switch( (int)type ) {
 	

Added: branches/pyapi_devel/source/blender/python/api2_2x/bpy_float.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/bpy_float.c	                        (rev 0)
+++ branches/pyapi_devel/source/blender/python/api2_2x/bpy_float.c	2007-07-11 10:32:37 UTC (rev 11228)
@@ -0,0 +1,158 @@
+/*
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * This is a new part of Blender.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+/* BPy_Float are for accessing settings in blender that can have keyframes added */
+#include "bpy_float.h"
+#include "gen_utils.h"
+#include "BKE_global.h"
+#include "DNA_scene_types.h"
+#include "BKE_utildefines.h"
+#include "BSE_editipo.h"
+#include "Ipocurve.h"
+
+static int BPyFloat_init(BPy_Float *self, PyObject *args, PyObject *kwds)
+{
+	if (PyFloat_Type.tp_init((PyObject *)self, args, kwds) < 0)
+		return -1;
+	self->genlib = NULL;
+	return 0;
+}
+
+/* Can be accessed directly from a getsetattr */
+PyObject *BPyFloat_CreatePyObject( float value, PyObject *self, int adrcode )
+{
+	BPy_Float *obj;
+	PyObject *args = PyTuple_New(0); 
+	
+	/* passing NULL for keywords is fine*/
+	obj = (BPy_Float *)BPyFloat_Type.tp_new(&BPyFloat_Type, args, NULL);
+	BPyFloat_Type.tp_init((PyObject *)obj, args, NULL);
+	obj->pyfloat.ob_fval = (double)value;
+	obj->genlib = (BPy_GenericLib *)self;
+	obj->adrcode = adrcode;
+	if (self) /* this is a genlib user */
+		Py_INCREF(self); 
+	
+	Py_DECREF(args);
+	return (PyObject *)obj;
+}
+
+PyObject * BPyFloat_keyframe(BPy_Float *self)
+{	
+	if (!self->genlib || !self->genlib->id)
+		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
+						"this blender float does not reference any keyable data or the data has been removed." ) );
+	
+	insertkey(self->genlib->id, GS(self->genlib->id), NULL, NULL, self->adrcode);
+	Py_RETURN_NONE;
+}
+
+PyObject * BPyFloat_getCurve(BPy_Float *self)
+{
+	IpoCurve *icu = verify_ipocurve(self->genlib->id, GS(self->genlib->id), NULL, NULL, self->adrcode);
+	if (icu) {
+		return IpoCurve_CreatePyObject(icu);
+	} else {
+		Py_RETURN_NONE;
+	}
+}
+
+static PyMethodDef BPyFloat_methods[] = {
+	/* name, method, flags, doc */
+	{"keyframe", ( PyCFunction ) BPyFloat_keyframe, METH_NOARGS, ""},  
+	{NULL, NULL, 0, NULL}
+};
+
+static PyGetSetDef BPy_Float_getseters[] = {
+	{"curve",
+	 (getter)BPyFloat_getCurve, (setter)NULL,
+	 "the ipo curve for this value",
+	 NULL},
+	{NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
+};
+
+static void BPyFloat_dealloc( BPy_Float * self )
+{
+	if (self->genlib) {
+		Py_DECREF((PyObject *)(self->genlib));
+	}
+	PyFloat_Type.tp_dealloc((PyObject *)self);
+}
+
+
+PyTypeObject BPyFloat_Type = {
+	PyObject_HEAD_INIT(&PyType_Type)
+	0,
+	"BPyFloat",
+	sizeof(BPy_Float),
+	0,
+	(destructor)BPyFloat_dealloc,	/* tp_dealloc */
+	0,					/* tp_print */
+	0,					/* tp_getattr */
+	0,					/* tp_setattr */
+	0,					/* tp_compare */
+	0,					/* tp_repr */
+	0,					/* tp_as_number */
+	0,					/* tp_as_sequence */
+	0,					/* tp_as_mapping */
+	0,					/* tp_hash */
+	0,					/* tp_call */
+	0,					/* tp_str */
+	0,					/* tp_getattro */
+	0,					/* tp_setattro */
+	0,					/* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+	0,					/* tp_doc */
+	0,					/* tp_traverse */
+	/* TODO - tp_clear crashes! - do we need to sync materials before and after clearing? 
+	 * It looks like this is only used internally anyhow */
+	0,/*(inquiry)BPy_BPyFloat_clear*/					/* tp_clear */
+	0,					/* tp_richcompare */
+	0,					/* tp_weaklistoffset */
+	0,					/* tp_iter */
+	0,					/* tp_iternext */
+	BPyFloat_methods,	/* tp_methods */
+	0,					/* tp_members */
+	BPy_Float_getseters,	/* tp_getset */
+	&PyFloat_Type,		/* tp_base */

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list