8f6d7e8a182f545770abaa92dbe81a7bcffdc049
[blender.git] / source / blender / python / generic / mathutils_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 "BLI_utildefines.h"
33
34
35
36 #ifndef int32_t
37 #include "BLO_sys_types.h"
38 #endif
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, "mathutils.Euler(): takes no keyword args");
54                 return NULL;
55         }
56
57         if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
58                 return NULL;
59
60         switch(PyTuple_GET_SIZE(args)) {
61         case 0:
62                 break;
63         case 2:
64                 if((order=euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
65                         return NULL;
66                 /* intentionally pass through */
67         case 1:
68                 if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
69                         return NULL;
70                 break;
71         }
72         return newEulerObject(eul, order, Py_NEW, type);
73 }
74
75 short euler_order_from_string(const char *str, const char *error_prefix)
76 {
77         if((str[0] && str[1] && str[2] && str[3]=='\0')) {
78                 switch(*((int32_t *)str)) {
79                         case 'X'|'Y'<<8|'Z'<<16:        return EULER_ORDER_XYZ;
80                         case 'X'|'Z'<<8|'Y'<<16:        return EULER_ORDER_XZY;
81                         case 'Y'|'X'<<8|'Z'<<16:        return EULER_ORDER_YXZ;
82                         case 'Y'|'Z'<<8|'X'<<16:        return EULER_ORDER_YZX;
83                         case 'Z'|'X'<<8|'Y'<<16:        return EULER_ORDER_ZXY;
84                         case 'Z'|'Y'<<8|'X'<<16:        return EULER_ORDER_ZYX;
85                 }
86         }
87
88         PyErr_Format(PyExc_TypeError, "%s: invalid euler order '%s'", error_prefix, str);
89         return -1;
90 }
91
92 /* note: BaseMath_ReadCallback must be called beforehand */
93 static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
94 {
95         PyObject *ret;
96         int i;
97
98         ret= PyTuple_New(EULER_SIZE);
99
100         if(ndigits >= 0) {
101                 for(i= 0; i < EULER_SIZE; i++) {
102                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
103                 }
104         }
105         else {
106                 for(i= 0; i < EULER_SIZE; i++) {
107                         PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
108                 }
109         }
110
111         return ret;
112 }
113
114 //-----------------------------METHODS----------------------------
115 //return a quaternion representation of the euler
116
117 static char Euler_to_quaternion_doc[] =
118 ".. method:: to_quat()\n"
119 "\n"
120 "   Return a quaternion representation of the euler.\n"
121 "\n"
122 "   :return: Quaternion representation of the euler.\n"
123 "   :rtype: :class:`Quaternion`\n"
124 ;
125 static PyObject *Euler_to_quaternion(EulerObject * self)
126 {
127         float quat[4];
128
129         if(!BaseMath_ReadCallback(self))
130                 return NULL;
131
132         if(self->order==EULER_ORDER_XYZ)        eul_to_quat(quat, self->eul);
133         else                                                            eulO_to_quat(quat, self->eul, self->order);
134
135         return newQuaternionObject(quat, Py_NEW, NULL);
136 }
137
138 //return a matrix representation of the euler
139 static char Euler_to_matrix_doc[] =
140 ".. method:: to_matrix()\n"
141 "\n"
142 "   Return a matrix representation of the euler.\n"
143 "\n"
144 "   :return: A 3x3 roation matrix representation of the euler.\n"
145 "   :rtype: :class:`Matrix`\n"
146 ;
147 static PyObject *Euler_to_matrix(EulerObject * self)
148 {
149         float mat[9];
150
151         if(!BaseMath_ReadCallback(self))
152                 return NULL;
153
154         if(self->order==EULER_ORDER_XYZ)        eul_to_mat3((float (*)[3])mat, self->eul);
155         else                                                            eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
156
157         return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
158 }
159
160 //sets the euler to 0,0,0
161 static char Euler_zero_doc[] =
162 ".. method:: zero()\n"
163 "\n"
164 "   Set all values to zero.\n"
165 ;
166 static PyObject *Euler_zero(EulerObject * self)
167 {
168         zero_v3(self->eul);
169
170         (void)BaseMath_WriteCallback(self);
171         Py_RETURN_NONE;
172 }
173
174 static char Euler_rotate_axis_doc[] =
175 ".. method:: rotate_axis(axis, angle)\n"
176 "\n"
177 "   Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).\n"
178 "\n"
179 "   :arg axis: single character in ['X, 'Y', 'Z'].\n"
180 "   :type axis: string\n"
181 "   :arg angle: angle in radians.\n"
182 "   :type angle: float\n"
183 "   :return: an instance of itself\n"
184 "   :rtype: :class:`Euler`"
185 ;
186 static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
187 {
188         float angle = 0.0f;
189         const char *axis;
190
191         if(!PyArg_ParseTuple(args, "sf:rotate", &axis, &angle)){
192                 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected angle (float) and axis (x,y,z)");
193                 return NULL;
194         }
195         if(!(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0')){
196                 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'X', 'Y' or 'Z'");
197                 return NULL;
198         }
199
200         if(!BaseMath_ReadCallback(self))
201                 return NULL;
202
203         if(self->order == EULER_ORDER_XYZ)      rotate_eul(self->eul, *axis, angle);
204         else                                                            rotate_eulO(self->eul, self->order, *axis, angle);
205
206         (void)BaseMath_WriteCallback(self);
207         Py_INCREF(self);
208         return (PyObject *)self;
209 }
210
211 static char Euler_make_compatible_doc[] =
212 ".. method:: make_compatible(other)\n"
213 "\n"
214 "   Make this euler compatible with another, so interpolating between them works as intended.\n"
215 "\n"
216 "   :arg other: make compatible with this rotation.\n"
217 "   :type other: :class:`Euler`\n"
218 "   :return: an instance of itself.\n"
219 "   :rtype: :class:`Euler`\n"
220 "\n"
221 "   .. note:: the rotation order is not taken into account for this function.\n"
222 ;
223 static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
224 {
225         float teul[EULER_SIZE];
226
227         if(!BaseMath_ReadCallback(self))
228                 return NULL;
229
230         if(mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value, "euler.make_compatible(other), invalid 'other' arg") == -1)
231                 return NULL;
232
233         compatible_eul(self->eul, teul);
234
235         (void)BaseMath_WriteCallback(self);
236         Py_INCREF(self);
237         return (PyObject *)self;
238 }
239
240 //----------------------------Euler.rotate()-----------------------
241 // return a copy of the euler
242
243 static char Euler_copy_doc[] =
244 ".. function:: copy()\n"
245 "\n"
246 "   Returns a copy of this euler.\n"
247 "\n"
248 "   :return: A copy of the euler.\n"
249 "   :rtype: :class:`Euler`\n"
250 "\n"
251 "   .. note:: use this to get a copy of a wrapped euler with no reference to the original data.\n"
252 ;
253 static PyObject *Euler_copy(EulerObject *self)
254 {
255         if(!BaseMath_ReadCallback(self))
256                 return NULL;
257
258         return newEulerObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
259 }
260
261 //----------------------------print object (internal)--------------
262 //print the object to screen
263
264 static PyObject *Euler_repr(EulerObject * self)
265 {
266         PyObject *ret, *tuple;
267
268         if(!BaseMath_ReadCallback(self))
269                 return NULL;
270
271         tuple= Euler_ToTupleExt(self, -1);
272
273         ret= PyUnicode_FromFormat("Euler(%R)", tuple);
274
275         Py_DECREF(tuple);
276         return ret;
277 }
278
279 //------------------------tp_richcmpr
280 //returns -1 execption, 0 false, 1 true
281 static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
282 {
283         EulerObject *eulA = NULL, *eulB = NULL;
284         int result = 0;
285
286         if(EulerObject_Check(objectA)) {
287                 eulA = (EulerObject*)objectA;
288                 if(!BaseMath_ReadCallback(eulA))
289                         return NULL;
290         }
291         if(EulerObject_Check(objectB)) {
292                 eulB = (EulerObject*)objectB;
293                 if(!BaseMath_ReadCallback(eulB))
294                         return NULL;
295         }
296
297         if (!eulA || !eulB){
298                 if (comparison_type == Py_NE){
299                         Py_RETURN_TRUE;
300                 }else{
301                         Py_RETURN_FALSE;
302                 }
303         }
304         eulA = (EulerObject*)objectA;
305         eulB = (EulerObject*)objectB;
306
307         switch (comparison_type){
308                 case Py_EQ:
309                         result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
310                         break;
311                 case Py_NE:
312                         result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
313                         break;
314                 default:
315                         printf("The result of the comparison could not be evaluated");
316                         break;
317         }
318         if (result == 1){
319                 Py_RETURN_TRUE;
320         }else{
321                 Py_RETURN_FALSE;
322         }
323 }
324
325 //---------------------SEQUENCE PROTOCOLS------------------------
326 //----------------------------len(object)------------------------
327 //sequence length
328 static int Euler_len(EulerObject *UNUSED(self))
329 {
330         return EULER_SIZE;
331 }
332 //----------------------------object[]---------------------------
333 //sequence accessor (get)
334 static PyObject *Euler_item(EulerObject * self, int i)
335 {
336         if(i<0) i= EULER_SIZE-i;
337
338         if(i < 0 || i >= EULER_SIZE) {
339                 PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
340                 return NULL;
341         }
342
343         if(!BaseMath_ReadIndexCallback(self, i))
344                 return NULL;
345
346         return PyFloat_FromDouble(self->eul[i]);
347
348 }
349 //----------------------------object[]-------------------------
350 //sequence accessor (set)
351 static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
352 {
353         float f = PyFloat_AsDouble(value);
354
355         if(f == -1 && PyErr_Occurred()) { // parsed item not a number
356                 PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
357                 return -1;
358         }
359
360         if(i<0) i= EULER_SIZE-i;
361
362         if(i < 0 || i >= EULER_SIZE){
363                 PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range");
364                 return -1;
365         }
366
367         self->eul[i] = f;
368
369         if(!BaseMath_WriteIndexCallback(self, i))
370                 return -1;
371
372         return 0;
373 }
374 //----------------------------object[z:y]------------------------
375 //sequence slice (get)
376 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
377 {
378         PyObject *tuple;
379         int count;
380
381         if(!BaseMath_ReadCallback(self))
382                 return NULL;
383
384         CLAMP(begin, 0, EULER_SIZE);
385         if (end<0) end= (EULER_SIZE + 1) + end;
386         CLAMP(end, 0, EULER_SIZE);
387         begin= MIN2(begin, end);
388
389         tuple= PyTuple_New(end - begin);
390         for(count = begin; count < end; count++) {
391                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
392         }
393
394         return tuple;
395 }
396 //----------------------------object[z:y]------------------------
397 //sequence slice (set)
398 static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
399 {
400         int i, size;
401         float eul[EULER_SIZE];
402
403         if(!BaseMath_ReadCallback(self))
404                 return -1;
405
406         CLAMP(begin, 0, EULER_SIZE);
407         if (end<0) end= (EULER_SIZE + 1) + end;
408         CLAMP(end, 0, EULER_SIZE);
409         begin = MIN2(begin,end);
410
411         if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
412                 return -1;
413
414         if(size != (end - begin)){
415                 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
416                 return -1;
417         }
418
419         for(i= 0; i < EULER_SIZE; i++)
420                 self->eul[begin + i] = eul[i];
421
422         (void)BaseMath_WriteCallback(self);
423         return 0;
424 }
425
426 static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
427 {
428         if (PyIndex_Check(item)) {
429                 Py_ssize_t i;
430                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
431                 if (i == -1 && PyErr_Occurred())
432                         return NULL;
433                 if (i < 0)
434                         i += EULER_SIZE;
435                 return Euler_item(self, i);
436         } else if (PySlice_Check(item)) {
437                 Py_ssize_t start, stop, step, slicelength;
438
439                 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
440                         return NULL;
441
442                 if (slicelength <= 0) {
443                         return PyTuple_New(0);
444                 }
445                 else if (step == 1) {
446                         return Euler_slice(self, start, stop);
447                 }
448                 else {
449                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
450                         return NULL;
451                 }
452         }
453         else {
454                 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
455                 return NULL;
456         }
457 }
458
459
460 static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
461 {
462         if (PyIndex_Check(item)) {
463                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
464                 if (i == -1 && PyErr_Occurred())
465                         return -1;
466                 if (i < 0)
467                         i += EULER_SIZE;
468                 return Euler_ass_item(self, i, value);
469         }
470         else if (PySlice_Check(item)) {
471                 Py_ssize_t start, stop, step, slicelength;
472
473                 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
474                         return -1;
475
476                 if (step == 1)
477                         return Euler_ass_slice(self, start, stop, value);
478                 else {
479                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
480                         return -1;
481                 }
482         }
483         else {
484                 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
485                 return -1;
486         }
487 }
488
489 //-----------------PROTCOL DECLARATIONS--------------------------
490 static PySequenceMethods Euler_SeqMethods = {
491         (lenfunc) Euler_len,                                    /* sq_length */
492         (binaryfunc) NULL,                                              /* sq_concat */
493         (ssizeargfunc) NULL,                                    /* sq_repeat */
494         (ssizeargfunc) Euler_item,                              /* sq_item */
495         (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated  */
496         (ssizeobjargproc) Euler_ass_item,               /* sq_ass_item */
497         (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
498         (objobjproc) NULL,                                              /* sq_contains */
499         (binaryfunc) NULL,                                              /* sq_inplace_concat */
500         (ssizeargfunc) NULL,                                    /* sq_inplace_repeat */
501 };
502
503 static PyMappingMethods Euler_AsMapping = {
504         (lenfunc)Euler_len,
505         (binaryfunc)Euler_subscript,
506         (objobjargproc)Euler_ass_subscript
507 };
508
509 /*
510  * euler axis, euler.x/y/z
511  */
512 static PyObject *Euler_getAxis(EulerObject *self, void *type )
513 {
514         return Euler_item(self, GET_INT_FROM_POINTER(type));
515 }
516
517 static int Euler_setAxis(EulerObject *self, PyObject *value, void *type)
518 {
519         return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
520 }
521
522 /* rotation order */
523 static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
524 {
525         const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
526
527         if(!BaseMath_ReadCallback(self)) /* can read order too */
528                 return NULL;
529
530         return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]);
531 }
532
533 static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
534 {
535         const char *order_str= _PyUnicode_AsString(value);
536         short order= euler_order_from_string(order_str, "euler.order");
537
538         if(order == -1)
539                 return -1;
540
541         self->order= order;
542         (void)BaseMath_WriteCallback(self); /* order can be written back */
543         return 0;
544 }
545
546 /*****************************************************************************/
547 /* Python attributes get/set structure:                                      */
548 /*****************************************************************************/
549 static PyGetSetDef Euler_getseters[] = {
550         {(char *)"x", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler X axis in radians.\n\n:type: float", (void *)0},
551         {(char *)"y", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Y axis in radians.\n\n:type: float", (void *)1},
552         {(char *)"z", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Z axis in radians.\n\n:type: float", (void *)2},
553         {(char *)"order", (getter)Euler_getOrder, (setter)Euler_setOrder, (char *)"Euler rotation order.\n\n:type: string in ['XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX']", (void *)NULL},
554
555         {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
556         {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
557         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
558 };
559
560
561 //-----------------------METHOD DEFINITIONS ----------------------
562 static struct PyMethodDef Euler_methods[] = {
563         {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
564         {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
565         {"to_quaternion", (PyCFunction) Euler_to_quaternion, METH_NOARGS, Euler_to_quaternion_doc},
566         {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
567         {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
568         {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
569         {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
570         {NULL, NULL, 0, NULL}
571 };
572
573 //------------------PY_OBECT DEFINITION--------------------------
574 static char euler_doc[] =
575 "This object gives access to Eulers in Blender."
576 ;
577 PyTypeObject euler_Type = {
578         PyVarObject_HEAD_INIT(NULL, 0)
579         "mathutils.Euler",                                              //tp_name
580         sizeof(EulerObject),                    //tp_basicsize
581         0,                                                              //tp_itemsize
582         (destructor)BaseMathObject_dealloc,             //tp_dealloc
583         0,                                                              //tp_print
584         0,                                                              //tp_getattr
585         0,                                                              //tp_setattr
586         0,                                                              //tp_compare
587         (reprfunc) Euler_repr,                  //tp_repr
588         0,                              //tp_as_number
589         &Euler_SeqMethods,                              //tp_as_sequence
590         &Euler_AsMapping,                               //tp_as_mapping
591         0,                                                              //tp_hash
592         0,                                                              //tp_call
593         0,                                                              //tp_str
594         0,                                                              //tp_getattro
595         0,                                                              //tp_setattro
596         0,                                                              //tp_as_buffer
597         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
598         euler_doc, //tp_doc
599         0,                                                              //tp_traverse
600         0,                                                              //tp_clear
601         (richcmpfunc)Euler_richcmpr,    //tp_richcompare
602         0,                                                              //tp_weaklistoffset
603         0,                                                              //tp_iter
604         0,                                                              //tp_iternext
605         Euler_methods,                                  //tp_methods
606         0,                                                              //tp_members
607         Euler_getseters,                                //tp_getset
608         0,                                                              //tp_base
609         0,                                                              //tp_dict
610         0,                                                              //tp_descr_get
611         0,                                                              //tp_descr_set
612         0,                                                              //tp_dictoffset
613         0,                                                              //tp_init
614         0,                                                              //tp_alloc
615         Euler_new,                                              //tp_new
616         0,                                                              //tp_free
617         0,                                                              //tp_is_gc
618         0,                                                              //tp_bases
619         0,                                                              //tp_mro
620         0,                                                              //tp_cache
621         0,                                                              //tp_subclasses
622         0,                                                              //tp_weaklist
623         0                                                               //tp_del
624 };
625 //------------------------newEulerObject (internal)-------------
626 //creates a new euler object
627 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
628  (i.e. it was allocated elsewhere by MEM_mallocN())
629   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
630  (i.e. it must be created here with PyMEM_malloc())*/
631 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
632 {
633         EulerObject *self;
634
635         if(base_type)   self = (EulerObject *)base_type->tp_alloc(base_type, 0);
636         else                    self = PyObject_NEW(EulerObject, &euler_Type);
637
638         /* init callbacks as NULL */
639         self->cb_user= NULL;
640         self->cb_type= self->cb_subtype= 0;
641
642         if(type == Py_WRAP) {
643                 self->eul = eul;
644                 self->wrapped = Py_WRAP;
645         }
646         else if (type == Py_NEW){
647                 self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
648                 if(eul)
649                         copy_v3_v3(self->eul, eul);
650                 else
651                         zero_v3(self->eul);
652
653                 self->wrapped = Py_NEW;
654         }
655         else{
656                 return NULL;
657         }
658
659         self->order= order;
660         return (PyObject *)self;
661 }
662
663 PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
664 {
665         EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
666         if(self) {
667                 Py_INCREF(cb_user);
668                 self->cb_user=                  cb_user;
669                 self->cb_type=                  (unsigned char)cb_type;
670                 self->cb_subtype=               (unsigned char)cb_subtype;
671         }
672
673         return (PyObject *)self;
674 }