[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21877] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python: * Better support for vector-like objects in method arguments.
Tamito Kajiyama
rd6t-kjym at asahi-net.or.jp
Sat Jul 25 13:27:20 CEST 2009
Revision: 21877
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21877
Author: kjym3
Date: 2009-07-25 13:27:18 +0200 (Sat, 25 Jul 2009)
Log Message:
-----------
* Better support for vector-like objects in method arguments.
Now the following methods in the Freestyle Python API accept
not only Blender.Mathutils.Vector instances but also lists and
tuples having an appropriate number of elements.
FrsNoise::turbulence2()
FrsNoise::turbulence3()
FrsNoise::smoothNoise2()
FrsNoise::smoothNoise3()
SVertex::__init__()
SVertex::setPoint3D()
SVertex::setPoint2D()
SVertex::AddNormal()
FEdgeSharp::setNormalA()
FEdgeSharp::setNormalB()
FEdgeSmooth::setNormal()
CalligraphicShader::__init__()
StrokeAttribute::setAttributeVec2f()
StrokeAttribute::setAttributeVec3f()
StrokeAttribute::setColor()
StrokeVertex::setPoint()
* Added the following converters for the sake of the improvements
mentioned above.
Vec2f_ptr_from_PyObject()
Vec3f_ptr_from_PyObject()
Vec3r_ptr_from_PyObject()
Vec2f_ptr_from_PyList()
Vec3f_ptr_from_PyList()
Vec3r_ptr_from_PyList()
Vec2f_ptr_from_PyTuple()
Vec3f_ptr_from_PyTuple()
Vec3r_ptr_from_PyTuple()
Those converters with the suffixes _PyList and _PyTuple accept
only lists and tuples having an appropriate number of elements,
respectively, while those with the suffix _PyObject accept lists,
tuples, or Blender.Mathutils.Vector instances.
* Fixed a null pointer reference in Interface0D___dealloc__().
* Corrected the names of 3 methods in the FEdgeSmooth class.
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_FrsNoise.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface0D.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_StrokeAttribute.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/CurvePoint/BPy_StrokeVertex.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/FEdge/BPy_FEdgeSharp.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/FEdge/BPy_FEdgeSmooth.cpp
branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/StrokeShader/BPy_CalligraphicShader.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 2009-07-25 10:44:10 UTC (rev 21876)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp 2009-07-25 11:27:18 UTC (rev 21877)
@@ -376,6 +376,39 @@
return static_cast<Nature::EdgeNature>( PyInt_AsLong(obj) );
}
+Vec2f * Vec2f_ptr_from_PyObject( PyObject* obj ) {
+ Vec2f *v;
+ if( (v = Vec2f_ptr_from_Vector( obj )) )
+ return v;
+ if( (v = Vec2f_ptr_from_PyList( obj )) )
+ return v;
+ if( (v = Vec2f_ptr_from_PyTuple( obj )) )
+ return v;
+ return NULL;
+}
+
+Vec3f * Vec3f_ptr_from_PyObject( PyObject* obj ) {
+ Vec3f *v;
+ if( (v = Vec3f_ptr_from_Vector( obj )) )
+ return v;
+ if( (v = Vec3f_ptr_from_PyList( obj )) )
+ return v;
+ if( (v = Vec3f_ptr_from_PyTuple( obj )) )
+ return v;
+ return NULL;
+}
+
+Vec3r * Vec3r_ptr_from_PyObject( PyObject* obj ) {
+ Vec3r *v;
+ if( (v = Vec3r_ptr_from_Vector( obj )) )
+ return v;
+ if( (v = Vec3r_ptr_from_PyList( obj )) )
+ return v;
+ if( (v = Vec3r_ptr_from_PyTuple( obj )) )
+ return v;
+ return NULL;
+}
+
Vec2f * Vec2f_ptr_from_Vector( PyObject* obj ) {
PyObject *v;
if (!VectorObject_Check(obj) || ((VectorObject *)obj)->size != 2)
@@ -424,7 +457,59 @@
return new Vec3r(x,y,z);
}
+Vec2f * Vec2f_ptr_from_PyList( PyObject* obj ) {
+ if( !PyList_Check(obj) || PyList_Size(obj) != 2 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
+ return new Vec2f(x,y);
+}
+Vec3f * Vec3f_ptr_from_PyList( PyObject* obj ) {
+ if( !PyList_Check(obj) || PyList_Size(obj) != 3 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
+ float z = PyFloat_AsDouble(PyList_GetItem(obj, 2));
+ return new Vec3f(x,y,z);
+}
+
+Vec3r * Vec3r_ptr_from_PyList( PyObject* obj ) {
+ if( !PyList_Check(obj) || PyList_Size(obj) != 3 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
+ float z = PyFloat_AsDouble(PyList_GetItem(obj, 2));
+ return new Vec3r(x,y,z);
+}
+
+Vec2f * Vec2f_ptr_from_PyTuple( PyObject* obj ) {
+ if( !PyTuple_Check(obj) || PyTuple_Size(obj) != 2 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
+ return new Vec2f(x,y);
+}
+
+Vec3f * Vec3f_ptr_from_PyTuple( PyObject* obj ) {
+ if( !PyTuple_Check(obj) || PyTuple_Size(obj) != 3 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
+ float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2));
+ return new Vec3f(x,y,z);
+}
+
+Vec3r * Vec3r_ptr_from_PyTuple( PyObject* obj ) {
+ if( !PyTuple_Check(obj) || PyTuple_Size(obj) != 3 )
+ return NULL;
+ float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
+ float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
+ float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2));
+ return new Vec3r(x,y,z);
+}
+
+
///////////////////////////////////////////////////////////////////////////////////////////
#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 2009-07-25 10:44:10 UTC (rev 21876)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h 2009-07-25 11:27:18 UTC (rev 21877)
@@ -115,9 +115,18 @@
IntegrationType IntegrationType_from_BPy_IntegrationType( PyObject* obj );
Stroke::MediumType MediumType_from_BPy_MediumType( PyObject* obj );
Nature::EdgeNature EdgeNature_from_BPy_Nature( PyObject* obj );
+Vec2f * Vec2f_ptr_from_PyObject( PyObject* obj );
+Vec3f * Vec3f_ptr_from_PyObject( PyObject* obj );
+Vec3r * Vec3r_ptr_from_PyObject( PyObject* obj );
Vec2f * Vec2f_ptr_from_Vector( PyObject* obj );
Vec3f * Vec3f_ptr_from_Vector( PyObject* obj );
Vec3r * Vec3r_ptr_from_Vector( PyObject* obj );
+Vec2f * Vec2f_ptr_from_PyList( PyObject* obj );
+Vec3f * Vec3f_ptr_from_PyList( PyObject* obj );
+Vec3r * Vec3r_ptr_from_PyList( PyObject* obj );
+Vec2f * Vec2f_ptr_from_PyTuple( PyObject* obj );
+Vec3f * Vec3f_ptr_from_PyTuple( PyObject* obj );
+Vec3r * Vec3r_ptr_from_PyTuple( PyObject* obj );
Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsNoise.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsNoise.cpp 2009-07-25 10:44:10 UTC (rev 21876)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_FrsNoise.cpp 2009-07-25 11:27:18 UTC (rev 21877)
@@ -1,4 +1,5 @@
#include "BPy_FrsNoise.h"
+#include "BPy_Convert.h"
#include <sstream>
@@ -165,18 +166,16 @@
float f2, f3;
unsigned int i = 4;
- if(!( PyArg_ParseTuple(args, "O!ff|I", &PyList_Type, &obj1, &f2, &f3, &i) ))
+ if(!( PyArg_ParseTuple(args, "Off|I", &obj1, &f2, &f3, &i) ))
return NULL;
- if( PyList_Size(obj1) != 2 ) {
- stringstream msg("FrsNoise::turbulence2() accepts a list of 2 elements (");
- msg << PyList_Size(obj1) << " found)";
- PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+ Vec2f *v = Vec2f_ptr_from_PyObject(obj1);
+ if( !v ) {
+ PyErr_SetString(PyExc_TypeError, "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
return NULL;
}
-
- Vec2f v( PyFloat_AsDouble(PyList_GetItem(obj1, 0)), PyFloat_AsDouble(PyList_GetItem(obj1, 1)) );
-
- return PyFloat_FromDouble( self->n->turbulence2(v, f2, f3, i) );
+ float t = self->n->turbulence2(*v, f2, f3, i);
+ delete v;
+ return PyFloat_FromDouble( t );
}
PyObject * FrsNoise_turbulence3( BPy_FrsNoise *self , PyObject *args) {
@@ -184,20 +183,16 @@
float f2, f3;
unsigned int i = 4;
- if(!( PyArg_ParseTuple(args, "O!ff|I", &PyList_Type, &obj1, &f2, &f3, &i) ))
+ if(!( PyArg_ParseTuple(args, "Off|I", &obj1, &f2, &f3, &i) ))
return NULL;
- if( PyList_Size(obj1) != 3 ) {
- stringstream msg("FrsNoise::turbulence3() accepts a list of 3 elements (");
- msg << PyList_Size(obj1) << " found)";
- PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+ Vec3f *v = Vec3f_ptr_from_PyObject(obj1);
+ if( !v ) {
+ PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
return NULL;
}
-
- Vec3f v( PyFloat_AsDouble(PyList_GetItem(obj1, 0)),
- PyFloat_AsDouble(PyList_GetItem(obj1, 1)),
- PyFloat_AsDouble(PyList_GetItem(obj1, 2)) );
-
- return PyFloat_FromDouble( self->n->turbulence3(v, f2, f3, i) );
+ float t = self->n->turbulence3(*v, f2, f3, i);
+ delete v;
+ return PyFloat_FromDouble( t );
}
PyObject * FrsNoise_smoothNoise1( BPy_FrsNoise *self , PyObject *args) {
@@ -212,37 +207,31 @@
PyObject * FrsNoise_smoothNoise2( BPy_FrsNoise *self , PyObject *args) {
PyObject *obj;
- if(!( PyArg_ParseTuple(args, "O", &PyList_Type, &obj) ))
+ if(!( PyArg_ParseTuple(args, "O", &obj) ))
return NULL;
- if( PyList_Size(obj) != 2 ) {
- stringstream msg("FrsNoise::smoothNoise2() accepts a list of 2 elements (");
- msg << PyList_Size(obj) << " found)";
- PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+ Vec2f *v = Vec2f_ptr_from_PyObject(obj);
+ if( !v ) {
+ PyErr_SetString(PyExc_TypeError, "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
return NULL;
}
-
- Vec2f v( PyFloat_AsDouble(PyList_GetItem(obj, 0)), PyFloat_AsDouble(PyList_GetItem(obj, 1)) );
-
- return PyFloat_FromDouble( self->n->smoothNoise2(v) );
+ float t = self->n->smoothNoise2(*v);
+ delete v;
+ return PyFloat_FromDouble( t );
}
PyObject * FrsNoise_smoothNoise3( BPy_FrsNoise *self , PyObject *args) {
PyObject *obj;
- if(!( PyArg_ParseTuple(args, "O", &PyList_Type, &obj) ))
+ if(!( PyArg_ParseTuple(args, "O", &obj) ))
return NULL;
- if( PyList_Size(obj) != 3 ) {
- stringstream msg("FrsNoise::smoothNoise3() accepts a list of 3 elements (");
- msg << PyList_Size(obj) << " found)";
- PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+ Vec3f *v = Vec3f_ptr_from_PyObject(obj);
+ if( !v ) {
+ PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
return NULL;
}
-
- Vec3f v( PyFloat_AsDouble(PyList_GetItem(obj, 0)),
- PyFloat_AsDouble(PyList_GetItem(obj, 1)),
- PyFloat_AsDouble(PyList_GetItem(obj, 2)) );
-
- return PyFloat_FromDouble( self->n->smoothNoise3(v) );
+ float t = self->n->smoothNoise3(*v);
+ delete v;
+ return PyFloat_FromDouble( t );
}
Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface0D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface0D.cpp 2009-07-25 10:44:10 UTC (rev 21876)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface0D.cpp 2009-07-25 11:27:18 UTC (rev 21877)
@@ -189,8 +189,7 @@
void Interface0D___dealloc__(BPy_Interface0D* self)
{
- if( self->if0D->py_if0D )
- delete self->if0D;
+ delete self->if0D;
self->ob_type->tp_free((PyObject*)self);
}
Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_StrokeAttribute.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_StrokeAttribute.cpp 2009-07-25 10:44:10 UTC (rev 21876)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_StrokeAttribute.cpp 2009-07-25 11:27:18 UTC (rev 21877)
@@ -328,15 +328,17 @@
if(!( PyArg_ParseTuple(args, "O|OO", &obj1, &obj2, &obj3) ))
return NULL;
-
- if( PyList_Check(obj1) && !obj2 && !obj3 ){
+
+ if( obj1 && !obj2 && !obj3 ){
+
+ Vec3f *v = Vec3f_ptr_from_PyObject(obj1);
+ if( !v ) {
+ PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
+ return NULL;
+ }
+ self->sa->setColor( *v );
+ delete v;
- Vec3f v( PyFloat_AsDouble( PyList_GetItem(obj1, 0) ),
- PyFloat_AsDouble( PyList_GetItem(obj1, 1) ),
- PyFloat_AsDouble( PyList_GetItem(obj1, 2) ) );
-
- self->sa->setColor( v );
-
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list