[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42748] trunk/blender/source/blender: patch [#29651] Add a __str__ Method to Matutils Matrices so print(matrix) Shows Columns as Columns

Campbell Barton ideasman42 at gmail.com
Tue Dec 20 03:54:38 CET 2011


Revision: 42748
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42748
Author:   campbellbarton
Date:     2011-12-20 02:54:25 +0000 (Tue, 20 Dec 2011)
Log Message:
-----------
patch [#29651] Add a __str__ Method to Matutils Matrices so print(matrix) Shows Columns as Columns
from Andrew Hale

converted from python string formatting to using BLI_dynstr

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

Modified: trunk/blender/source/blender/blenlib/BLI_dynstr.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_dynstr.h	2011-12-20 02:04:35 UTC (rev 42747)
+++ trunk/blender/source/blender/blenlib/BLI_dynstr.h	2011-12-20 02:54:25 UTC (rev 42748)
@@ -100,6 +100,17 @@
 	 */
 char*	BLI_dynstr_get_cstring			(DynStr *ds);
 
+/**
+ * Get a DynStr's contents as a c-string.
+ * <i> The str argument must be allocated to be at
+ * least the size of BLI_dynstr_get_len(ds). </i>
+ *
+ * @param ds The DynStr of interest.
+ * @param str The string to fill.
+ * @return The contents of @a ds as a c-string.
+ */
+void	BLI_dynstr_get_cstring_ex		(DynStr *ds, char *str);
+
 	/**
 	 * Free the DynStr
 	 * 

Modified: trunk/blender/source/blender/blenlib/intern/BLI_dynstr.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_dynstr.c	2011-12-20 02:04:35 UTC (rev 42747)
+++ trunk/blender/source/blender/blenlib/intern/BLI_dynstr.c	2011-12-20 02:54:25 UTC (rev 42748)
@@ -226,11 +226,11 @@
 	return ds->curlen;
 }
 
-char *BLI_dynstr_get_cstring(DynStr *ds)
+void BLI_dynstr_get_cstring_ex(DynStr *ds, char *rets)
 {
-	char *s, *rets= MEM_mallocN(ds->curlen+1, "dynstr_cstring");
+	char *s;
 	DynStrElem *dse;
-	
+
 	for (s= rets, dse= ds->elems; dse; dse= dse->next) {
 		int slen= strlen(dse->str);
 
@@ -239,7 +239,12 @@
 		s+= slen;
 	}
 	rets[ds->curlen]= '\0';
-	
+}
+
+char *BLI_dynstr_get_cstring(DynStr *ds)
+{
+	char *rets= MEM_mallocN(ds->curlen+1, "dynstr_cstring");
+	BLI_dynstr_get_cstring_ex(ds, rets);
 	return rets;
 }
 

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-20 02:04:35 UTC (rev 42747)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-20 02:54:25 UTC (rev 42748)
@@ -35,6 +35,8 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_string.h"
+#include "BLI_dynstr.h"
 
 static PyObject *Matrix_copy(MatrixObject *self);
 static int Matrix_ass_slice(MatrixObject *self, int begin, int end, PyObject *value);
@@ -1320,6 +1322,53 @@
 	return NULL;
 }
 
+static PyObject* Matrix_str(MatrixObject *self)
+{
+	DynStr *ds;
+	char *ds_buf;
+	int ds_size;
+
+	int row, col, *maxsize;
+	PyObject *ret;
+	char dummy_buf[1];
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return NULL;
+
+	ds= BLI_dynstr_new();
+
+	maxsize= PyMem_Malloc(self->row_size * sizeof(int));
+
+	/* First determine the maximum width for each column */
+	for (col = 0; col < self->row_size; col++) {
+		maxsize[col]= 0;
+		for (row = 0; row < self->col_size; row++) {
+			int size= BLI_snprintf(dummy_buf, sizeof(dummy_buf), "%.4f", MATRIX_ITEM(self, col, row));
+			maxsize[col]= MAX2(maxsize[col], size);
+		}
+	}
+
+	/* Now write the unicode string to be printed */
+	BLI_dynstr_appendf(ds, "<Matrix %dx%d (", self->col_size, self->row_size);
+	for (row = 0; row < self->col_size; row++) {
+		for (col = 0; col < self->row_size; col++) {
+			BLI_dynstr_appendf(ds, col ? ", %*.4f" : "%*.4f", maxsize[col], MATRIX_ITEM(self, col, row));
+		}
+		BLI_dynstr_append(ds, row + 1 != self->col_size ? ")\n             " : ")");
+	}
+	BLI_dynstr_append(ds, " >");
+
+	ds_size= BLI_dynstr_get_len(ds) + 1; /* space for \n */
+	ds_buf= PyMem_Malloc(ds_size);
+	BLI_dynstr_get_cstring_ex(ds, ds_buf);
+	BLI_dynstr_free(ds);
+
+	PyMem_Free(maxsize);
+	ret= PyUnicode_FromStringAndSize(ds_buf, ds_size);
+	PyMem_Free(ds_buf);
+	return ret;
+}
+
 static PyObject* Matrix_richcmpr(PyObject *a, PyObject *b, int op)
 {
 	PyObject *res;
@@ -1906,7 +1955,7 @@
 	&Matrix_AsMapping,					/*tp_as_mapping*/
 	NULL,								/*tp_hash*/
 	NULL,								/*tp_call*/
-	NULL,								/*tp_str*/
+	(reprfunc) Matrix_str,				/*tp_str*/
 	NULL,								/*tp_getattro*/
 	NULL,								/*tp_setattro*/
 	NULL,								/*tp_as_buffer*/




More information about the Bf-blender-cvs mailing list