Mathutils API: Euler support for rotation order.
[blender.git] / source / blender / python / generic / euler.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joseph Gilbert
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "Mathutils.h"
30
31 #include "BLI_math.h"
32 #include "BKE_utildefines.h"
33 #include "BLI_blenlib.h"
34
35
36 //----------------------------------Mathutils.Euler() -------------------
37 //makes a new euler for you to play with
38 static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
39 {
40         PyObject *listObject = NULL;
41         int size, i;
42         float eul[3];
43         PyObject *e;
44         short order= 0;  // TODO, add order option
45
46         size = PyTuple_GET_SIZE(args);
47         if (size == 1) {
48                 listObject = PyTuple_GET_ITEM(args, 0);
49                 if (PySequence_Check(listObject)) {
50                         size = PySequence_Length(listObject);
51                 } else { // Single argument was not a sequence
52                         PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
53                         return NULL;
54                 }
55         } else if (size == 0) {
56                 //returns a new empty 3d euler
57                 return newEulerObject(NULL, order, Py_NEW, NULL);
58         } else {
59                 listObject = args;
60         }
61
62         if (size != 3) { // Invalid euler size
63                 PyErr_SetString(PyExc_AttributeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
64                 return NULL;
65         }
66
67         for (i=0; i<size; i++) {
68                 e = PySequence_GetItem(listObject, i);
69                 if (e == NULL) { // Failed to read sequence
70                         Py_DECREF(listObject);
71                         PyErr_SetString(PyExc_RuntimeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
72                         return NULL;
73                 }
74
75                 eul[i]= (float)PyFloat_AsDouble(e);
76                 Py_DECREF(e);
77                 
78                 if(eul[i]==-1 && PyErr_Occurred()) { // parsed item is not a number
79                         PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
80                         return NULL;
81                 }
82         }
83         return newEulerObject(eul, order, Py_NEW, NULL);
84 }
85
86 short euler_order_from_string(const char *str, const char *error_prefix)
87 {
88         if((str[0] && str[1] && str[2] && str[3]=='\0')) {
89                 switch(*((int32_t *)str)) {
90                         case 'X'|'Y'<<8|'Z'<<16:        return 0;
91                         case 'X'|'Z'<<8|'Y'<<16:        return 1;
92                         case 'Y'|'X'<<8|'Z'<<16:        return 2;
93                         case 'Y'|'Z'<<8|'X'<<16:        return 3;
94                         case 'Z'|'X'<<8|'Y'<<16:        return 4;
95                         case 'Z'|'Y'<<8|'X'<<16:        return 5;
96                 }
97         }
98
99         PyErr_Format(PyExc_TypeError, "%s: invalid euler order '%s'", error_prefix, str);
100         return -1;
101 }
102
103 //-----------------------------METHODS----------------------------
104 //----------------------------Euler.toQuat()----------------------
105 //return a quaternion representation of the euler
106
107 static char Euler_ToQuat_doc[] =
108 ".. method:: to_quat()\n"
109 "\n"
110 "   Return a quaternion representation of the euler.\n"
111 "\n"
112 "   :return: Quaternion representation of the euler.\n"
113 "   :rtype: :class:`Quaternion`\n";
114
115 static PyObject *Euler_ToQuat(EulerObject * self)
116 {
117         float quat[4];
118
119         if(!BaseMath_ReadCallback(self))
120                 return NULL;
121
122         if(self->order==0)      eul_to_quat(quat, self->eul);
123         else                            eulO_to_quat(quat, self->eul, self->order);
124
125         return newQuaternionObject(quat, Py_NEW, NULL);
126 }
127 //----------------------------Euler.toMatrix()---------------------
128 //return a matrix representation of the euler
129 static char Euler_ToMatrix_doc[] =
130 ".. method:: to_matrix()\n"
131 "\n"
132 "   Return a matrix representation of the euler.\n"
133 "\n"
134 "   :return: A 3x3 roation matrix representation of the euler.\n"
135 "   :rtype: :class:`Matrix`\n";
136
137 static PyObject *Euler_ToMatrix(EulerObject * self)
138 {
139         float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
140
141         if(!BaseMath_ReadCallback(self))
142                 return NULL;
143
144         if(self->order==0)      eul_to_mat3((float (*)[3])mat, self->eul);
145         else                            eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
146
147         return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
148 }
149 //----------------------------Euler.unique()-----------------------
150 //sets the x,y,z values to a unique euler rotation
151 // TODO, check if this works with rotation order!!!
152 static char Euler_Unique_doc[] =
153 ".. method:: unique()\n"
154 "\n"
155 "   Calculate a unique rotation for this euler. Avoids gimble lock.\n"
156 "\n"
157 "   :return: an instance of itself\n"
158 "   :rtype: :class:`Euler`\n";
159
160 static PyObject *Euler_Unique(EulerObject * self)
161 {
162 #define PI_2            (Py_PI * 2.0)
163 #define PI_HALF         (Py_PI / 2.0)
164 #define PI_INV          (1.0 / Py_PI)
165
166         double heading, pitch, bank;
167
168         if(!BaseMath_ReadCallback(self))
169                 return NULL;
170
171         heading = self->eul[0];
172         pitch = self->eul[1];
173         bank = self->eul[2];
174
175         //wrap heading in +180 / -180
176         pitch += Py_PI;
177         pitch -= floor(pitch * PI_INV) * PI_2;
178         pitch -= Py_PI;
179
180
181         if(pitch < -PI_HALF) {
182                 pitch = -Py_PI - pitch;
183                 heading += Py_PI;
184                 bank += Py_PI;
185         } else if(pitch > PI_HALF) {
186                 pitch = Py_PI - pitch;
187                 heading += Py_PI;
188                 bank += Py_PI;
189         }
190         //gimbal lock test
191         if(fabs(pitch) > PI_HALF - 1e-4) {
192                 heading += bank;
193                 bank = 0.0f;
194         } else {
195                 bank += Py_PI;
196                 bank -= (floor(bank * PI_INV)) * PI_2;
197                 bank -= Py_PI;
198         }
199
200         heading += Py_PI;
201         heading -= (floor(heading * PI_INV)) * PI_2;
202         heading -= Py_PI;
203
204         BaseMath_WriteCallback(self);
205         Py_INCREF(self);
206         return (PyObject *)self;
207 }
208 //----------------------------Euler.zero()-------------------------
209 //sets the euler to 0,0,0
210 static char Euler_Zero_doc[] =
211 ".. method:: zero()\n"
212 "\n"
213 "   Set all values to zero.\n"
214 "\n"
215 "   :return: an instance of itself\n"
216 "   :rtype: :class:`Euler`\n";
217
218 static PyObject *Euler_Zero(EulerObject * self)
219 {
220         self->eul[0] = 0.0;
221         self->eul[1] = 0.0;
222         self->eul[2] = 0.0;
223
224         BaseMath_WriteCallback(self);
225         Py_INCREF(self);
226         return (PyObject *)self;
227 }
228 //----------------------------Euler.rotate()-----------------------
229 //rotates a euler a certain amount and returns the result
230 //should return a unique euler rotation (i.e. no 720 degree pitches :)
231 static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
232 {
233         float angle = 0.0f;
234         char *axis;
235
236         if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
237                 PyErr_SetString(PyExc_TypeError, "euler.rotate():expected angle (float) and axis (x,y,z)");
238                 return NULL;
239         }
240         if(ELEM3(*axis, 'x', 'y', 'z') && axis[1]=='\0'){
241                 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'x', 'y' or 'z'");
242                 return NULL;
243         }
244
245         if(!BaseMath_ReadCallback(self))
246                 return NULL;
247
248         if(self->order == 0)    rotate_eul(self->eul, *axis, angle);
249         else                                    rotate_eulO(self->eul, self->order, *axis, angle);
250
251         BaseMath_WriteCallback(self);
252         Py_INCREF(self);
253         return (PyObject *)self;
254 }
255
256 static char Euler_MakeCompatible_doc[] =
257 ".. method:: make_compatible(other)\n"
258 "\n"
259 "   Make this euler compatible with another, so interpolating between them works as intended.\n"
260 "\n"
261 "   :arg other: make compatible with this rotation.\n"
262 "   :type other: :class:`Euler`\n"
263 "   :return: an instance of itself.\n"
264 "   :rtype: :class:`Euler`\n"
265 "\n"
266 "   .. note:: the order of eulers must match or an exception is raised.\n";
267
268 static PyObject *Euler_MakeCompatible(EulerObject * self, EulerObject *value)
269 {
270         if(!EulerObject_Check(value)) {
271                 PyErr_SetString(PyExc_TypeError, "euler.make_compatible(euler): expected a single euler argument.");
272                 return NULL;
273         }
274         
275         if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
276                 return NULL;
277
278         if(self->order != value->order) {
279                 PyErr_SetString(PyExc_ValueError, "euler.make_compatible(euler): rotation orders don't match\n");
280                 return NULL;
281         }
282
283         compatible_eul(self->eul, value->eul);
284
285         BaseMath_WriteCallback(self);
286         Py_INCREF(self);
287         return (PyObject *)self;
288 }
289
290 //----------------------------Euler.rotate()-----------------------
291 // return a copy of the euler
292
293 static char Euler_copy_doc[] =
294 ".. function:: copy()\n"
295 "\n"
296 "   Returns a copy of this euler.\n"
297 "\n"
298 "   :return: A copy of the euler.\n"
299 "   :rtype: :class:`Euler`\n"
300 "\n"
301 "   .. note:: use this to get a copy of a wrapped euler with no reference to the original data.\n";
302
303 static PyObject *Euler_copy(EulerObject * self, PyObject *args)
304 {
305         if(!BaseMath_ReadCallback(self))
306                 return NULL;
307
308         return newEulerObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
309 }
310
311 //----------------------------print object (internal)--------------
312 //print the object to screen
313 static PyObject *Euler_repr(EulerObject * self)
314 {
315         char str[64];
316
317         if(!BaseMath_ReadCallback(self))
318                 return NULL;
319
320         sprintf(str, "[%.6f, %.6f, %.6f](euler)", self->eul[0], self->eul[1], self->eul[2]);
321         return PyUnicode_FromString(str);
322 }
323 //------------------------tp_richcmpr
324 //returns -1 execption, 0 false, 1 true
325 static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
326 {
327         EulerObject *eulA = NULL, *eulB = NULL;
328         int result = 0;
329
330         if(EulerObject_Check(objectA)) {
331                 eulA = (EulerObject*)objectA;
332                 if(!BaseMath_ReadCallback(eulA))
333                         return NULL;
334         }
335         if(EulerObject_Check(objectB)) {
336                 eulB = (EulerObject*)objectB;
337                 if(!BaseMath_ReadCallback(eulB))
338                         return NULL;
339         }
340
341         if (!eulA || !eulB){
342                 if (comparison_type == Py_NE){
343                         Py_RETURN_TRUE;
344                 }else{
345                         Py_RETURN_FALSE;
346                 }
347         }
348         eulA = (EulerObject*)objectA;
349         eulB = (EulerObject*)objectB;
350
351         switch (comparison_type){
352                 case Py_EQ:
353                         result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
354                         break;
355                 case Py_NE:
356                         result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
357                         break;
358                 default:
359                         printf("The result of the comparison could not be evaluated");
360                         break;
361         }
362         if (result == 1){
363                 Py_RETURN_TRUE;
364         }else{
365                 Py_RETURN_FALSE;
366         }
367 }
368
369 //---------------------SEQUENCE PROTOCOLS------------------------
370 //----------------------------len(object)------------------------
371 //sequence length
372 static int Euler_len(EulerObject * self)
373 {
374         return 3;
375 }
376 //----------------------------object[]---------------------------
377 //sequence accessor (get)
378 static PyObject *Euler_item(EulerObject * self, int i)
379 {
380         if(i<0) i= 3-i;
381         
382         if(i < 0 || i >= 3) {
383                 PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
384                 return NULL;
385         }
386
387         if(!BaseMath_ReadIndexCallback(self, i))
388                 return NULL;
389
390         return PyFloat_FromDouble(self->eul[i]);
391
392 }
393 //----------------------------object[]-------------------------
394 //sequence accessor (set)
395 static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
396 {
397         float f = PyFloat_AsDouble(value);
398
399         if(f == -1 && PyErr_Occurred()) { // parsed item not a number
400                 PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
401                 return -1;
402         }
403
404         if(i<0) i= 3-i;
405         
406         if(i < 0 || i >= 3){
407                 PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n");
408                 return -1;
409         }
410         
411         self->eul[i] = f;
412
413         if(!BaseMath_WriteIndexCallback(self, i))
414                 return -1;
415
416         return 0;
417 }
418 //----------------------------object[z:y]------------------------
419 //sequence slice (get)
420 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
421 {
422         PyObject *list = NULL;
423         int count;
424
425         if(!BaseMath_ReadCallback(self))
426                 return NULL;
427
428         CLAMP(begin, 0, 3);
429         if (end<0) end= 4+end;
430         CLAMP(end, 0, 3);
431         begin = MIN2(begin,end);
432
433         list = PyList_New(end - begin);
434         for(count = begin; count < end; count++) {
435                 PyList_SetItem(list, count - begin,
436                                 PyFloat_FromDouble(self->eul[count]));
437         }
438
439         return list;
440 }
441 //----------------------------object[z:y]------------------------
442 //sequence slice (set)
443 static int Euler_ass_slice(EulerObject * self, int begin, int end,
444                              PyObject * seq)
445 {
446         int i, y, size = 0;
447         float eul[3];
448         PyObject *e;
449
450         if(!BaseMath_ReadCallback(self))
451                 return -1;
452
453         CLAMP(begin, 0, 3);
454         if (end<0) end= 4+end;
455         CLAMP(end, 0, 3);
456         begin = MIN2(begin,end);
457
458         size = PySequence_Length(seq);
459         if(size != (end - begin)){
460                 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
461                 return -1;
462         }
463
464         for (i = 0; i < size; i++) {
465                 e = PySequence_GetItem(seq, i);
466                 if (e == NULL) { // Failed to read sequence
467                         PyErr_SetString(PyExc_RuntimeError, "euler[begin:end] = []: unable to read sequence");
468                         return -1;
469                 }
470
471                 eul[i] = (float)PyFloat_AsDouble(e);
472                 Py_DECREF(e);
473
474                 if(eul[i]==-1 && PyErr_Occurred()) { // parsed item not a number
475                         PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: sequence argument not a number");
476                         return -1;
477                 }
478         }
479         //parsed well - now set in vector
480         for(y = 0; y < 3; y++){
481                 self->eul[begin + y] = eul[y];
482         }
483
484         BaseMath_WriteCallback(self);
485         return 0;
486 }
487 //-----------------PROTCOL DECLARATIONS--------------------------
488 static PySequenceMethods Euler_SeqMethods = {
489         (lenfunc) Euler_len,                                            /* sq_length */
490         (binaryfunc) 0,                                                         /* sq_concat */
491         (ssizeargfunc) 0,                                                               /* sq_repeat */
492         (ssizeargfunc) Euler_item,                                      /* sq_item */
493         (ssizessizeargfunc) Euler_slice,                                /* sq_slice */
494         (ssizeobjargproc) Euler_ass_item,                               /* sq_ass_item */
495         (ssizessizeobjargproc) Euler_ass_slice,                 /* sq_ass_slice */
496 };
497
498
499 /*
500  * vector axis, vector.x/y/z/w
501  */
502 static PyObject *Euler_getAxis( EulerObject * self, void *type )
503 {
504         return Euler_item(self, GET_INT_FROM_POINTER(type));
505 }
506
507 static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
508 {
509         return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
510 }
511
512 /* rotation order */
513 static PyObject *Euler_getOrder(EulerObject *self, void *type)
514 {
515         static char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
516         return PyUnicode_FromString(order[self->order]);
517 }
518
519 static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
520 {
521         char *order_str= _PyUnicode_AsString(value);
522         short order= euler_order_from_string(order_str, "euler.order");
523
524         if(order < 0)
525                 return -1;
526
527         if(self->cb_user) {
528                 PyErr_SetString(PyExc_TypeError, "euler.order: assignment is not allowed on eulers with an owner");
529                 return -1;
530         }
531
532         self->order= order;
533         return 0;
534 }
535
536 /*****************************************************************************/
537 /* Python attributes get/set structure:                                      */
538 /*****************************************************************************/
539 static PyGetSetDef Euler_getseters[] = {
540         {"x", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler X axis in radians. **type** float", (void *)0},
541         {"y", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler Y axis in radians. **type** float", (void *)1},
542         {"z", (getter)Euler_getAxis, (setter)Euler_setAxis, "Euler Z axis in radians. **type** float", (void *)2},
543         {"order", (getter)Euler_getOrder, (setter)Euler_setOrder, "Euler rotation order. **type** string in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']", (void *)NULL},
544
545         {"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL},
546         {"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL},
547         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
548 };
549
550
551 //-----------------------METHOD DEFINITIONS ----------------------
552 static struct PyMethodDef Euler_methods[] = {
553         {"zero", (PyCFunction) Euler_Zero, METH_NOARGS, Euler_Zero_doc},
554         {"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
555         {"to_matrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
556         {"to_quat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
557         {"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, NULL},
558         {"make_compatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc},
559         {"__copy__", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
560         {"copy", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
561         {NULL, NULL, 0, NULL}
562 };
563
564 //------------------PY_OBECT DEFINITION--------------------------
565 static char euler_doc[] =
566 "This object gives access to Eulers in Blender.\n"
567 "\n"
568 ".. literalinclude:: ../examples/mathutils_euler.py\n";
569
570 PyTypeObject euler_Type = {
571         PyVarObject_HEAD_INIT(NULL, 0)
572         "euler",                                                //tp_name
573         sizeof(EulerObject),                    //tp_basicsize
574         0,                                                              //tp_itemsize
575         (destructor)BaseMathObject_dealloc,             //tp_dealloc
576         0,                                                              //tp_print
577         0,                                                              //tp_getattr
578         0,                                                              //tp_setattr
579         0,                                                              //tp_compare
580         (reprfunc) Euler_repr,                  //tp_repr
581         0,                              //tp_as_number
582         &Euler_SeqMethods,                              //tp_as_sequence
583         0,                                                              //tp_as_mapping
584         0,                                                              //tp_hash
585         0,                                                              //tp_call
586         0,                                                              //tp_str
587         0,                                                              //tp_getattro
588         0,                                                              //tp_setattro
589         0,                                                              //tp_as_buffer
590         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
591         euler_doc, //tp_doc
592         0,                                                              //tp_traverse
593         0,                                                              //tp_clear
594         (richcmpfunc)Euler_richcmpr,    //tp_richcompare
595         0,                                                              //tp_weaklistoffset
596         0,                                                              //tp_iter
597         0,                                                              //tp_iternext
598         Euler_methods,                                  //tp_methods
599         0,                                                              //tp_members
600         Euler_getseters,                                //tp_getset
601         0,                                                              //tp_base
602         0,                                                              //tp_dict
603         0,                                                              //tp_descr_get
604         0,                                                              //tp_descr_set
605         0,                                                              //tp_dictoffset
606         0,                                                              //tp_init
607         0,                                                              //tp_alloc
608         Euler_new,                                              //tp_new
609         0,                                                              //tp_free
610         0,                                                              //tp_is_gc
611         0,                                                              //tp_bases
612         0,                                                              //tp_mro
613         0,                                                              //tp_cache
614         0,                                                              //tp_subclasses
615         0,                                                              //tp_weaklist
616         0                                                               //tp_del
617 };
618 //------------------------newEulerObject (internal)-------------
619 //creates a new euler object
620 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
621  (i.e. it was allocated elsewhere by MEM_mallocN())
622   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
623  (i.e. it must be created here with PyMEM_malloc())*/
624 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
625 {
626         EulerObject *self;
627         int x;
628
629         if(base_type)   self = (EulerObject *)base_type->tp_alloc(base_type, 0);
630         else                    self = PyObject_NEW(EulerObject, &euler_Type);
631
632         /* init callbacks as NULL */
633         self->cb_user= NULL;
634         self->cb_type= self->cb_subtype= 0;
635
636         if(type == Py_WRAP){
637                 self->eul = eul;
638                 self->wrapped = Py_WRAP;
639         }else if (type == Py_NEW){
640                 self->eul = PyMem_Malloc(3 * sizeof(float));
641                 if(!eul) { //new empty
642                         for(x = 0; x < 3; x++) {
643                                 self->eul[x] = 0.0f;
644                         }
645                 }else{
646                         VECCOPY(self->eul, eul);
647                 }
648                 self->wrapped = Py_NEW;
649         }else{ //bad type
650                 return NULL;
651         }
652
653         self->order= order;
654         return (PyObject *)self;
655 }
656
657 PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
658 {
659         EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
660         if(self) {
661                 Py_INCREF(cb_user);
662                 self->cb_user=                  cb_user;
663                 self->cb_type=                  (unsigned char)cb_type;
664                 self->cb_subtype=               (unsigned char)cb_subtype;
665         }
666
667         return (PyObject *)self;
668 }