Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / Director.cpp
1 #include "Director.h"
2
3 #include "BPy_Convert.h"
4
5 #include "BPy_BinaryPredicate0D.h"
6 #include "BPy_BinaryPredicate1D.h"
7 #include "BPy_FrsMaterial.h"
8 #include "BPy_Id.h"
9 #include "BPy_UnaryFunction0D.h"
10 #include "BPy_UnaryFunction1D.h"
11 #include "BPy_UnaryPredicate0D.h"
12 #include "BPy_UnaryPredicate1D.h"
13 #include "BPy_StrokeShader.h"
14 #include "Iterator/BPy_ChainingIterator.h"
15 #include "Iterator/BPy_Interface0DIterator.h"
16 #include "Interface1D/BPy_Stroke.h"
17 #include "Interface1D/BPy_ViewEdge.h"
18 #include "BPy_ViewShape.h"
19
20 #include "UnaryFunction0D/BPy_UnaryFunction0DDouble.h"
21 #include "UnaryFunction0D/BPy_UnaryFunction0DEdgeNature.h"
22 #include "UnaryFunction0D/BPy_UnaryFunction0DFloat.h"
23 #include "UnaryFunction0D/BPy_UnaryFunction0DId.h"
24 #include "UnaryFunction0D/BPy_UnaryFunction0DMaterial.h"
25 #include "UnaryFunction0D/BPy_UnaryFunction0DUnsigned.h"
26 #include "UnaryFunction0D/BPy_UnaryFunction0DVec2f.h"
27 #include "UnaryFunction0D/BPy_UnaryFunction0DVec3f.h"
28 #include "UnaryFunction0D/BPy_UnaryFunction0DVectorViewShape.h"
29 #include "UnaryFunction0D/BPy_UnaryFunction0DViewShape.h"
30
31 #include "UnaryFunction1D/BPy_UnaryFunction1DDouble.h"
32 #include "UnaryFunction1D/BPy_UnaryFunction1DEdgeNature.h"
33 #include "UnaryFunction1D/BPy_UnaryFunction1DFloat.h"
34 #include "UnaryFunction1D/BPy_UnaryFunction1DUnsigned.h"
35 #include "UnaryFunction1D/BPy_UnaryFunction1DVec2f.h"
36 #include "UnaryFunction1D/BPy_UnaryFunction1DVec3f.h"
37 #include "UnaryFunction1D/BPy_UnaryFunction1DVectorViewShape.h"
38
39
40 //   BinaryPredicate0D: __call__
41 int Director_BPy_BinaryPredicate0D___call__( BinaryPredicate0D *bp0D, Interface0D& i1, Interface0D& i2 ) {
42         if (!bp0D->py_bp0D) { // internal error
43                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_bp0D) not initialized");
44                 return -1;
45         }
46         PyObject *arg1 = Any_BPy_Interface0D_from_Interface0D(i1);
47         PyObject *arg2 = Any_BPy_Interface0D_from_Interface0D(i2);
48         if (!arg1 || !arg2) {
49                 Py_XDECREF(arg1);
50                 Py_XDECREF(arg2);
51                 return -1;
52         }
53         PyObject *result = PyObject_CallMethod( bp0D->py_bp0D, "__call__", "OO", arg1, arg2 );
54         Py_DECREF(arg1);
55         Py_DECREF(arg2);
56         if (!result)
57                 return -1;
58         int ret = PyObject_IsTrue(result);
59         Py_DECREF(result);
60         if (ret < 0)
61                 return -1;
62         bp0D->result = ret;
63         return 0;
64 }
65
66
67 //   BinaryPredicate1D: __call__
68 int Director_BPy_BinaryPredicate1D___call__( BinaryPredicate1D *bp1D, Interface1D& i1, Interface1D& i2 ) {
69         if (!bp1D->py_bp1D) { // internal error
70                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_bp1D) not initialized");
71                 return -1;
72         }
73         PyObject *arg1 = Any_BPy_Interface1D_from_Interface1D(i1);
74         PyObject *arg2 = Any_BPy_Interface1D_from_Interface1D(i2);
75         if (!arg1 || !arg2) {
76                 Py_XDECREF(arg1);
77                 Py_XDECREF(arg2);
78                 return -1;
79         }
80         PyObject *result = PyObject_CallMethod( bp1D->py_bp1D, "__call__", "OO", arg1, arg2 );
81         Py_DECREF(arg1);
82         Py_DECREF(arg2);
83         if (!result)
84                 return -1;
85         int ret = PyObject_IsTrue(result);
86         Py_DECREF(result);
87         if (ret < 0)
88                 return -1;
89         bp1D->result = ret;
90         return 0;
91 }
92
93
94 //   UnaryPredicate0D: __call__
95 int Director_BPy_UnaryPredicate0D___call__( UnaryPredicate0D *up0D, Interface0DIterator& if0D_it ) {
96         if (!up0D->py_up0D) { // internal error
97                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_up0D) not initialized");
98                 return -1;
99         }
100         PyObject *arg = BPy_Interface0DIterator_from_Interface0DIterator(if0D_it, 0);
101         if (!arg)
102                 return -1;
103         PyObject *result = PyObject_CallMethod( up0D->py_up0D, "__call__", "O", arg );
104         Py_DECREF(arg);
105         if (!result)
106                 return -1;
107         int ret = PyObject_IsTrue(result);
108         Py_DECREF(result);
109         if (ret < 0)
110                 return -1;
111         up0D->result = ret;
112         return 0;
113 }
114
115
116 //   UnaryPredicate1D: __call__
117 int Director_BPy_UnaryPredicate1D___call__( UnaryPredicate1D *up1D, Interface1D& if1D ) {
118         if (!up1D->py_up1D) { // internal error
119                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_up1D) not initialized");
120                 return -1;
121         }
122         PyObject *arg = Any_BPy_Interface1D_from_Interface1D(if1D);
123         if (!arg)
124                 return -1;
125         PyObject *result = PyObject_CallMethod( up1D->py_up1D, "__call__", "O", arg );
126         Py_DECREF(arg);
127         if (!result)
128                 return -1;
129         int ret = PyObject_IsTrue(result);
130         Py_DECREF(result);
131         if (ret < 0)
132                 return -1;
133         up1D->result = ret;
134         return 0;
135 }
136
137
138 //   StrokeShader: shade
139 int Director_BPy_StrokeShader_shade( StrokeShader *ss, Stroke& s ) {
140         if (!ss->py_ss) { // internal error
141                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_ss) not initialized");
142                 return -1;
143         }
144         PyObject *arg = BPy_Stroke_from_Stroke(s);
145         if (!arg)
146                 return -1;
147         PyObject *result = PyObject_CallMethod( ss->py_ss, "shade", "O", arg );
148         Py_DECREF(arg);
149         if (!result)
150                 return -1;
151         Py_DECREF(result);
152         return 0;
153 }
154
155 //   ChainingIterator: init, traverse
156 int Director_BPy_ChainingIterator_init( ChainingIterator *c_it ) {
157         if (!c_it->py_c_it) { // internal error
158                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_c_it) not initialized");
159                 return -1;
160         }
161         PyObject *result = PyObject_CallMethod( c_it->py_c_it, "init", "");
162         if (!result)
163                 return -1;
164         Py_DECREF(result);
165         return 0;
166 }
167
168 int Director_BPy_ChainingIterator_traverse( ChainingIterator *c_it, AdjacencyIterator& a_it ) {
169         if (!c_it->py_c_it) { // internal error
170                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_c_it) not initialized");
171                 return -1;
172         }
173         PyObject *arg = BPy_AdjacencyIterator_from_AdjacencyIterator(a_it);
174         if (!arg)
175                 return -1;
176         PyObject *result = PyObject_CallMethod( c_it->py_c_it, "traverse", "O", arg );
177         Py_DECREF(arg);
178         if (!result)
179                 return -1;
180         if (BPy_ViewEdge_Check(result)) {
181                 c_it->result = ((BPy_ViewEdge *) result)->ve;
182         } else if (result == Py_None) {
183                 c_it->result = NULL;
184         } else {
185                 PyErr_SetString(PyExc_RuntimeError, "traverse method returned a wrong value");
186                 Py_DECREF(result);
187                 return -1;
188         }
189         Py_DECREF(result);
190         return 0;
191 }
192
193
194 // BPy_UnaryFunction{0D,1D}: __call__
195 int Director_BPy_UnaryFunction0D___call__( void *uf0D, PyObject *obj, Interface0DIterator& if0D_it) {
196
197         if (!obj) { // internal error
198                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_uf0D) not initialized");
199                 return -1;
200         }
201         PyObject *arg = BPy_Interface0DIterator_from_Interface0DIterator(if0D_it, 0);
202         if (!arg)
203                 return -1;
204         PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
205         Py_DECREF(arg);
206         if (!result)
207                 return -1;
208         
209         if( BPy_UnaryFunction0DDouble_Check(obj) ) {    
210                 ((UnaryFunction0D<double> *) uf0D)->result = PyFloat_AsDouble(result);
211
212         } else if ( BPy_UnaryFunction0DEdgeNature_Check(obj) ) {
213                 ((UnaryFunction0D<Nature::EdgeNature> *) uf0D)->result = EdgeNature_from_BPy_Nature(result);
214         
215         } else if ( BPy_UnaryFunction0DFloat_Check(obj) ) {
216                 ((UnaryFunction0D<float> *) uf0D)->result = PyFloat_AsDouble(result);
217         
218         } else if ( BPy_UnaryFunction0DId_Check(obj) ) {
219                 ((UnaryFunction0D<Id> *) uf0D)->result = *( ((BPy_Id *) result)->id );
220         
221         } else if ( BPy_UnaryFunction0DMaterial_Check(obj) ) {
222                 ((UnaryFunction0D<FrsMaterial> *) uf0D)->result = *( ((BPy_FrsMaterial *) result)->m );
223         
224         } else if ( BPy_UnaryFunction0DUnsigned_Check(obj) ) {
225                 ((UnaryFunction0D<unsigned> *) uf0D)->result = PyLong_AsLong(result);
226         
227         } else if ( BPy_UnaryFunction0DVec2f_Check(obj) ) {
228                 Vec2f *v = Vec2f_ptr_from_Vector( result );
229                 ((UnaryFunction0D<Vec2f> *) uf0D)->result = *v;
230                 delete v; 
231         
232         } else if ( BPy_UnaryFunction0DVec3f_Check(obj) ) {
233                 Vec3f *v = Vec3f_ptr_from_Vector( result );
234                 ((UnaryFunction0D<Vec3f> *) uf0D)->result = *v;
235                 delete v;
236         
237         } else if ( BPy_UnaryFunction0DVectorViewShape_Check(obj) ) {
238                 vector<ViewShape*> vec;
239                 for( int i = 0; i < PyList_Size(result); i++) {
240                         ViewShape *b = ( (BPy_ViewShape *) PyList_GetItem(result, i) )->vs;
241                         vec.push_back( b );
242                 }
243                         
244                 ((UnaryFunction0D< vector<ViewShape*> > *) uf0D)->result = vec;
245         
246         } else if ( BPy_UnaryFunction0DViewShape_Check(obj) ) {
247                 ((UnaryFunction0D<ViewShape*> *) uf0D)->result = ((BPy_ViewShape *) result)->vs;
248         
249         }       
250
251         Py_DECREF(result);
252         return 0;
253 }
254
255 int Director_BPy_UnaryFunction1D___call__( void *uf1D, PyObject *obj, Interface1D& if1D) {
256
257         if (!obj) { // internal error
258                 PyErr_SetString(PyExc_RuntimeError, "Reference to Python object (py_uf1D) not initialized");
259                 return -1;
260         }
261         PyObject *arg = Any_BPy_Interface1D_from_Interface1D(if1D);
262         if (!arg)
263                 return -1;
264         PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
265         Py_DECREF(arg);
266         if (!result)
267                 return -1;
268         
269         if( BPy_UnaryFunction1DDouble_Check(obj) ) {    
270                 ((UnaryFunction1D<double> *) uf1D)->result = PyFloat_AsDouble(result);
271
272         } else if ( BPy_UnaryFunction1DEdgeNature_Check(obj) ) {
273                 ((UnaryFunction1D<Nature::EdgeNature> *) uf1D)->result = EdgeNature_from_BPy_Nature(result);
274         
275         } else if ( BPy_UnaryFunction1DFloat_Check(obj) ) {
276                 ((UnaryFunction1D<float> *) uf1D)->result = PyFloat_AsDouble(result);
277         
278         } else if ( BPy_UnaryFunction1DUnsigned_Check(obj) ) {
279                 ((UnaryFunction1D<unsigned> *) uf1D)->result = PyLong_AsLong(result);
280         
281         } else if ( BPy_UnaryFunction1DVec2f_Check(obj) ) {
282                 Vec2f *v = Vec2f_ptr_from_Vector( result );
283                 ((UnaryFunction1D<Vec2f> *) uf1D)->result = *v;
284                 delete v; 
285         
286         } else if ( BPy_UnaryFunction1DVec3f_Check(obj) ) {
287                 Vec3f *v = Vec3f_ptr_from_Vector( result );
288                 ((UnaryFunction1D<Vec3f> *) uf1D)->result = *v;
289                 delete v;
290         
291         } else if ( BPy_UnaryFunction1DVectorViewShape_Check(obj) ) {
292                 vector<ViewShape*> vec;
293                 for( int i = 1; i < PyList_Size(result); i++) {
294                         ViewShape *b = ( (BPy_ViewShape *) PyList_GetItem(result, i) )->vs;
295                         vec.push_back( b );
296                 }
297                         
298                 ((UnaryFunction1D< vector<ViewShape*> > *) uf1D)->result = vec;
299         
300         } 
301
302         Py_DECREF(result);
303         return 0;
304 }