Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / BPy_ViewShape.cpp
1 #include "BPy_ViewShape.h"
2
3 #include "BPy_Convert.h"
4 #include "Interface0D/BPy_ViewVertex.h"
5 #include "Interface1D/BPy_ViewEdge.h"
6 #include "BPy_SShape.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 ///////////////////////////////////////////////////////////////////////////////////////////
13
14 /*---------------  Python API function prototypes for ViewShape instance  -----------*/
15 static int ViewShape___init__(BPy_ViewShape *self, PyObject *args, PyObject *kwds);
16 static void ViewShape___dealloc__(BPy_ViewShape *self);
17 static PyObject * ViewShape___repr__(BPy_ViewShape* self);
18
19 static PyObject * ViewShape_sshape( BPy_ViewShape *self );
20 static PyObject * ViewShape_vertices( BPy_ViewShape *self );
21 static PyObject * ViewShape_edges( BPy_ViewShape *self );
22 static PyObject * ViewShape_getId( BPy_ViewShape *self );
23 static PyObject * ViewShape_setSShape( BPy_ViewShape *self , PyObject *args);
24 static PyObject * ViewShape_setVertices( BPy_ViewShape *self , PyObject *args);
25 static PyObject * ViewShape_setEdges( BPy_ViewShape *self , PyObject *args);
26 static PyObject * ViewShape_AddEdge( BPy_ViewShape *self , PyObject *args);
27 static PyObject * ViewShape_AddVertex( BPy_ViewShape *self , PyObject *args);
28
29 /*---------------------- BPy_ViewShape instance definitions ----------------------------*/
30 static PyMethodDef BPy_ViewShape_methods[] = {
31         {"sshape", ( PyCFunction ) ViewShape_sshape, METH_NOARGS, "() Returns the SShape on top of which this ViewShape is built. "},
32         {"vertices", ( PyCFunction ) ViewShape_vertices, METH_NOARGS, "() Returns the list of ViewVertex contained in this ViewShape."},
33         {"edges", ( PyCFunction ) ViewShape_edges, METH_NOARGS, "() Returns the list of ViewEdge contained in this ViewShape. "},
34         {"getId", ( PyCFunction ) ViewShape_getId, METH_NOARGS, "() Returns the ViewShape id. "},
35         {"setSShape", ( PyCFunction ) ViewShape_setSShape, METH_VARARGS, "(SShape ss) Sets the SShape on top of which the ViewShape is built. "},
36         {"setVertices", ( PyCFunction ) ViewShape_setVertices, METH_VARARGS, "([<ViewVertex>]) Sets the list of ViewVertex contained in this ViewShape."},
37         {"setEdges", ( PyCFunction ) ViewShape_setEdges, METH_VARARGS, "([<ViewEdge>]) Sets the list of ViewEdge contained in this ViewShape."},
38         {"AddEdge", ( PyCFunction ) ViewShape_AddEdge, METH_VARARGS, "(ViewEdge ve) Adds a ViewEdge to the list "},
39         {"AddVertex", ( PyCFunction ) ViewShape_AddVertex, METH_VARARGS, "(ViewVertex ve) Adds a ViewVertex to the list. "},
40         {NULL, NULL, 0, NULL}
41 };
42
43 /*-----------------------BPy_ViewShape type definition ------------------------------*/
44
45 PyTypeObject ViewShape_Type = {
46         PyVarObject_HEAD_INIT(NULL, 0)
47         "ViewShape",                    /* tp_name */
48         sizeof(BPy_ViewShape),          /* tp_basicsize */
49         0,                              /* tp_itemsize */
50         (destructor)ViewShape___dealloc__, /* tp_dealloc */
51         0,                              /* tp_print */
52         0,                              /* tp_getattr */
53         0,                              /* tp_setattr */
54         0,                              /* tp_reserved */
55         (reprfunc)ViewShape___repr__,   /* tp_repr */
56         0,                              /* tp_as_number */
57         0,                              /* tp_as_sequence */
58         0,                              /* tp_as_mapping */
59         0,                              /* tp_hash  */
60         0,                              /* tp_call */
61         0,                              /* tp_str */
62         0,                              /* tp_getattro */
63         0,                              /* tp_setattro */
64         0,                              /* tp_as_buffer */
65         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
66         "ViewShape objects",            /* tp_doc */
67         0,                              /* tp_traverse */
68         0,                              /* tp_clear */
69         0,                              /* tp_richcompare */
70         0,                              /* tp_weaklistoffset */
71         0,                              /* tp_iter */
72         0,                              /* tp_iternext */
73         BPy_ViewShape_methods,          /* tp_methods */
74         0,                              /* tp_members */
75         0,                              /* tp_getset */
76         0,                              /* tp_base */
77         0,                              /* tp_dict */
78         0,                              /* tp_descr_get */
79         0,                              /* tp_descr_set */
80         0,                              /* tp_dictoffset */
81         (initproc)ViewShape___init__,   /* tp_init */
82         0,                              /* tp_alloc */
83         PyType_GenericNew,              /* tp_new */
84 };
85
86 //-------------------MODULE INITIALIZATION--------------------------------
87 int ViewShape_Init( PyObject *module )
88 {
89         if( module == NULL )
90                 return -1;
91
92         if( PyType_Ready( &ViewShape_Type ) < 0 )
93                 return -1;
94
95         Py_INCREF( &ViewShape_Type );
96         PyModule_AddObject(module, "ViewShape", (PyObject *)&ViewShape_Type);
97         return 0;
98 }
99
100 //------------------------INSTANCE METHODS ----------------------------------
101
102 int ViewShape___init__(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
103 {
104         PyObject *obj;
105
106     if (! PyArg_ParseTuple(args, "|O", &obj) )
107         return -1;
108
109         if( !obj ) {
110                 self->vs = new ViewShape();
111         
112         } else if( BPy_SShape_Check(obj) ) {
113                 self->vs = new ViewShape( ((BPy_SShape *) obj)->ss );
114         
115         } else if( BPy_ViewShape_Check(obj) ) {
116                 self->vs = new ViewShape(*( ((BPy_ViewShape *) obj)->vs ));
117
118         } else {
119                 PyErr_SetString(PyExc_TypeError, "invalid argument");
120                 return -1;
121         }
122         self->borrowed = 0;
123         
124     return 0;
125 }
126
127 void ViewShape___dealloc__(BPy_ViewShape *self)
128 {
129         if( self->vs && !self->borrowed )
130                 delete self->vs;
131     Py_TYPE(self)->tp_free((PyObject*)self);
132 }
133
134 PyObject * ViewShape___repr__(BPy_ViewShape *self)
135 {
136     return PyUnicode_FromFormat("ViewShape - address: %p", self->vs );
137 }
138
139 PyObject * ViewShape_sshape( BPy_ViewShape *self ) {
140         return BPy_SShape_from_SShape( *(self->vs->sshape()) );
141 }
142
143
144 PyObject * ViewShape_vertices( BPy_ViewShape *self ) {
145         PyObject *py_vertices = PyList_New(NULL);
146
147         vector< ViewVertex * > vertices = self->vs->vertices();
148         vector< ViewVertex * >::iterator it;
149         
150         for( it = vertices.begin(); it != vertices.end(); it++ ) {
151                 PyList_Append( py_vertices, Any_BPy_ViewVertex_from_ViewVertex(*( *it )) );
152         }
153         
154         return py_vertices;
155 }
156
157
158 PyObject * ViewShape_edges( BPy_ViewShape *self ) {
159         PyObject *py_edges = PyList_New(NULL);
160
161         vector< ViewEdge * > edges = self->vs->edges();
162         vector< ViewEdge * >::iterator it;
163         
164         for( it = edges.begin(); it != edges.end(); it++ ) {
165                 PyList_Append( py_edges, BPy_ViewEdge_from_ViewEdge(*( *it )) );
166         }
167         
168         return py_edges;
169 }
170
171 PyObject * ViewShape_getId( BPy_ViewShape *self ) {
172         Id id( self->vs->getId() );
173         return BPy_Id_from_Id( id );
174 }
175
176 PyObject * ViewShape_setSShape( BPy_ViewShape *self , PyObject *args) {
177         PyObject *py_ss = 0;
178
179         if(!( PyArg_ParseTuple(args, "O!", &SShape_Type, &py_ss) ))
180                 return NULL;
181         
182         self->vs->setSShape( ((BPy_SShape *) py_ss)->ss );
183
184         Py_RETURN_NONE;
185 }
186
187 PyObject * ViewShape_setVertices( BPy_ViewShape *self , PyObject *args) {
188         PyObject *list = 0;
189         PyObject *tmp;
190         
191         if(!( PyArg_ParseTuple(args, "O!", &PyList_Type, &list) ))
192                 return NULL;
193         
194         vector< ViewVertex *> v;
195         
196         for( int i=0; i < PyList_Size(list); i++ ) {
197                 tmp = PyList_GetItem(list, i);
198                 if( BPy_ViewVertex_Check(tmp) )
199                         v.push_back( ((BPy_ViewVertex *) tmp)->vv );
200                 else {
201                         PyErr_SetString(PyExc_TypeError, "argument must be list of ViewVertex objects");
202                         return NULL;
203                 }
204         }
205                 
206         self->vs->setVertices( v );
207
208         Py_RETURN_NONE;
209 }
210
211 PyObject * ViewShape_setEdges( BPy_ViewShape *self , PyObject *args) {
212         PyObject *list = 0;
213         PyObject *tmp;
214
215         if(!( PyArg_ParseTuple(args, "O!", &PyList_Type, &list) ))
216                 return NULL;
217
218         vector<ViewEdge *> v;
219
220         for( int i=0; i < PyList_Size(list); i++ ) {
221                 tmp = PyList_GetItem(list, i);
222                 if( BPy_ViewEdge_Check(tmp) )
223                         v.push_back( ((BPy_ViewEdge *) tmp)->ve );
224                 else {
225                         PyErr_SetString(PyExc_TypeError, "argument must be list of ViewEdge objects");
226                         return NULL;
227                 }
228         }
229
230         self->vs->setEdges( v );
231
232         Py_RETURN_NONE;
233 }
234
235 PyObject * ViewShape_AddEdge( BPy_ViewShape *self , PyObject *args) {
236         PyObject *py_ve = 0;
237
238         if(!( PyArg_ParseTuple(args, "O!", &ViewEdge_Type, &py_ve) ))
239                 return NULL;
240         
241         self->vs->AddEdge( ((BPy_ViewEdge *) py_ve)->ve );
242
243         Py_RETURN_NONE;
244 }
245
246 PyObject * ViewShape_AddVertex( BPy_ViewShape *self , PyObject *args) {
247         PyObject *py_vv = 0;
248
249         if(!( PyArg_ParseTuple(args, "O!", &ViewVertex_Type, &py_vv) ))
250                 return NULL;
251         
252         self->vs->AddVertex( ((BPy_ViewVertex *) py_vv)->vv );
253
254         Py_RETURN_NONE;
255 }
256
257 // virtual ViewShape *  dupplicate ()
258
259 ///////////////////////////////////////////////////////////////////////////////////////////
260
261 #ifdef __cplusplus
262 }
263 #endif