[Bf-blender-cvs] [ec1c9e32587] master: Python API: add a Matrix.Diagonal constructor to mathutils.

Alexander Gavrilov noreply at git.blender.org
Sun May 19 14:04:42 CEST 2019


Commit: ec1c9e325874eab4d75d0240473da2ca3c858a46
Author: Alexander Gavrilov
Date:   Sun May 19 14:52:11 2019 +0300
Branches: master
https://developer.blender.org/rBec1c9e325874eab4d75d0240473da2ca3c858a46

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

===================================================================

M	source/blender/python/mathutils/mathutils_Matrix.c

===================================================================

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,



More information about the Bf-blender-cvs mailing list