Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / BPy_Interface1D.cpp
1 #include "BPy_Interface1D.h"
2
3 #include "BPy_Convert.h"
4 #include "Interface1D/BPy_FrsCurve.h"
5 #include "Interface1D/Curve/BPy_Chain.h"
6 #include "Interface1D/BPy_FEdge.h"
7 #include "Interface1D/FEdge/BPy_FEdgeSharp.h"
8 #include "Interface1D/FEdge/BPy_FEdgeSmooth.h"
9 #include "Interface1D/BPy_Stroke.h"
10 #include "Interface1D/BPy_ViewEdge.h"
11
12 #include "BPy_MediumType.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 ///////////////////////////////////////////////////////////////////////////////////////////
19
20 /*---------------  Python API function prototypes for Interface1D instance  -----------*/
21 static int Interface1D___init__(BPy_Interface1D *self, PyObject *args, PyObject *kwds);
22 static void Interface1D___dealloc__(BPy_Interface1D *self);
23 static PyObject * Interface1D___repr__(BPy_Interface1D *self);
24
25 static PyObject *Interface1D_getExactTypeName( BPy_Interface1D *self );
26 static PyObject *Interface1D_getVertices( BPy_Interface1D *self );
27 static PyObject *Interface1D_getPoints( BPy_Interface1D *self );
28 static PyObject *Interface1D_getLength2D( BPy_Interface1D *self );
29 static PyObject *Interface1D_getId( BPy_Interface1D *self );
30 static PyObject *Interface1D_getNature( BPy_Interface1D *self );
31 static PyObject *Interface1D_getTimeStamp( BPy_Interface1D *self );
32 static PyObject *Interface1D_setTimeStamp( BPy_Interface1D *self, PyObject *args);
33 static PyObject * Interface1D_verticesBegin( BPy_Interface1D *self );
34 static PyObject * Interface1D_verticesEnd( BPy_Interface1D *self );
35 static PyObject * Interface1D_pointsBegin( BPy_Interface1D *self, PyObject *args );
36 static PyObject * Interface1D_pointsEnd( BPy_Interface1D *self, PyObject *args );
37
38 /*----------------------Interface1D instance definitions ----------------------------*/
39 static PyMethodDef BPy_Interface1D_methods[] = {
40         {"getExactTypeName", ( PyCFunction ) Interface1D_getExactTypeName, METH_NOARGS, "() Returns the string of the name of the interface."},
41         {"getVertices", ( PyCFunction ) Interface1D_getVertices, METH_NOARGS, "Returns the vertices"},
42         {"getPoints", ( PyCFunction ) Interface1D_getPoints, METH_NOARGS, "Returns the points. The difference with getVertices() is that here we can iterate over points of the 1D element at any given sampling. At each call, a virtual point is created."},
43         {"getLength2D", ( PyCFunction ) Interface1D_getLength2D, METH_NOARGS, "Returns the 2D length of the 1D element"},
44         {"getId", ( PyCFunction ) Interface1D_getId, METH_NOARGS, "Returns the Id of the 1D element"},
45         {"getNature", ( PyCFunction ) Interface1D_getNature, METH_NOARGS, "Returns the nature of the 1D element"},
46         {"getTimeStamp", ( PyCFunction ) Interface1D_getTimeStamp, METH_NOARGS, "Returns the time stamp of the 1D element. Mainly used for selection"},
47         {"setTimeStamp", ( PyCFunction ) Interface1D_setTimeStamp, METH_VARARGS, "Sets the time stamp for the 1D element"},
48         {"verticesBegin", ( PyCFunction ) Interface1D_verticesBegin, METH_NOARGS, ""},
49         {"verticesEnd", ( PyCFunction ) Interface1D_verticesEnd, METH_NOARGS, ""},
50         {"pointsBegin", ( PyCFunction ) Interface1D_pointsBegin, METH_VARARGS, ""},
51         {"pointsEnd", ( PyCFunction ) Interface1D_pointsEnd, METH_VARARGS, ""},
52
53         {NULL, NULL, 0, NULL}
54 };
55
56 /*-----------------------BPy_Interface1D type definition ------------------------------*/
57
58 PyTypeObject Interface1D_Type = {
59         PyVarObject_HEAD_INIT(NULL, 0)
60         "Interface1D",                  /* tp_name */
61         sizeof(BPy_Interface1D),        /* tp_basicsize */
62         0,                              /* tp_itemsize */
63         (destructor)Interface1D___dealloc__, /* tp_dealloc */
64         0,                              /* tp_print */
65         0,                              /* tp_getattr */
66         0,                              /* tp_setattr */
67         0,                              /* tp_reserved */
68         (reprfunc)Interface1D___repr__, /* tp_repr */
69         0,                              /* tp_as_number */
70         0,                              /* tp_as_sequence */
71         0,                              /* tp_as_mapping */
72         0,                              /* tp_hash  */
73         0,                              /* tp_call */
74         0,                              /* tp_str */
75         0,                              /* tp_getattro */
76         0,                              /* tp_setattro */
77         0,                              /* tp_as_buffer */
78         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
79         "Interface1D objects",          /* tp_doc */
80         0,                              /* tp_traverse */
81         0,                              /* tp_clear */
82         0,                              /* tp_richcompare */
83         0,                              /* tp_weaklistoffset */
84         0,                              /* tp_iter */
85         0,                              /* tp_iternext */
86         BPy_Interface1D_methods,        /* tp_methods */
87         0,                              /* tp_members */
88         0,                              /* tp_getset */
89         0,                              /* tp_base */
90         0,                              /* tp_dict */
91         0,                              /* tp_descr_get */
92         0,                              /* tp_descr_set */
93         0,                              /* tp_dictoffset */
94         (initproc)Interface1D___init__, /* tp_init */
95         0,                              /* tp_alloc */
96         PyType_GenericNew,              /* tp_new */
97 };
98
99 //-------------------MODULE INITIALIZATION--------------------------------
100 int Interface1D_Init( PyObject *module )
101 {
102         if( module == NULL )
103                 return -1;
104
105         if( PyType_Ready( &Interface1D_Type ) < 0 )
106                 return -1;
107         Py_INCREF( &Interface1D_Type );
108         PyModule_AddObject(module, "Interface1D", (PyObject *)&Interface1D_Type);
109         
110         if( PyType_Ready( &FrsCurve_Type ) < 0 )
111                 return -1;
112         Py_INCREF( &FrsCurve_Type );
113         PyModule_AddObject(module, "FrsCurve", (PyObject *)&FrsCurve_Type);
114
115         if( PyType_Ready( &Chain_Type ) < 0 )
116                 return -1;
117         Py_INCREF( &Chain_Type );
118         PyModule_AddObject(module, "Chain", (PyObject *)&Chain_Type);
119         
120         if( PyType_Ready( &FEdge_Type ) < 0 )
121                 return -1;
122         Py_INCREF( &FEdge_Type );
123         PyModule_AddObject(module, "FEdge", (PyObject *)&FEdge_Type);
124
125         if( PyType_Ready( &FEdgeSharp_Type ) < 0 )
126                 return -1;
127         Py_INCREF( &FEdgeSharp_Type );
128         PyModule_AddObject(module, "FEdgeSharp", (PyObject *)&FEdgeSharp_Type);
129         
130         if( PyType_Ready( &FEdgeSmooth_Type ) < 0 )
131                 return -1;
132         Py_INCREF( &FEdgeSmooth_Type );
133         PyModule_AddObject(module, "FEdgeSmooth", (PyObject *)&FEdgeSmooth_Type);
134
135         if( PyType_Ready( &Stroke_Type ) < 0 )
136                 return -1;
137         Py_INCREF( &Stroke_Type );
138         PyModule_AddObject(module, "Stroke", (PyObject *)&Stroke_Type);
139
140         PyDict_SetItemString( Stroke_Type.tp_dict, "DRY_MEDIUM", BPy_MediumType_DRY_MEDIUM );
141         PyDict_SetItemString( Stroke_Type.tp_dict, "HUMID_MEDIUM", BPy_MediumType_HUMID_MEDIUM );
142         PyDict_SetItemString( Stroke_Type.tp_dict, "OPAQUE_MEDIUM", BPy_MediumType_OPAQUE_MEDIUM );
143
144         if( PyType_Ready( &ViewEdge_Type ) < 0 )
145                 return -1;
146         Py_INCREF( &ViewEdge_Type );
147         PyModule_AddObject(module, "ViewEdge", (PyObject *)&ViewEdge_Type);
148
149         return 0;
150 }
151
152 //------------------------INSTANCE METHODS ----------------------------------
153
154 int Interface1D___init__(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
155 {
156     if ( !PyArg_ParseTuple(args, "") )
157         return -1;
158         self->if1D = new Interface1D();
159         self->borrowed = 0;
160         return 0;
161 }
162
163 void Interface1D___dealloc__(BPy_Interface1D* self)
164 {
165         if( self->if1D && !self->borrowed )
166                 delete self->if1D;
167     Py_TYPE(self)->tp_free((PyObject*)self);
168 }
169
170 PyObject * Interface1D___repr__(BPy_Interface1D* self)
171 {
172     return PyUnicode_FromFormat("type: %s - address: %p", self->if1D->getExactTypeName().c_str(), self->if1D );
173 }
174
175 PyObject *Interface1D_getExactTypeName( BPy_Interface1D *self ) {
176         return PyUnicode_FromFormat( self->if1D->getExactTypeName().c_str() );  
177 }
178
179 PyObject *Interface1D_getVertices( BPy_Interface1D *self ) {
180         return PyList_New(0);
181 }
182
183 PyObject *Interface1D_getPoints( BPy_Interface1D *self ) {
184         return PyList_New(0);
185 }
186
187 PyObject *Interface1D_getLength2D( BPy_Interface1D *self ) {
188         return PyFloat_FromDouble( (double) self->if1D->getLength2D() );
189 }
190
191 PyObject *Interface1D_getId( BPy_Interface1D *self ) {
192         Id id( self->if1D->getId() );
193         return BPy_Id_from_Id( id );
194 }
195
196 PyObject *Interface1D_getNature( BPy_Interface1D *self ) {
197         return BPy_Nature_from_Nature( self->if1D->getNature() );
198 }
199
200 PyObject *Interface1D_getTimeStamp( BPy_Interface1D *self ) {
201         return PyLong_FromLong( self->if1D->getTimeStamp() );
202 }
203
204 PyObject *Interface1D_setTimeStamp( BPy_Interface1D *self, PyObject *args) {
205         int timestamp = 0 ;
206
207         if( !PyArg_ParseTuple(args, "i", &timestamp) )
208                 return NULL;
209         
210         self->if1D->setTimeStamp( timestamp );
211
212         Py_RETURN_NONE;
213 }
214
215 PyObject * Interface1D_verticesBegin( BPy_Interface1D *self ) {
216         Interface0DIterator if0D_it( self->if1D->verticesBegin() );
217         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 0 );
218 }
219
220 PyObject * Interface1D_verticesEnd( BPy_Interface1D *self ) {
221         Interface0DIterator if0D_it( self->if1D->verticesEnd() );
222         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 1 );
223 }
224
225
226 PyObject * Interface1D_pointsBegin( BPy_Interface1D *self, PyObject *args ) {
227         float f = 0;
228
229         if(!( PyArg_ParseTuple(args, "|f", &f)  ))
230                 return NULL;
231         
232         Interface0DIterator if0D_it( self->if1D->pointsBegin(f) );
233         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 0 );
234 }
235
236 PyObject * Interface1D_pointsEnd( BPy_Interface1D *self, PyObject *args ) {
237         float f = 0;
238
239         if(!( PyArg_ParseTuple(args, "|f", &f)  ))
240                 return NULL;
241         
242         Interface0DIterator if0D_it( self->if1D->pointsEnd(f) );
243         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it, 1 );
244 }
245
246 ///////////////////////////////////////////////////////////////////////////////////////////
247
248 #ifdef __cplusplus
249 }
250 #endif
251
252