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