* Added a generic helper function for parsing PyObject arguments as N-dimensional...
[blender.git] / source / blender / freestyle / intern / python / BPy_Convert.cpp
index 01567b18142ba829df8307b6a38aa32dfc9d7104..59cee23409ffd128763d6e42e18b362114a401f5 100644 (file)
@@ -208,7 +208,7 @@ PyObject * BPy_Nature_from_Nature( unsigned short n ) {
        PyObject *py_n;
 
        PyObject *args = PyTuple_New(1);
-       PyTuple_SetItem( args, 0, PyLong_FromLong(n) );
+       PyTuple_SET_ITEM( args, 0, PyLong_FromLong(n) );
        py_n =  Nature_Type.tp_new(&Nature_Type, args, NULL);
        Py_DECREF(args);
 
@@ -235,7 +235,7 @@ PyObject * BPy_MediumType_from_MediumType( Stroke::MediumType n ) {
        PyObject *py_mt;
 
        PyObject *args = PyTuple_New(1);
-       PyTuple_SetItem( args, 0, PyLong_FromLong(n) );
+       PyTuple_SET_ITEM( args, 0, PyLong_FromLong(n) );
        py_mt = MediumType_Type.tp_new( &MediumType_Type, args, NULL );
        Py_DECREF(args);
 
@@ -334,7 +334,7 @@ PyObject * BPy_IntegrationType_from_IntegrationType( IntegrationType i ) {
        PyObject *py_it;
 
        PyObject *args = PyTuple_New(1);
-       PyTuple_SetItem( args, 0, PyLong_FromLong(i) );
+       PyTuple_SET_ITEM( args, 0, PyLong_FromLong(i) );
        py_it = IntegrationType_Type.tp_new( &IntegrationType_Type, args, NULL );
        Py_DECREF(args);
 
@@ -353,8 +353,8 @@ PyObject * BPy_CurvePoint_from_CurvePoint( CurvePoint& cp ) {
 PyObject * BPy_directedViewEdge_from_directedViewEdge( ViewVertex::directedViewEdge& dve ) {
        PyObject *py_dve = PyTuple_New(2);
        
-       PyTuple_SetItem( py_dve, 0, BPy_ViewEdge_from_ViewEdge(*(dve.first)) );
-       PyTuple_SetItem( py_dve, 1, PyBool_from_bool(dve.second) );
+       PyTuple_SET_ITEM( py_dve, 0, BPy_ViewEdge_from_ViewEdge(*(dve.first)) );
+       PyTuple_SET_ITEM( py_dve, 1, PyBool_from_bool(dve.second) );
        
        return py_dve;
 }
@@ -483,6 +483,8 @@ Vec3f * Vec3f_ptr_from_PyObject( PyObject* obj ) {
        Vec3f *v;
        if( (v = Vec3f_ptr_from_Vector( obj )) )
                return v;
+       if( (v = Vec3f_ptr_from_Color( obj )) )
+               return v;
        if( (v = Vec3f_ptr_from_PyList( obj )) )
                return v;
        if( (v = Vec3f_ptr_from_PyTuple( obj )) )
@@ -494,6 +496,8 @@ Vec3r * Vec3r_ptr_from_PyObject( PyObject* obj ) {
        Vec3r *v;
        if( (v = Vec3r_ptr_from_Vector( obj )) )
                return v;
+       if( (v = Vec3r_ptr_from_Color( obj )) )
+               return v;
        if( (v = Vec3r_ptr_from_PyList( obj )) )
                return v;
        if( (v = Vec3r_ptr_from_PyTuple( obj )) )
@@ -502,53 +506,49 @@ Vec3r * Vec3r_ptr_from_PyObject( PyObject* obj ) {
 }
 
 Vec2f * Vec2f_ptr_from_Vector( PyObject* obj ) {
-       PyObject *v;
        if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 2)
                return NULL;
-       v = PyObject_GetAttrString(obj,"x");
-       float x = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       v = PyObject_GetAttrString(obj,"y");
-       float y = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       
-       return new Vec2f(x,y);
+       float x = ((VectorObject *)obj)->vec[0];
+       float y = ((VectorObject *)obj)->vec[1];
+       return new Vec2f(x, y);
 }
 
 Vec3f * Vec3f_ptr_from_Vector( PyObject* obj ) {
-       PyObject *v;
        if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 3)
                return NULL;
-       v = PyObject_GetAttrString(obj,"x");
-       float x = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       v = PyObject_GetAttrString(obj,"y");
-       float y = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       v = PyObject_GetAttrString(obj,"z");
-       float z = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       
+       float x = ((VectorObject *)obj)->vec[0];
+       float y = ((VectorObject *)obj)->vec[1];
+       float z = ((VectorObject *)obj)->vec[2];
        return new Vec3f(x,y,z);
 }
 
 Vec3r * Vec3r_ptr_from_Vector( PyObject* obj ) {
-       PyObject *v;
        if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 3)
                return NULL;
-       v = PyObject_GetAttrString(obj,"x");
-       double x = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       v = PyObject_GetAttrString(obj,"y");
-       double y = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       v = PyObject_GetAttrString(obj,"z");
-       double z = PyFloat_AsDouble( v );
-       Py_DECREF( v );
-       
+       real x = ((VectorObject *)obj)->vec[0];
+       real y = ((VectorObject *)obj)->vec[1];
+       real z = ((VectorObject *)obj)->vec[2];
        return new Vec3r(x,y,z);
 }
 
+Vec3f * Vec3f_ptr_from_Color( PyObject* obj ) {
+       if (!ColorObject_Check(obj))
+               return NULL;
+       float r = ((ColorObject *)obj)->col[0];
+       float g = ((ColorObject *)obj)->col[1];
+       float b = ((ColorObject *)obj)->col[2];
+       return new Vec3f(r,g,b);
+}
+
+Vec3r * Vec3r_ptr_from_Color( PyObject* obj ) {
+       if (!ColorObject_Check(obj))
+               return NULL;
+       real r = ((ColorObject *)obj)->col[0];
+       real g = ((ColorObject *)obj)->col[1];
+       real b = ((ColorObject *)obj)->col[2];
+       return new Vec3r(r,g,b);
+}
+
 Vec2f * Vec2f_ptr_from_PyList( PyObject* obj ) {
        if( !PyList_Check(obj) || PyList_Size(obj) != 2 )
                return NULL;
@@ -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;
+}
+
 
 ///////////////////////////////////////////////////////////////////////////////////////////