diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2019-05-19 14:52:11 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2019-05-19 15:03:27 +0300 |
commit | ec1c9e325874eab4d75d0240473da2ca3c858a46 (patch) | |
tree | 379211ce5aa8d0c972addd1dc0828df5962050b7 /source/blender/python/mathutils | |
parent | d150c893f4faac26236011a17d486ed51655b50b (diff) |
Python API: add a Matrix.Diagonal constructor to mathutils.
For some reason there seems to be no way to do the very simple and
obvious task of converting a scale vector to a matrix via mathutils.
The Matrix.Scale constructor does something complicated instead.
Reviewers: brecht, campbellbarton
Differential Revision: https://developer.blender.org/D4893
Diffstat (limited to 'source/blender/python/mathutils')
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index 7cc9ba50d90..2e664616639 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -585,6 +585,34 @@ static PyObject *C_Matrix_Translation(PyObject *cls, PyObject *value) return Matrix_CreatePyObject(&mat[0][0], 4, 4, (PyTypeObject *)cls); } +/* ----------------------------------mathutils.Matrix.Diagonal() ------------- */ +PyDoc_STRVAR(C_Matrix_Diagonal_doc, + ".. classmethod:: Diagonal(vector)\n" + "\n" + " Create a diagonal (scaling) matrix using the values from the vector.\n" + "\n" + " :arg vector: The vector of values for the diagonal.\n" + " :type vector: :class:`Vector`\n" + " :return: A diagonal matrix.\n" + " :rtype: :class:`Matrix`\n"); +static PyObject *C_Matrix_Diagonal(PyObject *cls, PyObject *value) +{ + float mat[16] = {0.0f}; + float vec[4]; + + int size = mathutils_array_parse( + vec, 2, 4, value, "mathutils.Matrix.Diagonal(vector), invalid vector arg"); + + if (size == -1) { + return NULL; + } + + for (int i = 0; i < size; i++) { + mat[size * i + i] = vec[i]; + } + + return Matrix_CreatePyObject(mat, size, size, (PyTypeObject *)cls); +} /* ----------------------------------mathutils.Matrix.Scale() ------------- */ /* mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc. */ PyDoc_STRVAR(C_Matrix_Scale_doc, @@ -3085,6 +3113,7 @@ static struct PyMethodDef Matrix_methods[] = { {"Rotation", (PyCFunction)C_Matrix_Rotation, METH_VARARGS | METH_CLASS, C_Matrix_Rotation_doc}, {"Scale", (PyCFunction)C_Matrix_Scale, METH_VARARGS | METH_CLASS, C_Matrix_Scale_doc}, {"Shear", (PyCFunction)C_Matrix_Shear, METH_VARARGS | METH_CLASS, C_Matrix_Shear_doc}, + {"Diagonal", (PyCFunction)C_Matrix_Diagonal, METH_O | METH_CLASS, C_Matrix_Diagonal_doc}, {"Translation", (PyCFunction)C_Matrix_Translation, METH_O | METH_CLASS, |