69237922666a3e14cf2cae43c6565a5ff1552840
[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         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_ToQuat_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_ToQuat(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_ToMatrix_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_ToMatrix(EulerObject * self)
148 {
149         float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
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 x,y,z values to a unique euler rotation
161 // TODO, check if this works with rotation order!!!
162 static char Euler_Unique_doc[] =
163 ".. method:: unique()\n"
164 "\n"
165 "   Calculate a unique rotation for this euler. Avoids gimble lock.\n"
166 "\n"
167 "   :return: an instance of itself\n"
168 "   :rtype: :class:`Euler`\n";
169
170 static PyObject *Euler_Unique(EulerObject * self)
171 {
172 #define PI_2            (Py_PI * 2.0)
173 #define PI_HALF         (Py_PI / 2.0)
174 #define PI_INV          (1.0 / Py_PI)
175
176         double heading, pitch, bank;
177
178         if(!BaseMath_ReadCallback(self))
179                 return NULL;
180
181         heading = self->eul[0];
182         pitch = self->eul[1];
183         bank = self->eul[2];
184
185         //wrap heading in +180 / -180
186         pitch += Py_PI;
187         pitch -= floor(pitch * PI_INV) * PI_2;
188         pitch -= Py_PI;
189
190
191         if(pitch < -PI_HALF) {
192                 pitch = -Py_PI - pitch;
193                 heading += Py_PI;
194                 bank += Py_PI;
195         } else if(pitch > PI_HALF) {
196                 pitch = Py_PI - pitch;
197                 heading += Py_PI;
198                 bank += Py_PI;
199         }
200         //gimbal lock test
201         if(fabs(pitch) > PI_HALF - 1e-4) {
202                 heading += bank;
203                 bank = 0.0f;
204         } else {
205                 bank += Py_PI;
206                 bank -= (floor(bank * PI_INV)) * PI_2;
207                 bank -= Py_PI;
208         }
209
210         heading += Py_PI;
211         heading -= (floor(heading * PI_INV)) * PI_2;
212         heading -= Py_PI;
213
214         (void)BaseMath_WriteCallback(self);
215         Py_INCREF(self);
216         return (PyObject *)self;
217 }
218
219 //sets the euler to 0,0,0
220 static char Euler_Zero_doc[] =
221 ".. method:: zero()\n"
222 "\n"
223 "   Set all values to zero.\n"
224 "\n"
225 "   :return: an instance of itself\n"
226 "   :rtype: :class:`Euler`\n";
227
228 static PyObject *Euler_Zero(EulerObject * self)
229 {
230         self->eul[0] = 0.0;
231         self->eul[1] = 0.0;
232         self->eul[2] = 0.0;
233
234         (void)BaseMath_WriteCallback(self);
235         Py_INCREF(self);
236         return (PyObject *)self;
237 }
238
239 static char Euler_rotate_axis_doc[] =
240 ".. method:: rotate_axis(axis, angle)\n"
241 "\n"
242 "   Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).\n"
243 "\n"
244 "   :arg axis: single character in ['X, 'Y', 'Z'].\n"
245 "   :type axis: string\n"
246 "   :arg angle: angle in radians.\n"
247 "   :type angle: float\n"
248 "   :return: an instance of itself\n"
249 "   :rtype: :class:`Euler`";
250
251 static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
252 {
253         float angle = 0.0f;
254         char *axis;
255
256         if(!PyArg_ParseTuple(args, "sf:rotate", &axis, &angle)){
257                 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected angle (float) and axis (x,y,z)");
258                 return NULL;
259         }
260         if(!(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0')){
261                 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'X', 'Y' or 'Z'");
262                 return NULL;
263         }
264
265         if(!BaseMath_ReadCallback(self))
266                 return NULL;
267
268         if(self->order == EULER_ORDER_XYZ)      rotate_eul(self->eul, *axis, angle);
269         else                                                            rotate_eulO(self->eul, self->order, *axis, angle);
270
271         (void)BaseMath_WriteCallback(self);
272         Py_INCREF(self);
273         return (PyObject *)self;
274 }
275
276 static char Euler_MakeCompatible_doc[] =
277 ".. method:: make_compatible(other)\n"
278 "\n"
279 "   Make this euler compatible with another, so interpolating between them works as intended.\n"
280 "\n"
281 "   :arg other: make compatible with this rotation.\n"
282 "   :type other: :class:`Euler`\n"
283 "   :return: an instance of itself.\n"
284 "   :rtype: :class:`Euler`\n"
285 "\n"
286 "   .. note:: the order of eulers must match or an exception is raised.\n";
287
288 static PyObject *Euler_MakeCompatible(EulerObject * self, EulerObject *value)
289 {
290         if(!EulerObject_Check(value)) {
291                 PyErr_SetString(PyExc_TypeError, "euler.make_compatible(euler): expected a single euler argument");
292                 return NULL;
293         }
294         
295         if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
296                 return NULL;
297
298         if(self->order != value->order) {
299                 PyErr_SetString(PyExc_ValueError, "euler.make_compatible(euler): rotation orders don't match");
300                 return NULL;
301         }
302
303         compatible_eul(self->eul, value->eul);
304
305         (void)BaseMath_WriteCallback(self);
306         Py_INCREF(self);
307         return (PyObject *)self;
308 }
309
310 //----------------------------Euler.rotate()-----------------------
311 // return a copy of the euler
312
313 static char Euler_copy_doc[] =
314 ".. function:: copy()\n"
315 "\n"
316 "   Returns a copy of this euler.\n"
317 "\n"
318 "   :return: A copy of the euler.\n"
319 "   :rtype: :class:`Euler`\n"
320 "\n"
321 "   .. note:: use this to get a copy of a wrapped euler with no reference to the original data.\n";
322
323 static PyObject *Euler_copy(EulerObject *self)
324 {
325         if(!BaseMath_ReadCallback(self))
326                 return NULL;
327
328         return newEulerObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
329 }
330
331 //----------------------------print object (internal)--------------
332 //print the object to screen
333
334 static PyObject *Euler_repr(EulerObject * self)
335 {
336         PyObject *ret, *tuple;
337         
338         if(!BaseMath_ReadCallback(self))
339                 return NULL;
340
341         tuple= Euler_ToTupleExt(self, -1);
342
343         ret= PyUnicode_FromFormat("Euler(%R)", tuple);
344
345         Py_DECREF(tuple);
346         return ret;
347 }
348
349 //------------------------tp_richcmpr
350 //returns -1 execption, 0 false, 1 true
351 static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
352 {
353         EulerObject *eulA = NULL, *eulB = NULL;
354         int result = 0;
355
356         if(EulerObject_Check(objectA)) {
357                 eulA = (EulerObject*)objectA;
358                 if(!BaseMath_ReadCallback(eulA))
359                         return NULL;
360         }
361         if(EulerObject_Check(objectB)) {
362                 eulB = (EulerObject*)objectB;
363                 if(!BaseMath_ReadCallback(eulB))
364                         return NULL;
365         }
366
367         if (!eulA || !eulB){
368                 if (comparison_type == Py_NE){
369                         Py_RETURN_TRUE;
370                 }else{
371                         Py_RETURN_FALSE;
372                 }
373         }
374         eulA = (EulerObject*)objectA;
375         eulB = (EulerObject*)objectB;
376
377         switch (comparison_type){
378                 case Py_EQ:
379                         result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
380                         break;
381                 case Py_NE:
382                         result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
383                         break;
384                 default:
385                         printf("The result of the comparison could not be evaluated");
386                         break;
387         }
388         if (result == 1){
389                 Py_RETURN_TRUE;
390         }else{
391                 Py_RETURN_FALSE;
392         }
393 }
394
395 //---------------------SEQUENCE PROTOCOLS------------------------
396 //----------------------------len(object)------------------------
397 //sequence length
398 static int Euler_len(EulerObject *UNUSED(self))
399 {
400         return EULER_SIZE;
401 }
402 //----------------------------object[]---------------------------
403 //sequence accessor (get)
404 static PyObject *Euler_item(EulerObject * self, int i)
405 {
406         if(i<0) i= EULER_SIZE-i;
407         
408         if(i < 0 || i >= EULER_SIZE) {
409                 PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
410                 return NULL;
411         }
412
413         if(!BaseMath_ReadIndexCallback(self, i))
414                 return NULL;
415
416         return PyFloat_FromDouble(self->eul[i]);
417
418 }
419 //----------------------------object[]-------------------------
420 //sequence accessor (set)
421 static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
422 {
423         float f = PyFloat_AsDouble(value);
424
425         if(f == -1 && PyErr_Occurred()) { // parsed item not a number
426                 PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
427                 return -1;
428         }
429
430         if(i<0) i= EULER_SIZE-i;
431         
432         if(i < 0 || i >= EULER_SIZE){
433                 PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range");
434                 return -1;
435         }
436         
437         self->eul[i] = f;
438
439         if(!BaseMath_WriteIndexCallback(self, i))
440                 return -1;
441
442         return 0;
443 }
444 //----------------------------object[z:y]------------------------
445 //sequence slice (get)
446 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
447 {
448         PyObject *tuple;
449         int count;
450
451         if(!BaseMath_ReadCallback(self))
452                 return NULL;
453
454         CLAMP(begin, 0, EULER_SIZE);
455         if (end<0) end= (EULER_SIZE + 1) + end;
456         CLAMP(end, 0, EULER_SIZE);
457         begin= MIN2(begin, end);
458
459         tuple= PyTuple_New(end - begin);
460         for(count = begin; count < end; count++) {
461                 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
462         }
463
464         return tuple;
465 }
466 //----------------------------object[z:y]------------------------
467 //sequence slice (set)
468 static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
469 {
470         int i, size;
471         float eul[EULER_SIZE];
472
473         if(!BaseMath_ReadCallback(self))
474                 return -1;
475
476         CLAMP(begin, 0, EULER_SIZE);
477         if (end<0) end= (EULER_SIZE + 1) + end;
478         CLAMP(end, 0, EULER_SIZE);
479         begin = MIN2(begin,end);
480
481         if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
482                 return -1;
483
484         if(size != (end - begin)){
485                 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
486                 return -1;
487         }
488
489         for(i= 0; i < EULER_SIZE; i++)
490                 self->eul[begin + i] = eul[i];
491
492         (void)BaseMath_WriteCallback(self);
493         return 0;
494 }
495
496 static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
497 {
498         if (PyIndex_Check(item)) {
499                 Py_ssize_t i;
500                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
501                 if (i == -1 && PyErr_Occurred())
502                         return NULL;
503                 if (i < 0)
504                         i += EULER_SIZE;
505                 return Euler_item(self, i);
506         } else if (PySlice_Check(item)) {
507                 Py_ssize_t start, stop, step, slicelength;
508
509                 if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
510                         return NULL;
511
512                 if (slicelength <= 0) {
513                         return PyList_New(0);
514                 }
515                 else if (step == 1) {
516                         return Euler_slice(self, start, stop);
517                 }
518                 else {
519                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
520                         return NULL;
521                 }
522         }
523         else {
524                 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
525                 return NULL;
526         }
527 }
528
529
530 static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
531 {
532         if (PyIndex_Check(item)) {
533                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
534                 if (i == -1 && PyErr_Occurred())
535                         return -1;
536                 if (i < 0)
537                         i += EULER_SIZE;
538                 return Euler_ass_item(self, i, value);
539         }
540         else if (PySlice_Check(item)) {
541                 Py_ssize_t start, stop, step, slicelength;
542
543                 if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
544                         return -1;
545
546                 if (step == 1)
547                         return Euler_ass_slice(self, start, stop, value);
548                 else {
549                         PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
550                         return -1;
551                 }
552         }
553         else {
554                 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
555                 return -1;
556         }
557 }
558
559 //-----------------PROTCOL DECLARATIONS--------------------------
560 static PySequenceMethods Euler_SeqMethods = {
561         (lenfunc) Euler_len,                                    /* sq_length */
562         (binaryfunc) NULL,                                              /* sq_concat */
563         (ssizeargfunc) NULL,                                    /* sq_repeat */
564         (ssizeargfunc) Euler_item,                              /* sq_item */
565         (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated  */
566         (ssizeobjargproc) Euler_ass_item,               /* sq_ass_item */
567         (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
568         (objobjproc) NULL,                                              /* sq_contains */
569         (binaryfunc) NULL,                                              /* sq_inplace_concat */
570         (ssizeargfunc) NULL,                                    /* sq_inplace_repeat */
571 };
572
573 static PyMappingMethods Euler_AsMapping = {
574         (lenfunc)Euler_len,
575         (binaryfunc)Euler_subscript,
576         (objobjargproc)Euler_ass_subscript
577 };
578
579 /*
580  * euler axis, euler.x/y/z
581  */
582 static PyObject *Euler_getAxis(EulerObject *self, void *type )
583 {
584         return Euler_item(self, GET_INT_FROM_POINTER(type));
585 }
586
587 static int Euler_setAxis(EulerObject *self, PyObject *value, void *type)
588 {
589         return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
590 }
591
592 /* rotation order */
593 static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
594 {
595         const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
596
597         if(!BaseMath_ReadCallback(self)) /* can read order too */
598                 return NULL;
599
600         return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]);
601 }
602
603 static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
604 {
605         char *order_str= _PyUnicode_AsString(value);
606         short order= euler_order_from_string(order_str, "euler.order");
607
608         if(order == -1)
609                 return -1;
610
611         self->order= order;
612         (void)BaseMath_WriteCallback(self); /* order can be written back */
613         return 0;
614 }
615
616 /*****************************************************************************/
617 /* Python attributes get/set structure:                                      */
618 /*****************************************************************************/
619 static PyGetSetDef Euler_getseters[] = {
620         {(char *)"x", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler X axis in radians.\n\n:type: float", (void *)0},
621         {(char *)"y", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Y axis in radians.\n\n:type: float", (void *)1},
622         {(char *)"z", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Z axis in radians.\n\n:type: float", (void *)2},
623         {(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},
624
625         {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
626         {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
627         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
628 };
629
630
631 //-----------------------METHOD DEFINITIONS ----------------------
632 static struct PyMethodDef Euler_methods[] = {
633         {"zero", (PyCFunction) Euler_Zero, METH_NOARGS, Euler_Zero_doc},
634         {"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
635         {"to_matrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
636         {"to_quat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
637         {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
638         {"make_compatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc},
639         {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
640         {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
641         {NULL, NULL, 0, NULL}
642 };
643
644 //------------------PY_OBECT DEFINITION--------------------------
645 static char euler_doc[] =
646 "This object gives access to Eulers in Blender.";
647
648 PyTypeObject euler_Type = {
649         PyVarObject_HEAD_INIT(NULL, 0)
650         "mathutils.Euler",                                              //tp_name
651         sizeof(EulerObject),                    //tp_basicsize
652         0,                                                              //tp_itemsize
653         (destructor)BaseMathObject_dealloc,             //tp_dealloc
654         0,                                                              //tp_print
655         0,                                                              //tp_getattr
656         0,                                                              //tp_setattr
657         0,                                                              //tp_compare
658         (reprfunc) Euler_repr,                  //tp_repr
659         0,                              //tp_as_number
660         &Euler_SeqMethods,                              //tp_as_sequence
661         &Euler_AsMapping,                               //tp_as_mapping
662         0,                                                              //tp_hash
663         0,                                                              //tp_call
664         0,                                                              //tp_str
665         0,                                                              //tp_getattro
666         0,                                                              //tp_setattro
667         0,                                                              //tp_as_buffer
668         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
669         euler_doc, //tp_doc
670         0,                                                              //tp_traverse
671         0,                                                              //tp_clear
672         (richcmpfunc)Euler_richcmpr,    //tp_richcompare
673         0,                                                              //tp_weaklistoffset
674         0,                                                              //tp_iter
675         0,                                                              //tp_iternext
676         Euler_methods,                                  //tp_methods
677         0,                                                              //tp_members
678         Euler_getseters,                                //tp_getset
679         0,                                                              //tp_base
680         0,                                                              //tp_dict
681         0,                                                              //tp_descr_get
682         0,                                                              //tp_descr_set
683         0,                                                              //tp_dictoffset
684         0,                                                              //tp_init
685         0,                                                              //tp_alloc
686         Euler_new,                                              //tp_new
687         0,                                                              //tp_free
688         0,                                                              //tp_is_gc
689         0,                                                              //tp_bases
690         0,                                                              //tp_mro
691         0,                                                              //tp_cache
692         0,                                                              //tp_subclasses
693         0,                                                              //tp_weaklist
694         0                                                               //tp_del
695 };
696 //------------------------newEulerObject (internal)-------------
697 //creates a new euler object
698 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
699  (i.e. it was allocated elsewhere by MEM_mallocN())
700   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
701  (i.e. it must be created here with PyMEM_malloc())*/
702 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
703 {
704         EulerObject *self;
705
706         if(base_type)   self = (EulerObject *)base_type->tp_alloc(base_type, 0);
707         else                    self = PyObject_NEW(EulerObject, &euler_Type);
708
709         /* init callbacks as NULL */
710         self->cb_user= NULL;
711         self->cb_type= self->cb_subtype= 0;
712
713         if(type == Py_WRAP) {
714                 self->eul = eul;
715                 self->wrapped = Py_WRAP;
716         }
717         else if (type == Py_NEW){
718                 self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
719                 if(eul)
720                         copy_v3_v3(self->eul, eul);
721                 else
722                         zero_v3(self->eul);
723
724                 self->wrapped = Py_NEW;
725         }
726         else{
727                 return NULL;
728         }
729
730         self->order= order;
731         return (PyObject *)self;
732 }
733
734 PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
735 {
736         EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
737         if(self) {
738                 Py_INCREF(cb_user);
739                 self->cb_user=                  cb_user;
740                 self->cb_type=                  (unsigned char)cb_type;
741                 self->cb_subtype=               (unsigned char)cb_subtype;
742         }
743
744         return (PyObject *)self;
745 }