3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
23 * Contributor(s): Joseph Gilbert
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/python/mathutils/mathutils_Euler.c
29 * \ingroup pymathutils
35 #include "mathutils.h"
38 #include "BLI_utildefines.h"
42 //----------------------------------mathutils.Euler() -------------------
43 //makes a new euler for you to play with
44 static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47 const char *order_str= NULL;
49 float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
50 short order= EULER_ORDER_XYZ;
52 if (kwds && PyDict_Size(kwds)) {
53 PyErr_SetString(PyExc_TypeError,
55 "takes no keyword args");
59 if (!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
62 switch (PyTuple_GET_SIZE(args)) {
66 if ((order=euler_order_from_string(order_str, "mathutils.Euler()")) == -1)
68 /* intentionally pass through */
70 if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
74 return Euler_CreatePyObject(eul, order, Py_NEW, type);
77 /* internal use, assume read callback is done */
78 static const char *euler_order_str(EulerObject *self)
80 static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
81 return order[self->order-EULER_ORDER_XYZ];
84 short euler_order_from_string(const char *str, const char *error_prefix)
86 if ((str[0] && str[1] && str[2] && str[3]=='\0')) {
87 switch (*((PY_INT32_T *)str)) {
88 case 'X'|'Y'<<8|'Z'<<16: return EULER_ORDER_XYZ;
89 case 'X'|'Z'<<8|'Y'<<16: return EULER_ORDER_XZY;
90 case 'Y'|'X'<<8|'Z'<<16: return EULER_ORDER_YXZ;
91 case 'Y'|'Z'<<8|'X'<<16: return EULER_ORDER_YZX;
92 case 'Z'|'X'<<8|'Y'<<16: return EULER_ORDER_ZXY;
93 case 'Z'|'Y'<<8|'X'<<16: return EULER_ORDER_ZYX;
97 PyErr_Format(PyExc_ValueError,
98 "%s: invalid euler order '%s'",
103 /* note: BaseMath_ReadCallback must be called beforehand */
104 static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
109 ret= PyTuple_New(EULER_SIZE);
112 for (i= 0; i < EULER_SIZE; i++) {
113 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
117 for (i= 0; i < EULER_SIZE; i++) {
118 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
125 //-----------------------------METHODS----------------------------
126 //return a quaternion representation of the euler
128 PyDoc_STRVAR(Euler_to_quaternion_doc,
129 ".. method:: to_quaternion()\n"
131 " Return a quaternion representation of the euler.\n"
133 " :return: Quaternion representation of the euler.\n"
134 " :rtype: :class:`Quaternion`\n"
136 static PyObject *Euler_to_quaternion(EulerObject * self)
140 if (BaseMath_ReadCallback(self) == -1)
143 eulO_to_quat(quat, self->eul, self->order);
145 return Quaternion_CreatePyObject(quat, Py_NEW, NULL);
148 //return a matrix representation of the euler
149 PyDoc_STRVAR(Euler_to_matrix_doc,
150 ".. method:: to_matrix()\n"
152 " Return a matrix representation of the euler.\n"
154 " :return: A 3x3 roation matrix representation of the euler.\n"
155 " :rtype: :class:`Matrix`\n"
157 static PyObject *Euler_to_matrix(EulerObject * self)
161 if (BaseMath_ReadCallback(self) == -1)
164 eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
166 return Matrix_CreatePyObject(mat, 3, 3 , Py_NEW, NULL);
169 PyDoc_STRVAR(Euler_zero_doc,
170 ".. method:: zero()\n"
172 " Set all values to zero.\n"
174 static PyObject *Euler_zero(EulerObject * self)
178 if (BaseMath_WriteCallback(self) == -1)
184 PyDoc_STRVAR(Euler_rotate_axis_doc,
185 ".. method:: rotate_axis(axis, angle)\n"
187 " Rotates the euler a certain amount and returning a unique euler rotation\n"
188 " (no 720 degree pitches).\n"
190 " :arg axis: single character in ['X, 'Y', 'Z'].\n"
191 " :type axis: string\n"
192 " :arg angle: angle in radians.\n"
193 " :type angle: float\n"
195 static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
198 int axis; /* actually a character */
200 if (!PyArg_ParseTuple(args, "Cf:rotate", &axis, &angle)) {
201 PyErr_SetString(PyExc_TypeError,
202 "Euler.rotate_axis(): "
203 "expected an axis 'X', 'Y', 'Z' and an angle (float)");
207 if (!(ELEM3(axis, 'X', 'Y', 'Z'))) {
208 PyErr_SetString(PyExc_ValueError,
209 "Euler.rotate_axis(): "
210 "expected axis to be 'X', 'Y' or 'Z'");
214 if (BaseMath_ReadCallback(self) == -1)
218 rotate_eulO(self->eul, self->order, (char)axis, angle);
220 (void)BaseMath_WriteCallback(self);
225 PyDoc_STRVAR(Euler_rotate_doc,
226 ".. method:: rotate(other)\n"
228 " Rotates the euler a by another mathutils value.\n"
230 " :arg other: rotation component of mathutils value\n"
231 " :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
233 static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
235 float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
237 if (BaseMath_ReadCallback(self) == -1)
240 if (mathutils_any_to_rotmat(other_rmat, value, "euler.rotate(value)") == -1)
243 eulO_to_mat3(self_rmat, self->eul, self->order);
244 mul_m3_m3m3(rmat, other_rmat, self_rmat);
246 mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
248 (void)BaseMath_WriteCallback(self);
252 PyDoc_STRVAR(Euler_make_compatible_doc,
253 ".. method:: make_compatible(other)\n"
255 " Make this euler compatible with another,\n"
256 " so interpolating between them works as intended.\n"
258 " .. note:: the rotation order is not taken into account for this function.\n"
260 static PyObject *Euler_make_compatible(EulerObject * self, PyObject *value)
262 float teul[EULER_SIZE];
264 if (BaseMath_ReadCallback(self) == -1)
267 if (mathutils_array_parse(teul, EULER_SIZE, EULER_SIZE, value,
268 "euler.make_compatible(other), invalid 'other' arg") == -1)
273 compatible_eul(self->eul, teul);
275 (void)BaseMath_WriteCallback(self);
280 //----------------------------Euler.rotate()-----------------------
281 // return a copy of the euler
283 PyDoc_STRVAR(Euler_copy_doc,
284 ".. function:: copy()\n"
286 " Returns a copy of this euler.\n"
288 " :return: A copy of the euler.\n"
289 " :rtype: :class:`Euler`\n"
291 " .. note:: use this to get a copy of a wrapped euler with\n"
292 " no reference to the original data.\n"
294 static PyObject *Euler_copy(EulerObject *self)
296 if (BaseMath_ReadCallback(self) == -1)
299 return Euler_CreatePyObject(self->eul, self->order, Py_NEW, Py_TYPE(self));
302 //----------------------------print object (internal)--------------
303 //print the object to screen
305 static PyObject *Euler_repr(EulerObject * self)
307 PyObject *ret, *tuple;
309 if (BaseMath_ReadCallback(self) == -1)
312 tuple= Euler_ToTupleExt(self, -1);
314 ret= PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self));
320 static PyObject* Euler_richcmpr(PyObject *a, PyObject *b, int op)
323 int ok= -1; /* zero is true */
325 if (EulerObject_Check(a) && EulerObject_Check(b)) {
326 EulerObject *eulA= (EulerObject*)a;
327 EulerObject *eulB= (EulerObject*)b;
329 if (BaseMath_ReadCallback(eulA) == -1 || BaseMath_ReadCallback(eulB) == -1)
332 ok= ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
337 ok = !ok; /* pass through */
339 res = ok ? Py_False : Py_True;
346 res = Py_NotImplemented;
353 return Py_INCREF(res), res;
356 //---------------------SEQUENCE PROTOCOLS------------------------
357 //----------------------------len(object)------------------------
359 static int Euler_len(EulerObject *UNUSED(self))
363 //----------------------------object[]---------------------------
364 //sequence accessor (get)
365 static PyObject *Euler_item(EulerObject * self, int i)
367 if (i<0) i= EULER_SIZE-i;
369 if (i < 0 || i >= EULER_SIZE) {
370 PyErr_SetString(PyExc_IndexError,
372 "array index out of range");
376 if (BaseMath_ReadIndexCallback(self, i) == -1)
379 return PyFloat_FromDouble(self->eul[i]);
382 //----------------------------object[]-------------------------
383 //sequence accessor (set)
384 static int Euler_ass_item(EulerObject * self, int i, PyObject *value)
386 float f = PyFloat_AsDouble(value);
388 if (f == -1 && PyErr_Occurred()) { // parsed item not a number
389 PyErr_SetString(PyExc_TypeError,
390 "euler[attribute] = x: "
391 "argument not a number");
395 if (i<0) i= EULER_SIZE-i;
397 if (i < 0 || i >= EULER_SIZE) {
398 PyErr_SetString(PyExc_IndexError,
399 "euler[attribute] = x: "
400 "array assignment index out of range");
406 if (BaseMath_WriteIndexCallback(self, i) == -1)
411 //----------------------------object[z:y]------------------------
412 //sequence slice (get)
413 static PyObject *Euler_slice(EulerObject * self, int begin, int end)
418 if (BaseMath_ReadCallback(self) == -1)
421 CLAMP(begin, 0, EULER_SIZE);
422 if (end<0) end= (EULER_SIZE + 1) + end;
423 CLAMP(end, 0, EULER_SIZE);
424 begin= MIN2(begin, end);
426 tuple= PyTuple_New(end - begin);
427 for (count = begin; count < end; count++) {
428 PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->eul[count]));
433 //----------------------------object[z:y]------------------------
434 //sequence slice (set)
435 static int Euler_ass_slice(EulerObject *self, int begin, int end, PyObject *seq)
438 float eul[EULER_SIZE];
440 if (BaseMath_ReadCallback(self) == -1)
443 CLAMP(begin, 0, EULER_SIZE);
444 if (end<0) end= (EULER_SIZE + 1) + end;
445 CLAMP(end, 0, EULER_SIZE);
446 begin = MIN2(begin, end);
448 if ((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
451 if (size != (end - begin)) {
452 PyErr_SetString(PyExc_ValueError,
453 "euler[begin:end] = []: "
454 "size mismatch in slice assignment");
458 for (i= 0; i < EULER_SIZE; i++)
459 self->eul[begin + i] = eul[i];
461 (void)BaseMath_WriteCallback(self);
465 static PyObject *Euler_subscript(EulerObject *self, PyObject *item)
467 if (PyIndex_Check(item)) {
469 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
470 if (i == -1 && PyErr_Occurred())
474 return Euler_item(self, i);
476 else if (PySlice_Check(item)) {
477 Py_ssize_t start, stop, step, slicelength;
479 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
482 if (slicelength <= 0) {
483 return PyTuple_New(0);
485 else if (step == 1) {
486 return Euler_slice(self, start, stop);
489 PyErr_SetString(PyExc_IndexError,
490 "slice steps not supported with eulers");
495 PyErr_Format(PyExc_TypeError,
496 "euler indices must be integers, not %.200s",
497 Py_TYPE(item)->tp_name);
503 static int Euler_ass_subscript(EulerObject *self, PyObject *item, PyObject *value)
505 if (PyIndex_Check(item)) {
506 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
507 if (i == -1 && PyErr_Occurred())
511 return Euler_ass_item(self, i, value);
513 else if (PySlice_Check(item)) {
514 Py_ssize_t start, stop, step, slicelength;
516 if (PySlice_GetIndicesEx((void *)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
520 return Euler_ass_slice(self, start, stop, value);
522 PyErr_SetString(PyExc_IndexError,
523 "slice steps not supported with euler");
528 PyErr_Format(PyExc_TypeError,
529 "euler indices must be integers, not %.200s",
530 Py_TYPE(item)->tp_name);
535 //-----------------PROTCOL DECLARATIONS--------------------------
536 static PySequenceMethods Euler_SeqMethods = {
537 (lenfunc) Euler_len, /* sq_length */
538 (binaryfunc) NULL, /* sq_concat */
539 (ssizeargfunc) NULL, /* sq_repeat */
540 (ssizeargfunc) Euler_item, /* sq_item */
541 (ssizessizeargfunc) NULL, /* sq_slice, deprecated */
542 (ssizeobjargproc) Euler_ass_item, /* sq_ass_item */
543 (ssizessizeobjargproc) NULL, /* sq_ass_slice, deprecated */
544 (objobjproc) NULL, /* sq_contains */
545 (binaryfunc) NULL, /* sq_inplace_concat */
546 (ssizeargfunc) NULL, /* sq_inplace_repeat */
549 static PyMappingMethods Euler_AsMapping = {
551 (binaryfunc)Euler_subscript,
552 (objobjargproc)Euler_ass_subscript
556 * euler axis, euler.x/y/z
558 static PyObject *Euler_getAxis(EulerObject *self, void *type)
560 return Euler_item(self, GET_INT_FROM_POINTER(type));
563 static int Euler_setAxis(EulerObject *self, PyObject *value, void *type)
565 return Euler_ass_item(self, GET_INT_FROM_POINTER(type), value);
569 static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure))
571 if (BaseMath_ReadCallback(self) == -1) /* can read order too */
574 return PyUnicode_FromString(euler_order_str(self));
577 static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure))
579 const char *order_str= _PyUnicode_AsString(value);
580 short order= euler_order_from_string(order_str, "euler.order");
586 (void)BaseMath_WriteCallback(self); /* order can be written back */
590 /*****************************************************************************/
591 /* Python attributes get/set structure: */
592 /*****************************************************************************/
593 static PyGetSetDef Euler_getseters[] = {
594 {(char *)"x", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler X axis in radians.\n\n:type: float", (void *)0},
595 {(char *)"y", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Y axis in radians.\n\n:type: float", (void *)1},
596 {(char *)"z", (getter)Euler_getAxis, (setter)Euler_setAxis, (char *)"Euler Z axis in radians.\n\n:type: float", (void *)2},
597 {(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},
599 {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},
600 {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL},
601 {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
605 //-----------------------METHOD DEFINITIONS ----------------------
606 static struct PyMethodDef Euler_methods[] = {
607 {"zero", (PyCFunction) Euler_zero, METH_NOARGS, Euler_zero_doc},
608 {"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
609 {"to_quaternion", (PyCFunction) Euler_to_quaternion, METH_NOARGS, Euler_to_quaternion_doc},
610 {"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
611 {"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
612 {"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
613 {"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
614 {"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
615 {NULL, NULL, 0, NULL}
618 //------------------PY_OBECT DEFINITION--------------------------
619 PyDoc_STRVAR(euler_doc,
620 "This object gives access to Eulers in Blender."
622 PyTypeObject euler_Type = {
623 PyVarObject_HEAD_INIT(NULL, 0)
624 "mathutils.Euler", //tp_name
625 sizeof(EulerObject), //tp_basicsize
627 (destructor)BaseMathObject_dealloc, //tp_dealloc
632 (reprfunc) Euler_repr, //tp_repr
634 &Euler_SeqMethods, //tp_as_sequence
635 &Euler_AsMapping, //tp_as_mapping
642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, //tp_flags
644 (traverseproc)BaseMathObject_traverse, //tp_traverse
645 (inquiry)BaseMathObject_clear, //tp_clear
646 (richcmpfunc)Euler_richcmpr, //tp_richcompare
647 0, //tp_weaklistoffset
650 Euler_methods, //tp_methods
652 Euler_getseters, //tp_getset
666 NULL, //tp_subclasses
670 //------------------------Euler_CreatePyObject (internal)-------------
671 //creates a new euler object
672 /*pass Py_WRAP - if vector is a WRAPPER for data allocated by BLENDER
673 (i.e. it was allocated elsewhere by MEM_mallocN())
674 pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
675 (i.e. it must be created here with PyMEM_malloc())*/
676 PyObject *Euler_CreatePyObject(float *eul, short order, int type, PyTypeObject *base_type)
680 self= base_type ? (EulerObject *)base_type->tp_alloc(base_type, 0) :
681 (EulerObject *)PyObject_GC_New(EulerObject, &euler_Type);
684 /* init callbacks as NULL */
686 self->cb_type= self->cb_subtype= 0;
688 if (type == Py_WRAP) {
690 self->wrapped = Py_WRAP;
692 else if (type == Py_NEW) {
693 self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
695 copy_v3_v3(self->eul, eul);
701 self->wrapped = Py_NEW;
704 Py_FatalError("Euler(): invalid type!");
710 return (PyObject *)self;
713 PyObject *Euler_CreatePyObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
715 EulerObject *self= (EulerObject *)Euler_CreatePyObject(NULL, order, Py_NEW, NULL);
718 self->cb_user= cb_user;
719 self->cb_type= (unsigned char)cb_type;
720 self->cb_subtype= (unsigned char)cb_subtype;
721 PyObject_GC_Track(self);
724 return (PyObject *)self;