Merged changes in the trunk up to revision 42116.
[blender.git] / source / blender / python / mathutils / mathutils_Matrix.c
index 76a0994c3aa0a7744e8d55310dc738f52fc0fb8d..2f62f7141cf59af4185c66abff6dea79e8971e01 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -25,8 +24,8 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/python/generic/mathutils_Matrix.c
- *  \ingroup pygen
+/** \file blender/python/mathutils/mathutils_Matrix.c
+ *  \ingroup pymathutils
  */
 
 
@@ -55,10 +54,10 @@ static int mathutils_matrix_vector_get(BaseMathObject *bmo, int subtype)
        MatrixObject *self= (MatrixObject *)bmo->cb_user;
        int i;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
-       for(i=0; i < self->col_size; i++)
+       for (i=0; i < self->col_size; i++)
                bmo->data[i]= self->matrix[subtype][i];
 
        return 0;
@@ -69,10 +68,10 @@ static int mathutils_matrix_vector_set(BaseMathObject *bmo, int subtype)
        MatrixObject *self= (MatrixObject *)bmo->cb_user;
        int i;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
-       for(i=0; i < self->col_size; i++)
+       for (i=0; i < self->col_size; i++)
                self->matrix[subtype][i]= bmo->data[i];
 
        (void)BaseMath_WriteCallback(self);
@@ -83,7 +82,7 @@ static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int subtype, i
 {
        MatrixObject *self= (MatrixObject *)bmo->cb_user;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
        bmo->data[index]= self->matrix[subtype][index];
@@ -94,7 +93,7 @@ static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int subtype, i
 {
        MatrixObject *self= (MatrixObject *)bmo->cb_user;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
        self->matrix[subtype][index]= bmo->data[index];
@@ -117,16 +116,16 @@ Mathutils_Callback mathutils_matrix_vector_cb = {
 //create a new matrix type
 static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-       if(kwds && PyDict_Size(kwds)) {
+       if (kwds && PyDict_Size(kwds)) {
                PyErr_SetString(PyExc_TypeError,
-                               "mathutils.Matrix(): "
+                               "Matrix(): "
                                "takes no keyword args");
                return NULL;
        }
 
        switch(PyTuple_GET_SIZE(args)) {
                case 0:
-                       return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW, type);
+                       return (PyObject *) Matrix_CreatePyObject(NULL, 4, 4, Py_NEW, type);
                case 1:
                {
                        PyObject *arg= PyTuple_GET_ITEM(args, 0);
@@ -134,15 +133,15 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                        /* -1 is an error, size checks will accunt for this */
                        const unsigned short row_size= PySequence_Size(arg);
 
-                       if(row_size >= 2 && row_size <= 4) {
+                       if (row_size >= 2 && row_size <= 4) {
                                PyObject *item= PySequence_GetItem(arg, 0);
                                const unsigned short col_size= PySequence_Size(item);
                                Py_XDECREF(item);
 
-                               if(col_size >= 2 && col_size <= 4) {
+                               if (col_size >= 2 && col_size <= 4) {
                                        /* sane row & col size, new matrix and assign as slice  */
-                                       PyObject *matrix= newMatrixObject(NULL, row_size, col_size, Py_NEW, type);
-                                       if(Matrix_ass_slice((MatrixObject *)matrix, 0, INT_MAX, arg) == 0) {
+                                       PyObject *matrix= Matrix_CreatePyObject(NULL, row_size, col_size, Py_NEW, type);
+                                       if (Matrix_ass_slice((MatrixObject *)matrix, 0, INT_MAX, arg) == 0) {
                                                return matrix;
                                        }
                                        else { /* matrix ok, slice assignment not */
@@ -155,7 +154,7 @@ static PyObject *Matrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
        /* will overwrite error */
        PyErr_SetString(PyExc_TypeError,
-                       "mathutils.Matrix(): "
+                       "Matrix(): "
                        "expects no args or 2-4 numeric sequences");
        return NULL;
 }
@@ -164,7 +163,7 @@ static PyObject *matrix__apply_to_copy(PyNoArgsFunction matrix_func, MatrixObjec
 {
        PyObject *ret= Matrix_copy(self);
        PyObject *ret_dummy= matrix_func(ret);
-       if(ret_dummy) {
+       if (ret_dummy) {
                Py_DECREF(ret_dummy);
                return (PyObject *)ret;
        }
@@ -214,18 +213,18 @@ static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args)
        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(!PyArg_ParseTuple(args, "di|O", &angle, &matSize, &vec)) {
+       if (!PyArg_ParseTuple(args, "di|O", &angle, &matSize, &vec)) {
                PyErr_SetString(PyExc_TypeError,
-                               "mathutils.RotationMatrix(angle, size, axis): "
+                               "Matrix.Rotation(angle, size, axis): "
                                "expected float int and a string or vector");
                return NULL;
        }
 
-       if(vec && PyUnicode_Check(vec)) {
+       if (vec && PyUnicode_Check(vec)) {
                axis= _PyUnicode_AsString((PyObject *)vec);
-               if(axis==NULL || axis[0]=='\0' || axis[1]!='\0' || axis[0] < 'X' || axis[0] > 'Z') {
+               if (axis==NULL || axis[0]=='\0' || axis[1]!='\0' || axis[0] < 'X' || axis[0] > 'Z') {
                        PyErr_SetString(PyExc_ValueError,
-                                       "mathutils.RotationMatrix(): "
+                                       "Matrix.Rotation(): "
                                        "3rd argument axis value must be a 3D vector "
                                        "or a string in 'X', 'Y', 'Z'");
                        return NULL;
@@ -238,77 +237,54 @@ static PyObject *C_Matrix_Rotation(PyObject *cls, PyObject *args)
 
        angle= angle_wrap_rad(angle);
 
-       if(matSize != 2 && matSize != 3 && matSize != 4) {
+       if (matSize != 2 && matSize != 3 && matSize != 4) {
                PyErr_SetString(PyExc_ValueError,
-                               "mathutils.RotationMatrix(): "
+                               "Matrix.Rotation(): "
                                "can only return a 2x2 3x3 or 4x4 matrix");
                return NULL;
        }
-       if(matSize == 2 && (vec != NULL)) {
+       if (matSize == 2 && (vec != NULL)) {
                PyErr_SetString(PyExc_ValueError,
-                               "mathutils.RotationMatrix(): "
+                               "Matrix.Rotation(): "
                                "cannot create a 2x2 rotation matrix around arbitrary axis");
                return NULL;
        }
-       if((matSize == 3 || matSize == 4) && (axis == NULL) && (vec == NULL)) {
+       if ((matSize == 3 || matSize == 4) && (axis == NULL) && (vec == NULL)) {
                PyErr_SetString(PyExc_ValueError,
-                               "mathutils.RotationMatrix(): "
+                               "Matrix.Rotation(): "
                                "axis of rotation for 3d and 4d matrices is required");
                return NULL;
        }
 
        /* check for valid vector/axis above */
-       if(vec) {
+       if (vec) {
                float tvec[3];
 
-               if (mathutils_array_parse(tvec, 3, 3, vec, "mathutils.RotationMatrix(angle, size, axis), invalid 'axis' arg") == -1)
+               if (mathutils_array_parse(tvec, 3, 3, vec, "Matrix.Rotation(angle, size, axis), invalid 'axis' arg") == -1)
                        return NULL;
 
                axis_angle_to_mat3((float (*)[3])mat, tvec, angle);
        }
-       else if(matSize == 2) {
+       else if (matSize == 2) {
+               const float angle_cos= cosf(angle);
+               const float angle_sin= sinf(angle);
+
                //2D rotation matrix
-               mat[0] = (float) cos (angle);
-               mat[1] = (float) sin (angle);
-               mat[2] = -((float) sin(angle));
-               mat[3] = (float) cos(angle);
-       }
-       else if(strcmp(axis, "X") == 0) {
-               //rotation around X
-               mat[0] = 1.0f;
-               mat[4] = (float) cos(angle);
-               mat[5] = (float) sin(angle);
-               mat[7] = -((float) sin(angle));
-               mat[8] = (float) cos(angle);
-       }
-       else if(strcmp(axis, "Y") == 0) {
-               //rotation around Y
-               mat[0] = (float) cos(angle);
-               mat[2] = -((float) sin(angle));
-               mat[4] = 1.0f;
-               mat[6] = (float) sin(angle);
-               mat[8] = (float) cos(angle);
-       }
-       else if(strcmp(axis, "Z") == 0) {
-               //rotation around Z
-               mat[0] = (float) cos(angle);
-               mat[1] = (float) sin(angle);
-               mat[3] = -((float) sin(angle));
-               mat[4] = (float) cos(angle);
-               mat[8] = 1.0f;
+               mat[0] =  angle_cos;
+               mat[1] =  angle_sin;
+               mat[2] = -angle_sin;
+               mat[3] =  angle_cos;
        }
        else {
-               /* should never get here */
-               PyErr_SetString(PyExc_ValueError,
-                               "mathutils.RotationMatrix(): unknown error");
-               return NULL;
+               /* valid axis checked above */
+               single_axis_angle_to_mat3((float (*)[3])mat, axis[0], angle);
        }
 
-       if(matSize == 4) {
+       if (matSize == 4) {
                matrix_3x3_as_4x4(mat);
        }
        //pass to matrix creation
-       return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
+       return Matrix_CreatePyObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
 }
 
 
@@ -332,7 +308,7 @@ static PyObject *C_Matrix_Translation(PyObject *cls, PyObject *value)
        /* create a identity matrix and add translation */
        unit_m4((float(*)[4]) mat);
        copy_v3_v3(mat + 12, tvec); /* 12, 13, 14 */
-       return newMatrixObject(mat, 4, 4, Py_NEW, (PyTypeObject *)cls);
+       return Matrix_CreatePyObject(mat, 4, 4, Py_NEW, (PyTypeObject *)cls);
 }
 //----------------------------------mathutils.Matrix.Scale() -------------
 //mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc.
@@ -360,23 +336,23 @@ static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args)
        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(!PyArg_ParseTuple(args, "fi|O:Matrix.Scale", &factor, &matSize, &vec)) {
+       if (!PyArg_ParseTuple(args, "fi|O:Matrix.Scale", &factor, &matSize, &vec)) {
                return NULL;
        }
-       if(matSize != 2 && matSize != 3 && matSize != 4) {
+       if (matSize != 2 && matSize != 3 && matSize != 4) {
                PyErr_SetString(PyExc_ValueError,
                                "Matrix.Scale(): "
                                "can only return a 2x2 3x3 or 4x4 matrix");
                return NULL;
        }
-       if(vec) {
+       if (vec) {
                vec_size= (matSize == 2 ? 2 : 3);
-               if(mathutils_array_parse(tvec, vec_size, vec_size, vec, "Matrix.Scale(factor, size, axis), invalid 'axis' arg") == -1) {
+               if (mathutils_array_parse(tvec, vec_size, vec_size, vec, "Matrix.Scale(factor, size, axis), invalid 'axis' arg") == -1) {
                        return NULL;
                }
        }
-       if(vec == NULL) {       //scaling along axis
-               if(matSize == 2) {
+       if (vec == NULL) {      //scaling along axis
+               if (matSize == 2) {
                        mat[0] = factor;
                        mat[3] = factor;
                }
@@ -390,14 +366,14 @@ static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args)
                //normalize arbitrary axis
                float norm = 0.0f;
                int x;
-               for(x = 0; x < vec_size; x++) {
+               for (x = 0; x < vec_size; x++) {
                        norm += tvec[x] * tvec[x];
                }
                norm = (float) sqrt(norm);
-               for(x = 0; x < vec_size; x++) {
+               for (x = 0; x < vec_size; x++) {
                        tvec[x] /= norm;
                }
-               if(matSize == 2) {
+               if (matSize == 2) {
                        mat[0] = 1 + ((factor - 1) *(tvec[0] * tvec[0]));
                        mat[1] =     ((factor - 1) *(tvec[0] * tvec[1]));
                        mat[2] =     ((factor - 1) *(tvec[0] * tvec[1]));
@@ -415,11 +391,11 @@ static PyObject *C_Matrix_Scale(PyObject *cls, PyObject *args)
                        mat[8] = 1 + ((factor - 1) *(tvec[2] * tvec[2]));
                }
        }
-       if(matSize == 4) {
+       if (matSize == 4) {
                matrix_3x3_as_4x4(mat);
        }
        //pass to matrix creation
-       return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
+       return Matrix_CreatePyObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
 }
 //----------------------------------mathutils.Matrix.OrthoProjection() ---
 //mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc.
@@ -446,21 +422,21 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
        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(!PyArg_ParseTuple(args, "Oi:Matrix.OrthoProjection", &axis, &matSize)) {
+       if (!PyArg_ParseTuple(args, "Oi:Matrix.OrthoProjection", &axis, &matSize)) {
                return NULL;
        }
-       if(matSize != 2 && matSize != 3 && matSize != 4) {
+       if (matSize != 2 && matSize != 3 && matSize != 4) {
                PyErr_SetString(PyExc_ValueError,
-                               "mathutils.Matrix.OrthoProjection(): "
+                               "Matrix.OrthoProjection(): "
                                "can only return a 2x2 3x3 or 4x4 matrix");
                return NULL;
        }
 
-       if(PyUnicode_Check(axis)) {     //ortho projection onto cardinal plane
+       if (PyUnicode_Check(axis)) {    //ortho projection onto cardinal plane
                Py_ssize_t plane_len;
                const char *plane= _PyUnicode_AsStringAndSize(axis, &plane_len);
-               if(matSize == 2) {
-                       if(plane_len == 1 && plane[0]=='X') {
+               if (matSize == 2) {
+                       if (plane_len == 1 && plane[0]=='X') {
                                mat[0]= 1.0f;
                        }
                        else if (plane_len == 1 && plane[0]=='Y') {
@@ -468,14 +444,14 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
                        }
                        else {
                                PyErr_Format(PyExc_ValueError,
-                                            "mathutils.Matrix.OrthoProjection(): "
+                                            "Matrix.OrthoProjection(): "
                                             "unknown plane, expected: X, Y, not '%.200s'",
                                             plane);
                                return NULL;
                        }
                }
                else {
-                       if(plane_len == 2 && plane[0]=='X' && plane[1]=='Y') {
+                       if (plane_len == 2 && plane[0]=='X' && plane[1]=='Y') {
                                mat[0]= 1.0f;
                                mat[4]= 1.0f;
                        }
@@ -489,7 +465,7 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
                        }
                        else {
                                PyErr_Format(PyExc_ValueError,
-                                            "mathutils.Matrix.OrthoProjection(): "
+                                            "Matrix.OrthoProjection(): "
                                             "unknown plane, expected: XY, XZ, YZ, not '%.200s'",
                                             plane);
                                return NULL;
@@ -502,25 +478,25 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
                int vec_size= (matSize == 2 ? 2 : 3);
                float tvec[4];
 
-               if(mathutils_array_parse(tvec, vec_size, vec_size, axis, "Matrix.OrthoProjection(axis, size), invalid 'axis' arg") == -1) {
+               if (mathutils_array_parse(tvec, vec_size, vec_size, axis, "Matrix.OrthoProjection(axis, size), invalid 'axis' arg") == -1) {
                        return NULL;
                }
 
                //normalize arbitrary axis
-               for(x = 0; x < vec_size; x++) {
+               for (x = 0; x < vec_size; x++) {
                        norm += tvec[x] * tvec[x];
                }
                norm = (float) sqrt(norm);
-               for(x = 0; x < vec_size; x++) {
+               for (x = 0; x < vec_size; x++) {
                        tvec[x] /= norm;
                }
-               if(matSize == 2) {
+               if (matSize == 2) {
                        mat[0] = 1 - (tvec[0] * tvec[0]);
                        mat[1] = -(tvec[0] * tvec[1]);
                        mat[2] = -(tvec[0] * tvec[1]);
                        mat[3] = 1 - (tvec[1] * tvec[1]);
                }
-               else if(matSize > 2) {
+               else if (matSize > 2) {
                        mat[0] = 1 - (tvec[0] * tvec[0]);
                        mat[1] = -(tvec[0] * tvec[1]);
                        mat[2] = -(tvec[0] * tvec[2]);
@@ -532,11 +508,11 @@ static PyObject *C_Matrix_OrthoProjection(PyObject *cls, PyObject *args)
                        mat[8] = 1 - (tvec[2] * tvec[2]);
                }
        }
-       if(matSize == 4) {
+       if (matSize == 4) {
                matrix_3x3_as_4x4(mat);
        }
        //pass to matrix creation
-       return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
+       return Matrix_CreatePyObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
 }
 
 PyDoc_STRVAR(C_Matrix_Shear_doc,
@@ -563,22 +539,22 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args)
        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(!PyArg_ParseTuple(args, "siO:Matrix.Shear", &plane, &matSize, &fac)) {
+       if (!PyArg_ParseTuple(args, "siO:Matrix.Shear", &plane, &matSize, &fac)) {
                return NULL;
        }
-       if(matSize != 2 && matSize != 3 && matSize != 4) {
+       if (matSize != 2 && matSize != 3 && matSize != 4) {
                PyErr_SetString(PyExc_ValueError,
-                               "mathutils.Matrix.Shear(): "
+                               "Matrix.Shear(): "
                                "can only return a 2x2 3x3 or 4x4 matrix");
                return NULL;
        }
 
-       if(matSize == 2) {
+       if (matSize == 2) {
                float const factor= PyFloat_AsDouble(fac);
 
-               if(factor==-1.0f && PyErr_Occurred()) {
+               if (factor==-1.0f && PyErr_Occurred()) {
                        PyErr_SetString(PyExc_TypeError,
-                                       "mathutils.Matrix.Shear(): "
+                                       "Matrix.Shear(): "
                                        "the factor to be a float");
                        return NULL;
                }
@@ -587,10 +563,10 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args)
                mat[0] = 1.0f;
                mat[3] = 1.0f;
 
-               if(strcmp(plane, "X") == 0) {
+               if (strcmp(plane, "X") == 0) {
                        mat[2] = factor;
                }
-               else if(strcmp(plane, "Y") == 0) {
+               else if (strcmp(plane, "Y") == 0) {
                        mat[1] = factor;
                }
                else {
@@ -604,7 +580,7 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args)
                /* 3 or 4, apply as 3x3, resize later if needed */
                float factor[2];
 
-               if(mathutils_array_parse(factor, 2, 2, fac, "Matrix.Shear()") < 0) {
+               if (mathutils_array_parse(factor, 2, 2, fac, "Matrix.Shear()") < 0) {
                        return NULL;
                }
 
@@ -613,31 +589,31 @@ static PyObject *C_Matrix_Shear(PyObject *cls, PyObject *args)
                mat[4] = 1.0f;
                mat[8] = 1.0f;
 
-               if(strcmp(plane, "XY") == 0) {
+               if (strcmp(plane, "XY") == 0) {
                        mat[6] = factor[0];
                        mat[7] = factor[1];
                }
-               else if(strcmp(plane, "XZ") == 0) {
+               else if (strcmp(plane, "XZ") == 0) {
                        mat[3] = factor[0];
                        mat[5] = factor[1];
                }
-               else if(strcmp(plane, "YZ") == 0) {
+               else if (strcmp(plane, "YZ") == 0) {
                        mat[1] = factor[0];
                        mat[2] = factor[1];
                }
                else {
                        PyErr_SetString(PyExc_ValueError,
-                                       "mathutils.Matrix.Shear(): "
+                                       "Matrix.Shear(): "
                                        "expected: X, Y, XY, XZ, YZ");
                        return NULL;
                }
        }
 
-       if(matSize == 4) {
+       if (matSize == 4) {
                matrix_3x3_as_4x4(mat);
        }
        //pass to matrix creation
-       return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
+       return Matrix_CreatePyObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
 }
 
 void matrix_as_3x3(float mat[3][3], MatrixObject *self)
@@ -650,11 +626,11 @@ void matrix_as_3x3(float mat[3][3], MatrixObject *self)
 /* assumes rowsize == colsize is checked and the read callback has run */
 static float matrix_determinant_internal(MatrixObject *self)
 {
-       if(self->row_size == 2) {
+       if (self->row_size == 2) {
                return determinant_m2(self->matrix[0][0], self->matrix[0][1],
                                         self->matrix[1][0], self->matrix[1][1]);
        }
-       else if(self->row_size == 3) {
+       else if (self->row_size == 3) {
                return determinant_m3(self->matrix[0][0], self->matrix[0][1],
                                         self->matrix[0][2], self->matrix[1][0],
                                         self->matrix[1][1], self->matrix[1][2],
@@ -680,24 +656,24 @@ static PyObject *Matrix_to_quaternion(MatrixObject *self)
 {
        float quat[4];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if((self->col_size < 3) || (self->row_size < 3) || (self->col_size != self->row_size)) {
+       if ((self->col_size < 3) || (self->row_size < 3) || (self->col_size != self->row_size)) {
                PyErr_SetString(PyExc_ValueError,
-                               "matrix.to_quat(): "
+                               "Matrix.to_quat(): "
                                "inappropriate matrix size - expects 3x3 or 4x4 matrix");
                return NULL;
        }
-       if(self->col_size == 3){
+       if (self->col_size == 3) {
                mat3_to_quat(quat, (float (*)[3])self->contigPtr);
        }
        else {
                mat4_to_quat(quat, (float (*)[4])self->contigPtr);
        }
 
-       return newQuaternionObject(quat, Py_NEW, NULL);
+       return Quaternion_CreatePyObject(quat, Py_NEW, NULL);
 }
 
 /*---------------------------matrix.toEuler() --------------------*/
@@ -727,21 +703,21 @@ static PyObject *Matrix_to_euler(MatrixObject *self, PyObject *args)
        float tmat[3][3];
        float (*mat)[3];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
+       if (!PyArg_ParseTuple(args, "|sO!:to_euler", &order_str, &euler_Type, &eul_compat))
                return NULL;
 
-       if(eul_compat) {
-               if(BaseMath_ReadCallback(eul_compat) == -1)
+       if (eul_compat) {
+               if (BaseMath_ReadCallback(eul_compat) == -1)
                        return NULL;
 
                copy_v3_v3(eul_compatf, eul_compat->eul);
        }
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if(self->col_size ==3 && self->row_size ==3) {
+       if (self->col_size ==3 && self->row_size ==3) {
                mat= (float (*)[3])self->contigPtr;
        }
        else if (self->col_size ==4 && self->row_size ==4) {
@@ -750,28 +726,28 @@ static PyObject *Matrix_to_euler(MatrixObject *self, PyObject *args)
        }
        else {
                PyErr_SetString(PyExc_ValueError,
-                               "matrix.to_euler(): "
+                               "Matrix.to_euler(): "
                                "inappropriate matrix size - expects 3x3 or 4x4 matrix");
                return NULL;
        }
 
-       if(order_str) {
-               order= euler_order_from_string(order_str, "matrix.to_euler()");
+       if (order_str) {
+               order= euler_order_from_string(order_str, "Matrix.to_euler()");
 
-               if(order == -1)
+               if (order == -1)
                        return NULL;
        }
 
-       if(eul_compat) {
-               if(order == 1)  mat3_to_compatible_eul(eul, eul_compatf, mat);
+       if (eul_compat) {
+               if (order == 1) mat3_to_compatible_eul(eul, eul_compatf, mat);
                else                    mat3_to_compatible_eulO(eul, eul_compatf, order, mat);
        }
        else {
-               if(order == 1)  mat3_to_eul(eul, mat);
+               if (order == 1) mat3_to_eul(eul, mat);
                else                    mat3_to_eulO(eul, order, mat);
        }
 
-       return newEulerObject(eul, order, Py_NEW, NULL);
+       return Euler_CreatePyObject(eul, order, Py_NEW, NULL);
 }
 
 PyDoc_STRVAR(Matrix_resize_4x4_doc,
@@ -783,32 +759,35 @@ static PyObject *Matrix_resize_4x4(MatrixObject *self)
 {
        int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
 
-       if(self->wrapped==Py_WRAP){
+       if (self->wrapped==Py_WRAP) {
                PyErr_SetString(PyExc_TypeError,
+                               "Matrix.resize_4x4(): "
                                "cannot resize wrapped data - make a copy and resize that");
                return NULL;
        }
-       if(self->cb_user){
+       if (self->cb_user) {
                PyErr_SetString(PyExc_TypeError,
+                               "Matrix.resize_4x4(): "
                                "cannot resize owned data - make a copy and resize that");
                return NULL;
        }
 
        self->contigPtr = PyMem_Realloc(self->contigPtr, (sizeof(float) * 16));
-       if(self->contigPtr == NULL) {
+       if (self->contigPtr == NULL) {
                PyErr_SetString(PyExc_MemoryError,
-                               "matrix.resize_4x4(): problem allocating pointer space");
+                               "Matrix.resize_4x4(): "
+                               "problem allocating pointer space");
                return NULL;
        }
        /*set row pointers*/
-       for(x = 0; x < 4; x++) {
+       for (x = 0; x < 4; x++) {
                self->matrix[x] = self->contigPtr + (x * 4);
        }
        /*move data to new spot in array + clean*/
-       for(blank_rows = (4 - self->row_size); blank_rows > 0; blank_rows--){
-               for(x = 0; x < 4; x++){
+       for (blank_rows = (4 - self->row_size); blank_rows > 0; blank_rows--) {
+               for (x = 0; x < 4; x++) {
                        index = (4 * (self->row_size + (blank_rows - 1))) + x;
-                       if (index == 10 || index == 15){
+                       if (index == 10 || index == 15) {
                                self->contigPtr[index] = 1.0f;
                        }
                        else {
@@ -816,14 +795,14 @@ static PyObject *Matrix_resize_4x4(MatrixObject *self)
                        }
                }
        }
-       for(x = 1; x <= self->row_size; x++){
+       for (x = 1; x <= self->row_size; x++) {
                first_row_elem = (self->col_size * (self->row_size - x));
                curr_pos = (first_row_elem + (self->col_size -1));
                new_pos = (4 * (self->row_size - x)) + (curr_pos - first_row_elem);
-               for(blank_columns = (4 - self->col_size); blank_columns > 0; blank_columns--){
+               for (blank_columns = (4 - self->col_size); blank_columns > 0; blank_columns--) {
                        self->contigPtr[new_pos + blank_columns] = 0.0f;
                }
-               for( ; curr_pos >= first_row_elem; curr_pos--){
+               for ( ; curr_pos >= first_row_elem; curr_pos--) {
                        self->contigPtr[new_pos] = self->contigPtr[curr_pos];
                        new_pos--;
                }
@@ -844,21 +823,22 @@ PyDoc_STRVAR(Matrix_to_4x4_doc,
 );
 static PyObject *Matrix_to_4x4(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(self->col_size==4 && self->row_size==4) {
-               return (PyObject *)newMatrixObject(self->contigPtr, 4, 4, Py_NEW, Py_TYPE(self));
+       if (self->col_size==4 && self->row_size==4) {
+               return (PyObject *)Matrix_CreatePyObject(self->contigPtr, 4, 4, Py_NEW, Py_TYPE(self));
        }
-       else if(self->col_size==3 && self->row_size==3) {
+       else if (self->col_size==3 && self->row_size==3) {
                float mat[4][4];
                copy_m4_m3(mat, (float (*)[3])self->contigPtr);
-               return (PyObject *)newMatrixObject((float *)mat, 4, 4, Py_NEW, Py_TYPE(self));
+               return (PyObject *)Matrix_CreatePyObject((float *)mat, 4, 4, Py_NEW, Py_TYPE(self));
        }
        /* TODO, 2x2 matrix */
 
        PyErr_SetString(PyExc_TypeError,
-                       "matrix.to_4x4(): inappropriate matrix size");
+                       "Matrix.to_4x4(): "
+                       "inappropriate matrix size");
        return NULL;
 }
 
@@ -874,18 +854,18 @@ static PyObject *Matrix_to_3x3(MatrixObject *self)
 {
        float mat[3][3];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if((self->col_size < 3) || (self->row_size < 3)) {
+       if ((self->col_size < 3) || (self->row_size < 3)) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.to_3x3(): inappropriate matrix size");
+                               "Matrix.to_3x3(): inappropriate matrix size");
                return NULL;
        }
 
        matrix_as_3x3(mat, self);
 
-       return newMatrixObject((float *)mat, 3, 3, Py_NEW, Py_TYPE(self));
+       return Matrix_CreatePyObject((float *)mat, 3, 3, Py_NEW, Py_TYPE(self));
 }
 
 PyDoc_STRVAR(Matrix_to_translation_doc,
@@ -898,17 +878,17 @@ PyDoc_STRVAR(Matrix_to_translation_doc,
 );
 static PyObject *Matrix_to_translation(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if((self->col_size < 3) || self->row_size < 4){
+       if ((self->col_size < 3) || self->row_size < 4) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.to_translation(): "
+                               "Matrix.to_translation(): "
                                "inappropriate matrix size");
                return NULL;
        }
 
-       return newVectorObject(self->matrix[3], 3, Py_NEW, NULL);
+       return Vector_CreatePyObject(self->matrix[3], 3, Py_NEW, NULL);
 }
 
 PyDoc_STRVAR(Matrix_to_scale_doc,
@@ -927,13 +907,13 @@ static PyObject *Matrix_to_scale(MatrixObject *self)
        float mat[3][3];
        float size[3];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if((self->col_size < 3) || (self->row_size < 3)) {
+       if ((self->col_size < 3) || (self->row_size < 3)) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.to_scale(): "
+                               "Matrix.to_scale(): "
                                "inappropriate matrix size, 3x3 minimum size");
                return NULL;
        }
@@ -943,7 +923,7 @@ static PyObject *Matrix_to_scale(MatrixObject *self)
        /* compatible mat4_to_loc_rot_size */
        mat3_to_rot_size(rot, size, mat);
 
-       return newVectorObject(size, 3, Py_NEW, NULL);
+       return Vector_CreatePyObject(size, 3, Py_NEW, NULL);
 }
 
 /*---------------------------matrix.invert() ---------------------*/
@@ -961,15 +941,17 @@ static PyObject *Matrix_invert(MatrixObject *self)
 
        int x, y, z = 0;
        float det = 0.0f;
-       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};
+       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(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(self->row_size != self->col_size){
+       if (self->row_size != self->col_size) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.invert(ed): "
+                               "Matrix.invert(ed): "
                                "only square matrices are supported");
                return NULL;
        }
@@ -977,25 +959,27 @@ static PyObject *Matrix_invert(MatrixObject *self)
        /*calculate the determinant*/
        det = matrix_determinant_internal(self);
 
-       if(det != 0) {
+       if (det != 0) {
                /*calculate the classical adjoint*/
-               if(self->row_size == 2) {
+               if (self->row_size == 2) {
                        mat[0] = self->matrix[1][1];
                        mat[1] = -self->matrix[0][1];
                        mat[2] = -self->matrix[1][0];
                        mat[3] = self->matrix[0][0];
-               } else if(self->row_size == 3) {
+               }
+               else if (self->row_size == 3) {
                        adjoint_m3_m3((float (*)[3]) mat,(float (*)[3])self->contigPtr);
-               } else if(self->row_size == 4) {
+               }
+               else if (self->row_size == 4) {
                        adjoint_m4_m4((float (*)[4]) mat, (float (*)[4])self->contigPtr);
                }
                /*divide by determinate*/
-               for(x = 0; x < (self->row_size * self->col_size); x++) {
+               for (x = 0; x < (self->row_size * self->col_size); x++) {
                        mat[x] /= det;
                }
                /*set values*/
-               for(x = 0; x < self->row_size; x++) {
-                       for(y = 0; y < self->col_size; y++) {
+               for (x = 0; x < self->row_size; x++) {
+                       for (y = 0; y < self->col_size; y++) {
                                self->matrix[x][y] = mat[z];
                                z++;
                        }
@@ -1005,6 +989,7 @@ static PyObject *Matrix_invert(MatrixObject *self)
        }
        else {
                PyErr_SetString(PyExc_ValueError,
+                               "Matrix.invert(ed): "
                                "matrix does not have an inverse");
                return NULL;
        }
@@ -1042,20 +1027,21 @@ static PyObject *Matrix_rotate(MatrixObject *self, PyObject *value)
 {
        float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(mathutils_any_to_rotmat(other_rmat, value, "matrix.rotate(value)") == -1)
+       if (mathutils_any_to_rotmat(other_rmat, value, "matrix.rotate(value)") == -1)
                return NULL;
 
-       if(self->col_size != 3 || self->row_size != 3) {
+       if (self->col_size != 3 || self->row_size != 3) {
                PyErr_SetString(PyExc_TypeError,
-                               "Matrix must have 3x3 dimensions");
+                               "Matrix.rotate(): "
+                               "must have 3x3 dimensions");
                return NULL;
        }
 
        matrix_as_3x3(self_rmat, self);
-       mul_m3_m3m3(rmat, self_rmat, other_rmat);
+       mul_m3_m3m3(rmat, other_rmat, self_rmat);
 
        copy_m3_m3((float (*)[3])(self->contigPtr), rmat);
 
@@ -1080,23 +1066,23 @@ static PyObject *Matrix_decompose(MatrixObject *self)
        float quat[4];
        float size[3];
 
-       if(self->col_size != 4 || self->row_size != 4) {
+       if (self->col_size != 4 || self->row_size != 4) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.decompose(): "
+                               "Matrix.decompose(): "
                                "inappropriate matrix size - expects 4x4 matrix");
                return NULL;
        }
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        mat4_to_loc_rot_size(loc, rot, size, (float (*)[4])self->contigPtr);
        mat3_to_quat(quat, rot);
 
        ret= PyTuple_New(3);
-       PyTuple_SET_ITEM(ret, 0, newVectorObject(loc, 3, Py_NEW, NULL));
-       PyTuple_SET_ITEM(ret, 1, newQuaternionObject(quat, Py_NEW, NULL));
-       PyTuple_SET_ITEM(ret, 2, newVectorObject(size, 3, Py_NEW, NULL));
+       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(loc, 3, Py_NEW, NULL));
+       PyTuple_SET_ITEM(ret, 1, Quaternion_CreatePyObject(quat, Py_NEW, NULL));
+       PyTuple_SET_ITEM(ret, 2, Vector_CreatePyObject(size, 3, Py_NEW, NULL));
 
        return ret;
 }
@@ -1106,7 +1092,7 @@ static PyObject *Matrix_decompose(MatrixObject *self)
 PyDoc_STRVAR(Matrix_lerp_doc,
 ".. function:: lerp(other, factor)\n"
 "\n"
-"   Returns the interpolation of two matricies.\n"
+"   Returns the interpolation of two matrices.\n"
 "\n"
 "   :arg other: value to interpolate with.\n"
 "   :type other: :class:`Matrix`\n"
@@ -1120,21 +1106,21 @@ static PyObject *Matrix_lerp(MatrixObject *self, PyObject *args)
        MatrixObject *mat2 = NULL;
        float fac, mat[MATRIX_MAX_DIM*MATRIX_MAX_DIM];
 
-       if(!PyArg_ParseTuple(args, "O!f:lerp", &matrix_Type, &mat2, &fac))
+       if (!PyArg_ParseTuple(args, "O!f:lerp", &matrix_Type, &mat2, &fac))
                return NULL;
 
-       if(self->row_size != mat2->row_size || self->col_size != mat2->col_size) {
+       if (self->row_size != mat2->row_size || self->col_size != mat2->col_size) {
                PyErr_SetString(PyExc_ValueError,
-                               "matrix.lerp(): "
+                               "Matrix.lerp(): "
                                "expects both matrix objects of the same dimensions");
                return NULL;
        }
 
-       if(BaseMath_ReadCallback(self) == -1 || BaseMath_ReadCallback(mat2) == -1)
+       if (BaseMath_ReadCallback(self) == -1 || BaseMath_ReadCallback(mat2) == -1)
                return NULL;
 
        /* TODO, different sized matrix */
-       if(self->row_size==4 && self->col_size==4) {
+       if (self->row_size==4 && self->col_size==4) {
                blend_m4_m4m4((float (*)[4])mat, (float (*)[4])self->contigPtr, (float (*)[4])mat2->contigPtr, fac);
        }
        else if (self->row_size==3 && self->col_size==3) {
@@ -1142,12 +1128,12 @@ static PyObject *Matrix_lerp(MatrixObject *self, PyObject *args)
        }
        else {
                PyErr_SetString(PyExc_ValueError,
-                               "matrix.lerp(): "
+                               "Matrix.lerp(): "
                                "only 3x3 and 4x4 matrices supported");
                return NULL;
        }
 
-       return (PyObject*)newMatrixObject(mat, self->row_size, self->col_size, Py_NEW, Py_TYPE(self));
+       return (PyObject*)Matrix_CreatePyObject(mat, self->row_size, self->col_size, Py_NEW, Py_TYPE(self));
 }
 
 /*---------------------------matrix.determinant() ----------------*/
@@ -1163,12 +1149,12 @@ PyDoc_STRVAR(Matrix_determinant_doc,
 );
 static PyObject *Matrix_determinant(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(self->row_size != self->col_size){
+       if (self->row_size != self->col_size) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.determinant: "
+                               "Matrix.determinant(): "
                                "only square matrices are supported");
                return NULL;
        }
@@ -1187,21 +1173,22 @@ static PyObject *Matrix_transpose(MatrixObject *self)
 {
        float t = 0.0f;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(self->row_size != self->col_size){
+       if (self->row_size != self->col_size) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.transpose(d): "
+                               "Matrix.transpose(d): "
                                "only square matrices are supported");
                return NULL;
        }
 
-       if(self->row_size == 2) {
+       if (self->row_size == 2) {
                t = self->matrix[1][0];
                self->matrix[1][0] = self->matrix[0][1];
                self->matrix[0][1] = t;
-       } else if(self->row_size == 3) {
+       }
+       else if (self->row_size == 3) {
                transpose_m3((float (*)[3])self->contigPtr);
        }
        else {
@@ -1236,9 +1223,9 @@ PyDoc_STRVAR(Matrix_zero_doc,
 );
 static PyObject *Matrix_zero(MatrixObject *self)
 {
-       fill_vn(self->contigPtr, self->row_size * self->col_size, 0.0f);
+       fill_vn_fl(self->contigPtr, self->row_size * self->col_size, 0.0f);
 
-       if(BaseMath_WriteCallback(self) == -1)
+       if (BaseMath_WriteCallback(self) == -1)
                return NULL;
 
        Py_RETURN_NONE;
@@ -1256,29 +1243,30 @@ PyDoc_STRVAR(Matrix_identity_doc,
 );
 static PyObject *Matrix_identity(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(self->row_size != self->col_size){
+       if (self->row_size != self->col_size) {
                PyErr_SetString(PyExc_TypeError,
-                               "matrix.identity: "
+                               "Matrix.identity(): "
                                "only square matrices are supported");
                return NULL;
        }
 
-       if(self->row_size == 2) {
+       if (self->row_size == 2) {
                self->matrix[0][0] = 1.0f;
                self->matrix[0][1] = 0.0f;
                self->matrix[1][0] = 0.0f;
                self->matrix[1][1] = 1.0f;
-       } else if(self->row_size == 3) {
+       }
+       else if (self->row_size == 3) {
                unit_m3((float (*)[3])self->contigPtr);
        }
        else {
                unit_m4((float (*)[4])self->contigPtr);
        }
 
-       if(BaseMath_WriteCallback(self) == -1)
+       if (BaseMath_WriteCallback(self) == -1)
                return NULL;
 
        Py_RETURN_NONE;
@@ -1295,10 +1283,10 @@ PyDoc_STRVAR(Matrix_copy_doc,
 );
 static PyObject *Matrix_copy(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       return (PyObject*)newMatrixObject((float (*))self->contigPtr, self->row_size, self->col_size, Py_NEW, Py_TYPE(self));
+       return (PyObject*)Matrix_CreatePyObject((float (*))self->contigPtr, self->row_size, self->col_size, Py_NEW, Py_TYPE(self));
 }
 
 /*----------------------------print object (internal)-------------*/
@@ -1308,12 +1296,12 @@ static PyObject *Matrix_repr(MatrixObject *self)
        int x, y;
        PyObject *rows[MATRIX_MAX_DIM]= {NULL};
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       for(x = 0; x < self->row_size; x++){
+       for (x = 0; x < self->row_size; x++) {
                rows[x]= PyTuple_New(self->col_size);
-               for(y = 0; y < self->col_size; y++) {
+               for (y = 0; y < self->col_size; y++) {
                        PyTuple_SET_ITEM(rows[x], y, PyFloat_FromDouble(self->matrix[x][y]));
                }
        }
@@ -1344,7 +1332,7 @@ static PyObject* Matrix_richcmpr(PyObject *a, PyObject *b, int op)
                MatrixObject *matA= (MatrixObject*)a;
                MatrixObject *matB= (MatrixObject*)b;
 
-               if(BaseMath_ReadCallback(matA) == -1 || BaseMath_ReadCallback(matB) == -1)
+               if (BaseMath_ReadCallback(matA) == -1 || BaseMath_ReadCallback(matB) == -1)
                        return NULL;
 
                ok=     (       (matA->col_size == matB->col_size) &&
@@ -1386,16 +1374,16 @@ 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(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
-       if(i < 0 || i >= self->row_size) {
+       if (i < 0 || i >= self->row_size) {
                PyErr_SetString(PyExc_IndexError,
                                "matrix[attribute]: "
                                "array index out of range");
                return NULL;
        }
-       return newVectorObject_cb((PyObject *)self, self->col_size, mathutils_matrix_vector_cb_index, i);
+       return Vector_CreatePyObject_cb((PyObject *)self, self->col_size, mathutils_matrix_vector_cb_index, i);
 }
 /*----------------------------object[]-------------------------
   sequence accessor (set) */
@@ -1403,16 +1391,16 @@ static PyObject *Matrix_item(MatrixObject *self, int i)
 static int Matrix_ass_item(MatrixObject *self, int i, PyObject *value)
 {
        float vec[4];
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
-       if(i >= self->row_size || i < 0){
+       if (i >= self->row_size || i < 0) {
                PyErr_SetString(PyExc_IndexError,
                                "matrix[attribute] = x: bad column");
                return -1;
        }
 
-       if(mathutils_array_parse(vec, self->col_size, self->col_size, value, "matrix[i] = value assignment") < 0) {
+       if (mathutils_array_parse(vec, self->col_size, self->col_size, value, "matrix[i] = value assignment") < 0) {
                return -1;
        }
 
@@ -1430,7 +1418,7 @@ static PyObject *Matrix_slice(MatrixObject *self, int begin, int end)
        PyObject *tuple;
        int count;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        CLAMP(begin, 0, self->row_size);
@@ -1438,9 +1426,9 @@ static PyObject *Matrix_slice(MatrixObject *self, int begin, int end)
        begin= MIN2(begin, end);
 
        tuple= PyTuple_New(end - begin);
-       for(count= begin; count < end; count++) {
+       for (count= begin; count < end; count++) {
                PyTuple_SET_ITEM(tuple, count - begin,
-                               newVectorObject_cb((PyObject *)self, self->col_size, mathutils_matrix_vector_cb_index, count));
+                               Vector_CreatePyObject_cb((PyObject *)self, self->col_size, mathutils_matrix_vector_cb_index, count));
 
        }
 
@@ -1452,7 +1440,7 @@ static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *va
 {
        PyObject *value_fast= NULL;
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return -1;
 
        CLAMP(begin, 0, self->row_size);
@@ -1460,7 +1448,7 @@ static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *va
        begin = MIN2(begin, end);
 
        /* non list/tuple cases */
-       if(!(value_fast=PySequence_Fast(value, "matrix[begin:end] = value"))) {
+       if (!(value_fast=PySequence_Fast(value, "matrix[begin:end] = value"))) {
                /* PySequence_Fast sets the error */
                return -1;
        }
@@ -1469,7 +1457,7 @@ static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *va
                int i;
                float mat[16];
 
-               if(PySequence_Fast_GET_SIZE(value_fast) != size) {
+               if (PySequence_Fast_GET_SIZE(value_fast) != size) {
                        Py_DECREF(value_fast);
                        PyErr_SetString(PyExc_ValueError,
                                        "matrix[begin:end] = []: "
@@ -1482,7 +1470,7 @@ static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *va
                        /*parse each sub sequence*/
                        PyObject *item= PySequence_Fast_GET_ITEM(value_fast, i);
 
-                       if(mathutils_array_parse(&mat[i * self->col_size], self->col_size, self->col_size, item, "matrix[begin:end] = value assignment") < 0) {
+                       if (mathutils_array_parse(&mat[i * self->col_size], self->col_size, self->col_size, item, "matrix[begin:end] = value assignment") < 0) {
                                return -1;
                        }
                }
@@ -1506,17 +1494,18 @@ static PyObject *Matrix_add(PyObject *m1, PyObject *m2)
        mat1 = (MatrixObject*)m1;
        mat2 = (MatrixObject*)m2;
 
-       if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
-               PyErr_SetString(PyExc_TypeError,
-                               "Matrix addition: "
-                               "arguments not valid for this operation");
+       if (!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
+               PyErr_Format(PyExc_TypeError,
+                            "Matrix addition: (%s + %s) "
+                            "invalid type for this operation",
+                            Py_TYPE(m1)->tp_name, Py_TYPE(m2)->tp_name);
                return NULL;
        }
 
-       if(BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1)
+       if (BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1)
                return NULL;
 
-       if(mat1->row_size != mat2->row_size || mat1->col_size != mat2->col_size){
+       if (mat1->row_size != mat2->row_size || mat1->col_size != mat2->col_size) {
                PyErr_SetString(PyExc_TypeError,
                                "Matrix addition: "
                                "matrices must have the same dimensions for this operation");
@@ -1525,7 +1514,7 @@ static PyObject *Matrix_add(PyObject *m1, PyObject *m2)
 
        add_vn_vnvn(mat, mat1->contigPtr, mat2->contigPtr, mat1->row_size * mat1->col_size);
 
-       return newMatrixObject(mat, mat1->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
+       return Matrix_CreatePyObject(mat, mat1->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
 }
 /*------------------------obj - obj------------------------------
   subtraction*/
@@ -1537,17 +1526,19 @@ static PyObject *Matrix_sub(PyObject *m1, PyObject *m2)
        mat1 = (MatrixObject*)m1;
        mat2 = (MatrixObject*)m2;
 
-       if(!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
-               PyErr_SetString(PyExc_TypeError,
-                               "Matrix addition: "
-                               "arguments not valid for this operation");
+       if (!MatrixObject_Check(m1) || !MatrixObject_Check(m2)) {
+               PyErr_Format(PyExc_TypeError,
+                            "Matrix subtraction: (%s - %s) "
+                            "invalid type for this operation",
+                            Py_TYPE(m1)->tp_name, Py_TYPE(m2)->tp_name
+                            );
                return NULL;
        }
 
-       if(BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1)
+       if (BaseMath_ReadCallback(mat1) == -1 || BaseMath_ReadCallback(mat2) == -1)
                return NULL;
 
-       if(mat1->row_size != mat2->row_size || mat1->col_size != mat2->col_size){
+       if (mat1->row_size != mat2->row_size || mat1->col_size != mat2->col_size) {
                PyErr_SetString(PyExc_TypeError,
                                "Matrix addition: "
                                "matrices must have the same dimensions for this operation");
@@ -1556,7 +1547,7 @@ static PyObject *Matrix_sub(PyObject *m1, PyObject *m2)
 
        sub_vn_vnvn(mat, mat1->contigPtr, mat2->contigPtr, mat1->row_size * mat1->col_size);
 
-       return newMatrixObject(mat, mat1->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
+       return Matrix_CreatePyObject(mat, mat1->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
 }
 /*------------------------obj * obj------------------------------
   mulplication*/
@@ -1564,7 +1555,7 @@ static PyObject *matrix_mul_float(MatrixObject *mat, const float scalar)
 {
        float tmat[16];
        mul_vn_vn_fl(tmat, mat->contigPtr, mat->row_size * mat->col_size, scalar);
-       return newMatrixObject(tmat, mat->row_size, mat->col_size, Py_NEW, Py_TYPE(mat));
+       return Matrix_CreatePyObject(tmat, mat->row_size, mat->col_size, Py_NEW, Py_TYPE(mat));
 }
 
 static PyObject *Matrix_mul(PyObject *m1, PyObject *m2)
@@ -1573,18 +1564,18 @@ static PyObject *Matrix_mul(PyObject *m1, PyObject *m2)
 
        MatrixObject *mat1 = NULL, *mat2 = NULL;
 
-       if(MatrixObject_Check(m1)) {
+       if (MatrixObject_Check(m1)) {
                mat1 = (MatrixObject*)m1;
-               if(BaseMath_ReadCallback(mat1) == -1)
+               if (BaseMath_ReadCallback(mat1) == -1)
                        return NULL;
        }
-       if(MatrixObject_Check(m2)) {
+       if (MatrixObject_Check(m2)) {
                mat2 = (MatrixObject*)m2;
-               if(BaseMath_ReadCallback(mat2) == -1)
+               if (BaseMath_ReadCallback(mat2) == -1)
                        return NULL;
        }
 
-       if(mat1 && mat2) {
+       if (mat1 && mat2) {
                /*MATRIX * MATRIX*/
                float mat[16]= {0.0f, 0.0f, 0.0f, 0.0f,
                                                0.0f, 0.0f, 0.0f, 0.0f,
@@ -1593,9 +1584,9 @@ static PyObject *Matrix_mul(PyObject *m1, PyObject *m2)
                double dot = 0.0f;
                int x, y, z;
 
-               for(x = 0; x < mat2->row_size; x++) {
-                       for(y = 0; y < mat1->col_size; y++) {
-                               for(z = 0; z < mat1->row_size; z++) {
+               for (x = 0; x < mat2->row_size; x++) {
+                       for (y = 0; y < mat1->col_size; y++) {
+                               for (z = 0; z < mat1->row_size; z++) {
                                        dot += (mat1->matrix[z][y] * mat2->matrix[x][z]);
                                }
                                mat[((x * mat1->col_size) + y)] = (float)dot;
@@ -1603,17 +1594,29 @@ static PyObject *Matrix_mul(PyObject *m1, PyObject *m2)
                        }
                }
 
-               return newMatrixObject(mat, mat2->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
+               return Matrix_CreatePyObject(mat, mat2->row_size, mat1->col_size, Py_NEW, Py_TYPE(mat1));
        }
-       else if(mat2) {
+       else if (mat2) {
                /*FLOAT/INT * MATRIX */
                if (((scalar= PyFloat_AsDouble(m1)) == -1.0f && PyErr_Occurred())==0) {
                        return matrix_mul_float(mat2, scalar);
                }
        }
-       else if(mat1) {
+       else if (mat1) {
+               /*VEC * MATRIX */
+               if (VectorObject_Check(m2)) {
+                       VectorObject *vec2= (VectorObject *)m2;
+                       float tvec[4];
+                       if (BaseMath_ReadCallback(vec2) == -1)
+                               return NULL;
+                       if (column_vector_multiplication(tvec, vec2, mat1) == -1) {
+                               return NULL;
+                       }
+
+                       return Vector_CreatePyObject(tvec, vec2->size, Py_NEW, Py_TYPE(m2));
+               }
                /*FLOAT/INT * MATRIX */
-               if (((scalar= PyFloat_AsDouble(m2)) == -1.0f && PyErr_Occurred())==0) {
+               else if (((scalar= PyFloat_AsDouble(m2)) == -1.0f && PyErr_Occurred())==0) {
                        return matrix_mul_float(mat1, scalar);
                }
        }
@@ -1629,7 +1632,7 @@ static PyObject *Matrix_mul(PyObject *m1, PyObject *m2)
 }
 static PyObject* Matrix_inv(MatrixObject *self)
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        return Matrix_invert(self);
@@ -1660,7 +1663,8 @@ static PyObject *Matrix_subscript(MatrixObject* self, PyObject* item)
                if (i < 0)
                        i += self->row_size;
                return Matrix_item(self, i);
-       } else if (PySlice_Check(item)) {
+       }
+       else if (PySlice_Check(item)) {
                Py_ssize_t start, stop, step, slicelength;
 
                if (PySlice_GetIndicesEx((void *)item, self->row_size, &start, &stop, &step, &slicelength) < 0)
@@ -1674,7 +1678,7 @@ static PyObject *Matrix_subscript(MatrixObject* self, PyObject* item)
                }
                else {
                        PyErr_SetString(PyExc_IndexError,
-                                       "slice steps not supported with matricies");
+                                       "slice steps not supported with matrices");
                        return NULL;
                }
        }
@@ -1706,7 +1710,7 @@ static int Matrix_ass_subscript(MatrixObject* self, PyObject* item, PyObject* va
                        return Matrix_ass_slice(self, start, stop, value);
                else {
                        PyErr_SetString(PyExc_IndexError,
-                                       "slice steps not supported with matricies");
+                                       "slice steps not supported with matrices");
                        return -1;
                }
        }
@@ -1776,13 +1780,13 @@ static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closur
 {
        float mat[3][3];
 
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if((self->col_size < 3) || (self->row_size < 3)) {
+       if ((self->col_size < 3) || (self->row_size < 3)) {
                PyErr_SetString(PyExc_AttributeError,
-                               "matrix.median_scale: "
+                               "Matrix.median_scale: "
                                "inappropriate matrix size, 3x3 minimum");
                return NULL;
        }
@@ -1794,17 +1798,17 @@ static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closur
 
 static PyObject *Matrix_is_negative_get(MatrixObject *self, void *UNUSED(closure))
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if(self->col_size == 4 && self->row_size == 4)
+       if (self->col_size == 4 && self->row_size == 4)
                return PyBool_FromLong(is_negative_m4((float (*)[4])self->contigPtr));
-       else if(self->col_size == 3 && self->row_size == 3)
+       else if (self->col_size == 3 && self->row_size == 3)
                return PyBool_FromLong(is_negative_m3((float (*)[3])self->contigPtr));
        else {
                PyErr_SetString(PyExc_AttributeError,
-                               "matrix.is_negative: "
+                               "Matrix.is_negative: "
                                "inappropriate matrix size - expects 3x3 or 4x4 matrix");
                return NULL;
        }
@@ -1812,17 +1816,17 @@ static PyObject *Matrix_is_negative_get(MatrixObject *self, void *UNUSED(closure
 
 static PyObject *Matrix_is_orthogonal_get(MatrixObject *self, void *UNUSED(closure))
 {
-       if(BaseMath_ReadCallback(self) == -1)
+       if (BaseMath_ReadCallback(self) == -1)
                return NULL;
 
        /*must be 3-4 cols, 3-4 rows, square matrix*/
-       if(self->col_size == 4 && self->row_size == 4)
+       if (self->col_size == 4 && self->row_size == 4)
                return PyBool_FromLong(is_orthogonal_m4((float (*)[4])self->contigPtr));
-       else if(self->col_size == 3 && self->row_size == 3)
+       else if (self->col_size == 3 && self->row_size == 3)
                return PyBool_FromLong(is_orthogonal_m3((float (*)[3])self->contigPtr));
        else {
                PyErr_SetString(PyExc_AttributeError,
-                               "matrix.is_orthogonal: "
+                               "Matrix.is_orthogonal: "
                                "inappropriate matrix size - expects 3x3 or 4x4 matrix");
                return NULL;
        }
@@ -1936,7 +1940,7 @@ PyTypeObject matrix_Type = {
        NULL                                                            /*tp_del*/
 };
 
-/*------------------------newMatrixObject (internal)-------------
+/*------------------------Matrix_CreatePyObject (internal)-------------
 creates a new matrix object
 self->matrix     self->contiguous_ptr (reference to data.xxx)
           [0]------------->[0]
@@ -1952,13 +1956,13 @@ 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, const unsigned short rowSize, const unsigned short colSize, int type, PyTypeObject *base_type)
+PyObject *Matrix_CreatePyObject(float *mat, const unsigned short rowSize, const unsigned short colSize, int type, PyTypeObject *base_type)
 {
        MatrixObject *self;
        int x, row, col;
 
        /*matrix objects can be any 2-4row x 2-4col matrix*/
-       if(rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4) {
+       if (rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4) {
                PyErr_SetString(PyExc_RuntimeError,
                                "Matrix(): "
                                "row and column sizes must be between 2 and 4");
@@ -1968,7 +1972,7 @@ PyObject *newMatrixObject(float *mat, const unsigned short rowSize, const unsign
        self= base_type ?       (MatrixObject *)base_type->tp_alloc(base_type, 0) :
                                                (MatrixObject *)PyObject_GC_New(MatrixObject, &matrix_Type);
 
-       if(self) {
+       if (self) {
                self->row_size = rowSize;
                self->col_size = colSize;
 
@@ -1976,30 +1980,30 @@ PyObject *newMatrixObject(float *mat, const unsigned short rowSize, const unsign
                self->cb_user= NULL;
                self->cb_type= self->cb_subtype= 0;
 
-               if(type == Py_WRAP){
+               if (type == Py_WRAP) {
                        self->contigPtr = mat;
                        /*pointer array points to contigous memory*/
-                       for(x = 0; x < rowSize; x++) {
+                       for (x = 0; x < rowSize; x++) {
                                self->matrix[x] = self->contigPtr + (x * colSize);
                        }
                        self->wrapped = Py_WRAP;
                }
-               else if (type == Py_NEW){
+               else if (type == Py_NEW) {
                        self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float));
-                       if(self->contigPtr == NULL) { /*allocation failure*/
+                       if (self->contigPtr == NULL) { /*allocation failure*/
                                PyErr_SetString(PyExc_MemoryError,
                                                "Matrix(): "
                                                "problem allocating pointer space");
                                return NULL;
                        }
                        /*pointer array points to contigous memory*/
-                       for(x = 0; x < rowSize; x++) {
+                       for (x = 0; x < rowSize; x++) {
                                self->matrix[x] = self->contigPtr + (x * colSize);
                        }
                        /*parse*/
-                       if(mat) {       /*if a float array passed*/
-                               for(row = 0; row < rowSize; row++) {
-                                       for(col = 0; col < colSize; col++) {
+                       if (mat) {      /*if a float array passed*/
+                               for (row = 0; row < rowSize; row++) {
+                                       for (col = 0; col < colSize; col++) {
                                                self->matrix[row][col] = mat[(row * colSize) + col];
                                        }
                                }
@@ -2018,10 +2022,10 @@ PyObject *newMatrixObject(float *mat, const unsigned short rowSize, const unsign
        return (PyObject *) self;
 }
 
-PyObject *newMatrixObject_cb(PyObject *cb_user, int rowSize, int colSize, int cb_type, int cb_subtype)
+PyObject *Matrix_CreatePyObject_cb(PyObject *cb_user, int rowSize, int colSize, int cb_type, int cb_subtype)
 {
-       MatrixObject *self= (MatrixObject *)newMatrixObject(NULL, rowSize, colSize, Py_NEW, NULL);
-       if(self) {
+       MatrixObject *self= (MatrixObject *)Matrix_CreatePyObject(NULL, rowSize, colSize, Py_NEW, NULL);
+       if (self) {
                Py_INCREF(cb_user);
                self->cb_user=                  cb_user;
                self->cb_type=                  (unsigned char)cb_type;