[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42813] trunk/blender/source/blender/ python/mathutils: Matrix.translation wrapper vector, continent accessing to matrix[3][0:3].

Campbell Barton ideasman42 at gmail.com
Thu Dec 22 00:12:20 CET 2011


Revision: 42813
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42813
Author:   campbellbarton
Date:     2011-12-21 23:12:16 +0000 (Wed, 21 Dec 2011)
Log Message:
-----------
Matrix.translation wrapper vector, continent accessing to matrix[3][0:3].

this is a part of patch 29534, being applied separately

from patch [#29534] Change Matrix Representation and Access in Python to Conform with Standard Notation
by Andrew Hale (trumanblending)

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils.c
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h

Modified: trunk/blender/source/blender/python/mathutils/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-21 22:56:06 UTC (rev 42812)
+++ trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-21 23:12:16 UTC (rev 42813)
@@ -456,6 +456,7 @@
 	Py_INCREF(item);
 
 	mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb);
+	mathutils_matrix_column_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_column_cb);
 
 	return submodule;
 }

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-21 22:56:06 UTC (rev 42812)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-21 23:12:16 UTC (rev 42813)
@@ -115,6 +115,87 @@
 };
 /* matrix vector callbacks, this is so you can do matrix[i][j] = val  */
 
+/* matrix row callbacks */
+int mathutils_matrix_column_cb_index= -1;
+
+static int mathutils_matrix_column_check(BaseMathObject *bmo)
+{
+	MatrixObject *self= (MatrixObject *)bmo->cb_user;
+	return BaseMath_ReadCallback(self);
+}
+
+static int mathutils_matrix_column_get(BaseMathObject *bmo, int col)
+{
+	MatrixObject *self= (MatrixObject *)bmo->cb_user;
+	int num_row;
+	int row;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	/* for 'translation' size will always be '3' even on 4x4 vec */
+	num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size);
+
+	for (row = 0; row < num_row; row++) {
+		bmo->data[row] = MATRIX_ITEM(self, row, col);
+	}
+
+	return 0;
+}
+
+static int mathutils_matrix_column_set(BaseMathObject *bmo, int col)
+{
+	MatrixObject *self= (MatrixObject *)bmo->cb_user;
+	int num_row;
+	int row;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	/* for 'translation' size will always be '3' even on 4x4 vec */
+	num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size);
+
+	for (row = 0; row < num_row; row++) {
+		MATRIX_ITEM(self, row, col) = bmo->data[row];
+	}
+
+	(void)BaseMath_WriteCallback(self);
+	return 0;
+}
+
+static int mathutils_matrix_column_get_index(BaseMathObject *bmo, int col, int row)
+{
+	MatrixObject *self= (MatrixObject *)bmo->cb_user;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	bmo->data[row]= MATRIX_ITEM(self, row, col);
+	return 0;
+}
+
+static int mathutils_matrix_column_set_index(BaseMathObject *bmo, int col, int row)
+{
+	MatrixObject *self= (MatrixObject *)bmo->cb_user;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	MATRIX_ITEM(self, row, col) = bmo->data[row];
+
+	(void)BaseMath_WriteCallback(self);
+	return 0;
+}
+
+Mathutils_Callback mathutils_matrix_column_cb = {
+	mathutils_matrix_column_check,
+	mathutils_matrix_column_get,
+	mathutils_matrix_column_set,
+	mathutils_matrix_column_get_index,
+	mathutils_matrix_column_set_index
+};
+/* matrix column callbacks, this is so you can do matrix.translation = Vector()  */
+
 //----------------------------------mathutils.Matrix() -----------------
 //mat is a 1D array of floats - row[0][0], row[0][1], row[1][0], etc.
 //create a new matrix type
@@ -1813,6 +1894,51 @@
 	return PyLong_FromLong((long) self->num_row);
 }
 
+static PyObject *Matrix_translation_get(MatrixObject *self, void *UNUSED(closure))
+{
+	PyObject *ret;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return NULL;
+
+	/*must be 4x4 square matrix*/
+	if (self->num_row != 4 || self->num_col != 4) {
+		PyErr_SetString(PyExc_AttributeError,
+		                "Matrix.translation: "
+		                "inappropriate matrix size, must be 4x4");
+		return NULL;
+	}
+
+	ret = (PyObject *)Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_column_cb_index, 3);
+
+	return ret;
+}
+
+static int Matrix_translation_set(MatrixObject *self, PyObject *value, void *UNUSED(closure))
+{
+	float tvec[3];
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	/*must be 4x4 square matrix*/
+	if (self->num_row != 4 || self->num_col != 4) {
+		PyErr_SetString(PyExc_AttributeError,
+		                "Matrix.translation: "
+		                "inappropriate matrix size, must be 4x4");
+		return -1;
+	}
+
+	if ((mathutils_array_parse(tvec, 3, 3, value, "Matrix.translation")) == -1)
+		return -1;
+
+	copy_v3_v3(((float (*)[4])self->matrix)[3], tvec);
+
+	(void)BaseMath_WriteCallback(self);
+
+	return 0;
+}
+
 static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closure))
 {
 	float mat[3][3];
@@ -1876,6 +2002,7 @@
 	{(char *)"row_size", (getter)Matrix_getRowSize, (setter)NULL, (char *)"The row size of the matrix (readonly).\n\n:type: int", NULL},
 	{(char *)"col_size", (getter)Matrix_getColSize, (setter)NULL, (char *)"The column size of the matrix (readonly).\n\n:type: int", NULL},
 	{(char *)"median_scale", (getter)Matrix_median_scale_get, (setter)NULL, (char *)"The average scale applied to each axis (readonly).\n\n:type: float", NULL},
+	{(char *)"translation", (getter)Matrix_translation_get, (setter)Matrix_translation_set, (char *)"The translation component of the matrix.\n\n:type: Vector", NULL},
 	{(char *)"is_negative", (getter)Matrix_is_negative_get, (setter)NULL, (char *)"True if this matrix results in a negative scale, 3x3 and 4x4 only, (readonly).\n\n:type: bool", NULL},
 	{(char *)"is_orthogonal", (getter)Matrix_is_orthogonal_get, (setter)NULL, (char *)"True if this matrix is orthogonal, 3x3 and 4x4 only, (readonly).\n\n:type: bool", NULL},
 	{(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL},

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h	2011-12-21 22:56:06 UTC (rev 42812)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h	2011-12-21 23:12:16 UTC (rev 42813)
@@ -71,7 +71,9 @@
 PyObject *Matrix_CreatePyObject_cb(PyObject *user, int num_col, int num_row, int cb_type, int cb_subtype);
 
 extern int mathutils_matrix_vector_cb_index;
+extern int mathutils_matrix_column_cb_index;
 extern struct Mathutils_Callback mathutils_matrix_vector_cb;
+extern struct Mathutils_Callback mathutils_matrix_column_cb;
 
 void matrix_as_3x3(float mat[3][3], MatrixObject *self);
 




More information about the Bf-blender-cvs mailing list