4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
24 * Contributor(s): Joseph Gilbert
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/python/generic/mathutils_Euler.c
36 #include "mathutils.h"
39 #include "BLI_utildefines.h"
42 #include "BLO_sys_types.h"
47 //----------------------------------mathutils.Euler() -------------------
48 //makes a new euler for you to play with
49 static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
52 const char *order_str= NULL;
54 float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
55 short order= EULER_ORDER_XYZ;
57 if(kwds && PyDict_Size(kwds)) {
58 PyErr_SetString(PyExc_TypeError, "mathutils.Euler(): takes no keyword args");
62 if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
65 switch(PyTuple_GET_SIZE(args)) {
69 if((order=euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
71 /* intentionally pass through */
73 if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
77 return newEulerObject(eul, order, Py_NEW, type);
80 /* internal use, assuem read callback is done */
81 static const char *euler_order_str(EulerObject *self)
83 static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
84 return order[self->order-EULER_ORDER_XYZ];
87 short euler_order_from_string(const char *str, const char *error_prefix)
89 if((str[0] && str[1] && str[2] && str[3]=='\0')) {
90 switch(*((int32_t *)str)) {
91 case 'X'|'Y'<<8|'Z'<<16: return EULER_ORDER_XYZ;
92 case 'X'|'Z'<<8|'Y'<<16: return EULER_ORDER_XZY;
93 case 'Y'|'X'<<8|'Z'<<16: return EULER_ORDER_YXZ;
94 case 'Y'|'Z'<<8|'X'<<16: return EULER_ORDER_YZX;
95 case 'Z'|'X'<<8|'Y'<<16: return EULER_ORDER_ZXY;
96 case 'Z'|'Y'<<8|'X'<<16: return EULER_ORDER_ZYX;
100 PyErr_Format(PyExc_TypeError, "%s: invalid euler order '%s'", error_prefix, str);
104 /* note: BaseMath_ReadCallback must be called beforehand */
105 static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
110 ret= PyTuple_New(EULER_SIZE);
113 for(i= 0; i < EULER_SIZE; i++) {
114 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
118 for(i= 0; i < EULER_SIZE; i++) {
119 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
126 //-----------------------------METHODS----------------------------
127 //return a quaternion representation of the euler
129 static char Euler_to_quaternion_doc[] =
130 ".. method:: to_quaternion()\n"
132 " Return a quaternion representation of the euler.\n"
134 " :return: Quaternion representation of the euler.\n"
135 " :rtype: :class:`Quaternion`\n"
137 static PyObject *Euler_to_quaternion(EulerObject * self)
141 if(BaseMath_ReadCallback(self) == -1)
144 eulO_to_quat(quat, self->eul, self->order);
146 return newQuaternionObject(quat, Py_NEW, NULL);
149 //return a matrix representation of the euler
150 static char Euler_to_matrix_doc[] =
151 ".. method:: to_matrix()\n"
153 " Return a matrix representation of the euler.\n"
155 " :return: A 3x3 roation matrix representation of the euler.\n"
156 " :rtype: :class:`Matrix`\n"
158 static PyObject *Euler_to_matrix(EulerObject * self)
162 if(BaseMath_ReadCallback(self) == -1)
165 eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
167 return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
170 //sets the euler to 0,0,0
171 static char Euler_zero_doc[] =
172 ".. method:: zero()\n"
174 " Set all values to zero.\n"
176 static PyObject *Euler_zero(EulerObject * self)
180 if(BaseMath_WriteCallback(self) == -1)
186 static char Euler_rotate_axis_doc[] =
187 ".. method:: rotate_axis(axis, angle)\n"
189 " Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).\n"
191 " :arg axis: single character in ['X, 'Y', 'Z'].\n"
192 " :type axis: string\n"
193 " :arg angle: angle in radians.\n"
194 " :type angle: float\n"
196 static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
201 if(!PyArg_ParseTuple(args, "sf:rotate", &axis, &angle)){
202 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected angle (float) and axis (x, y, z)");
205 if(!(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0')){
206 PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'X', 'Y' or 'Z'");
210 if(BaseMath_ReadCallback(self) == -1)
214 rotate_eulO(self->eul, self->order, *axis, angle);
216 (void)BaseMath_WriteCallback(self);
221 static char Euler_rotate_doc[] =
222 ".. method:: rotate(other)\n"
224 " Rotates the euler a by another mathutils value.\n"
226 " :arg other: rotation component of mathutils value\n"
227 " :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
229 static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
231 float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
233 if(BaseMath_ReadCallback(self) == -1)
236 if(mathutils_any_to_rotmat(other_rmat, value, "euler.rotate(value)") == -1)
239 eulO_to_mat3(self_rmat, self->eul, self->order);
240 mul_m3_m3m3(rmat, self_rmat, other_rmat);
242 mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
244 (void)BaseMath_WriteCallback(self);
248 static char Euler_make_compatible_doc[] =
249 ".. method:: make_compatible(other)\n"
251 " Make this euler compatible with another, so interpolating between them works as intended.\n"
253 " .. note:: the rotation order is not taken into account for this function.\n"
255 static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
257 float teul[EULER_SIZE];
259 if(BaseMath_ReadCallback(self) == -1)
262 if(mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value, "euler.make_compatible(other), invalid 'other' arg") == -1)
265 compatible_eul(self->eul, teul);
267 (void)BaseMath_WriteCallback(self);
272 //----------------------------Euler.rotate()-----------------------
273 // return a copy of the euler
275 static char Euler_copy_doc[] =
276 ".. function:: copy()\n"
278 " Returns a copy of this euler.\n"
280 " :return: A copy of the euler.\n"
281 " :rtype: :class:`Euler`\n"
283 " .. note:: use this to get a copy of a wrapped euler with no reference to the original data.\n"
285 static PyObject *Euler_copy(EulerObject *self)
287 if(BaseMath_ReadCallback(self) == -1)
290 return newEulerObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
293 //----------------------------print object (internal)--------------
294 //print the object to screen
296 static PyObject *Euler_repr(EulerObject * self)
298 PyObject *ret, *tuple;
300 if(BaseMath_ReadCallback(self) == -1)
303 tuple= Euler_ToTupleExt(self, -1);
305 ret= PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
311 static PyObject* Euler_richcmpr(PyObject *a, PyObject *b, int op)
314 int ok= -1; /* zero is true */
316 if (EulerObject_Check(a) && EulerObject_Check(b)) {
317 EulerObject *eulA= (EulerObject*)a;
318 EulerObject *eulB= (EulerObject*)b;
320 if(BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1)
323 ok= ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
328 ok = !ok; /* pass through */
330 res = ok ? Py_False : Py_True;
337 res = Py_NotImplemented;
344 return Py_INCREF(res), res;
347 //---------------------SEQUENCE PROTOCOLS------------------------
348 //----------------------------len(object)------------------------
350 static int Euler_len(EulerObject *UNUSED(self))
354 //----------------------------object[]---------------------------
355 //sequence accessor (get)
356 static PyObject *Euler_item(EulerObject * self, int i)
358 if(i<0) i= EULER_SIZE-i;
360 if(i < 0 || i >= EULER_SIZE) {
361 PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
365 if(BaseMath_ReadIndexCallback(self, i) == -1)
368 return PyFloat_FromDouble(self->eul[i]);
371 //----------------------------object[]-------------------------
372 //sequence accessor (set)
373 static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
375 float f = PyFloat_AsDouble(value);
377 if(f == -1 && PyErr_Occurred()) { // parsed item not a number
378 PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
382 if(i<0) i= EULER_SIZE-i;
384 if(i < 0 || i >= EULER_SIZE){
385 PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range");
391 if(BaseMath_WriteIndexCallback(self, i) == -1)
396 //----------------------------object[z:y]------------------------
397 //sequence slice (get)
398 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
403 if(BaseMath_ReadCallback(self) == -1)
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);
411 tuple= PyTuple_New(end - begin);
412 for(count = begin; count < end; count++) {
413 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
418 //----------------------------object[z:y]------------------------
419 //sequence slice (set)
420 static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
423 float eul[EULER_SIZE];
425 if(BaseMath_ReadCallback(self) == -1)
428 CLAMP(begin, 0, EULER_SIZE);
429 if (end<0) end= (EULER_SIZE + 1) + end;
430 CLAMP(end, 0, EULER_SIZE);
431 begin = MIN2(begin, end);
433 if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
436 if(size != (end - begin)){
437 PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
441 for(i= 0; i < EULER_SIZE; i++)
442 self->eul[begin + i] = eul[i];
444 (void)BaseMath_WriteCallback(self);
448 static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
450 if (PyIndex_Check(item)) {
452 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
453 if (i == -1 && PyErr_Occurred())
457 return Euler_item(self, i);
459 else if (PySlice_Check(item)) {
460 Py_ssize_t start, stop, step, slicelength;
462 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
465 if (slicelength <= 0) {
466 return PyTuple_New(0);
468 else if (step == 1) {
469 return Euler_slice(self, start, stop);
472 PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
477 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
483 static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
485 if (PyIndex_Check(item)) {
486 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
487 if (i == -1 && PyErr_Occurred())
491 return Euler_ass_item(self, i, value);
493 else if (PySlice_Check(item)) {
494 Py_ssize_t start, stop, step, slicelength;
496 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
500 return Euler_ass_slice(self, start, stop, value);
502 PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
507 PyErr_Format(PyExc_TypeError, "euler indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
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 */
526 static PyMappingMethods Euler_AsMapping = {
528 (binaryfunc)Euler_subscript,
529 (objobjargproc)Euler_ass_subscript
533 * euler axis, euler.x/y/z
535 static PyObject *Euler_getAxis(EulerObject *self, void *type)
537 return Euler_item(self, GET_INT_FROM_POINTER(type));
540 static int Euler_setAxis(EulerObject *self, PyObject *value, void *type)
542 return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
546 static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
548 if(BaseMath_ReadCallback(self) == -1) /* can read order too */
551 return PyUnicode_FromString(euler_order_str(self));
554 static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
556 const char *order_str= _PyUnicode_AsString(value);
557 short order= euler_order_from_string(order_str, "euler.order");
563 (void)BaseMath_WriteCallback(self); /* order can be written back */
567 /*****************************************************************************/
568 /* Python attributes get/set structure: */
569 /*****************************************************************************/
570 static PyGetSetDef Euler_getseters[] = {
571 {(char *)"x", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler X axis in radians.\n\n:type: float", (void *)0},
572 {(char *)"y", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Y axis in radians.\n\n:type: float", (void *)1},
573 {(char *)"z", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Z axis in radians.\n\n:type: float", (void *)2},
574 {(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},
576 {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
577 {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
578 {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
582 //-----------------------METHOD DEFINITIONS ----------------------
583 static struct PyMethodDef Euler_methods[] = {
584 {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
585 {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
586 {"to_quaternion", (PyCFunction) Euler_to_quaternion, METH_NOARGS, Euler_to_quaternion_doc},
587 {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
588 {"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
589 {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
590 {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
591 {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
592 {NULL, NULL, 0, NULL}
595 //------------------PY_OBECT DEFINITION--------------------------
596 static char euler_doc[] =
597 "This object gives access to Eulers in Blender."
599 PyTypeObject euler_Type = {
600 PyVarObject_HEAD_INIT(NULL, 0)
601 "mathutils.Euler", //tp_name
602 sizeof(EulerObject), //tp_basicsize
604 (destructor)BaseMathObject_dealloc, //tp_dealloc
609 (reprfunc) Euler_repr, //tp_repr
611 &Euler_SeqMethods, //tp_as_sequence
612 &Euler_AsMapping, //tp_as_mapping
619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
621 (traverseproc)BaseMathObject_traverse, //tp_traverse
622 (inquiry)BaseMathObject_clear, //tp_clear
623 (richcmpfunc)Euler_richcmpr, //tp_richcompare
624 0, //tp_weaklistoffset
627 Euler_methods, //tp_methods
629 Euler_getseters, //tp_getset
643 NULL, //tp_subclasses
647 //------------------------newEulerObject (internal)-------------
648 //creates a new euler object
649 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
650 (i.e. it was allocated elsewhere by MEM_mallocN())
651 pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
652 (i.e. it must be created here with PyMEM_malloc())*/
653 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
657 self= base_type ? (EulerObject *)base_type->tp_alloc(base_type, 0) :
658 (EulerObject *)PyObject_GC_New(EulerObject, &euler_Type);
661 /* init callbacks as NULL */
663 self->cb_type= self->cb_subtype= 0;
665 if(type == Py_WRAP) {
667 self->wrapped = Py_WRAP;
669 else if (type == Py_NEW) {
670 self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
672 copy_v3_v3(self->eul, eul);
678 self->wrapped = Py_NEW;
681 PyErr_SetString(PyExc_RuntimeError, "Euler(): invalid type");
688 return (PyObject *)self;
691 PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
693 EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
696 self->cb_user= cb_user;
697 self->cb_type= (unsigned char)cb_type;
698 self->cb_subtype= (unsigned char)cb_subtype;
699 PyObject_GC_Track(self);
702 return (PyObject *)self;