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