Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / BPy_Nature.cpp
1 #include "BPy_Nature.h"
2
3 #include "BPy_Convert.h"
4
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8
9 ///////////////////////////////////////////////////////////////////////////////////////////
10
11 static PyObject *BPy_Nature___and__(PyObject *a, PyObject *b);
12 static PyObject *BPy_Nature___xor__(PyObject *a, PyObject *b);
13 static PyObject *BPy_Nature___or__(PyObject *a, PyObject *b);
14
15 /*-----------------------BPy_Nature number method definitions --------------------*/
16
17 PyNumberMethods nature_as_number = {
18         0,                              /* binaryfunc nb_add */
19         0,                              /* binaryfunc nb_subtract */
20         0,                              /* binaryfunc nb_multiply */
21         0,                              /* binaryfunc nb_remainder */
22         0,                              /* binaryfunc nb_divmod */
23         0,                              /* ternaryfunc nb_power */
24         0,                              /* unaryfunc nb_negative */
25         0,                              /* unaryfunc nb_positive */
26         0,                              /* unaryfunc nb_absolute */
27         0,                              /* inquiry nb_bool */
28         0,                              /* unaryfunc nb_invert */
29         0,                              /* binaryfunc nb_lshift */
30         0,                              /* binaryfunc nb_rshift */
31         (binaryfunc)BPy_Nature___and__, /* binaryfunc nb_and */
32         (binaryfunc)BPy_Nature___xor__, /* binaryfunc nb_xor */
33         (binaryfunc)BPy_Nature___or__,  /* binaryfunc nb_or */
34         0,                              /* unaryfunc nb_int */
35         0,                              /* void *nb_reserved */
36         0,                              /* unaryfunc nb_float */
37         0,                              /* binaryfunc nb_inplace_add */
38         0,                              /* binaryfunc nb_inplace_subtract */
39         0,                              /* binaryfunc nb_inplace_multiply */
40         0,                              /* binaryfunc nb_inplace_remainder */
41         0,                              /* ternaryfunc nb_inplace_power */
42         0,                              /* binaryfunc nb_inplace_lshift */
43         0,                              /* binaryfunc nb_inplace_rshift */
44         0,                              /* binaryfunc nb_inplace_and */
45         0,                              /* binaryfunc nb_inplace_xor */
46         0,                              /* binaryfunc nb_inplace_or */
47         0,                              /* binaryfunc nb_floor_divide */
48         0,                              /* binaryfunc nb_true_divide */
49         0,                              /* binaryfunc nb_inplace_floor_divide */
50         0,                              /* binaryfunc nb_inplace_true_divide */
51         0,                              /* unaryfunc nb_index */
52 };
53
54 /*-----------------------BPy_Nature type definition ------------------------------*/
55
56 PyTypeObject Nature_Type = {
57         PyObject_HEAD_INIT(NULL)
58         "Nature",                       /* tp_name */
59         sizeof(PyLongObject),           /* tp_basicsize */
60         0,                              /* tp_itemsize */
61         0,                              /* tp_dealloc */
62         0,                              /* tp_print */
63         0,                              /* tp_getattr */
64         0,                              /* tp_setattr */
65         0,                              /* tp_reserved */
66         0,                              /* tp_repr */
67         &nature_as_number,              /* tp_as_number */
68         0,                              /* tp_as_sequence */
69         0,                              /* tp_as_mapping */
70         0,                              /* tp_hash  */
71         0,                              /* tp_call */
72         0,                              /* tp_str */
73         0,                              /* tp_getattro */
74         0,                              /* tp_setattro */
75         0,                              /* tp_as_buffer */
76         Py_TPFLAGS_DEFAULT,             /* tp_flags */
77         "Nature objects",               /* tp_doc */
78         0,                              /* tp_traverse */
79         0,                              /* tp_clear */
80         0,                              /* tp_richcompare */
81         0,                              /* tp_weaklistoffset */
82         0,                              /* tp_iter */
83         0,                              /* tp_iternext */
84         0,                              /* tp_methods */
85         0,                              /* tp_members */
86         0,                              /* tp_getset */
87         &PyLong_Type,                   /* tp_base */
88         0,                              /* tp_dict */
89         0,                              /* tp_descr_get */
90         0,                              /* tp_descr_set */
91         0,                              /* tp_dictoffset */
92         0,                              /* tp_init */
93         0,                              /* tp_alloc */
94         0,                              /* tp_new */
95 };
96
97 /*-----------------------BPy_Nature instance definitions ----------------------------------*/
98
99 static PyLongObject _Nature_POINT = {
100         PyVarObject_HEAD_INIT(&Nature_Type, 1)
101         { Nature::POINT }
102 };
103 static PyLongObject _Nature_S_VERTEX = {
104         PyVarObject_HEAD_INIT(&Nature_Type, 1)
105         { Nature::S_VERTEX }
106 };
107 static PyLongObject _Nature_VIEW_VERTEX = {
108         PyVarObject_HEAD_INIT(&Nature_Type, 1)
109         { Nature::VIEW_VERTEX }
110 };
111 static PyLongObject _Nature_NON_T_VERTEX = {
112         PyVarObject_HEAD_INIT(&Nature_Type, 1)
113         { Nature::NON_T_VERTEX }
114 };
115 static PyLongObject _Nature_T_VERTEX = {
116         PyVarObject_HEAD_INIT(&Nature_Type, 1)
117         { Nature::T_VERTEX }
118 };
119 static PyLongObject _Nature_CUSP = {
120         PyVarObject_HEAD_INIT(&Nature_Type, 1)
121         { Nature::CUSP }
122 };
123 static PyLongObject _Nature_NO_FEATURE = {
124         PyVarObject_HEAD_INIT(&Nature_Type, 1)
125         { Nature::NO_FEATURE }
126 };
127 static PyLongObject _Nature_SILHOUETTE = {
128         PyVarObject_HEAD_INIT(&Nature_Type, 1)
129         { Nature::SILHOUETTE }
130 };
131 static PyLongObject _Nature_BORDER = {
132         PyVarObject_HEAD_INIT(&Nature_Type, 1)
133         { Nature::BORDER }
134 };
135 static PyLongObject _Nature_CREASE = {
136         PyVarObject_HEAD_INIT(&Nature_Type, 1)
137         { Nature::CREASE }
138 };
139 static PyLongObject _Nature_RIDGE = {
140         PyVarObject_HEAD_INIT(&Nature_Type, 1)
141         { Nature::RIDGE }
142 };
143 static PyLongObject _Nature_VALLEY = {
144         PyVarObject_HEAD_INIT(&Nature_Type, 1)
145         { Nature::VALLEY }
146 };
147 static PyLongObject _Nature_SUGGESTIVE_CONTOUR = {
148         PyVarObject_HEAD_INIT(&Nature_Type, 1)
149         { Nature::SUGGESTIVE_CONTOUR }
150 };
151
152 #define BPy_Nature_POINT               ((PyObject *)&_Nature_POINT)
153 #define BPy_Nature_S_VERTEX            ((PyObject *)&_Nature_S_VERTEX)
154 #define BPy_Nature_VIEW_VERTEX         ((PyObject *)&_Nature_VIEW_VERTEX)
155 #define BPy_Nature_NON_T_VERTEX        ((PyObject *)&_Nature_NON_T_VERTEX)
156 #define BPy_Nature_T_VERTEX            ((PyObject *)&_Nature_T_VERTEX)
157 #define BPy_Nature_CUSP                ((PyObject *)&_Nature_CUSP)
158 #define BPy_Nature_NO_FEATURE          ((PyObject *)&_Nature_NO_FEATURE)
159 #define BPy_Nature_SILHOUETTE          ((PyObject *)&_Nature_SILHOUETTE)
160 #define BPy_Nature_BORDER              ((PyObject *)&_Nature_BORDER)
161 #define BPy_Nature_CREASE              ((PyObject *)&_Nature_CREASE)
162 #define BPy_Nature_RIDGE               ((PyObject *)&_Nature_RIDGE)
163 #define BPy_Nature_VALLEY              ((PyObject *)&_Nature_VALLEY)
164 #define BPy_Nature_SUGGESTIVE_CONTOUR  ((PyObject *)&_Nature_SUGGESTIVE_CONTOUR)
165
166 //-------------------MODULE INITIALIZATION--------------------------------
167 int Nature_Init( PyObject *module )
168 {       
169         if( module == NULL )
170                 return -1;
171
172         if( PyType_Ready( &Nature_Type ) < 0 )
173                 return -1;
174         Py_INCREF( &Nature_Type );
175         PyModule_AddObject(module, "Nature", (PyObject *)&Nature_Type);
176
177         // VertexNature
178         PyDict_SetItemString( Nature_Type.tp_dict, "POINT", BPy_Nature_POINT);
179         PyDict_SetItemString( Nature_Type.tp_dict, "S_VERTEX", BPy_Nature_S_VERTEX);
180         PyDict_SetItemString( Nature_Type.tp_dict, "VIEW_VERTEX", BPy_Nature_VIEW_VERTEX );
181         PyDict_SetItemString( Nature_Type.tp_dict, "NON_T_VERTEX", BPy_Nature_NON_T_VERTEX );
182         PyDict_SetItemString( Nature_Type.tp_dict, "T_VERTEX", BPy_Nature_T_VERTEX );
183         PyDict_SetItemString( Nature_Type.tp_dict, "CUSP", BPy_Nature_CUSP );
184         
185         // EdgeNature
186         PyDict_SetItemString( Nature_Type.tp_dict, "NO_FEATURE", BPy_Nature_NO_FEATURE );
187         PyDict_SetItemString( Nature_Type.tp_dict, "SILHOUETTE", BPy_Nature_SILHOUETTE );
188         PyDict_SetItemString( Nature_Type.tp_dict, "BORDER", BPy_Nature_BORDER );
189         PyDict_SetItemString( Nature_Type.tp_dict, "CREASE", BPy_Nature_CREASE );
190         PyDict_SetItemString( Nature_Type.tp_dict, "RIDGE", BPy_Nature_RIDGE );
191         PyDict_SetItemString( Nature_Type.tp_dict, "VALLEY", BPy_Nature_VALLEY );
192         PyDict_SetItemString( Nature_Type.tp_dict, "SUGGESTIVE_CONTOUR", BPy_Nature_SUGGESTIVE_CONTOUR );
193
194         return 0;
195 }
196
197 static PyObject *
198 BPy_Nature_bitwise(PyObject *a, int op, PyObject *b)
199 {
200         BPy_Nature *result;
201
202         if (!BPy_Nature_Check(a) || !BPy_Nature_Check(b)) {
203                 PyErr_SetString(PyExc_TypeError, "operands must be a Nature object");
204                 return NULL;
205         }
206         result = PyObject_NewVar(BPy_Nature, &Nature_Type, 1);
207         if (!result)
208                 return NULL;
209         assert(Py_SIZE(a) == 1 && Py_SIZE(b) == 1 && Py_SIZE(result) == 1);
210         switch (op) {
211         case '&':
212                 result->i.ob_digit[0] = (((PyLongObject *)a)->ob_digit[0]) & (((PyLongObject *)b)->ob_digit)[0];
213                 break;
214         case '^':
215                 result->i.ob_digit[0] = (((PyLongObject *)a)->ob_digit[0]) ^ (((PyLongObject *)b)->ob_digit)[0];
216                 break;
217         case '|':
218                 result->i.ob_digit[0] = (((PyLongObject *)a)->ob_digit[0]) | (((PyLongObject *)b)->ob_digit)[0];
219                 break;
220         }
221         return (PyObject *)result;
222 }
223
224 static PyObject *
225 BPy_Nature___and__(PyObject *a, PyObject *b)
226 {
227         return BPy_Nature_bitwise(a, '&', b);
228 }
229
230 static PyObject *
231 BPy_Nature___xor__(PyObject *a, PyObject *b)
232 {
233         return BPy_Nature_bitwise(a, '^', b);
234 }
235
236 static PyObject *
237 BPy_Nature___or__(PyObject *a, PyObject *b)
238 {
239         return BPy_Nature_bitwise(a, '|', b);
240 }
241
242 ///////////////////////////////////////////////////////////////////////////////////////////
243
244 #ifdef __cplusplus
245 }
246 #endif
247