[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22096] branches/soc-2008-mxcurioni/source /blender/freestyle/intern: * Implemented Python' s iterator protocol in Interface0DIterator and

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Sat Aug 1 00:13:48 CEST 2009


Revision: 22096
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22096
Author:   kjym3
Date:     2009-08-01 00:13:48 +0200 (Sat, 01 Aug 2009)

Log Message:
-----------
* Implemented Python's iterator protocol in Interface0DIterator and
orientedViewEdgeIterator.

* Simplified Python-related error handling in C++ class definitions.
The definitions of the following C++ methods were simplified and most
code segments using the C/Python API were moved to Director.cpp.

  ChainingIterator::init()
  ChainingIterator::traverse()
  UnaryPredicate0D::operator()()
  UnaryPredicate1D::operator()()
  BinaryPredicate0D::operator()()
  BinaryPredicate1D::operator()()
  UnaryFunction0D::operator()()
  UnaryFunction1D::operator()()
  StrokeShader.shade()

* Moved part of the introspection-based automatic type conversion code
from BPy_Interface0DIterator.cpp and Director.cpp to BPy_Convert.cpp
for the sake of better code organization.

* Fixed an uninitialized member in StrokeVertexIterator___init__().

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_Interface1D.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface0D/BPy_ViewVertex.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_FEdge.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_FrsCurve.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_ViewEdge.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_CurvePointIterator.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_Interface0DIterator.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_Interface0DIterator.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_orientedViewEdgeIterator.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_orientedViewEdgeIterator.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/ChainingIterators.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/Predicates0D.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/Predicates1D.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/StrokeShader.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/view_map/Functions0D.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/view_map/Functions1D.h

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-31 21:44:59 UTC (rev 22095)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2009-07-31 22:13:48 UTC (rev 22096)
@@ -83,6 +83,23 @@
 }
 
 PyObject * BPy_Interface0D_from_Interface0D( Interface0D& if0D ) {
+	if (typeid(if0D) == typeid(CurvePoint)) {
+		return BPy_CurvePoint_from_CurvePoint_ptr(dynamic_cast<CurvePoint*>(&if0D));
+	} else if (typeid(if0D) == typeid(StrokeVertex)) {
+		return BPy_StrokeVertex_from_StrokeVertex_ptr(dynamic_cast<StrokeVertex*>(&if0D));
+	} else if (typeid(if0D) == typeid(SVertex)) {
+		return BPy_SVertex_from_SVertex_ptr(dynamic_cast<SVertex*>(&if0D));
+	} else if (typeid(if0D) == typeid(ViewVertex)) {
+		return BPy_ViewVertex_from_ViewVertex_ptr(dynamic_cast<ViewVertex*>(&if0D));
+	} else if (typeid(if0D) == typeid(NonTVertex)) {
+		return BPy_NonTVertex_from_NonTVertex_ptr(dynamic_cast<NonTVertex*>(&if0D));
+	} else if (typeid(if0D) == typeid(TVertex)) {
+		return BPy_TVertex_from_TVertex_ptr(dynamic_cast<TVertex*>(&if0D));
+	} else if (typeid(if0D) != typeid(Interface0D)) {
+		string msg("unexpected type: " + if0D.getExactTypeName());
+		PyErr_SetString(PyExc_TypeError, msg.c_str());
+		return NULL;
+	}
 	PyObject *py_if0D =  Interface0D_Type.tp_new( &Interface0D_Type, 0, 0 );
 	((BPy_Interface0D *) py_if0D)->if0D = &if0D;
 
@@ -90,6 +107,17 @@
 }
 
 PyObject * BPy_Interface1D_from_Interface1D( Interface1D& if1D ) {
+	if (typeid(if1D) == typeid(ViewEdge)) {
+		return BPy_ViewEdge_from_ViewEdge_ptr(dynamic_cast<ViewEdge*>(&if1D));
+	} else if (typeid(if1D) == typeid(Chain)) {
+		return BPy_Chain_from_Chain_ptr(dynamic_cast<Chain*>(&if1D));
+	} else if (typeid(if1D) == typeid(Stroke)) {
+		return BPy_Stroke_from_Stroke_ptr(dynamic_cast<Stroke*>(&if1D));
+	} else if (typeid(if1D) != typeid(Interface1D)) {
+		string msg("unexpected type: " + if1D.getExactTypeName());
+		PyErr_SetString(PyExc_TypeError, msg.c_str());
+		return NULL;
+	}
 	PyObject *py_if1D =  Interface1D_Type.tp_new( &Interface1D_Type, 0, 0 );
 	((BPy_Interface1D *) py_if1D)->if1D = &if1D;
 
@@ -281,10 +309,11 @@
 	return py_a_it;
 }
 
-PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it ) {
+PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it, int reversed ) {
 	PyObject *py_if0D_it = Interface0DIterator_Type.tp_new( &Interface0DIterator_Type, 0, 0 );
 	((BPy_Interface0DIterator *) py_if0D_it)->if0D_it = new Interface0DIterator( if0D_it );
 	((BPy_Interface0DIterator *) py_if0D_it)->py_it.it = ((BPy_Interface0DIterator *) py_if0D_it)->if0D_it;
+	((BPy_Interface0DIterator *) py_if0D_it)->reversed = reversed;
 
 	return py_if0D_it;
 }
@@ -315,10 +344,11 @@
 }
 
 
-PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it ) {
+PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it, int reversed ) {
 	PyObject *py_ove_it = orientedViewEdgeIterator_Type.tp_new( &orientedViewEdgeIterator_Type, 0, 0 );
 	((BPy_orientedViewEdgeIterator *) py_ove_it)->ove_it = new ViewVertexInternal::orientedViewEdgeIterator( ove_it );
 	((BPy_orientedViewEdgeIterator *) py_ove_it)->py_it.it = ((BPy_orientedViewEdgeIterator *) py_ove_it)->ove_it;
+	((BPy_orientedViewEdgeIterator *) py_ove_it)->reversed = reversed;
 	
 	return py_ove_it;
 }

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-31 21:44:59 UTC (rev 22095)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h	2009-07-31 22:13:48 UTC (rev 22096)
@@ -97,11 +97,11 @@
 PyObject * BPy_ViewShape_from_ViewShape( ViewShape& vs );
 
 PyObject * BPy_AdjacencyIterator_from_AdjacencyIterator( AdjacencyIterator& a_it );
-PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it );
+PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it, int reversed );
 PyObject * BPy_CurvePointIterator_from_CurvePointIterator( CurveInternal::CurvePointIterator& cp_it );
 PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator( StrokeInternal::StrokeVertexIterator& sv_it, int reversed);
 PyObject * BPy_SVertexIterator_from_SVertexIterator( ViewEdgeInternal::SVertexIterator& sv_it );
-PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it );
+PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it, int reversed );
 PyObject * BPy_ViewEdgeIterator_from_ViewEdgeIterator( ViewEdgeInternal::ViewEdgeIterator& ve_it );
 PyObject * BPy_ChainingIterator_from_ChainingIterator( ChainingIterator& c_it );
 PyObject * BPy_ChainPredicateIterator_from_ChainPredicateIterator( ChainPredicateIterator& cp_it );

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface1D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface1D.cpp	2009-07-31 21:44:59 UTC (rev 22095)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Interface1D.cpp	2009-07-31 22:13:48 UTC (rev 22096)
@@ -256,12 +256,12 @@
 
 PyObject * Interface1D_verticesBegin( BPy_Interface1D *self ) {
 	Interface0DIterator if0D_it( self->if1D->verticesBegin() );
-	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
+	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 0 );
 }
 
 PyObject * Interface1D_verticesEnd( BPy_Interface1D *self ) {
 	Interface0DIterator if0D_it( self->if1D->verticesEnd() );
-	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
+	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 1 );
 }
 
 
@@ -272,7 +272,7 @@
 		return NULL;
 	
 	Interface0DIterator if0D_it( self->if1D->pointsBegin(f) );
-	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
+	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 0 );
 }
 
 PyObject * Interface1D_pointsEnd( BPy_Interface1D *self, PyObject *args ) {
@@ -282,7 +282,7 @@
 		return NULL;
 	
 	Interface0DIterator if0D_it( self->if1D->pointsEnd(f) );
-	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
+	return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 1 );
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp	2009-07-31 21:44:59 UTC (rev 22095)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp	2009-07-31 22:13:48 UTC (rev 22096)
@@ -43,88 +43,113 @@
 
 
 //   BinaryPredicate0D: __call__
-int Director_BPy_BinaryPredicate0D___call__( PyObject *obj, Interface0D& i1, Interface0D& i2) {
+int Director_BPy_BinaryPredicate0D___call__( BinaryPredicate0D *bp0D, Interface0D& i1, Interface0D& i2 ) {
+	if (!bp0D->py_bp0D) { // internal error
+		PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_bp0D) not initialized");
+		return -1;
+	}
 	PyObject *arg1 = BPy_Interface0D_from_Interface0D(i1);
 	PyObject *arg2 = BPy_Interface0D_from_Interface0D(i2);
-	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
+	if (!arg1 || !arg2) {
+		Py_XDECREF(arg1);
+		Py_XDECREF(arg2);
+		return -1;
+	}
+	PyObject *result = PyObject_CallMethod( bp0D->py_bp0D, "__call__", "OO", arg1, arg2 );
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
 	if (!result)
 		return -1;
 	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
-	return ret;
+	if (ret < 0)
+		return -1;
+	bp0D->result = ret;
+	return 0;
 }
 
 
 //   BinaryPredicate1D: __call__
-int Director_BPy_BinaryPredicate1D___call__( PyObject *obj, Interface1D& i1, Interface1D& i2) {
-	PyObject *arg1, *arg2;
-	if (typeid(i1) == typeid(ViewEdge)) {
-		arg1 = BPy_ViewEdge_from_ViewEdge_ptr(dynamic_cast<ViewEdge*>(&i1));
-		arg2 = BPy_ViewEdge_from_ViewEdge_ptr(dynamic_cast<ViewEdge*>(&i2));
-	} else if (typeid(i1) == typeid(Chain)) {
-		arg1 = BPy_Chain_from_Chain_ptr(dynamic_cast<Chain*>(&i1));
-		arg2 = BPy_Chain_from_Chain_ptr(dynamic_cast<Chain*>(&i2));
-	} else if (typeid(i1) == typeid(Stroke)) {
-		arg1 = BPy_Stroke_from_Stroke_ptr(dynamic_cast<Stroke*>(&i1));
-		arg2 = BPy_Stroke_from_Stroke_ptr(dynamic_cast<Stroke*>(&i2));
-	} else {
-		cerr << "Warning: cast to " + i1.getExactTypeName() + " not implemented" << endl;
-		arg1 = BPy_Interface1D_from_Interface1D(i1);
-		arg2 = BPy_Interface1D_from_Interface1D(i2);
+int Director_BPy_BinaryPredicate1D___call__( BinaryPredicate1D *bp1D, Interface1D& i1, Interface1D& i2 ) {
+	if (!bp1D->py_bp1D) { // internal error
+		PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_bp1D) not initialized");
+		return -1;
 	}
-	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
+	PyObject *arg1 = BPy_Interface1D_from_Interface1D(i1);
+	PyObject *arg2 = BPy_Interface1D_from_Interface1D(i2);
+	if (!arg1 || !arg2) {
+		Py_XDECREF(arg1);
+		Py_XDECREF(arg2);
+		return -1;
+	}
+	PyObject *result = PyObject_CallMethod( bp1D->py_bp1D, "__call__", "OO", arg1, arg2 );
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
 	if (!result)
 		return -1;
 	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
-	return ret;
+	if (ret < 0)
+		return -1;
+	bp1D->result = ret;
+	return 0;
 }
 
 
 //   UnaryPredicate0D: __call__
-int Director_BPy_UnaryPredicate0D___call__( PyObject *obj, Interface0DIterator& if0D_it) {
-	PyObject *arg = BPy_Interface0DIterator_from_Interface0DIterator(if0D_it);
-	PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list