Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-12-20 06:54:25 +0400
committerCampbell Barton <ideasman42@gmail.com>2011-12-20 06:54:25 +0400
commit553cf289a7aab9a2a53331d623fa2cfa475b7728 (patch)
tree667611c9d5427d6c858a9bb57b3006f2aaf066fc /source
parenta7a6049978fc0789bce2064d6746c87277793cce (diff)
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
Diffstat (limited to 'source')
-rw-r--r--source/blender/blenlib/BLI_dynstr.h11
-rw-r--r--source/blender/blenlib/intern/BLI_dynstr.c13
-rw-r--r--source/blender/python/mathutils/mathutils_Matrix.c51
3 files changed, 70 insertions, 5 deletions
diff --git a/source/blender/blenlib/BLI_dynstr.h b/source/blender/blenlib/BLI_dynstr.h
index e27a315a023..861da665a30 100644
--- a/source/blender/blenlib/BLI_dynstr.h
+++ b/source/blender/blenlib/BLI_dynstr.h
@@ -100,6 +100,17 @@ int BLI_dynstr_get_len (DynStr *ds);
*/
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
*
diff --git a/source/blender/blenlib/intern/BLI_dynstr.c b/source/blender/blenlib/intern/BLI_dynstr.c
index 349bc3492e7..ad52de180aa 100644
--- a/source/blender/blenlib/intern/BLI_dynstr.c
+++ b/source/blender/blenlib/intern/BLI_dynstr.c
@@ -226,11 +226,11 @@ int BLI_dynstr_get_len(DynStr *ds)
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 @@ char *BLI_dynstr_get_cstring(DynStr *ds)
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;
}
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c
index 44799bb7943..b98a6236719 100644
--- a/source/blender/python/mathutils/mathutils_Matrix.c
+++ b/source/blender/python/mathutils/mathutils_Matrix.c
@@ -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 @@ static PyObject *Matrix_repr(MatrixObject *self)
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 @@ PyTypeObject matrix_Type = {
&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*/