[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53048] trunk/blender/source/blender/ python/mathutils/mathutils_Matrix.c: add Matrix.normalized(), handy for transforming normals.

Brecht Van Lommel brechtvanlommel at pandora.be
Sun Dec 16 09:04:53 CET 2012


In general, normals should transformed with the inverse transpose
matrix, then renormalized afterwards, as far as I know that's the only
way it works correct with non-uniform object scale. I don't think this
function should be documented as useful for transforming normals?

Brecht.

On Sun, Dec 16, 2012 at 5:05 AM, Campbell Barton <ideasman42 at gmail.com> wrote:
> Revision: 53048
>           http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53048
> Author:   campbellbarton
> Date:     2012-12-16 04:05:16 +0000 (Sun, 16 Dec 2012)
> Log Message:
> -----------
> add Matrix.normalized(), handy for transforming normals.
>
> Modified Paths:
> --------------
>     trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
>
> Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
> ===================================================================
> --- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c    2012-12-16 02:53:28 UTC (rev 53047)
> +++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c    2012-12-16 04:05:16 UTC (rev 53048)
> @@ -1533,6 +1533,53 @@
>         return matrix__apply_to_copy((PyNoArgsFunction)Matrix_transpose, self);
>  }
>
> +/*---------------------------matrix.normalize() ------------------*/
> +PyDoc_STRVAR(Matrix_normalize_doc,
> +".. method:: normalize()\n"
> +"\n"
> +"   Normalize each of the columns of the matrix (useful for transforming unit length normals).\n"
> +);
> +static PyObject *Matrix_normalize(MatrixObject *self)
> +{
> +       if (BaseMath_ReadCallback(self) == -1)
> +               return NULL;
> +
> +       if (self->num_col != self->num_row) {
> +               PyErr_SetString(PyExc_ValueError,
> +                               "Matrix.normalize(): "
> +                               "only square matrices are supported");
> +               return NULL;
> +       }
> +
> +       if (self->num_col == 3) {
> +               normalize_m3((float (*)[3])self->matrix);
> +       }
> +       else if (self->num_col == 4) {
> +               normalize_m4((float (*)[4])self->matrix);
> +       }
> +       else {
> +               PyErr_SetString(PyExc_ValueError,
> +                               "Matrix.normalize(): "
> +                               "can only use a 3x3 or 4x4 matrix");
> +       }
> +
> +       (void)BaseMath_WriteCallback(self);
> +       Py_RETURN_NONE;
> +}
> +
> +PyDoc_STRVAR(Matrix_normalized_doc,
> +".. method:: normalized()\n"
> +"\n"
> +"   Return a row normalized matrix\n"
> +"\n"
> +"   :return: a row normalized matrix\n"
> +"   :rtype: :class:`Matrix`\n"
> +);
> +static PyObject *Matrix_normalized(MatrixObject *self)
> +{
> +       return matrix__apply_to_copy((PyNoArgsFunction)Matrix_normalize, self);
> +}
> +
>  /*---------------------------matrix.zero() -----------------------*/
>  PyDoc_STRVAR(Matrix_zero_doc,
>  ".. method:: zero()\n"
> @@ -2371,6 +2418,8 @@
>         /* operate on original or copy */
>         {"transpose", (PyCFunction) Matrix_transpose, METH_NOARGS, Matrix_transpose_doc},
>         {"transposed", (PyCFunction) Matrix_transposed, METH_NOARGS, Matrix_transposed_doc},
> +       {"normalize", (PyCFunction) Matrix_normalize, METH_NOARGS, Matrix_normalize_doc},
> +       {"normalized", (PyCFunction) Matrix_normalized, METH_NOARGS, Matrix_normalized_doc},
>         {"invert", (PyCFunction) Matrix_invert, METH_NOARGS, Matrix_invert_doc},
>         {"inverted", (PyCFunction) Matrix_inverted, METH_NOARGS, Matrix_inverted_doc},
>         {"adjugate", (PyCFunction) Matrix_adjugate, METH_NOARGS, Matrix_adjugate_doc},
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs


More information about the Bf-committers mailing list