[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54287] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python: * Added a generic helper function for parsing PyObject arguments as N-dimensional float array .

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Mon Feb 4 01:23:38 CET 2013


Revision: 54287
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54287
Author:   kjym3
Date:     2013-02-04 00:23:37 +0000 (Mon, 04 Feb 2013)
Log Message:
-----------
* Added a generic helper function for parsing PyObject arguments as N-dimensional float array.

* Local helpers were replaced with the generic one.  This also fixed a memory leak in the setter
function StrokeVertex_point_set.

* Made minor code style changes.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2013-02-04 00:18:09 UTC (rev 54286)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2013-02-04 00:23:37 UTC (rev 54287)
@@ -601,7 +601,26 @@
 	return new Vec3r(x,y,z);
 }
 
+// helper for argument parsing
 
+int float_array_from_PyObject(PyObject *obj, float *v, int n)
+{
+	if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == n) {
+		for (int i = 0; i < n; i++)
+			v[i] = ((VectorObject *)obj)->vec[i];
+	} else if (PyList_Check(obj) && PyList_Size(obj) == n) {
+		for (int i = 0; i < n; i++)
+			v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i));
+	} else if (PyTuple_Check(obj) && PyTuple_Size(obj) == n) {
+		for (int i = 0; i < n; i++)
+			v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i));
+	} else {
+		return 0;
+	}
+	return 1;
+}
+
+
 ///////////////////////////////////////////////////////////////////////////////////////////
 
 #ifdef __cplusplus

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h	2013-02-04 00:18:09 UTC (rev 54286)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h	2013-02-04 00:23:37 UTC (rev 54287)
@@ -137,6 +137,7 @@
 Vec3f * Vec3f_ptr_from_PyTuple( PyObject* obj );
 Vec3r * Vec3r_ptr_from_PyTuple( PyObject* obj );
 
+int float_array_from_PyObject(PyObject *obj, float *v, int n);
 
 
 ///////////////////////////////////////////////////////////////////////////////////////////

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp	2013-02-04 00:18:09 UTC (rev 54286)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp	2013-02-04 00:23:37 UTC (rev 54287)
@@ -58,23 +58,6 @@
 "   :arg iShininess: The shininess coefficient.\n"
 "   :type iShininess: :class:float\n";
 
-static int Vec4(PyObject *obj, float *v)
-{
-	if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == 4) {
-		for (int i = 0; i < 4; i++)
-			v[i] = ((VectorObject *)obj)->vec[i];
-	} else if( PyList_Check(obj) && PyList_Size(obj) == 4 ) {
-		for (int i = 0; i < 4; i++)
-			v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i));
-	} else if( PyTuple_Check(obj) && PyTuple_Size(obj) == 4 ) {
-		for (int i = 0; i < 4; i++)
-			v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i));
-	} else {
-		return 0;
-	}
-	return 1;
-}
-
 static int FrsMaterial___init__(BPy_FrsMaterial *self, PyObject *args, PyObject *kwds)
 {
 	PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0, *obj4 = 0;
@@ -94,7 +77,10 @@
 		}
 		self->m = new FrsMaterial( *m );
 
-	} else if( Vec4(obj1, f1) && obj2 && Vec4(obj2, f2) && obj3 && Vec4(obj3, f3) && obj4 && Vec4(obj4, f4) ) {
+	} else if( float_array_from_PyObject(obj1, f1, 4) && obj2 &&
+		       float_array_from_PyObject(obj2, f2, 4) && obj3 &&
+		       float_array_from_PyObject(obj3, f3, 4) && obj4 &&
+		       float_array_from_PyObject(obj4, f4, 4) ) {
 		self->m = new FrsMaterial(f1, f2, f3, f4, f5);
 
 	} else {
@@ -280,8 +266,7 @@
 PyDoc_STRVAR(FrsMaterial_diffuse_doc,
 "RGBA components of the diffuse color of the material.\n"
 "\n"
-":type: mathutils.Vector"
-);
+":type: mathutils.Vector");
 
 static PyObject *FrsMaterial_diffuse_get(BPy_FrsMaterial *self, void *UNUSED(closure))
 {
@@ -291,7 +276,7 @@
 static int FrsMaterial_diffuse_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
 {
 	float color[4];
-	if (!Vec4((PyObject *)value, color)) {
+	if (!float_array_from_PyObject(value, color, 4)) {
 		PyErr_SetString(PyExc_ValueError, "value must be a 4-dimensional vector");
 		return -1;
 	}
@@ -302,8 +287,7 @@
 PyDoc_STRVAR(FrsMaterial_specular_doc,
 "RGBA components of the specular color of the material.\n"
 "\n"
-":type: mathutils.Vector"
-);
+":type: mathutils.Vector");
 
 static PyObject *FrsMaterial_specular_get(BPy_FrsMaterial *self, void *UNUSED(closure))
 {
@@ -313,7 +297,7 @@
 static int FrsMaterial_specular_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
 {
 	float color[4];
-	if (!Vec4((PyObject *)value, color)) {
+	if (!float_array_from_PyObject(value, color, 4)) {
 		PyErr_SetString(PyExc_ValueError, "value must be a 4-dimensional vector");
 		return -1;
 	}
@@ -324,8 +308,7 @@
 PyDoc_STRVAR(FrsMaterial_ambient_doc,
 "RGBA components of the ambient color of the material.\n"
 "\n"
-":type: mathutils.Color"
-);
+":type: mathutils.Color");
 
 static PyObject *FrsMaterial_ambient_get(BPy_FrsMaterial *self, void *UNUSED(closure))
 {
@@ -335,7 +318,7 @@
 static int FrsMaterial_ambient_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
 {
 	float color[4];
-	if (!Vec4((PyObject *)value, color)) {
+	if (!float_array_from_PyObject(value, color, 4)) {
 		PyErr_SetString(PyExc_ValueError, "value must be a 4-dimensional vector");
 		return -1;
 	}
@@ -346,8 +329,7 @@
 PyDoc_STRVAR(FrsMaterial_emission_doc,
 "RGBA components of the emissive color of the material.\n"
 "\n"
-":type: mathutils.Color"
-);
+":type: mathutils.Color");
 
 static PyObject *FrsMaterial_emission_get(BPy_FrsMaterial *self, void *UNUSED(closure))
 {
@@ -357,7 +339,7 @@
 static int FrsMaterial_emission_set(BPy_FrsMaterial *self, PyObject *value, void *UNUSED(closure))
 {
 	float color[4];
-	if (!Vec4((PyObject *)value, color)) {
+	if (!float_array_from_PyObject(value, color, 4)) {
 		PyErr_SetString(PyExc_ValueError, "value must be a 4-dimensional vector");
 		return -1;
 	}
@@ -368,8 +350,7 @@
 PyDoc_STRVAR(FrsMaterial_shininess_doc,
 "Shininess coefficient of the material.\n"
 "\n"
-":type: float"
-);
+":type: float");
 
 static PyObject *FrsMaterial_shininess_get(BPy_FrsMaterial *self, void *UNUSED(closure))
 {

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp	2013-02-04 00:18:09 UTC (rev 54286)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp	2013-02-04 00:23:37 UTC (rev 54287)
@@ -188,8 +188,7 @@
 PyDoc_STRVAR(StrokeVertex_attribute_doc,
 "StrokeAttribute for this StrokeVertex.\n"
 "\n"
-":type: StrokeAttribute"
-);
+":type: StrokeAttribute");
 
 static PyObject *StrokeVertex_attribute_get(BPy_StrokeVertex *self, void *UNUSED(closure))
 {
@@ -209,8 +208,7 @@
 PyDoc_STRVAR(StrokeVertex_curvilinear_abscissa_doc,
 "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
 "\n"
-":type: float"
-);
+":type: float");
 
 static PyObject *StrokeVertex_curvilinear_abscissa_get(BPy_StrokeVertex *self, void *UNUSED(closure))
 {
@@ -231,8 +229,7 @@
 PyDoc_STRVAR(StrokeVertex_point_doc,
 "2D point coordinates.\n"
 "\n"
-":type: mathutils.Vector"
-);
+":type: mathutils.Vector");
 
 static PyObject *StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(closure))
 {
@@ -241,13 +238,13 @@
 
 static int StrokeVertex_point_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
 {
-	Vec2f *v = Vec2f_ptr_from_PyObject(value);
-	if (!v) {
+	float v[2];
+	if (!float_array_from_PyObject(value, v, 2)) {
 		PyErr_SetString(PyExc_ValueError, "value must be a 2-dimensional vector");
 		return -1;
 	}
-	self->sv->setX(v->x());
-	self->sv->setY(v->y());
+	self->sv->setX(v[0]);
+	self->sv->setY(v[1]);
 	return 0;
 }
 
@@ -255,8 +252,7 @@
 "Stroke length (it is only a value retained by the StrokeVertex,\n"
 "and it won't change the real stroke length).\n"
 "\n"
-":type: float"
-);
+":type: float");
 
 static PyObject *StrokeVertex_stroke_length_get(BPy_StrokeVertex *self, void *UNUSED(closure))
 {
@@ -277,8 +273,7 @@
 PyDoc_STRVAR(StrokeVertex_u_doc,
 "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
 "\n"
-":type: float"
-);
+":type: float");
 
 static PyObject *StrokeVertex_u_get(BPy_StrokeVertex *self, void *UNUSED(closure))
 {




More information about the Bf-blender-cvs mailing list