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