PyAPI: add utilities PyTuple_SET_ITEMS, Py_INCREF_RET
[blender.git] / source / blender / python / mathutils / mathutils_Euler.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Gilbert
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/mathutils/mathutils_Euler.c
24  *  \ingroup pymathutils
25  */
26
27
28 #include <Python.h>
29
30 #include "mathutils.h"
31
32 #include "BLI_math.h"
33 #include "BLI_utildefines.h"
34 #include "../generic/python_utildefines.h"
35
36 #ifndef MATH_STANDALONE
37 #  include "BLI_dynstr.h"
38 #endif
39
40 #define EULER_SIZE 3
41
42 /* ----------------------------------mathutils.Euler() ------------------- */
43 /* makes a new euler for you to play with */
44 static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
45 {
46         PyObject *seq = NULL;
47         const char *order_str = NULL;
48
49         float eul[EULER_SIZE] = {0.0f, 0.0f, 0.0f};
50         short order = EULER_ORDER_XYZ;
51
52         if (kwds && PyDict_Size(kwds)) {
53                 PyErr_SetString(PyExc_TypeError,
54                                 "mathutils.Euler(): "
55                                 "takes no keyword args");
56                 return NULL;
57         }
58
59         if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
60                 return NULL;
61
62         switch (PyTuple_GET_SIZE(args)) {
63                 case 0:
64                         break;
65                 case 2:
66                         if ((order = euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
67                                 return NULL;
68                         /* fall-through */
69                 case 1:
70                         if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
71                                 return NULL;
72                         break;
73         }
74         return Euler_CreatePyObject(eul, order, type);
75 }
76
77 /* internal use, assume read callback is done */
78 static const char *euler_order_str(EulerObject *self)
79 {
80         static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
81         return order[self->order - EULER_ORDER_XYZ];
82 }
83
84 short euler_order_from_string(const char *str, const char *error_prefix)
85 {
86         if ((str[0] && str[1] && str[2] && str[3] == '\0')) {
87
88 #ifdef __LITTLE_ENDIAN__
89 #  define MAKE_ID3(a, b, c)  (((a)) | ((b) << 8) | ((c) << 16))
90 #else
91 #  define MAKE_ID3(a, b, c)  (((a) << 24) | ((b) << 16) | ((c) << 8))
92 #endif
93
94                 switch (*((PY_INT32_T *)str)) {
95                         case MAKE_ID3('X', 'Y', 'Z'): return EULER_ORDER_XYZ;
96                         case MAKE_ID3('X', 'Z', 'Y'): return EULER_ORDER_XZY;
97                         case MAKE_ID3('Y', 'X', 'Z'): return EULER_ORDER_YXZ;
98                         case MAKE_ID3('Y', 'Z', 'X'): return EULER_ORDER_YZX;
99                         case MAKE_ID3('Z', 'X', 'Y'): return EULER_ORDER_ZXY;
100                         case MAKE_ID3('Z', 'Y', 'X'): return EULER_ORDER_ZYX;
101                 }
102
103 #undef MAKE_ID3
104         }
105
106         PyErr_Format(PyExc_ValueError,
107                      "%s: invalid euler order '%s'",
108                      error_prefix, str);
109         return -1;
110 }
111
112 /* note: BaseMath_ReadCallback must be called beforehand */
113 static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
114 {
115         PyObject *ret;
116         int i;
117
118         ret = PyTuple_New(EULER_SIZE);
119
120         if (ndigits >= 0) {
121                 for (i = 0; i < EULER_SIZE; i++) {
122                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
123                 }
124         }
125         else {
126                 for (i = 0; i < EULER_SIZE; i++) {
127                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
128                 }
129         }
130
131         return ret;
132 }
133
134 /* -----------------------------METHODS----------------------------
135  * return a quaternion representation of the euler */
136
137 PyDoc_STRVAR(Euler_to_quaternion_doc,
138 ".. method:: to_quaternion()\n"
139 "\n"
140 "   Return a quaternion representation of the euler.\n"
141 "\n"
142 "   :return: Quaternion representation of the euler.\n"
143 "   :rtype: :class:`Quaternion`\n"
144 );
145 static PyObject *Euler_to_quaternion(EulerObject *self)
146 {
147         float quat[4];
148
149         if (BaseMath_ReadCallback(self) == -1)
150                 return NULL;
151
152         eulO_to_quat(quat, self->eul, self->order);
153
154         return Quaternion_CreatePyObject(quat, NULL);
155 }
156
157 /* return a matrix representation of the euler */
158 PyDoc_STRVAR(Euler_to_matrix_doc,
159 ".. method:: to_matrix()\n"
160 "\n"
161 "   Return a matrix representation of the euler.\n"
162 "\n"
163 "   :return: A 3x3 roation matrix representation of the euler.\n"
164 "   :rtype: :class:`Matrix`\n"
165 );
166 static PyObject *Euler_to_matrix(EulerObject *self)
167 {
168         float mat[9];
169
170         if (BaseMath_ReadCallback(self) == -1)
171                 return NULL;
172
173         eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
174
175         return Matrix_CreatePyObject(mat, 3, 3, NULL);
176 }
177
178 PyDoc_STRVAR(Euler_zero_doc,
179 ".. method:: zero()\n"
180 "\n"
181 "   Set all values to zero.\n"
182 );
183 static PyObject *Euler_zero(EulerObject *self)
184 {
185         zero_v3(self->eul);
186
187         if (BaseMath_WriteCallback(self) == -1)
188                 return NULL;
189
190         Py_RETURN_NONE;
191 }
192
193 PyDoc_STRVAR(Euler_rotate_axis_doc,
194 ".. method:: rotate_axis(axis, angle)\n"
195 "\n"
196 "   Rotates the euler a certain amount and returning a unique euler rotation\n"
197 "   (no 720 degree pitches).\n"
198 "\n"
199 "   :arg axis: single character in ['X, 'Y', 'Z'].\n"
200 "   :type axis: string\n"
201 "   :arg angle: angle in radians.\n"
202 "   :type angle: float\n"
203 );
204 static PyObject *Euler_rotate_axis(EulerObject *self, PyObject *args)
205 {
206         float angle = 0.0f;
207         int axis; /* actually a character */
208
209         if (!PyArg_ParseTuple(args, "Cf:rotate_axis", &axis, &angle)) {
210                 PyErr_SetString(PyExc_TypeError,
211                                 "Euler.rotate_axis(): "
212                                 "expected an axis 'X', 'Y', 'Z' and an angle (float)");
213                 return NULL;
214         }
215
216         if (!(ELEM(axis, 'X', 'Y', 'Z'))) {
217                 PyErr_SetString(PyExc_ValueError,
218                                 "Euler.rotate_axis(): "
219                                 "expected axis to be 'X', 'Y' or 'Z'");
220                 return NULL;
221         }
222
223         if (BaseMath_ReadCallback(self) == -1)
224                 return NULL;
225
226
227         rotate_eulO(self->eul, self->order, (char)axis, angle);
228
229         (void)BaseMath_WriteCallback(self);
230
231         Py_RETURN_NONE;
232 }
233
234 PyDoc_STRVAR(Euler_rotate_doc,
235 ".. method:: rotate(other)\n"
236 "\n"
237 "   Rotates the euler by another mathutils value.\n"
238 "\n"
239 "   :arg other: rotation component of mathutils value\n"
240 "   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
241 );
242 static PyObject *Euler_rotate(EulerObject *self, PyObject *value)
243 {
244         float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
245
246         if (BaseMath_ReadCallback(self) == -1)
247                 return NULL;
248
249         if (mathutils_any_to_rotmat(other_rmat, value, "euler.rotate(value)") == -1)
250                 return NULL;
251
252         eulO_to_mat3(self_rmat, self->eul, self->order);
253         mul_m3_m3m3(rmat, other_rmat, self_rmat);
254
255         mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
256
257         (void)BaseMath_WriteCallback(self);
258         Py_RETURN_NONE;
259 }
260
261 PyDoc_STRVAR(Euler_make_compatible_doc,
262 ".. method:: make_compatible(other)\n"
263 "\n"
264 "   Make this euler compatible with another,\n"
265 "   so interpolating between them works as intended.\n"
266 "\n"
267 "   .. note:: the rotation order is not taken into account for this function.\n"
268 );
269 static PyObject *Euler_make_compatible(EulerObject *self, PyObject *value)
270 {
271         float teul[EULER_SIZE];
272
273         if (BaseMath_ReadCallback(self) == -1)
274                 return NULL;
275
276         if (mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value,
277                                   "euler.make_compatible(other), invalid 'other' arg") == -1)
278         {
279                 return NULL;
280         }
281
282         compatible_eul(self->eul, teul);
283
284         (void)BaseMath_WriteCallback(self);
285
286         Py_RETURN_NONE;
287 }
288
289 /* ----------------------------Euler.rotate()-----------------------
290  * return a copy of the euler */
291
292 PyDoc_STRVAR(Euler_copy_doc,
293 ".. function:: copy()\n"
294 "\n"
295 "   Returns a copy of this euler.\n"
296 "\n"
297 "   :return: A copy of the euler.\n"
298 "   :rtype: :class:`Euler`\n"
299 "\n"
300 "   .. note:: use this to get a copy of a wrapped euler with\n"
301 "      no reference to the original data.\n"
302 );
303 static PyObject *Euler_copy(EulerObject *self)
304 {
305         if (BaseMath_ReadCallback(self) == -1)
306                 return NULL;
307
308         return Euler_CreatePyObject(self->eul, self->order, Py_TYPE(self));
309 }
310 static PyObject *Euler_deepcopy(EulerObject *self, PyObject *args)
311 {
312         if (!mathutils_deepcopy_args_check(args))
313                 return NULL;
314         return Euler_copy(self);
315 }
316
317 /* ----------------------------print object (internal)--------------
318  * print the object to screen */
319
320 static PyObject *Euler_repr(EulerObject *self)
321 {
322         PyObject *ret, *tuple;
323
324         if (BaseMath_ReadCallback(self) == -1)
325                 return NULL;
326
327         tuple = Euler_ToTupleExt(self, -1);
328
329         ret = PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
330
331         Py_DECREF(tuple);
332         return ret;
333 }
334
335 #ifndef MATH_STANDALONE
336 static PyObject *Euler_str(EulerObject *self)
337 {
338         DynStr *ds;
339
340         if (BaseMath_ReadCallback(self) == -1)
341                 return NULL;
342
343         ds = BLI_dynstr_new();
344
345         BLI_dynstr_appendf(ds, "<Euler (x=%.4f, y=%.4f, z=%.4f), order='%s'>",
346                            self->eul[0], self->eul[1], self->eul[2], euler_order_str(self));
347
348         return mathutils_dynstr_to_py(ds); /* frees ds */
349 }
350 #endif
351
352 static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
353 {
354         PyObject *res;
355         int ok = -1; /* zero is true */
356
357         if (EulerObject_Check(a) && EulerObject_Check(b)) {
358                 EulerObject *eulA = (EulerObject *)a;
359                 EulerObject *eulB = (EulerObject *)b;
360
361                 if (BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1)
362                         return NULL;
363
364                 ok = ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
365         }
366
367         switch (op) {
368                 case Py_NE:
369                         ok = !ok;
370                         /* fall-through */
371                 case Py_EQ:
372                         res = ok ? Py_False : Py_True;
373                         break;
374
375                 case Py_LT:
376                 case Py_LE:
377                 case Py_GT:
378                 case Py_GE:
379                         res = Py_NotImplemented;
380                         break;
381                 default:
382                         PyErr_BadArgument();
383                         return NULL;
384         }
385
386         return Py_INCREF_RET(res);
387 }
388
389 /* ---------------------SEQUENCE PROTOCOLS------------------------ */
390 /* ----------------------------len(object)------------------------ */
391 /* sequence length */
392 static int Euler_len(EulerObject *UNUSED(self))
393 {
394         return EULER_SIZE;
395 }
396 /* ----------------------------object[]--------------------------- */
397 /* sequence accessor (get) */
398 static PyObject *Euler_item(EulerObject *self, int i)
399 {
400         if (i < 0) i = EULER_SIZE - i;
401
402         if (i < 0 || i >= EULER_SIZE) {
403                 PyErr_SetString(PyExc_IndexError,
404                                 "euler[attribute]: "
405                                 "array index out of range");
406                 return NULL;
407         }
408
409         if (BaseMath_ReadIndexCallback(self, i) == -1)
410                 return NULL;
411
412         return PyFloat_FromDouble(self->eul[i]);
413
414 }
415 /* ----------------------------object[]------------------------- */
416 /* sequence accessor (set) */
417 static int Euler_ass_item(EulerObject *self, int i, PyObject *value)
418 {
419         float f = PyFloat_AsDouble(value);
420
421         if (f == -1 && PyErr_Occurred()) {  /* parsed item not a number */
422                 PyErr_SetString(PyExc_TypeError,
423                                 "euler[attribute] = x: "
424                                 "assigned value not a number");
425                 return -1;
426         }
427
428         if (i < 0) i = EULER_SIZE - i;
429
430         if (i < 0 || i >= EULER_SIZE) {
431                 PyErr_SetString(PyExc_IndexError,
432                                 "euler[attribute] = x: "
433                                 "array assignment index out of range");
434                 return -1;
435         }
436
437         self->eul[i] = f;
438
439         if (BaseMath_WriteIndexCallback(self, i) == -1)
440                 return -1;
441
442         return 0;
443 }
444 /* ----------------------------object[z:y]------------------------ */
445 /* sequence slice (get) */
446 static PyObject *Euler_slice(EulerObject *self, int begin, int end)
447 {
448         PyObject *tuple;
449         int count;
450
451         if (BaseMath_ReadCallback(self) == -1)
452                 return NULL;
453
454         CLAMP(begin, 0, EULER_SIZE);
455         if (end < 0) end = (EULER_SIZE + 1) + end;
456         CLAMP(end, 0, EULER_SIZE);
457         begin = MIN2(begin, end);
458
459         tuple = PyTuple_New(end - begin);
460         for (count = begin; count < end; count++) {
461                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
462         }
463
464         return tuple;
465 }
466 /* ----------------------------object[z:y]------------------------ */
467 /* sequence slice (set) */
468 static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
469 {
470         int i, size;
471         float eul[EULER_SIZE];
472
473         if (BaseMath_ReadCallback(self) == -1)
474                 return -1;
475
476         CLAMP(begin, 0, EULER_SIZE);
477         if (end < 0) end = (EULER_SIZE + 1) + end;
478         CLAMP(end, 0, EULER_SIZE);
479         begin = MIN2(begin, end);
480
481         if ((size = mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
482                 return -1;
483
484         if (size != (end - begin)) {
485                 PyErr_SetString(PyExc_ValueError,
486                                 "euler[begin:end] = []: "
487                                 "size mismatch in slice assignment");
488                 return -1;
489         }
490
491         for (i = 0; i < EULER_SIZE; i++)
492                 self->eul[begin + i] = eul[i];
493
494         (void)BaseMath_WriteCallback(self);
495         return 0;
496 }
497
498 static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
499 {
500         if (PyIndex_Check(item)) {
501                 Py_ssize_t i;
502                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
503                 if (i == -1 && PyErr_Occurred())
504                         return NULL;
505                 if (i < 0)
506                         i += EULER_SIZE;
507                 return Euler_item(self, i);
508         }
509         else if (PySlice_Check(item)) {
510                 Py_ssize_t start, stop, step, slicelength;
511
512                 if (PySlice_GetIndicesEx(item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
513                         return NULL;
514
515                 if (slicelength <= 0) {
516                         return PyTuple_New(0);
517                 }
518                 else if (step == 1) {
519                         return Euler_slice(self, start, stop);
520                 }
521                 else {
522                         PyErr_SetString(PyExc_IndexError,
523                                         "slice steps not supported with eulers");
524                         return NULL;
525                 }
526         }
527         else {
528                 PyErr_Format(PyExc_TypeError,
529                              "euler indices must be integers, not %.200s",
530                              Py_TYPE(item)->tp_name);
531                 return NULL;
532         }
533 }
534
535
536 static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
537 {
538         if (PyIndex_Check(item)) {
539                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
540                 if (i == -1 && PyErr_Occurred())
541                         return -1;
542                 if (i < 0)
543                         i += EULER_SIZE;
544                 return Euler_ass_item(self, i, value);
545         }
546         else if (PySlice_Check(item)) {
547                 Py_ssize_t start, stop, step, slicelength;
548
549                 if (PySlice_GetIndicesEx(item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
550                         return -1;
551
552                 if (step == 1)
553                         return Euler_ass_slice(self, start, stop, value);
554                 else {
555                         PyErr_SetString(PyExc_IndexError,
556                                         "slice steps not supported with euler");
557                         return -1;
558                 }
559         }
560         else {
561                 PyErr_Format(PyExc_TypeError,
562                              "euler indices must be integers, not %.200s",
563                              Py_TYPE(item)->tp_name);
564                 return -1;
565         }
566 }
567
568 /* -----------------PROTCOL DECLARATIONS-------------------------- */
569 static PySequenceMethods Euler_SeqMethods = {
570         (lenfunc) Euler_len,                    /* sq_length */
571         (binaryfunc) NULL,                      /* sq_concat */
572         (ssizeargfunc) NULL,                    /* sq_repeat */
573         (ssizeargfunc) Euler_item,              /* sq_item */
574         (ssizessizeargfunc) NULL,               /* sq_slice, deprecated  */
575         (ssizeobjargproc) Euler_ass_item,       /* sq_ass_item */
576         (ssizessizeobjargproc) NULL,            /* sq_ass_slice, deprecated */
577         (objobjproc) NULL,                      /* sq_contains */
578         (binaryfunc) NULL,                      /* sq_inplace_concat */
579         (ssizeargfunc) NULL,                    /* sq_inplace_repeat */
580 };
581
582 static PyMappingMethods Euler_AsMapping = {
583         (lenfunc)Euler_len,
584         (binaryfunc)Euler_subscript,
585         (objobjargproc)Euler_ass_subscript
586 };
587
588 /* euler axis, euler.x/y/z */
589
590 PyDoc_STRVAR(Euler_axis_doc,
591 "Euler axis angle in radians.\n\n:type: float"
592 );
593 static PyObject *Euler_axis_get(EulerObject *self, void *type)
594 {
595         return Euler_item(self, GET_INT_FROM_POINTER(type));
596 }
597
598 static int Euler_axis_set(EulerObject *self, PyObject *value, void *type)
599 {
600         return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
601 }
602
603 /* rotation order */
604
605 PyDoc_STRVAR(Euler_order_doc,
606 "Euler rotation order.\n\n:type: string in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']"
607 );
608 static PyObject *Euler_order_get(EulerObject *self, void *UNUSED(closure))
609 {
610         if (BaseMath_ReadCallback(self) == -1) /* can read order too */
611                 return NULL;
612
613         return PyUnicode_FromString(euler_order_str(self));
614 }
615
616 static int Euler_order_set(EulerObject *self, PyObject *value, void *UNUSED(closure))
617 {
618         const char *order_str = _PyUnicode_AsString(value);
619         short order = euler_order_from_string(order_str, "euler.order");
620
621         if (order == -1)
622                 return -1;
623
624         self->order = order;
625         (void)BaseMath_WriteCallback(self); /* order can be written back */
626         return 0;
627 }
628
629 /*****************************************************************************/
630 /* Python attributes get/set structure:                                      */
631 /*****************************************************************************/
632 static PyGetSetDef Euler_getseters[] = {
633         {(char *)"x", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)0},
634         {(char *)"y", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)1},
635         {(char *)"z", (getter)Euler_axis_get, (setter)Euler_axis_set, Euler_axis_doc, (void *)2},
636         {(char *)"order", (getter)Euler_order_get, (setter)Euler_order_set, Euler_order_doc, (void *)NULL},
637
638         {(char *)"is_wrapped", (getter)BaseMathObject_is_wrapped_get, (setter)NULL, BaseMathObject_is_wrapped_doc, NULL},
639         {(char *)"owner", (getter)BaseMathObject_owner_get, (setter)NULL, BaseMathObject_owner_doc, NULL},
640         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
641 };
642
643
644 /* -----------------------METHOD DEFINITIONS ---------------------- */
645 static struct PyMethodDef Euler_methods[] = {
646         {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
647         {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
648         {"to_quaternion", (PyCFunction) Euler_to_quaternion, METH_NOARGS, Euler_to_quaternion_doc},
649         {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
650         {"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
651         {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
652         {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
653         {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
654         {"__deepcopy__", (PyCFunction) Euler_deepcopy, METH_VARARGS, Euler_copy_doc},
655         {NULL, NULL, 0, NULL}
656 };
657
658 /* ------------------PY_OBECT DEFINITION-------------------------- */
659 PyDoc_STRVAR(euler_doc,
660 "This object gives access to Eulers in Blender."
661 );
662 PyTypeObject euler_Type = {
663         PyVarObject_HEAD_INIT(NULL, 0)
664         "Euler",                        /* tp_name */
665         sizeof(EulerObject),            /* tp_basicsize */
666         0,                              /* tp_itemsize */
667         (destructor)BaseMathObject_dealloc,     /* tp_dealloc */
668         NULL,                           /* tp_print */
669         NULL,                           /* tp_getattr */
670         NULL,                           /* tp_setattr */
671         NULL,                           /* tp_compare */
672         (reprfunc) Euler_repr,          /* tp_repr */
673         NULL,                           /* tp_as_number */
674         &Euler_SeqMethods,              /* tp_as_sequence */
675         &Euler_AsMapping,               /* tp_as_mapping */
676         NULL,                           /* tp_hash */
677         NULL,                           /* tp_call */
678 #ifndef MATH_STANDALONE
679         (reprfunc) Euler_str,           /* tp_str */
680 #else
681         NULL,                           /* tp_str */
682 #endif
683         NULL,                           /* tp_getattro */
684         NULL,                           /* tp_setattro */
685         NULL,                           /* tp_as_buffer */
686         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
687         euler_doc, /* tp_doc */
688         (traverseproc)BaseMathObject_traverse,  /* tp_traverse */
689         (inquiry)BaseMathObject_clear,  /* tp_clear */
690         (richcmpfunc)Euler_richcmpr,    /* tp_richcompare */
691         0,                              /* tp_weaklistoffset */
692         NULL,                           /* tp_iter */
693         NULL,                           /* tp_iternext */
694         Euler_methods,                  /* tp_methods */
695         NULL,                           /* tp_members */
696         Euler_getseters,                /* tp_getset */
697         NULL,                           /* tp_base */
698         NULL,                           /* tp_dict */
699         NULL,                           /* tp_descr_get */
700         NULL,                           /* tp_descr_set */
701         0,                              /* tp_dictoffset */
702         NULL,                           /* tp_init */
703         NULL,                           /* tp_alloc */
704         Euler_new,                      /* tp_new */
705         NULL,                           /* tp_free */
706         NULL,                           /* tp_is_gc */
707         NULL,                           /* tp_bases */
708         NULL,                           /* tp_mro */
709         NULL,                           /* tp_cache */
710         NULL,                           /* tp_subclasses */
711         NULL,                           /* tp_weaklist */
712         NULL                            /* tp_del */
713 };
714
715
716 PyObject *Euler_CreatePyObject(
717         const float eul[3], const short order,
718         PyTypeObject *base_type)
719 {
720         EulerObject *self;
721         float *eul_alloc;
722
723         eul_alloc = PyMem_Malloc(EULER_SIZE * sizeof(float));
724         if (UNLIKELY(eul_alloc == NULL)) {
725                 PyErr_SetString(PyExc_MemoryError,
726                                 "Euler(): "
727                                 "problem allocating data");
728                 return NULL;
729         }
730
731         self = BASE_MATH_NEW(EulerObject, euler_Type, base_type);
732         if (self) {
733                 self->eul = eul_alloc;
734
735                 /* init callbacks as NULL */
736                 self->cb_user = NULL;
737                 self->cb_type = self->cb_subtype = 0;
738
739                 if (eul) {
740                         copy_v3_v3(self->eul, eul);
741                 }
742                 else {
743                         zero_v3(self->eul);
744                 }
745
746                 self->flag = BASE_MATH_FLAG_DEFAULT;
747                 self->order = order;
748         }
749         else {
750                 PyMem_Free(eul_alloc);
751         }
752
753         return (PyObject *)self;
754 }
755
756 PyObject *Euler_CreatePyObject_wrap(
757         float eul[3], const short order,
758         PyTypeObject *base_type)
759 {
760         EulerObject *self;
761
762         self = BASE_MATH_NEW(EulerObject, euler_Type, base_type);
763         if (self) {
764                 /* init callbacks as NULL */
765                 self->cb_user = NULL;
766                 self->cb_type = self->cb_subtype = 0;
767
768                 self->eul = eul;
769                 self->flag = BASE_MATH_FLAG_DEFAULT | BASE_MATH_FLAG_IS_WRAP;
770
771                 self->order = order;
772         }
773
774         return (PyObject *)self;
775 }
776
777 PyObject *Euler_CreatePyObject_cb(
778         PyObject *cb_user, const short order,
779         unsigned char cb_type, unsigned char cb_subtype)
780 {
781         EulerObject *self = (EulerObject *)Euler_CreatePyObject(NULL, order, NULL);
782         if (self) {
783                 Py_INCREF(cb_user);
784                 self->cb_user         = cb_user;
785                 self->cb_type         = cb_type;
786                 self->cb_subtype      = cb_subtype;
787                 PyObject_GC_Track(self);
788         }
789
790         return (PyObject *)self;
791 }