2.5: warning fixes
[blender.git] / source / blender / python / generic / matrix.c
index db5b4ab08bfaef761ee04d1699abb7e3f9bba34a..be3e704460a40393210e34bd34e89c633aa22181 100644 (file)
@@ -37,15 +37,18 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject*
 /* matrix vector callbacks */
 int mathutils_matrix_vector_cb_index= -1;
 
-static int mathutils_matrix_vector_check(MatrixObject *self)
+static int mathutils_matrix_vector_check(PyObject *self_p)
 {
-       return Matrix_ReadCallback(self);
+       MatrixObject *self= (MatrixObject*)self_p;
+       return BaseMath_ReadCallback(self);
 }
 
-static int mathutils_matrix_vector_get(MatrixObject *self, int subtype, float *vec_from)
+static int mathutils_matrix_vector_get(PyObject *self_p, int subtype, float *vec_from)
 {
+       MatrixObject *self= (MatrixObject*)self_p;
        int i;
-       if(!Matrix_ReadCallback(self))
+
+       if(!BaseMath_ReadCallback(self))
                return 0;
 
        for(i=0; i<self->colSize; i++)
@@ -54,36 +57,42 @@ static int mathutils_matrix_vector_get(MatrixObject *self, int subtype, float *v
        return 1;
 }
 
-static int mathutils_matrix_vector_set(MatrixObject *self, int subtype, float *vec_to)
+static int mathutils_matrix_vector_set(PyObject *self_p, int subtype, float *vec_to)
 {
+       MatrixObject *self= (MatrixObject*)self_p;
        int i;
-       if(!Matrix_ReadCallback(self))
+
+       if(!BaseMath_ReadCallback(self))
                return 0;
 
        for(i=0; i<self->colSize; i++)
                self->matrix[subtype][i]= vec_to[i];
 
-       Matrix_WriteCallback(self);
+       BaseMath_WriteCallback(self);
        return 1;
 }
 
-static int mathutils_matrix_vector_get_index(MatrixObject *self, int subtype, float *vec_from, int index)
+static int mathutils_matrix_vector_get_index(PyObject *self_p, int subtype, float *vec_from, int index)
 {
-       if(!Matrix_ReadCallback(self))
+       MatrixObject *self= (MatrixObject*)self_p;
+
+       if(!BaseMath_ReadCallback(self))
                return 0;
 
        vec_from[index]= self->matrix[subtype][index];
        return 1;
 }
 
-static int mathutils_matrix_vector_set_index(MatrixObject *self, int subtype, float *vec_to, int index)
+static int mathutils_matrix_vector_set_index(PyObject *self_p, int subtype, float *vec_to, int index)
 {
-       if(!Matrix_ReadCallback(self))
+       MatrixObject *self= (MatrixObject*)self_p;
+
+       if(!BaseMath_ReadCallback(self))
                return 0;
 
        self->matrix[subtype][index]= vec_to[index];
 
-       Matrix_WriteCallback(self);
+       BaseMath_WriteCallback(self);
        return 1;
 }
 
@@ -97,18 +106,6 @@ Mathutils_Callback mathutils_matrix_vector_cb = {
 /* matrix vector callbacks, this is so you can do matrix[i][j] = val  */
 
 /*-------------------------DOC STRINGS ---------------------------*/
-static char Matrix_Zero_doc[] = "() - set all values in the matrix to 0";
-static char Matrix_Identity_doc[] = "() - set the square matrix to it's identity matrix";
-static char Matrix_Transpose_doc[] = "() - set the matrix to it's transpose";
-static char Matrix_Determinant_doc[] = "() - return the determinant of the matrix";
-static char Matrix_Invert_doc[] =  "() - set the matrix to it's inverse if an inverse is possible";
-static char Matrix_TranslationPart_doc[] = "() - return a vector encompassing the translation of the matrix";
-static char Matrix_RotationPart_doc[] = "() - return a vector encompassing the rotation of the matrix";
-static char Matrix_scalePart_doc[] = "() - convert matrix to a 3D vector";
-static char Matrix_Resize4x4_doc[] = "() - resize the matrix to a 4x4 square matrix";
-static char Matrix_toEuler_doc[] = "(eul_compat) - convert matrix to a euler angle rotation, optional euler argument that the new euler will be made compatible with.";
-static char Matrix_toQuat_doc[] = "() - convert matrix to a quaternion rotation";
-static char Matrix_copy_doc[] = "() - return a copy of the matrix";
 
 static PyObject *Matrix_Zero( MatrixObject * self );
 static PyObject *Matrix_Identity( MatrixObject * self );
@@ -125,19 +122,19 @@ static PyObject *Matrix_copy( MatrixObject * self );
 
 /*-----------------------METHOD DEFINITIONS ----------------------*/
 static struct PyMethodDef Matrix_methods[] = {
-       {"zero", (PyCFunction) Matrix_Zero, METH_NOARGS, Matrix_Zero_doc},
-       {"identity", (PyCFunction) Matrix_Identity, METH_NOARGS, Matrix_Identity_doc},
-       {"transpose", (PyCFunction) Matrix_Transpose, METH_NOARGS, Matrix_Transpose_doc},
-       {"determinant", (PyCFunction) Matrix_Determinant, METH_NOARGS, Matrix_Determinant_doc},
-       {"invert", (PyCFunction) Matrix_Invert, METH_NOARGS, Matrix_Invert_doc},
-       {"translationPart", (PyCFunction) Matrix_TranslationPart, METH_NOARGS, Matrix_TranslationPart_doc},
-       {"rotationPart", (PyCFunction) Matrix_RotationPart, METH_NOARGS, Matrix_RotationPart_doc},
-       {"scalePart", (PyCFunction) Matrix_scalePart, METH_NOARGS, Matrix_scalePart_doc},
-       {"resize4x4", (PyCFunction) Matrix_Resize4x4, METH_NOARGS, Matrix_Resize4x4_doc},
-       {"toEuler", (PyCFunction) Matrix_toEuler, METH_VARARGS, Matrix_toEuler_doc},
-       {"toQuat", (PyCFunction) Matrix_toQuat, METH_NOARGS, Matrix_toQuat_doc},
-       {"copy", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc},
-       {"__copy__", (PyCFunction) Matrix_copy, METH_NOARGS, Matrix_copy_doc},
+       {"zero", (PyCFunction) Matrix_Zero, METH_NOARGS, NULL},
+       {"identity", (PyCFunction) Matrix_Identity, METH_NOARGS, NULL},
+       {"transpose", (PyCFunction) Matrix_Transpose, METH_NOARGS, NULL},
+       {"determinant", (PyCFunction) Matrix_Determinant, METH_NOARGS, NULL},
+       {"invert", (PyCFunction) Matrix_Invert, METH_NOARGS, NULL},
+       {"translationPart", (PyCFunction) Matrix_TranslationPart, METH_NOARGS, NULL},
+       {"rotationPart", (PyCFunction) Matrix_RotationPart, METH_NOARGS, NULL},
+       {"scalePart", (PyCFunction) Matrix_scalePart, METH_NOARGS, NULL},
+       {"resize4x4", (PyCFunction) Matrix_Resize4x4, METH_NOARGS, NULL},
+       {"toEuler", (PyCFunction) Matrix_toEuler, METH_VARARGS, NULL},
+       {"toQuat", (PyCFunction) Matrix_toQuat, METH_NOARGS, NULL},
+       {"copy", (PyCFunction) Matrix_copy, METH_NOARGS, NULL},
+       {"__copy__", (PyCFunction) Matrix_copy, METH_NOARGS, NULL},
        {NULL, NULL, 0, NULL}
 };
 
@@ -158,20 +155,16 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                PyErr_SetString(PyExc_AttributeError, "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
                return NULL;
        } else if (argSize == 0) { //return empty 4D matrix
-               return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW);
+               return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW, NULL);
        }else if (argSize == 1){
                //copy constructor for matrix objects
                argObject = PyTuple_GET_ITEM(args, 0);
                if(MatrixObject_Check(argObject)){
                        mat = (MatrixObject*)argObject;
-                       if(!Matrix_ReadCallback(mat))
+                       if(!BaseMath_ReadCallback(mat))
                                return NULL;
 
-                       argSize = mat->rowSize; //rows
-                       seqSize = mat->colSize; //col
-                       for(i = 0; i < (seqSize * argSize); i++){
-                               matrix[i] = mat->contigPtr[i];
-                       }
+                       memcpy(matrix, mat->contigPtr, sizeof(float) * mat->rowSize * mat->colSize);
                }
        }else{ //2-4 arguments (all seqs? all same size?)
                for(i =0; i < argSize; i++){
@@ -216,7 +209,7 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                        }
                }
        }
-       return newMatrixObject(matrix, argSize, seqSize, Py_NEW);
+       return newMatrixObject(matrix, argSize, seqSize, Py_NEW, NULL);
 }
 
 /*-----------------------------METHODS----------------------------*/
@@ -225,7 +218,7 @@ static PyObject *Matrix_toQuat(MatrixObject * self)
 {
        float quat[4];
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        /*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -239,25 +232,34 @@ static PyObject *Matrix_toQuat(MatrixObject * self)
                Mat4ToQuat((float (*)[4])*self->matrix, quat);
        }
        
-       return newQuaternionObject(quat, Py_NEW);
+       return newQuaternionObject(quat, Py_NEW, NULL);
 }
 /*---------------------------Matrix.toEuler() --------------------*/
 PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
 {
        float eul[3], eul_compatf[3];
        EulerObject *eul_compat = NULL;
+#ifdef USE_MATHUTILS_DEG
        int x;
+#endif
        
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(!PyArg_ParseTuple(args, "|O!:toEuler", &euler_Type, &eul_compat))
                return NULL;
        
        if(eul_compat) {
+               if(!BaseMath_ReadCallback(eul_compat))
+                       return NULL;
+
+#ifdef USE_MATHUTILS_DEG
                for(x = 0; x < 3; x++) {
                        eul_compatf[x] = eul_compat->eul[x] * ((float)Py_PI / 180);
                }
+#else
+               VECCOPY(eul_compatf, eul_compat->eul);
+#endif
        }
        
        /*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -275,11 +277,13 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args)
                PyErr_SetString(PyExc_AttributeError, "Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
                return NULL;
        }
+#ifdef USE_MATHUTILS_DEG
        /*have to convert to degrees*/
        for(x = 0; x < 3; x++) {
                eul[x] *= (float) (180 / Py_PI);
        }
-       return newEulerObject(eul, Py_NEW);
+#endif
+       return newEulerObject(eul, Py_NEW, NULL);
 }
 /*---------------------------Matrix.resize4x4() ------------------*/
 PyObject *Matrix_Resize4x4(MatrixObject * self)
@@ -343,7 +347,7 @@ PyObject *Matrix_TranslationPart(MatrixObject * self)
 {
        float vec[4];
        
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(self->colSize < 3 || self->rowSize < 4){
@@ -355,7 +359,7 @@ PyObject *Matrix_TranslationPart(MatrixObject * self)
        vec[1] = self->matrix[3][1];
        vec[2] = self->matrix[3][2];
 
-       return newVectorObject(vec, 3, Py_NEW);
+       return newVectorObject(vec, 3, Py_NEW, NULL);
 }
 /*---------------------------Matrix.rotationPart() ---------------*/
 PyObject *Matrix_RotationPart(MatrixObject * self)
@@ -363,7 +367,7 @@ PyObject *Matrix_RotationPart(MatrixObject * self)
        float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
 
        if(self->colSize < 3 || self->rowSize < 3){
@@ -381,7 +385,7 @@ PyObject *Matrix_RotationPart(MatrixObject * self)
        mat[7] = self->matrix[2][1];
        mat[8] = self->matrix[2][2];
 
-       return newMatrixObject(mat, 3, 3, Py_NEW);
+       return newMatrixObject(mat, 3, 3, Py_NEW, Py_TYPE(self));
 }
 /*---------------------------Matrix.scalePart() --------------------*/
 PyObject *Matrix_scalePart(MatrixObject * self)
@@ -389,7 +393,7 @@ PyObject *Matrix_scalePart(MatrixObject * self)
        float scale[3], rot[3];
        float mat[3][3], imat[3][3], tmat[3][3];
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        /*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -410,7 +414,7 @@ PyObject *Matrix_scalePart(MatrixObject * self)
        scale[0]= tmat[0][0];
        scale[1]= tmat[1][1];
        scale[2]= tmat[2][2];
-       return newVectorObject(scale, 3, Py_NEW);
+       return newVectorObject(scale, 3, Py_NEW, NULL);
 }
 /*---------------------------Matrix.invert() ---------------------*/
 PyObject *Matrix_Invert(MatrixObject * self)
@@ -422,7 +426,7 @@ PyObject *Matrix_Invert(MatrixObject * self)
        float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
 
        if(self->rowSize != self->colSize){
@@ -465,7 +469,7 @@ PyObject *Matrix_Invert(MatrixObject * self)
                return NULL;
        }
        
-       Matrix_WriteCallback(self);
+       BaseMath_WriteCallback(self);
        Py_INCREF(self);
        return (PyObject *)self;
 }
@@ -476,7 +480,7 @@ PyObject *Matrix_Determinant(MatrixObject * self)
 {
        float det = 0.0f;
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(self->rowSize != self->colSize){
@@ -504,7 +508,7 @@ PyObject *Matrix_Transpose(MatrixObject * self)
 {
        float t = 0.0f;
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(self->rowSize != self->colSize){
@@ -522,7 +526,7 @@ PyObject *Matrix_Transpose(MatrixObject * self)
                Mat4Transp((float (*)[4])*self->matrix);
        }
 
-       Matrix_WriteCallback(self);
+       BaseMath_WriteCallback(self);
        Py_INCREF(self);
        return (PyObject *)self;
 }
@@ -539,7 +543,7 @@ PyObject *Matrix_Zero(MatrixObject * self)
                }
        }
        
-       if(!Matrix_WriteCallback(self))
+       if(!BaseMath_WriteCallback(self))
                return NULL;
        
        Py_INCREF(self);
@@ -548,7 +552,7 @@ PyObject *Matrix_Zero(MatrixObject * self)
 /*---------------------------Matrix.identity(() ------------------*/
 PyObject *Matrix_Identity(MatrixObject * self)
 {
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(self->rowSize != self->colSize){
@@ -567,7 +571,7 @@ PyObject *Matrix_Identity(MatrixObject * self)
                Mat4One((float (*)[4]) *self->matrix);
        }
 
-       if(!Matrix_WriteCallback(self))
+       if(!BaseMath_WriteCallback(self))
                return NULL;
        
        Py_INCREF(self);
@@ -577,23 +581,10 @@ PyObject *Matrix_Identity(MatrixObject * self)
 /*---------------------------Matrix.inverted() ------------------*/
 PyObject *Matrix_copy(MatrixObject * self)
 {
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
-       return (PyObject*)(MatrixObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW);
-}
-
-/*----------------------------dealloc()(internal) ----------------*/
-/*free the py_object*/
-static void Matrix_dealloc(MatrixObject * self)
-{
-       PyMem_Free(self->matrix);
-       /*only free py_data*/
-       if(self->wrapped==Py_WRAP)
-               PyMem_Free(self->contigPtr);
-       
-       Py_XDECREF(self->cb_user);
-       PyObject_DEL(self);
+       return (PyObject*)newMatrixObject((float (*))*self->matrix, self->rowSize, self->colSize, Py_NEW, Py_TYPE(self));
 }
 
 /*----------------------------print object (internal)-------------*/
@@ -603,7 +594,7 @@ static PyObject *Matrix_repr(MatrixObject * self)
        int x, y;
        char buffer[48], str[1024];
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        BLI_strncpy(str,"",1024);
@@ -642,7 +633,7 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
        matA = (MatrixObject*)objectA;
        matB = (MatrixObject*)objectB;
 
-       if(!Matrix_ReadCallback(matA) || !Matrix_ReadCallback(matB))
+       if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB))
                return NULL;
        
        if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
@@ -678,8 +669,7 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
                Py_RETURN_FALSE;
        }
 }
-/*------------------------tp_doc*/
-static char MatrixObject_doc[] = "This is a wrapper for matrix objects.";
+
 /*---------------------SEQUENCE PROTOCOLS------------------------
   ----------------------------len(object)------------------------
   sequence length*/
@@ -692,7 +682,7 @@ static int Matrix_len(MatrixObject * self)
   the wrapped vector gives direct access to the matrix data*/
 static PyObject *Matrix_item(MatrixObject * self, int i)
 {
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        if(i < 0 || i >= self->rowSize) {
@@ -709,7 +699,7 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
        float vec[4];
        PyObject *m, *f;
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return -1;
        
        if(i >= self->rowSize || i < 0){
@@ -746,7 +736,7 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
                        self->matrix[i][y] = vec[y];
                }
                
-               Matrix_WriteCallback(self);
+               BaseMath_WriteCallback(self);
                return 0;
        }else{
                PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n");
@@ -761,7 +751,7 @@ static PyObject *Matrix_slice(MatrixObject * self, int begin, int end)
        PyObject *list = NULL;
        int count;
        
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
 
        CLAMP(begin, 0, self->rowSize);
@@ -779,15 +769,14 @@ static PyObject *Matrix_slice(MatrixObject * self, int begin, int end)
 }
 /*----------------------------object[z:y]------------------------
   sequence slice (set)*/
-static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
-                            PyObject * seq)
+static int Matrix_ass_slice(MatrixObject * self, int begin, int end, PyObject * seq)
 {
        int i, x, y, size, sub_size = 0;
        float mat[16], f;
        PyObject *subseq;
        PyObject *m;
 
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return -1;
        
        CLAMP(begin, 0, self->rowSize);
@@ -848,7 +837,7 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
                        self->matrix[begin + (int)floor(x / self->colSize)][x % self->colSize] = mat[x];
                }
                
-               Matrix_WriteCallback(self);
+               BaseMath_WriteCallback(self);
                return 0;
        }else{
                PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
@@ -872,7 +861,7 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
                return NULL;
        }
        
-       if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2))
+       if(!BaseMath_ReadCallback(mat1) || !BaseMath_ReadCallback(mat2))
                return NULL;
        
        if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
@@ -886,7 +875,7 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
                }
        }
 
-       return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
+       return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
 }
 /*------------------------obj - obj------------------------------
   subtraction*/
@@ -905,7 +894,7 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
                return NULL;
        }
        
-       if(!Matrix_ReadCallback(mat1) || !Matrix_ReadCallback(mat2))
+       if(!BaseMath_ReadCallback(mat1) || !BaseMath_ReadCallback(mat2))
                return NULL;
        
        if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
@@ -919,7 +908,7 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
                }
        }
 
-       return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
+       return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
 }
 /*------------------------obj * obj------------------------------
   mulplication*/
@@ -934,12 +923,12 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
 
        if(MatrixObject_Check(m1)) {
                mat1 = (MatrixObject*)m1;
-               if(!Matrix_ReadCallback(mat1))
+               if(!BaseMath_ReadCallback(mat1))
                        return NULL;
        }
        if(MatrixObject_Check(m2)) {
                mat2 = (MatrixObject*)m2;
-               if(!Matrix_ReadCallback(mat2))
+               if(!BaseMath_ReadCallback(mat2))
                        return NULL;
        }
 
@@ -958,7 +947,7 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
                        }
                }
                
-               return newMatrixObject(mat, mat1->rowSize, mat2->colSize, Py_NEW);
+               return newMatrixObject(mat, mat1->rowSize, mat2->colSize, Py_NEW, NULL);
        }
        
        if(mat1==NULL){
@@ -969,7 +958,7 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
                                        mat[((x * mat2->colSize) + y)] = scalar * mat2->matrix[x][y];
                                }
                        }
-                       return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW);
+                       return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW, NULL);
                }
                
                PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
@@ -988,7 +977,7 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
                                                mat[((x * mat1->colSize) + y)] = scalar * mat1->matrix[x][y];
                                        }
                                }
-                               return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
+                               return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW, NULL);
                        }
                }
                PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
@@ -1000,7 +989,7 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
 }
 static PyObject* Matrix_inv(MatrixObject *self)
 {
-       if(!Matrix_ReadCallback(self))
+       if(!BaseMath_ReadCallback(self))
                return NULL;
        
        return Matrix_Invert(self);
@@ -1008,7 +997,7 @@ static PyObject* Matrix_inv(MatrixObject *self)
 
 /*-----------------PROTOCOL DECLARATIONS--------------------------*/
 static PySequenceMethods Matrix_SeqMethods = {
-       (inquiry) Matrix_len,                                   /* sq_length */
+       (lenfunc) Matrix_len,                                   /* sq_length */
        (binaryfunc) 0,                                                 /* sq_concat */
        (ssizeargfunc) 0,                                                       /* sq_repeat */
        (ssizeargfunc) Matrix_item,                             /* sq_item */
@@ -1016,59 +1005,126 @@ static PySequenceMethods Matrix_SeqMethods = {
        (ssizeobjargproc) Matrix_ass_item,              /* sq_ass_item */
        (ssizessizeobjargproc) Matrix_ass_slice,        /* sq_ass_slice */
 };
-static PyNumberMethods Matrix_NumMethods = {
-       (binaryfunc) Matrix_add,                                /* __add__ */
-       (binaryfunc) Matrix_sub,                                /* __sub__ */
-       (binaryfunc) Matrix_mul,                                /* __mul__ */
-       (binaryfunc) 0,                                                 /* __div__ */
-       (binaryfunc) 0,                                                 /* __mod__ */
-       (binaryfunc) 0,                                                 /* __divmod__ */
-       (ternaryfunc) 0,                                                /* __pow__ */
-       (unaryfunc) 0,                                                  /* __neg__ */
-       (unaryfunc) 0,                                                  /* __pos__ */
-       (unaryfunc) 0,                                                  /* __abs__ */
-       (inquiry) 0,                                                    /* __nonzero__ */
-       (unaryfunc) Matrix_inv,                                 /* __invert__ */
-       (binaryfunc) 0,                                                 /* __lshift__ */
-       (binaryfunc) 0,                                                 /* __rshift__ */
-       (binaryfunc) 0,                                                 /* __and__ */
-       (binaryfunc) 0,                                                 /* __xor__ */
-       (binaryfunc) 0,                                                 /* __or__ */
-       /*(coercion)*/ 0,                                                       /* __coerce__ */
-       (unaryfunc) 0,                                                  /* __int__ */
-       (unaryfunc) 0,                                                  /* __long__ */
-       (unaryfunc) 0,                                                  /* __float__ */
-       (unaryfunc) 0,                                                  /* __oct__ */
-       (unaryfunc) 0,                                                  /* __hex__ */
-};
 
-static PyObject *Matrix_getRowSize( MatrixObject * self, void *type )
-{
-       return PyLong_FromLong((long) self->rowSize);
-}
 
-static PyObject *Matrix_getColSize( MatrixObject * self, void *type )
+static PyObject *Matrix_subscript(MatrixObject* self, PyObject* item)
 {
-       return PyLong_FromLong((long) self->colSize);
+       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 += self->rowSize;
+               return Matrix_item(self, i);
+       } else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)item, self->rowSize, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
+               }
+               else if (step == 1) {
+                       return Matrix_slice(self, start, stop);
+               }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with matricies");
+                       return NULL;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError,
+                            "vector indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
+               return NULL;
+       }
 }
 
-static PyObject *Matrix_getOwner( MatrixObject * self, void *type )
+static int Matrix_ass_subscript(MatrixObject* self, PyObject* item, PyObject* value)
 {
-       if(self->cb_user==NULL) {
-               Py_RETURN_NONE;
+       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 += self->rowSize;
+               return Matrix_ass_item(self, i, value);
+       }
+       else if (PySlice_Check(item)) {
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)item, self->rowSize, &start, &stop, &step, &slicelength) < 0)
+                       return -1;
+
+               if (step == 1)
+                       return Matrix_ass_slice(self, start, stop, value);
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with matricies");
+                       return -1;
+               }
        }
        else {
-               Py_INCREF(self->cb_user);
-               return self->cb_user;
+               PyErr_Format(PyExc_TypeError,
+                            "matrix indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
+               return -1;
        }
 }
 
-static PyObject *Matrix_getWrapped( MatrixObject * self, void *type )
+static PyMappingMethods Matrix_AsMapping = {
+       (lenfunc)Matrix_len,
+       (binaryfunc)Matrix_subscript,
+       (objobjargproc)Matrix_ass_subscript
+};
+
+
+static PyNumberMethods Matrix_NumMethods = {
+               (binaryfunc)    Matrix_add,     /*nb_add*/
+               (binaryfunc)    Matrix_sub,     /*nb_subtract*/
+               (binaryfunc)    Matrix_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)     Matrix_inv,     /*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 *Matrix_getRowSize( MatrixObject * self, void *type )
+{
+       return PyLong_FromLong((long) self->rowSize);
+}
+
+static PyObject *Matrix_getColSize( MatrixObject * self, void *type )
 {
-       if (self->wrapped == Py_WRAP)
-               Py_RETURN_TRUE;
-       else
-               Py_RETURN_FALSE;
+       return PyLong_FromLong((long) self->colSize);
 }
 
 /*****************************************************************************/
@@ -1077,24 +1133,19 @@ static PyObject *Matrix_getWrapped( MatrixObject * self, void *type )
 static PyGetSetDef Matrix_getseters[] = {
        {"rowSize", (getter)Matrix_getRowSize, (setter)NULL, "", NULL},
        {"colSize", (getter)Matrix_getColSize, (setter)NULL, "", NULL},
-       {"wrapped", (getter)Matrix_getWrapped, (setter)NULL, "", NULL},
-       {"__owner__",(getter)Matrix_getOwner, (setter)NULL, "Read only owner for vectors that depend on another object", NULL},
+       {"wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, "", NULL},
+       {"__owner__",(getter)BaseMathObject_getOwner, (setter)NULL, "",
+        NULL},
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
 };
 
 /*------------------PY_OBECT DEFINITION--------------------------*/
 PyTypeObject matrix_Type = {
-#if (PY_VERSION_HEX >= 0x02060000)
        PyVarObject_HEAD_INIT(NULL, 0)
-#else
-       /* python 2.5 and below */
-       PyObject_HEAD_INIT( NULL )  /* required py macro */
-       0,                          /* ob_size */
-#endif
        "matrix",                                               /*tp_name*/
        sizeof(MatrixObject),                   /*tp_basicsize*/
        0,                                                              /*tp_itemsize*/
-       (destructor)Matrix_dealloc,             /*tp_dealloc*/
+       (destructor)BaseMathObject_dealloc,             /*tp_dealloc*/
        0,                                                              /*tp_print*/
        0,                                                              /*tp_getattr*/
        0,                                                              /*tp_setattr*/
@@ -1102,15 +1153,15 @@ PyTypeObject matrix_Type = {
        (reprfunc) Matrix_repr,                 /*tp_repr*/
        &Matrix_NumMethods,                             /*tp_as_number*/
        &Matrix_SeqMethods,                             /*tp_as_sequence*/
-       0,                                                              /*tp_as_mapping*/
+       &Matrix_AsMapping,                              /*tp_as_mapping*/
        0,                                                              /*tp_hash*/
        0,                                                              /*tp_call*/
        0,                                                              /*tp_str*/
        0,                                                              /*tp_getattro*/
        0,                                                              /*tp_setattro*/
        0,                                                              /*tp_as_buffer*/
-       Py_TPFLAGS_DEFAULT,                             /*tp_flags*/
-       MatrixObject_doc,                               /*tp_doc*/
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+       0,                                                              /*tp_doc*/
        0,                                                              /*tp_traverse*/
        0,                                                              /*tp_clear*/
        (richcmpfunc)Matrix_richcmpr,   /*tp_richcompare*/
@@ -1154,7 +1205,7 @@ self->matrix[1][1] = self->contigPtr[4] */
  (i.e. it was allocated elsewhere by MEM_mallocN())
   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
  (i.e. it must be created here with PyMEM_malloc())*/
-PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
+PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type, PyTypeObject *base_type)
 {
        MatrixObject *self;
        int x, row, col;
@@ -1165,7 +1216,9 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
                return NULL;
        }
 
-       self = PyObject_NEW(MatrixObject, &matrix_Type);
+       if(base_type)   self = (MatrixObject *)base_type->tp_alloc(base_type, 0);
+       else                    self = PyObject_NEW(MatrixObject, &matrix_Type);
+
        self->rowSize = rowSize;
        self->colSize = colSize;
        
@@ -1223,7 +1276,7 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
 
 PyObject *newMatrixObject_cb(PyObject *cb_user, int rowSize, int colSize, int cb_type, int cb_subtype)
 {
-       MatrixObject *self= (MatrixObject *)newMatrixObject(NULL, rowSize, colSize, Py_NEW);
+       MatrixObject *self= (MatrixObject *)newMatrixObject(NULL, rowSize, colSize, Py_NEW, NULL);
        if(self) {
                Py_INCREF(cb_user);
                self->cb_user=                  cb_user;
@@ -1245,7 +1298,7 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject*
        double dot = 0.0f;
        int x, y, z = 0;
 
-       if(!Matrix_ReadCallback(mat) || !Vector_ReadCallback(vec))
+       if(!BaseMath_ReadCallback(mat) || !BaseMath_ReadCallback(vec))
                return NULL;
        
        if(mat->rowSize != vec->size){
@@ -1268,5 +1321,5 @@ static PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject*
                vecNew[z++] = (float)dot;
                dot = 0.0f;
        }
-       return newVectorObject(vecNew, vec->size, Py_NEW);
+       return newVectorObject(vecNew, vec->size, Py_NEW, NULL);
 }