rna/python mathutils module
authorCampbell Barton <ideasman42@gmail.com>
Sun, 25 Apr 2010 23:33:09 +0000 (23:33 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 25 Apr 2010 23:33:09 +0000 (23:33 +0000)
- return euler rotation values from rna now have correct rotation order.
- mathutils.Euler stored rotation order off by 1. (didnt work at all)
- Euler/Quat/Color sliceing working again.

source/blender/python/generic/mathutils_color.c
source/blender/python/generic/mathutils_euler.c
source/blender/python/generic/mathutils_matrix.c
source/blender/python/generic/mathutils_quat.c
source/blender/python/intern/bpy_rna.c

index 9d770c0e6fd8854f980bc82078365f40706ac51a..ce2a81c50056d9ff69b91667dd5d6073e95ac2d9 100644 (file)
@@ -27,6 +27,8 @@
 #include "BLI_math.h"
 #include "BKE_utildefines.h"
 
+#define COLOR_SIZE 3
+
 //----------------------------------mathutils.Color() -------------------
 //makes a new color for you to play with
 static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
@@ -37,7 +39,7 @@ static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwar
        case 0:
                break;
        case 1:
-               if((mathutils_array_parse(col, 3, 3, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1)
+               if((mathutils_array_parse(col, COLOR_SIZE, COLOR_SIZE, PyTuple_GET_ITEM(args, 0), "mathutils.Color()")) == -1)
                        return NULL;
                break;
        default:
@@ -55,15 +57,15 @@ static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
        PyObject *ret;
        int i;
 
-       ret= PyTuple_New(3);
+       ret= PyTuple_New(COLOR_SIZE);
 
        if(ndigits >= 0) {
-               for(i= 0; i < 3; i++) {
+               for(i= 0; i < COLOR_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
                }
        }
        else {
-               for(i= 0; i < 3; i++) {
+               for(i= 0; i < COLOR_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
                }
        }
@@ -137,10 +139,10 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
 
        switch (comparison_type){
                case Py_EQ:
-                       result = EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1);
+                       result = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
                        break;
                case Py_NE:
-                       result = !EXPP_VectorsAreEqual(colA->col, colB->col, 3, 1);
+                       result = !EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1);
                        break;
                default:
                        printf("The result of the comparison could not be evaluated");
@@ -158,15 +160,15 @@ static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int compar
 //sequence length
 static int Color_len(ColorObject * self)
 {
-       return 3;
+       return COLOR_SIZE;
 }
 //----------------------------object[]---------------------------
 //sequence accessor (get)
 static PyObject *Color_item(ColorObject * self, int i)
 {
-       if(i<0) i= 3-i;
+       if(i<0) i= COLOR_SIZE-i;
 
-       if(i < 0 || i >= 3) {
+       if(i < 0 || i >= COLOR_SIZE) {
                PyErr_SetString(PyExc_IndexError, "color[attribute]: array index out of range");
                return NULL;
        }
@@ -188,9 +190,9 @@ static int Color_ass_item(ColorObject * self, int i, PyObject * value)
                return -1;
        }
 
-       if(i<0) i= 3-i;
+       if(i<0) i= COLOR_SIZE-i;
 
-       if(i < 0 || i >= 3){
+       if(i < 0 || i >= COLOR_SIZE){
                PyErr_SetString(PyExc_IndexError, "color[attribute] = x: array assignment index out of range\n");
                return -1;
        }
@@ -212,9 +214,9 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       CLAMP(begin, 0, 3);
-       if (end<0) end= 4+end;
-       CLAMP(end, 0, 3);
+       CLAMP(begin, 0, COLOR_SIZE);
+       if (end<0) end= (COLOR_SIZE + 1) + end;
+       CLAMP(end, 0, COLOR_SIZE);
        begin = MIN2(begin,end);
 
        list = PyList_New(end - begin);
@@ -227,61 +229,116 @@ static PyObject *Color_slice(ColorObject * self, int begin, int end)
 }
 //----------------------------object[z:y]------------------------
 //sequence slice (set)
-static int Color_ass_slice(ColorObject * self, int begin, int end,
-                                PyObject * seq)
+static int Color_ass_slice(ColorObject * self, int begin, int end, PyObject * seq)
 {
-       int i, y, size = 0;
-       float col[3];
-       PyObject *e;
+       int i, size;
+       float col[COLOR_SIZE];
 
        if(!BaseMath_ReadCallback(self))
                return -1;
 
-       CLAMP(begin, 0, 3);
-       if (end<0) end= 4+end;
-       CLAMP(end, 0, 3);
+       CLAMP(begin, 0, COLOR_SIZE);
+       if (end<0) end= (COLOR_SIZE + 1) + end;
+       CLAMP(end, 0, COLOR_SIZE);
        begin = MIN2(begin,end);
 
-       size = PySequence_Length(seq);
+       if((size=mathutils_array_parse(col, 0, COLOR_SIZE, seq, "mathutils.Color[begin:end] = []")) == -1)
+               return -1;
+
        if(size != (end - begin)){
                PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: size mismatch in slice assignment");
                return -1;
        }
 
-       for (i = 0; i < size; i++) {
-               e = PySequence_GetItem(seq, i);
-               if (e == NULL) { // Failed to read sequence
-                       PyErr_SetString(PyExc_RuntimeError, "color[begin:end] = []: unable to read sequence");
-                       return -1;
+       for(i= 0; i < COLOR_SIZE; i++)
+               self->col[begin + i] = col[i];
+
+       BaseMath_WriteCallback(self);
+       return 0;
+}
+
+static PyObject *Color_subscript(ColorObject *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 += COLOR_SIZE;
+               return Color_item(self, i);
+       } else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
+               }
+               else if (step == 1) {
+                       return Color_slice(self, start, stop);
                }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
+                       return NULL;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "euler indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return NULL;
+       }
+}
+
+static int Color_ass_subscript(ColorObject *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 += COLOR_SIZE;
+               return Color_ass_item(self, i, value);
+       }
+       else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
 
-               col[i] = (float)PyFloat_AsDouble(e);
-               Py_DECREF(e);
+               if (PySlice_GetIndicesEx((PySliceObject*)item, COLOR_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return -1;
 
-               if(col[i]==-1 && PyErr_Occurred()) { // parsed item not a number
-                       PyErr_SetString(PyExc_TypeError, "color[begin:end] = []: sequence argument not a number");
+               if (step == 1)
+                       return Color_ass_slice(self, start, stop, value);
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
                        return -1;
                }
        }
-       //parsed well - now set in vector
-       for(y = 0; y < 3; y++){
-               self->col[begin + y] = col[y];
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "euler indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return -1;
        }
-
-       BaseMath_WriteCallback(self);
-       return 0;
 }
+
 //-----------------PROTCOL DECLARATIONS--------------------------
 static PySequenceMethods Color_SeqMethods = {
-       (lenfunc) Color_len,                                            /* sq_length */
-       (binaryfunc) 0,                                                         /* sq_concat */
-       (ssizeargfunc) 0,                                                               /* sq_repeat */
-       (ssizeargfunc) Color_item,                                      /* sq_item */
-       (ssizessizeargfunc) Color_slice,                                /* sq_slice */
-       (ssizeobjargproc) Color_ass_item,                               /* sq_ass_item */
-       (ssizessizeobjargproc) Color_ass_slice,                 /* sq_ass_slice */
+       (lenfunc) Color_len,                                    /* sq_length */
+       (binaryfunc) 0,                                                 /* sq_concat */
+       (ssizeargfunc) 0,                                               /* sq_repeat */
+       (ssizeargfunc) Color_item,                              /* sq_item */
+       (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated */
+       (ssizeobjargproc) Color_ass_item,               /* sq_ass_item */
+       (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
 };
 
+static PyMappingMethods Color_AsMapping = {
+       (lenfunc)Color_len,
+       (binaryfunc)Color_subscript,
+       (objobjargproc)Color_ass_subscript
+};
 
 /* color channel, vector.r/g/b */
 static PyObject *Color_getChannel( ColorObject * self, void *type )
@@ -414,7 +471,7 @@ PyTypeObject color_Type = {
        (reprfunc) Color_repr,                  //tp_repr
        0,                              //tp_as_number
        &Color_SeqMethods,                              //tp_as_sequence
-       0,                                                              //tp_as_mapping
+       &Color_AsMapping,                               //tp_as_mapping
        0,                                                              //tp_hash
        0,                                                              //tp_call
        0,                                                              //tp_str
@@ -458,7 +515,6 @@ PyTypeObject color_Type = {
 PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
 {
        ColorObject *self;
-       int x;
 
        if(base_type)   self = (ColorObject *)base_type->tp_alloc(base_type, 0);
        else                    self = PyObject_NEW(ColorObject, &color_Type);
@@ -470,17 +526,17 @@ PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
        if(type == Py_WRAP){
                self->col = col;
                self->wrapped = Py_WRAP;
-       }else if (type == Py_NEW){
-               self->col = PyMem_Malloc(3 * sizeof(float));
-               if(!col) { //new empty
-                       for(x = 0; x < 3; x++) {
-                               self->col[x] = 0.0f;
-                       }
-               }else{
-                       VECCOPY(self->col, col);
-               }
+       }
+       else if (type == Py_NEW){
+               self->col = PyMem_Malloc(COLOR_SIZE * sizeof(float));
+               if(col)
+                       copy_v3_v3(self->col, col);
+               else
+                       zero_v3(self->col);
+
                self->wrapped = Py_NEW;
-       }else{ //bad type
+       }
+       else {
                return NULL;
        }
 
index 2c8dbf419971a2b483b4e1a6c33850cf6a547355..e3635012d913d74d373c31aacb805dd39c5dffdb 100644 (file)
@@ -35,6 +35,8 @@
 #include "BLO_sys_types.h"
 #endif
 
+#define EULER_SIZE 3
+
 //----------------------------------mathutils.Euler() -------------------
 //makes a new euler for you to play with
 static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
@@ -42,8 +44,8 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar
        PyObject *seq= NULL;
        char *order_str= NULL;
 
-       float eul[3]= {0.0f, 0.0f, 0.0f};
-       short order= 0;
+       float eul[EULER_SIZE]= {0.0f, 0.0f, 0.0f};
+       short order= EULER_ORDER_XYZ;
 
        if(!PyArg_ParseTuple(args, "|Os:mathutils.Euler", &seq, &order_str))
                return NULL;
@@ -56,7 +58,7 @@ static PyObject *Euler_new(PyTypeObject * type, PyObject * args, PyObject * kwar
                        return NULL;
                /* intentionally pass through */
        case 1:
-               if (mathutils_array_parse(eul, 3, 3, seq, "mathutils.Euler()") == -1)
+               if (mathutils_array_parse(eul, EULER_SIZE, EULER_SIZE, seq, "mathutils.Euler()") == -1)
                        return NULL;
                break;
        }
@@ -67,12 +69,12 @@ short euler_order_from_string(const char *str, const char *error_prefix)
 {
        if((str[0] && str[1] && str[2] && str[3]=='\0')) {
                switch(*((int32_t *)str)) {
-                       case 'X'|'Y'<<8|'Z'<<16:        return 0;
-                       case 'X'|'Z'<<8|'Y'<<16:        return 1;
-                       case 'Y'|'X'<<8|'Z'<<16:        return 2;
-                       case 'Y'|'Z'<<8|'X'<<16:        return 3;
-                       case 'Z'|'X'<<8|'Y'<<16:        return 4;
-                       case 'Z'|'Y'<<8|'X'<<16:        return 5;
+                       case 'X'|'Y'<<8|'Z'<<16:        return EULER_ORDER_XYZ;
+                       case 'X'|'Z'<<8|'Y'<<16:        return EULER_ORDER_XZY;
+                       case 'Y'|'X'<<8|'Z'<<16:        return EULER_ORDER_YXZ;
+                       case 'Y'|'Z'<<8|'X'<<16:        return EULER_ORDER_YZX;
+                       case 'Z'|'X'<<8|'Y'<<16:        return EULER_ORDER_ZXY;
+                       case 'Z'|'Y'<<8|'X'<<16:        return EULER_ORDER_ZYX;
                }
        }
 
@@ -86,15 +88,15 @@ static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
        PyObject *ret;
        int i;
 
-       ret= PyTuple_New(3);
+       ret= PyTuple_New(EULER_SIZE);
 
        if(ndigits >= 0) {
-               for(i= 0; i < 3; i++) {
+               for(i= 0; i < EULER_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
                }
        }
        else {
-               for(i= 0; i < 3; i++) {
+               for(i= 0; i < EULER_SIZE; i++) {
                        PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
                }
        }
@@ -120,8 +122,8 @@ static PyObject *Euler_ToQuat(EulerObject * self)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       if(self->order==0)      eul_to_quat(quat, self->eul);
-       else                            eulO_to_quat(quat, self->eul, self->order);
+       if(self->order==EULER_ORDER_XYZ)        eul_to_quat(quat, self->eul);
+       else                                                            eulO_to_quat(quat, self->eul, self->order);
 
        return newQuaternionObject(quat, Py_NEW, NULL);
 }
@@ -142,8 +144,8 @@ static PyObject *Euler_ToMatrix(EulerObject * self)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       if(self->order==0)      eul_to_mat3((float (*)[3])mat, self->eul);
-       else                            eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
+       if(self->order==EULER_ORDER_XYZ)        eul_to_mat3((float (*)[3])mat, self->eul);
+       else                                                            eulO_to_mat3((float (*)[3])mat, self->eul, self->order);
 
        return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
 }
@@ -256,8 +258,8 @@ static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       if(self->order == 0)    rotate_eul(self->eul, *axis, angle);
-       else                                    rotate_eulO(self->eul, self->order, *axis, angle);
+       if(self->order == EULER_ORDER_XYZ)      rotate_eul(self->eul, *axis, angle);
+       else                                                            rotate_eulO(self->eul, self->order, *axis, angle);
 
        BaseMath_WriteCallback(self);
        Py_INCREF(self);
@@ -367,10 +369,10 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
 
        switch (comparison_type){
                case Py_EQ:
-                       result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
+                       result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
                        break;
                case Py_NE:
-                       result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, 3, 1);
+                       result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1);
                        break;
                default:
                        printf("The result of the comparison could not be evaluated");
@@ -388,15 +390,15 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
 //sequence length
 static int Euler_len(EulerObject * self)
 {
-       return 3;
+       return EULER_SIZE;
 }
 //----------------------------object[]---------------------------
 //sequence accessor (get)
 static PyObject *Euler_item(EulerObject * self, int i)
 {
-       if(i<0) i= 3-i;
+       if(i<0) i= EULER_SIZE-i;
        
-       if(i < 0 || i >= 3) {
+       if(i < 0 || i >= EULER_SIZE) {
                PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
                return NULL;
        }
@@ -418,9 +420,9 @@ static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
                return -1;
        }
 
-       if(i<0) i= 3-i;
+       if(i<0) i= EULER_SIZE-i;
        
-       if(i < 0 || i >= 3){
+       if(i < 0 || i >= EULER_SIZE){
                PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n");
                return -1;
        }
@@ -442,9 +444,9 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       CLAMP(begin, 0, 3);
-       if (end<0) end= 4+end;
-       CLAMP(end, 0, 3);
+       CLAMP(begin, 0, EULER_SIZE);
+       if (end<0) end= (EULER_SIZE + 1) + end;
+       CLAMP(end, 0, EULER_SIZE);
        begin = MIN2(begin,end);
 
        list = PyList_New(end - begin);
@@ -457,61 +459,117 @@ static PyObject *Euler_slice(EulerObject * self, int begin, int end)
 }
 //----------------------------object[z:y]------------------------
 //sequence slice (set)
-static int Euler_ass_slice(EulerObject * self, int begin, int end,
-                                PyObject * seq)
+static int Euler_ass_slice(EulerObject * self, int begin, int end, PyObject * seq)
 {
-       int i, y, size = 0;
-       float eul[3];
-       PyObject *e;
+       int i, size;
+       float eul[EULER_SIZE];
 
        if(!BaseMath_ReadCallback(self))
                return -1;
 
-       CLAMP(begin, 0, 3);
-       if (end<0) end= 4+end;
-       CLAMP(end, 0, 3);
+       CLAMP(begin, 0, EULER_SIZE);
+       if (end<0) end= (EULER_SIZE + 1) + end;
+       CLAMP(end, 0, EULER_SIZE);
        begin = MIN2(begin,end);
 
-       size = PySequence_Length(seq);
+       if((size=mathutils_array_parse(eul, 0, EULER_SIZE, seq, "mathutils.Euler[begin:end] = []")) == -1)
+               return -1;
+
        if(size != (end - begin)){
                PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
                return -1;
        }
 
-       for (i = 0; i < size; i++) {
-               e = PySequence_GetItem(seq, i);
-               if (e == NULL) { // Failed to read sequence
-                       PyErr_SetString(PyExc_RuntimeError, "euler[begin:end] = []: unable to read sequence");
-                       return -1;
+       for(i= 0; i < EULER_SIZE; i++)
+               self->eul[begin + i] = eul[i];
+
+       BaseMath_WriteCallback(self);
+       return 0;
+}
+
+static PyObject *Euler_subscript(EulerObject *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 += EULER_SIZE;
+               return Euler_item(self, i);
+       } else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
+               }
+               else if (step == 1) {
+                       return Euler_slice(self, start, stop);
                }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with eulers");
+                       return NULL;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "euler indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return NULL;
+       }
+}
+
+
+static int Euler_ass_subscript(EulerObject *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 += EULER_SIZE;
+               return Euler_ass_item(self, i, value);
+       }
+       else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
 
-               eul[i] = (float)PyFloat_AsDouble(e);
-               Py_DECREF(e);
+               if (PySlice_GetIndicesEx((PySliceObject*)item, EULER_SIZE, &start, &stop, &step, &slicelength) < 0)
+                       return -1;
 
-               if(eul[i]==-1 && PyErr_Occurred()) { // parsed item not a number
-                       PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: sequence argument not a number");
+               if (step == 1)
+                       return Euler_ass_slice(self, start, stop, value);
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with euler");
                        return -1;
                }
        }
-       //parsed well - now set in vector
-       for(y = 0; y < 3; y++){
-               self->eul[begin + y] = eul[y];
+       else {
+               PyErr_Format(PyExc_TypeError,
+                                "euler indices must be integers, not %.200s",
+                                item->ob_type->tp_name);
+               return -1;
        }
-
-       BaseMath_WriteCallback(self);
-       return 0;
 }
+
 //-----------------PROTCOL DECLARATIONS--------------------------
 static PySequenceMethods Euler_SeqMethods = {
-       (lenfunc) Euler_len,                                            /* sq_length */
-       (binaryfunc) 0,                                                         /* sq_concat */
-       (ssizeargfunc) 0,                                                               /* sq_repeat */
-       (ssizeargfunc) Euler_item,                                      /* sq_item */
-       (ssizessizeargfunc) Euler_slice,                                /* sq_slice */
-       (ssizeobjargproc) Euler_ass_item,                               /* sq_ass_item */
-       (ssizessizeobjargproc) Euler_ass_slice,                 /* sq_ass_slice */
+       (lenfunc) Euler_len,                                    /* sq_length */
+       (binaryfunc) 0,                                                 /* sq_concat */
+       (ssizeargfunc) 0,                                               /* sq_repeat */
+       (ssizeargfunc) Euler_item,                              /* sq_item */
+       (ssizessizeargfunc) NULL,                               /* sq_slice, deprecated  */
+       (ssizeobjargproc) Euler_ass_item,               /* sq_ass_item */
+       (ssizessizeobjargproc) NULL,                    /* sq_ass_slice, deprecated */
 };
 
+static PyMappingMethods Euler_AsMapping = {
+       (lenfunc)Euler_len,
+       (binaryfunc)Euler_subscript,
+       (objobjargproc)Euler_ass_subscript
+};
 
 /*
  * euler axis, euler.x/y/z
@@ -530,7 +588,7 @@ static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
 static PyObject *Euler_getOrder(EulerObject *self, void *type)
 {
        static char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"};
-       return PyUnicode_FromString(order[self->order]);
+       return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]);
 }
 
 static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
@@ -538,7 +596,7 @@ static int Euler_setOrder( EulerObject * self, PyObject * value, void * type )
        char *order_str= _PyUnicode_AsString(value);
        short order= euler_order_from_string(order_str, "euler.order");
 
-       if(order < 0)
+       if(order == -1)
                return -1;
 
        if(self->cb_user) {
@@ -595,7 +653,7 @@ PyTypeObject euler_Type = {
        (reprfunc) Euler_repr,                  //tp_repr
        0,                              //tp_as_number
        &Euler_SeqMethods,                              //tp_as_sequence
-       0,                                                              //tp_as_mapping
+       &Euler_AsMapping,                               //tp_as_mapping
        0,                                                              //tp_hash
        0,                                                              //tp_call
        0,                                                              //tp_str
@@ -639,7 +697,6 @@ PyTypeObject euler_Type = {
 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
 {
        EulerObject *self;
-       int x;
 
        if(base_type)   self = (EulerObject *)base_type->tp_alloc(base_type, 0);
        else                    self = PyObject_NEW(EulerObject, &euler_Type);
@@ -648,20 +705,20 @@ PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_t
        self->cb_user= NULL;
        self->cb_type= self->cb_subtype= 0;
 
-       if(type == Py_WRAP){
+       if(type == Py_WRAP) {
                self->eul = eul;
                self->wrapped = Py_WRAP;
-       }else if (type == Py_NEW){
-               self->eul = PyMem_Malloc(3 * sizeof(float));
-               if(!eul) { //new empty
-                       for(x = 0; x < 3; x++) {
-                               self->eul[x] = 0.0f;
-                       }
-               }else{
-                       VECCOPY(self->eul, eul);
-               }
+       }
+       else if (type == Py_NEW){
+               self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
+               if(eul)
+                       copy_v3_v3(self->eul, eul);
+               else
+                       zero_v3(self->eul);
+
                self->wrapped = Py_NEW;
-       }else{ //bad type
+       }
+       else{
                return NULL;
        }
 
index ddc14f83218b6deae320e7d44c068c15ba8f6f5a..79b6b6f0fde785eb8a7044d52d466ab418be9ae1 100644 (file)
@@ -245,7 +245,7 @@ static char Matrix_toEuler_doc[] =
 PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
 {
        char *order_str= NULL;
-       short order= 0;
+       short order= EULER_ORDER_XYZ;
        float eul[3], eul_compatf[3];
        EulerObject *eul_compat = NULL;
 
@@ -262,7 +262,7 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
                if(!BaseMath_ReadCallback(eul_compat))
                        return NULL;
 
-               VECCOPY(eul_compatf, eul_compat->eul);
+               copy_v3_v3(eul_compatf, eul_compat->eul);
        }
        
        /*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -279,16 +279,16 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
        if(order_str) {
                order= euler_order_from_string(order_str, "Matrix.to_euler()");
 
-               if(order < 0)
+               if(order == -1)
                        return NULL;
        }
 
        if(eul_compat) {
-               if(order == 0)  mat3_to_compatible_eul( eul, eul_compatf, mat);
+               if(order == 1)  mat3_to_compatible_eul( eul, eul_compatf, mat);
                else                    mat3_to_compatible_eulO(eul, eul_compatf, order, mat);
        }
        else {
-               if(order == 0)  mat3_to_eul(eul, mat);
+               if(order == 1)  mat3_to_eul(eul, mat);
                else                    mat3_to_eulO(eul, order, mat);
        }
 
index b9754f674c422eae82bb033e406dae21df1a384a..a1a5c4b2ccba0fe93a26fa6e32ac50e6bc8408cf 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");
@@ -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;
 
@@ -658,46 +711,52 @@ static PySequenceMethods Quaternion_SeqMethods = {
        (binaryfunc) 0,                                                         /* sq_concat */
        (ssizeargfunc) 0,                                                               /* sq_repeat */
        (ssizeargfunc) Quaternion_item,                         /* sq_item */
-       (ssizessizeargfunc) Quaternion_slice,                   /* sq_slice */
+       (ssizessizeargfunc) NULL,                       /* sq_slice, deprecated */
        (ssizeobjargproc) Quaternion_ass_item,          /* sq_ass_item */
-       (ssizessizeobjargproc) Quaternion_ass_slice,    /* sq_ass_slice */
+       (ssizessizeobjargproc) NULL,    /* sq_ass_slice, deprecated */
+};
+
+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 )
@@ -722,16 +781,11 @@ static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type )
 
 static PyObject *Quaternion_getAxisVec( QuaternionObject * self, 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
+
+       normalize_v3_v3(vec, self->quat+1);
+
+       /* 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) ){
@@ -745,7 +799,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw
 {
        PyObject *seq= NULL;
        double angle = 0.0f;
-       float quat[4]= {0.0f, 0.0f, 0.0f, 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;
@@ -754,7 +808,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw
        case 0:
                break;
        case 1:
-               if (mathutils_array_parse(quat, 4, 4, seq, "mathutils.Quaternion()") == -1)
+               if (mathutils_array_parse(quat, QUAT_SIZE, QUAT_SIZE, seq, "mathutils.Quaternion()") == -1)
                        return NULL;
                break;
        case 2:
@@ -816,10 +870,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
@@ -875,7 +929,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{
index f5c24b16e8e19a9548104985ae3d92fe06afb456..0f408ea3310b93321feed385a7e7f48d0fe1b5d4 100644 (file)
@@ -241,12 +241,22 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
                case PROP_EULER:
                case PROP_QUATERNION:
                        if(len==3) { /* euler */
+                               /* attempt to get order */
+                               /* TODO, keep order in sync */
+                               short order= ROT_MODE_XYZ;
+                               PropertyRNA *prop_eul_order= RNA_struct_find_property(ptr, "rotation_mode");
+                               if(prop_eul_order) {
+                                       order = RNA_property_enum_get(ptr, prop_eul_order);
+                                       if (order < ROT_MODE_XYZ || order > ROT_MODE_ZYX) /* could be quat or axisangle */
+                                               order= ROT_MODE_XYZ;
+                               }
+
                                if(is_thick) {
-                                       ret= newEulerObject(NULL, 0, Py_NEW, NULL); // TODO, get order from RNA
+                                       ret= newEulerObject(NULL, order, Py_NEW, NULL); // TODO, get order from RNA
                                        RNA_property_float_get_array(ptr, prop, ((EulerObject *)ret)->eul);
                                }
                                else {
-                                       PyObject *eul_cb= newEulerObject_cb(ret, 0, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
+                                       PyObject *eul_cb= newEulerObject_cb(ret, order, mathutils_rna_array_cb_index, MATHUTILS_CB_SUBTYPE_EUL); // TODO, get order from RNA
                                        Py_DECREF(ret); /* the euler owns now */
                                        ret= eul_cb; /* return the euler instead */
                                }