From 3c064f4553e4be988fe4fcec450b59b935fa3c80 Mon Sep 17 00:00:00 2001 From: Tamito Kajiyama Date: Mon, 4 Feb 2013 00:23:37 +0000 Subject: * 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. --- .../freestyle/intern/python/BPy_Convert.cpp | 19 +++++++++ .../blender/freestyle/intern/python/BPy_Convert.h | 1 + .../freestyle/intern/python/BPy_FrsMaterial.cpp | 45 +++++++--------------- .../Interface0D/CurvePoint/BPy_StrokeVertex.cpp | 23 +++++------ 4 files changed, 42 insertions(+), 46 deletions(-) (limited to 'source/blender/freestyle') diff --git a/source/blender/freestyle/intern/python/BPy_Convert.cpp b/source/blender/freestyle/intern/python/BPy_Convert.cpp index 04e778a0f80..59cee23409f 100644 --- a/source/blender/freestyle/intern/python/BPy_Convert.cpp +++ b/source/blender/freestyle/intern/python/BPy_Convert.cpp @@ -601,6 +601,25 @@ Vec3r * Vec3r_ptr_from_PyTuple( PyObject* obj ) { 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; +} + /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/source/blender/freestyle/intern/python/BPy_Convert.h b/source/blender/freestyle/intern/python/BPy_Convert.h index 178301262ff..7089b4683f1 100644 --- a/source/blender/freestyle/intern/python/BPy_Convert.h +++ b/source/blender/freestyle/intern/python/BPy_Convert.h @@ -137,6 +137,7 @@ Vec2f * Vec2f_ptr_from_PyTuple( PyObject* obj ); 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); /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp b/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp index 6442b5eef81..4222a44ff1c 100644 --- a/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp +++ b/source/blender/freestyle/intern/python/BPy_FrsMaterial.cpp @@ -58,23 +58,6 @@ static char FrsMaterial___doc__[] = " :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 @@ static int FrsMaterial___init__(BPy_FrsMaterial *self, PyObject *args, PyObject } 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 @@ void FrsMaterial_mathutils_register_callback() 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 PyObject *FrsMaterial_diffuse_get(BPy_FrsMaterial *self, void *UNUSED(clo 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 @@ static int FrsMaterial_diffuse_set(BPy_FrsMaterial *self, PyObject *value, void 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 PyObject *FrsMaterial_specular_get(BPy_FrsMaterial *self, void *UNUSED(cl 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 @@ static int FrsMaterial_specular_set(BPy_FrsMaterial *self, PyObject *value, void 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 PyObject *FrsMaterial_ambient_get(BPy_FrsMaterial *self, void *UNUSED(clo 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 @@ static int FrsMaterial_ambient_set(BPy_FrsMaterial *self, PyObject *value, void 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 PyObject *FrsMaterial_emission_get(BPy_FrsMaterial *self, void *UNUSED(cl 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 @@ static int FrsMaterial_emission_set(BPy_FrsMaterial *self, PyObject *value, void 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)) { diff --git a/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp b/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp index 3d7a29a540d..27f70caeb70 100644 --- a/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp +++ b/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp @@ -188,8 +188,7 @@ void StrokeVertex_mathutils_register_callback() 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 @@ static int StrokeVertex_attribute_set(BPy_StrokeVertex *self, PyObject *value, v 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 @@ static int StrokeVertex_curvilinear_abscissa_set(BPy_StrokeVertex *self, PyObjec 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 PyObject *StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(clo 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 @@ PyDoc_STRVAR(StrokeVertex_stroke_length_doc, "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 @@ static int StrokeVertex_stroke_length_set(BPy_StrokeVertex *self, PyObject *valu 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)) { -- cgit v1.2.3