diff --git a/source/gameengine/Ketsji/KX_PyMath.cpp b/source/gameengine/Ketsji/KX_PyMath.cpp index 24b31c7dba63..7d4649d64020 100644 --- a/source/gameengine/Ketsji/KX_PyMath.cpp +++ b/source/gameengine/Ketsji/KX_PyMath.cpp @@ -86,51 +86,6 @@ bool PyQuatTo(PyObject *pyval, mt::quat &qrot) return true; } -PyObject *PyObjectFrom(const mt::mat4 &mat) -{ -#ifdef USE_MATHUTILS - return Matrix_CreatePyObject((float *)mat.Data(), 4, 4, nullptr); -#else - PyObject *collist = PyList_New(4); - PyObject *col; - int i; - - for (i = 0; i < 4; i++) { - col = PyList_New(4); - PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i])); - PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i])); - PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i])); - PyList_SET_ITEM(col, 3, PyFloat_FromDouble(mat[3][i])); - PyList_SET_ITEM(collist, i, col); - } - - return collist; -#endif -} - -PyObject *PyObjectFrom(const mt::mat3 &mat) -{ -#ifdef USE_MATHUTILS - float fmat[9]; - mat.Pack(fmat); - return Matrix_CreatePyObject(fmat, 3, 3, nullptr); -#else - PyObject *collist = PyList_New(3); - PyObject *col; - int i; - - for (i = 0; i < 3; i++) { - col = PyList_New(3); - PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i])); - PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i])); - PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i])); - PyList_SET_ITEM(collist, i, col); - } - - return collist; -#endif -} - #ifdef USE_MATHUTILS PyObject *PyObjectFrom(const mt::quat &qrot) { @@ -140,55 +95,12 @@ PyObject *PyObjectFrom(const mt::quat &qrot) } #endif -PyObject *PyObjectFrom(const mt::vec4 &vec) -{ -#ifdef USE_MATHUTILS - return Vector_CreatePyObject(vec.Data(), 4, nullptr); -#else - PyObject *list = PyList_New(4); - PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); - PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); - PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2])); - PyList_SET_ITEM(list, 3, PyFloat_FromDouble(vec[3])); - return list; -#endif -} - -PyObject *PyObjectFrom(const mt::vec3 &vec) -{ -#ifdef USE_MATHUTILS - return Vector_CreatePyObject(vec.Data(), 3, nullptr); -#else - PyObject *list = PyList_New(3); - PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); - PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); - PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2])); - return list; -#endif -} - -PyObject *PyObjectFrom(const mt::vec2 &vec) -{ -#ifdef USE_MATHUTILS - return Vector_CreatePyObject(vec.Data(), 2, nullptr); -#else - PyObject *list = PyList_New(2); - PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); - PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); - return list; -#endif -} - PyObject *PyColorFromVector(const mt::vec3 &vec) { #ifdef USE_MATHUTILS return Color_CreatePyObject(vec.Data(), nullptr); #else - PyObject *list = PyList_New(3); - PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); - PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); - PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2])); - return list; + return PyObjectFrom(vec); #endif } diff --git a/source/gameengine/Ketsji/KX_PyMath.h b/source/gameengine/Ketsji/KX_PyMath.h index 971ae0289984..0e52be80f75c 100644 --- a/source/gameengine/Ketsji/KX_PyMath.h +++ b/source/gameengine/Ketsji/KX_PyMath.h @@ -232,44 +232,69 @@ bool PyQuatTo(PyObject *pyval, mt::quat &qrot); bool PyOrientationTo(PyObject *pyval, mt::mat3 &mat, const char *error_prefix); -/** - * Converts an mt::mat4 to a python object. - */ -PyObject *PyObjectFrom(const mt::mat4 &mat); - -/** - * Converts an mt::mat3 to a python object. - */ -PyObject *PyObjectFrom(const mt::mat3 &mat); +/// Converts an mt::matX to a python object. +template +PyObject *PyObjectFrom(const mt::Matrix &mat) +{ +#ifdef USE_MATHUTILS + float fmat[Rows * Cols]; + mat.Pack(fmat); + return Matrix_CreatePyObject(fmat, Rows, Cols, nullptr); +#else + PyObject *list = PyList_New(Rows); -/** - * Converts an mt::vec2 to a python object. - */ -PyObject *PyObjectFrom(const mt::vec2 &vec); + for (unsigned short i = 0; i < Rows; ++i) { + PyObject *row = PyList_New(Cols); + for (unsigned short j = 0; j < Cols; ++j) { + PyList_SET_ITEM(row, j, PyFloat_FromDouble(mat[i][j])); + } + PyList_SET_ITEM(list, i, row); + } -/** - * Converts an mt::vec3 to a python object - */ -PyObject *PyObjectFrom(const mt::vec3 &vec); + return list; +#endif +} #ifdef USE_MATHUTILS -/** - * Converts an mt::quat to a python object. - */ +/// Converts an mt::quat to a python object. PyObject *PyObjectFrom(const mt::quat &qrot); #endif -/** - * Converts an mt::vec4 to a python object. - */ -PyObject *PyObjectFrom(const mt::vec4 &pos); +/// Converts an mt::vecX to a python object. +template +PyObject *PyObjectFrom(const mt::Vector &vec) +{ +#ifdef USE_MATHUTILS + return Vector_CreatePyObject(vec.Data(), Size, nullptr); +#else + PyObject *list = PyList_New(Size); + for (unsigned short i = 0; i < Size; ++i) { + PyList_SET_ITEM(list, i, PyFloat_FromDouble(vec[i])); + } + return list; +#endif +} -/** - * Converts an mt::vec3 to a python color object. - */ +/// Converts an mt::vecX_packed to a python object. +template +PyObject *PyObjectFrom(const mt::VectorPacked &vec) +{ +#ifdef USE_MATHUTILS + return Vector_CreatePyObject(vec.data, Size, nullptr); +#else + PyObject *list = PyList_New(Size); + for (unsigned short i = 0; i < Size; ++i) { + PyList_SET_ITEM(list, i, PyFloat_FromDouble(vec.data[i])); + } + return list; +#endif +} + +/// Converts an mt::vec3 to a python color object. PyObject *PyColorFromVector(const mt::vec3 &vec); -template +/// Convert a float array to a python object. +template PyObject *PyObjectFrom(const float (&vec)[Size]) { #ifdef USE_MATHUTILS