svn merge -r 30566:30717 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / python / generic / mathutils_quat.c
index 322dcf31092440ae5a6b6febb0637eb270caef71..fe0d3cde3089c4c156a879af9c8699064fa3172d 100644 (file)
@@ -31,6 +31,8 @@
 #include "BLI_math.h"
 #include "BKE_utildefines.h"
 
+#define QUAT_SIZE 4
+
 //-----------------------------METHODS------------------------------
 
 /* note: BaseMath_ReadCallback must be called beforehand */
@@ -39,15 +41,15 @@ static PyObject *Quaternion_ToTupleExt(QuaternionObject *self, int ndigits)
        PyObject *ret;
        int i;
 
-       ret= PyTuple_New(4);
+       ret= PyTuple_New(QUAT_SIZE);
 
        if(ndigits >= 0) {
-               for(i= 0; i < 4; i++) {
+               for(i= 0; i < QUAT_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
                }
        }
        else {
-               for(i= 0; i < 4; i++) {
+               for(i= 0; i < QUAT_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
                }
        }
@@ -71,7 +73,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
 {
        float eul[3];
        char *order_str= NULL;
-       short order= 0;
+       short order= EULER_ORDER_XYZ;
        EulerObject *eul_compat = NULL;
        
        if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
@@ -83,7 +85,7 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
        if(order_str) {
                order= euler_order_from_string(order_str, "Matrix.to_euler()");
 
-               if(order < 0)
+               if(order == -1)
                        return NULL;
        }
 
@@ -95,12 +97,12 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args)
                
                quat_to_mat3(mat, self->quat);
 
-               if(order == 0)  mat3_to_compatible_eul(eul, eul_compat->eul, mat);
-               else                    mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat);
+               if(order == EULER_ORDER_XYZ)    mat3_to_compatible_eul(eul, eul_compat->eul, mat);
+               else                                                    mat3_to_compatible_eulO(eul, eul_compat->eul, order, mat);
        }
        else {
-               if(order == 0)  quat_to_eul(eul, self->quat);
-               else                    quat_to_eulO(eul, order, self->quat);
+               if(order == EULER_ORDER_XYZ)    quat_to_eul(eul, self->quat);
+               else                                                    quat_to_eulO(eul, order, self->quat);
        }
        
        return newEulerObject(eul, order, Py_NEW, NULL);
@@ -138,7 +140,7 @@ static char Quaternion_Cross_doc[] =
 
 static PyObject *Quaternion_Cross(QuaternionObject * self, QuaternionObject * value)
 {
-       float quat[4];
+       float quat[QUAT_SIZE];
        
        if (!QuaternionObject_Check(value)) {
                PyErr_SetString( PyExc_TypeError, "quat.cross(value): expected a quaternion argument" );
@@ -188,7 +190,7 @@ static char Quaternion_Difference_doc[] =
 
 static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject * value)
 {
-       float quat[4], tempQuat[4];
+       float quat[QUAT_SIZE], tempQuat[QUAT_SIZE];
        double dot = 0.0f;
        int x;
 
@@ -200,15 +202,11 @@ static PyObject *Quaternion_Difference(QuaternionObject * self, QuaternionObject
        if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
                return NULL;
 
-       tempQuat[0] = self->quat[0];
-       tempQuat[1] = - self->quat[1];
-       tempQuat[2] = - self->quat[2];
-       tempQuat[3] = - self->quat[3];
-
-       dot = sqrt(tempQuat[0] * tempQuat[0] + tempQuat[1] *  tempQuat[1] +
-                                  tempQuat[2] * tempQuat[2] + tempQuat[3] * tempQuat[3]);
+       copy_qt_qt(tempQuat, self->quat);
+       conjugate_qt(tempQuat);
+       dot = sqrt(dot_qtqt(tempQuat, tempQuat));
 
-       for(x = 0; x < 4; x++) {
+       for(x = 0; x < QUAT_SIZE; x++) {
                tempQuat[x] /= (float)(dot * dot);
        }
        mul_qt_qtqt(quat, tempQuat, value->quat);
@@ -230,7 +228,7 @@ static char Quaternion_Slerp_doc[] =
 static PyObject *Quaternion_Slerp(QuaternionObject *self, PyObject *args)
 {
        QuaternionObject *value;
-       float quat[4], fac;
+       float quat[QUAT_SIZE], fac;
 
        if(!PyArg_ParseTuple(args, "O!f:slerp", &quaternion_Type, &value, &fac)) {
                PyErr_SetString(PyExc_TypeError, "quat.slerp(): expected Quaternion types and float");
@@ -381,7 +379,7 @@ static PyObject *Quaternion_repr(QuaternionObject * self)
 
        tuple= Quaternion_ToTupleExt(self, -1);
 
-       ret= PyUnicode_FromFormat("Quaternion%R", tuple);
+       ret= PyUnicode_FromFormat("Quaternion(%R)", tuple);
 
        Py_DECREF(tuple);
        return ret;
@@ -415,10 +413,10 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
 
        switch (comparison_type){
                case Py_EQ:
-                       result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, 4, 1);
+                       result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
                        break;
                case Py_NE:
-                       result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, 4, 1);
+                       result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1);
                        if (result == 0){
                                result = 1;
                        }else{
@@ -441,15 +439,15 @@ static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int c
 //sequence length
 static int Quaternion_len(QuaternionObject * self)
 {
-       return 4;
+       return QUAT_SIZE;
 }
 //----------------------------object[]---------------------------
 //sequence accessor (get)
 static PyObject *Quaternion_item(QuaternionObject * self, int i)
 {
-       if(i<0) i= 4-i;
+       if(i<0) i= QUAT_SIZE-i;
 
-       if(i < 0 || i >= 4) {
+       if(i < 0 || i >= QUAT_SIZE) {
                PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n");
                return NULL;
        }
@@ -470,9 +468,9 @@ static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
                return -1;
        }
 
-       if(i<0) i= 4-i;
+       if(i<0) i= QUAT_SIZE-i;
 
-       if(i < 0 || i >= 4){
+       if(i < 0 || i >= QUAT_SIZE){
                PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n");
                return -1;
        }
@@ -493,9 +491,9 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       CLAMP(begin, 0, 4);
-       if (end<0) end= 5+end;
-       CLAMP(end, 0, 4);
+       CLAMP(begin, 0, QUAT_SIZE);
+       if (end<0) end= (QUAT_SIZE + 1) + end;
+       CLAMP(end, 0, QUAT_SIZE);
        begin = MIN2(begin,end);
 
        list = PyList_New(end - begin);
@@ -510,52 +508,107 @@ static PyObject *Quaternion_slice(QuaternionObject * self, int begin, int end)
 //sequence slice (set)
 static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end, PyObject * seq)
 {
-       int i, y, size = 0;
-       float quat[4];
-       PyObject *q;
+       int i, size;
+       float quat[QUAT_SIZE];
 
        if(!BaseMath_ReadCallback(self))
                return -1;
 
-       CLAMP(begin, 0, 4);
-       if (end<0) end= 5+end;
-       CLAMP(end, 0, 4);
+       CLAMP(begin, 0, QUAT_SIZE);
+       if (end<0) end= (QUAT_SIZE + 1) + end;
+       CLAMP(end, 0, QUAT_SIZE);
        begin = MIN2(begin,end);
 
-       size = PySequence_Length(seq);
+       if((size=mathutils_array_parse(quat, 0, QUAT_SIZE, seq, "mathutils.Quaternion[begin:end] = []")) == -1)
+               return -1;
+       
        if(size != (end - begin)){
-               PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment\n");
+               PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment");
                return -1;
        }
 
-       for (i = 0; i < size; i++) {
-               q = PySequence_GetItem(seq, i);
-               if (q == NULL) { // Failed to read sequence
-                       PyErr_SetString(PyExc_RuntimeError, "quaternion[begin:end] = []: unable to read sequence\n");
-                       return -1;
+       /* parsed well - now set in vector */
+       for(i= 0; i < size; i++)
+               self->quat[begin + i] = quat[i];
+
+       BaseMath_WriteCallback(self);
+       return 0;
+}
+
+
+static PyObject *Quaternion_subscript(QuaternionObject *self, PyObject *item)
+{
+       if (PyIndex_Check(item)) {
+               Py_ssize_t i;
+               i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
+               if (i < 0)
+                       i += QUAT_SIZE;
+               return Quaternion_item(self, i);
+       } else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
                }
+               else if (step == 1) {
+                       return Quaternion_slice(self, start, stop);
+               }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternions");
+                       return NULL;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "quaternion indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return NULL;
+       }
+}
 
-               quat[i]= (float)PyFloat_AsDouble(q);
-               Py_DECREF(q);
 
-               if(quat[i]==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
-                       PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: sequence argument not a number\n");
+static int Quaternion_ass_subscript(QuaternionObject *self, PyObject *item, PyObject *value)
+{
+       if (PyIndex_Check(item)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
                        return -1;
-               }
+               if (i < 0)
+                       i += QUAT_SIZE;
+               return Quaternion_ass_item(self, i, value);
        }
-       //parsed well - now set in vector
-       for(y = 0; y < size; y++)
-               self->quat[begin + y] = quat[y];
+       else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
 
-       BaseMath_WriteCallback(self);
-       return 0;
+               if (PySlice_GetIndicesEx((PySliceObject*)item, QUAT_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return -1;
+
+               if (step == 1)
+                       return Quaternion_ass_slice(self, start, stop, value);
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with quaternion");
+                       return -1;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "quaternion indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return -1;
+       }
 }
+
 //------------------------NUMERIC PROTOCOLS----------------------
 //------------------------obj + obj------------------------------
 //addition
 static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
 {
-       float quat[4];
+       float quat[QUAT_SIZE];
        QuaternionObject *quat1 = NULL, *quat2 = NULL;
 
        if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -576,7 +629,7 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
 static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
 {
        int x;
-       float quat[4];
+       float quat[QUAT_SIZE];
        QuaternionObject *quat1 = NULL, *quat2 = NULL;
 
        if(!QuaternionObject_Check(q1) || !QuaternionObject_Check(q2)) {
@@ -590,7 +643,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
        if(!BaseMath_ReadCallback(quat1) || !BaseMath_ReadCallback(quat2))
                return NULL;
 
-       for(x = 0; x < 4; x++) {
+       for(x = 0; x < QUAT_SIZE; x++) {
                quat[x] = quat1->quat[x] - quat2->quat[x];
        }
 
@@ -600,7 +653,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
 //mulplication
 static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
 {
-       float quat[4], scalar;
+       float quat[QUAT_SIZE], scalar;
        QuaternionObject *quat1 = NULL, *quat2 = NULL;
        VectorObject *vec = NULL;
 
@@ -654,50 +707,59 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
 
 //-----------------PROTOCOL DECLARATIONS--------------------------
 static PySequenceMethods Quaternion_SeqMethods = {
-       (lenfunc) Quaternion_len,                                       /* sq_length */
-       (binaryfunc) 0,                                                         /* sq_concat */
-       (ssizeargfunc) 0,                                                               /* sq_repeat */
-       (ssizeargfunc) Quaternion_item,                         /* sq_item */
-       (ssizessizeargfunc) Quaternion_slice,                   /* sq_slice */
-       (ssizeobjargproc) Quaternion_ass_item,          /* sq_ass_item */
-       (ssizessizeobjargproc) Quaternion_ass_slice,    /* sq_ass_slice */
+       (lenfunc) Quaternion_len,                               /* sq_length */
+       (binaryfunc) NULL,                                              /* sq_concat */
+       (ssizeargfunc) NULL,                                    /* sq_repeat */
+       (ssizeargfunc) Quaternion_item,                 /* sq_item */
+       (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated */
+       (ssizeobjargproc) Quaternion_ass_item,  /* sq_ass_item */
+       (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
+       (objobjproc) NULL,                                              /* sq_contains */
+       (binaryfunc) NULL,                                              /* sq_inplace_concat */
+       (ssizeargfunc) NULL,                                    /* sq_inplace_repeat */
+};
+
+static PyMappingMethods Quaternion_AsMapping = {
+       (lenfunc)Quaternion_len,
+       (binaryfunc)Quaternion_subscript,
+       (objobjargproc)Quaternion_ass_subscript
 };
 
 static PyNumberMethods Quaternion_NumMethods = {
-               (binaryfunc)    Quaternion_add, /*nb_add*/
-               (binaryfunc)    Quaternion_sub, /*nb_subtract*/
-               (binaryfunc)    Quaternion_mul, /*nb_multiply*/
-               0,                                                      /*nb_remainder*/
-               0,                                                      /*nb_divmod*/
-               0,                                                      /*nb_power*/
-               (unaryfunc)     0,      /*nb_negative*/
-               (unaryfunc)     0,      /*tp_positive*/
-               (unaryfunc)     0,      /*tp_absolute*/
-               (inquiry)       0,      /*tp_bool*/
-               (unaryfunc)     0,      /*nb_invert*/
-               0,                              /*nb_lshift*/
-               (binaryfunc)0,  /*nb_rshift*/
-               0,                              /*nb_and*/
-               0,                              /*nb_xor*/
-               0,                              /*nb_or*/
-               0,                              /*nb_int*/
-               0,                              /*nb_reserved*/
-               0,                              /*nb_float*/
-               0,                              /* nb_inplace_add */
-               0,                              /* nb_inplace_subtract */
-               0,                              /* nb_inplace_multiply */
-               0,                              /* nb_inplace_remainder */
-               0,                              /* nb_inplace_power */
-               0,                              /* nb_inplace_lshift */
-               0,                              /* nb_inplace_rshift */
-               0,                              /* nb_inplace_and */
-               0,                              /* nb_inplace_xor */
-               0,                              /* nb_inplace_or */
-               0,                              /* nb_floor_divide */
-               0,                              /* nb_true_divide */
-               0,                              /* nb_inplace_floor_divide */
-               0,                              /* nb_inplace_true_divide */
-               0,                              /* nb_index */
+       (binaryfunc)    Quaternion_add, /*nb_add*/
+       (binaryfunc)    Quaternion_sub, /*nb_subtract*/
+       (binaryfunc)    Quaternion_mul, /*nb_multiply*/
+       0,                                                      /*nb_remainder*/
+       0,                                                      /*nb_divmod*/
+       0,                                                      /*nb_power*/
+       (unaryfunc)     0,      /*nb_negative*/
+       (unaryfunc)     0,      /*tp_positive*/
+       (unaryfunc)     0,      /*tp_absolute*/
+       (inquiry)       0,      /*tp_bool*/
+       (unaryfunc)     0,      /*nb_invert*/
+       0,                              /*nb_lshift*/
+       (binaryfunc)0,  /*nb_rshift*/
+       0,                              /*nb_and*/
+       0,                              /*nb_xor*/
+       0,                              /*nb_or*/
+       0,                              /*nb_int*/
+       0,                              /*nb_reserved*/
+       0,                              /*nb_float*/
+       0,                              /* nb_inplace_add */
+       0,                              /* nb_inplace_subtract */
+       0,                              /* nb_inplace_multiply */
+       0,                              /* nb_inplace_remainder */
+       0,                              /* nb_inplace_power */
+       0,                              /* nb_inplace_lshift */
+       0,                              /* nb_inplace_rshift */
+       0,                              /* nb_inplace_and */
+       0,                              /* nb_inplace_xor */
+       0,                              /* nb_inplace_or */
+       0,                              /* nb_floor_divide */
+       0,                              /* nb_true_divide */
+       0,                              /* nb_inplace_floor_divide */
+       0,                              /* nb_inplace_true_divide */
+       0,                              /* nb_index */
 };
 
 static PyObject *Quaternion_getAxis( QuaternionObject * self, void *type )
@@ -712,125 +774,128 @@ static int Quaternion_setAxis( QuaternionObject * self, PyObject * value, void *
 
 static PyObject *Quaternion_getMagnitude( QuaternionObject * self, void *type )
 {
+       if(!BaseMath_ReadCallback(self))
+               return NULL;
+
        return PyFloat_FromDouble(sqrt(dot_qtqt(self->quat, self->quat)));
 }
 
 static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
 {
+       if(!BaseMath_ReadCallback(self))
+               return NULL;
+
        return PyFloat_FromDouble(2.0 * (saacos(self->quat[0])));
 }
 
-static PyObject *Quaternion_getAxisVec( QuaternionObject * self, void *type )
+static int Quaternion_setAngle(QuaternionObject * self, PyObject * value, void * type)
 {
-       int i;
-       float vec[3];
-       double mag = self->quat[0] * (Py_PI / 180);
-       mag = 2 * (saacos(mag));
-       mag = sin(mag / 2);
-       for(i = 0; i < 3; i++)
-               vec[i] = (float)(self->quat[i + 1] / mag);
-       
-       normalize_v3(vec);
-       //If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations
-       if( EXPP_FloatsAreEqual(vec[0], 0.0f, 10) &&
-               EXPP_FloatsAreEqual(vec[1], 0.0f, 10) &&
-               EXPP_FloatsAreEqual(vec[2], 0.0f, 10) ){
-               vec[0] = 1.0f;
+       float axis[3];
+       float angle;
+
+       if(!BaseMath_ReadCallback(self))
+               return -1;
+
+       quat_to_axis_angle(axis, &angle, self->quat);
+
+       angle = PyFloat_AsDouble(value);
+
+       if(angle==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
+               PyErr_SetString(PyExc_TypeError, "quaternion.angle = value: float expected");
+               return -1;
        }
-       return (PyObject *) newVectorObject(vec, 3, Py_NEW, NULL);
+
+       /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
+       if( EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
+               EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
+               EXPP_FloatsAreEqual(axis[2], 0.0f, 10)
+       ) {
+               axis[0] = 1.0f;
+       }
+       
+       axis_angle_to_quat(self->quat, axis, angle);
+
+       if(!BaseMath_WriteCallback(self))
+               return -1;
+
+       return 0;
 }
 
-//----------------------------------mathutils.Quaternion() --------------
-static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+static PyObject *Quaternion_getAxisVec(QuaternionObject *self, void *type)
 {
-       PyObject *listObject = NULL, *n, *q;
-       int size, i;
-       float quat[4];
-       double angle = 0.0f;
-
-       size = PyTuple_GET_SIZE(args);
-       if (size == 1 || size == 2) { //seq?
-               listObject = PyTuple_GET_ITEM(args, 0);
-               if (PySequence_Check(listObject)) {
-                       size = PySequence_Length(listObject);
-                       if ((size == 4 && PySequence_Length(args) !=1) ||
-                               (size == 3 && PySequence_Length(args) !=2) || (size >4 || size < 3)) {
-                               // invalid args/size
-                               PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                               return NULL;
-                       }
-                          if(size == 3){ //get angle in axis/angle
-                               n = PySequence_GetItem(args, 1);
-                               if(n == NULL) { // parsed item not a number or getItem fail
-                                       PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                                       return NULL;
-                               }
-
-                               angle = PyFloat_AsDouble(n);
-                               Py_DECREF(n);
-
-                               if (angle==-1 && PyErr_Occurred()) {
-                                       PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                                       return NULL;
-                               }
-                       }
-               }else{
-                       listObject = PyTuple_GET_ITEM(args, 1);
-                       if (size>1 && PySequence_Check(listObject)) {
-                               size = PySequence_Length(listObject);
-                               if (size != 3) {
-                                       // invalid args/size
-                                       PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                                       return NULL;
-                               }
-                               angle = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
-
-                               if (angle==-1 && PyErr_Occurred()) {
-                                       PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                                       return NULL;
-                               }
-                       } else { // argument was not a sequence
-                               PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                               return NULL;
-                       }
-               }
-       } else if (size == 0) { //returns a new empty quat
-               return newQuaternionObject(NULL, Py_NEW, NULL);
-       } else {
-               listObject = args;
+       float axis[3];
+       float angle;
+
+       if(!BaseMath_ReadCallback(self))
+               return NULL;
+       
+       quat_to_axis_angle(axis, &angle, self->quat);
+
+       /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree rotations */
+       if( EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
+               EXPP_FloatsAreEqual(axis[1], 0.0f, 10) &&
+               EXPP_FloatsAreEqual(axis[2], 0.0f, 10)
+       ) {
+               axis[0] = 1.0f;
        }
 
-       if (size == 3) { // invalid quat size
-               if(PySequence_Length(args) != 2){
-                       PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                       return NULL;
-               }
-       }else{
-               if(size != 4){
-                       PyErr_SetString(PyExc_AttributeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                       return NULL;
-               }
+       return (PyObject *) newVectorObject(axis, 3, Py_NEW, NULL);
+}
+
+static int Quaternion_setAxisVec(QuaternionObject *self, PyObject *value, void *type)
+{
+       float axis[3];
+       float angle;
+       
+       VectorObject *vec;
+
+       if(!BaseMath_ReadCallback(self))
+               return -1;
+
+       quat_to_axis_angle(axis, &angle, self->quat);
+
+       if(!VectorObject_Check(value)) {
+               PyErr_SetString(PyExc_TypeError, "quaternion.axis = value: expected a 3D Vector");
+               return -1;
        }
+       
+       vec= (VectorObject *)value;
+       if(!BaseMath_ReadCallback(vec))
+               return -1;
 
-       for (i=0; i<size; i++) { //parse
-               q = PySequence_GetItem(listObject, i);
-               if (q == NULL) { // Failed to read sequence
-                       PyErr_SetString(PyExc_RuntimeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
-                       return NULL;
-               }
+       axis_angle_to_quat(self->quat, vec->vec, angle);
 
-               quat[i] = PyFloat_AsDouble(q);
-               Py_DECREF(q);
+       if(!BaseMath_WriteCallback(self))
+               return -1;
 
-               if (quat[i]==-1 && PyErr_Occurred()) {
-                       PyErr_SetString(PyExc_TypeError, "mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
+       return 0;
+}
+
+//----------------------------------mathutils.Quaternion() --------------
+static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+       PyObject *seq= NULL;
+       float angle = 0.0f;
+       float quat[QUAT_SIZE]= {0.0f, 0.0f, 0.0f, 0.0f};
+
+       if(!PyArg_ParseTuple(args, "|Of:mathutils.Quaternion", &seq, &angle))
+               return NULL;
+
+       switch(PyTuple_GET_SIZE(args)) {
+       case 0:
+               break;
+       case 1:
+               if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
+                       return NULL;
+               break;
+       case 2:
+               if (mathutils_array_parse(quat, 3, 3, seq, "mathutils.Quaternion()") == -1)
                        return NULL;
-               }
-       }
 
-       if(size == 3) //calculate the quat based on axis/angle
                axis_angle_to_quat(quat, quat, angle);
-
+               break;
+       /* PyArg_ParseTuple assures no more then 2 */
+       }
        return newQuaternionObject(quat, Py_NEW, NULL);
 }
 
@@ -862,8 +927,8 @@ static PyGetSetDef Quaternion_getseters[] = {
        {"y", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Y axis. **type** float", (void *)2},
        {"z", (getter)Quaternion_getAxis, (setter)Quaternion_setAxis, "Quaternion Z axis. **type** float", (void *)3},
        {"magnitude", (getter)Quaternion_getMagnitude, (setter)NULL, "Size of the quaternion (readonly). **type** float", NULL},
-       {"angle", (getter)Quaternion_getAngle, (setter)NULL, "angle of the quaternion (readonly). **type** float", NULL},
-       {"axis",(getter)Quaternion_getAxisVec, (setter)NULL, "quaternion axis as a vector (readonly). **type** :class:`Vector`", NULL},
+       {"angle", (getter)Quaternion_getAngle, (setter)Quaternion_setAngle, "angle of the quaternion. **type** float", NULL},
+       {"axis",(getter)Quaternion_getAxisVec, (setter)Quaternion_setAxisVec, "quaternion axis as a vector. **type** :class:`Vector`", NULL},
        {"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, BaseMathObject_Wrapped_doc, NULL},
        {"_owner", (getter)BaseMathObject_getOwner, (setter)NULL, BaseMathObject_Owner_doc, NULL},
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
@@ -883,10 +948,10 @@ PyTypeObject quaternion_Type = {
        0,                                                              //tp_getattr
        0,                                                              //tp_setattr
        0,                                                              //tp_compare
-       (reprfunc) Quaternion_repr,                     //tp_repr
-       &Quaternion_NumMethods,                         //tp_as_number
-       &Quaternion_SeqMethods,                         //tp_as_sequence
-       0,                                                              //tp_as_mapping
+       (reprfunc) Quaternion_repr,             //tp_repr
+       &Quaternion_NumMethods,                 //tp_as_number
+       &Quaternion_SeqMethods,                 //tp_as_sequence
+       &Quaternion_AsMapping,                  //tp_as_mapping
        0,                                                              //tp_hash
        0,                                                              //tp_call
        0,                                                              //tp_str
@@ -942,7 +1007,7 @@ PyObject *newQuaternionObject(float *quat, int type, PyTypeObject *base_type)
                self->quat = quat;
                self->wrapped = Py_WRAP;
        }else if (type == Py_NEW){
-               self->quat = PyMem_Malloc(4 * sizeof(float));
+               self->quat = PyMem_Malloc(QUAT_SIZE * sizeof(float));
                if(!quat) { //new empty
                        unit_qt(self->quat);
                }else{