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