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