[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15685] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python/StrokeAttribute.cpp: soc-2008-mxcurioni: corrected StrokeAttribute, verified with test case.

Maxime Curioni maxime.curioni at gmail.com
Tue Jul 22 02:27:40 CEST 2008


Revision: 15685
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15685
Author:   mxcurioni
Date:     2008-07-22 02:27:40 +0200 (Tue, 22 Jul 2008)

Log Message:
-----------
soc-2008-mxcurioni: corrected StrokeAttribute, verified with test case.

I realized today that it will not be possible to implement getter/setter functionality easily for our Freestyle API. The reason is that Python does not support function overloading as-is. It is possible to 'fake' overloading by taking a general argument object and count the number of arguments in the object (rgbTuple_setCol in Blender's API is a good example of how to go about it). 

For the time being, we'll get around that problem. The tangible effect of that constraint is that all API setter functions return a 'None' PyObject, instead of returning an integer status code. It is important to note that this problem is due to Freestyle's API being C++ in nature. Fortunately, this shouldn't really impact the usage of the API.

If the Blender Python group wants me to correct that, I'll be able to do it. It is just going to take me quite some time correcting it, writing support functions for methods having different types of arguments.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/StrokeAttribute.cpp

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/StrokeAttribute.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/StrokeAttribute.cpp	2008-07-21 23:41:01 UTC (rev 15684)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/StrokeAttribute.cpp	2008-07-22 00:27:40 UTC (rev 15685)
@@ -28,13 +28,13 @@
 static PyObject * StrokeAttribute_isAttributeAvailableReal( BPy_StrokeAttribute *self, PyObject *args );
 static PyObject * StrokeAttribute_isAttributeAvailableVec2f( BPy_StrokeAttribute *self, PyObject *args );
 static PyObject * StrokeAttribute_isAttributeAvailableVec3f( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setColor( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setAlpha( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setThickness( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setVisible( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setAttributeReal( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setAttributeVec2f( BPy_StrokeAttribute *self, PyObject *args );
-static int StrokeAttribute_setAttributeVec3f( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setColor( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setAlpha( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setThickness( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setVisible( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setAttributeReal( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setAttributeVec2f( BPy_StrokeAttribute *self, PyObject *args );
+static PyObject * StrokeAttribute_setAttributeVec3f( BPy_StrokeAttribute *self, PyObject *args );
 
 
 /*----------------------StrokeAttribute instance definitions ----------------------------*/
@@ -64,6 +64,8 @@
 	{NULL, NULL, 0, NULL}
 };
 
+
+
 /*-----------------------BPy_StrokeAttribute type definition ------------------------------*/
 
 PyTypeObject StrokeAttribute_Type = {
@@ -124,7 +126,7 @@
   /*** Attribute descriptor and subclassing stuff ***/
 	BPy_StrokeAttribute_methods,	/* struct PyMethodDef *tp_methods; */
 	NULL,                       	/* struct PyMemberDef *tp_members; */
-	NULL,         					/* struct PyGetSetDef *tp_getset; */
+	NULL,  							/* struct PyGetSetDef *tp_getset; */
 	NULL,							/* struct _typeobject *tp_base; */
 	NULL,							/* PyObject *tp_dict; */
 	NULL,							/* descrgetfunc tp_descr_get; */
@@ -184,9 +186,7 @@
 											*( ((BPy_StrokeAttribute *) obj2)->sa ),
 											PyFloat_AsDouble( obj3 ) );	
 										
-	} else if( 	obj4 && obj5 && obj6 &&
-				PyFloat_Check(obj1) && PyFloat_Check(obj2) && PyFloat_Check(obj2) &&
-				PyFloat_Check(obj4) && PyFloat_Check(obj5) && PyFloat_Check(obj6) ) {
+	} else if( 	obj4 && obj5 && obj6 ) {
 	
 			self->sa = new StrokeAttribute(	PyFloat_AsDouble( obj1 ),
 											PyFloat_AsDouble( obj2 ),
@@ -212,18 +212,18 @@
 
 PyObject * StrokeAttribute___repr__(BPy_StrokeAttribute* self)
 {
-    return PyString_FromFormat("StrokeAttribute: r:%f g:%f b:%f a:%f - R:%f L:%f", 
-		self->sa->getColorR(), self->sa->getColorG(), self->sa->getColorB(), self->sa->getAlpha(),
-		self->sa->getThicknessR(), self->sa->getThicknessL() );
+	stringstream repr("StrokeAttribute:");
+	repr << " r: " << self->sa->getColorR()
+		 << " g: " << self->sa->getColorG()
+		 << " b: " << self->sa->getColorB()
+		 << " a: " << self->sa->getAlpha()
+		 << " - R: " << self->sa->getThicknessR() 
+		 << " L: " << self->sa->getThicknessL();
+
+	return PyString_FromFormat( repr.str().c_str() );
 }
 
 
-// PyObject *StrokeAttribute_getColor( BPy_StrokeAttribute *self ) {
-// 	float *c = self->sa->getColor();
-// 	Vec3f v( c[0], c[1], c[2]);
-// 	return Vector_from_Vec3f( v );
-// }
-
 PyObject *StrokeAttribute_getColorR( BPy_StrokeAttribute *self ) {
 	return PyFloat_FromDouble( self->sa->getColorR() );	
 }
@@ -245,11 +245,6 @@
 	return PyFloat_FromDouble( self->sa->getAlpha() );	
 }
 
-// PyObject *StrokeAttribute_getThickness( BPy_StrokeAttribute *self ) {
-// 	// vector
-// 	return PyString_FromString( self->sa->getExactTypeName() );	
-// }
-
 PyObject *StrokeAttribute_getThicknessR( BPy_StrokeAttribute *self ) {
 	return PyFloat_FromDouble( self->sa->getThicknessR() );	
 }
@@ -337,14 +332,14 @@
 }
 
 
-int StrokeAttribute_setColor( BPy_StrokeAttribute *self, PyObject *args ) {
+PyObject * StrokeAttribute_setColor( BPy_StrokeAttribute *self, PyObject *args ) {
 	PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0 ;
 
 	if(!( PyArg_ParseTuple(args, "O|OO", &obj1, &obj2, &obj3) )) {
 		cout << "ERROR: StrokeAttribute_setColor" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
-
+	
 	if( PyList_Check(obj1) && !obj2 && !obj3 ){
 		
 		Vec3f v( 	PyFloat_AsDouble( PyList_GetItem(obj1, 0) ),
@@ -352,39 +347,35 @@
 					PyFloat_AsDouble( PyList_GetItem(obj1, 2) )  );
 		
 		self->sa->setColor( v );
-		return 0;
 		
-	} else if( 	obj1 && PyFloat_Check(obj1) &&
-				obj2 && PyFloat_Check(obj2) &&
-				obj3 && PyFloat_Check(obj3)	   ){
-					
+	} else if( 	obj1 && obj2 && obj3 ){
+
 		self->sa->setColor(	PyFloat_AsDouble(obj1),
 							PyFloat_AsDouble(obj2),
 							PyFloat_AsDouble(obj3) );
-		return 0;
 	}
 	
-	return -1;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setAlpha( BPy_StrokeAttribute *self, PyObject *args ){
+PyObject * StrokeAttribute_setAlpha( BPy_StrokeAttribute *self, PyObject *args ){
 	float f = 0;
 
 	if(!( PyArg_ParseTuple(args, "f", &f) )) {
 		cout << "ERROR: StrokeAttribute_setAlpha" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 	
 	self->sa->setAlpha( f );
-	return 0;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setThickness( BPy_StrokeAttribute *self, PyObject *args )  {
+PyObject * StrokeAttribute_setThickness( BPy_StrokeAttribute *self, PyObject *args )  {
 	PyObject *obj1 = 0, *obj2 = 0;
 
 	if(!( PyArg_ParseTuple(args, "O|O", &obj1, &obj2) )) {
 		cout << "ERROR: StrokeAttribute_setThickness" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 
 	if( PyList_Check(obj1) && !obj2 ){
@@ -393,51 +384,48 @@
 					PyFloat_AsDouble( PyList_GetItem(obj1, 1) )  );
 		
 		self->sa->setThickness( v );
-		return 0;
 		
-	} else if( 	obj1 && PyFloat_Check(obj1) &&
-				obj2 && PyFloat_Check(obj2)	   ){
+	} else if( 	obj1 && obj2 ){
 					
 		self->sa->setThickness(	PyFloat_AsDouble(obj1),
 								PyFloat_AsDouble(obj2) );
-		return 0;
 	}
 	
-	return -1;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setVisible( BPy_StrokeAttribute *self, PyObject *args ) {
+PyObject * StrokeAttribute_setVisible( BPy_StrokeAttribute *self, PyObject *args ) {
 	int i = 0;
 
 	if(!( PyArg_ParseTuple(args, "i", &i) )) {
 		cout << "ERROR: StrokeAttribute_setVisible" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 
 	self->sa->setVisible( i );
-	return 0;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setAttributeReal( BPy_StrokeAttribute *self, PyObject *args ) {
+PyObject * StrokeAttribute_setAttributeReal( BPy_StrokeAttribute *self, PyObject *args ) {
 	char *s = 0;
 	double d = 0;
 
 	if(!( PyArg_ParseTuple(args, "sd", &s, &d) )) {
 		cout << "ERROR: StrokeAttribute_setAttributeReal" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 
 	self->sa->setAttributeReal( s, d );
-	return 0;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setAttributeVec2f( BPy_StrokeAttribute *self, PyObject *args ) {
+PyObject * StrokeAttribute_setAttributeVec2f( BPy_StrokeAttribute *self, PyObject *args ) {
 	char *s;
 	PyObject *obj = 0;
 
 	if(!( PyArg_ParseTuple(args, "sO", &s, &obj) )) {
 		cout << "ERROR: StrokeAttribute_setAttributeVec2f" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 
 	if( PyList_Check(obj) && PyList_Size(obj) > 1) {
@@ -446,20 +434,18 @@
 					PyFloat_AsDouble( PyList_GetItem(obj, 1) )  );
 
 		self->sa->setAttributeVec2f( s, v );
-		return 0;
-		
 	}
 	
-	return -1;
+	Py_RETURN_NONE;
 }
 
-int StrokeAttribute_setAttributeVec3f( BPy_StrokeAttribute *self, PyObject *args ) {
+PyObject * StrokeAttribute_setAttributeVec3f( BPy_StrokeAttribute *self, PyObject *args ) {
 	char *s;
 	PyObject *obj = 0;
 
 	if(!( PyArg_ParseTuple(args, "sO", &s, &obj) )) {
 		cout << "ERROR: StrokeAttribute_setAttributeVec3f" << endl;
-		return -1;
+		Py_RETURN_NONE;
 	}
 
 	if( PyList_Check(obj)  && PyList_Size(obj) > 2 ) {
@@ -469,11 +455,9 @@
 					PyFloat_AsDouble( PyList_GetItem(obj, 2) )  );
 
 		self->sa->setAttributeVec3f( s, v );
-		return 0;
-
 	}
 
-	return -1;
+	Py_RETURN_NONE;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////





More information about the Bf-blender-cvs mailing list