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