* 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.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@@ -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);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -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))
|
||||
{
|
||||
|
@@ -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))
|
||||
{
|
||||
|
Reference in New Issue
Block a user