__str__ functions for other mathutils types
authorCampbell Barton <ideasman42@gmail.com>
Tue, 20 Dec 2011 03:37:55 +0000 (03:37 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 20 Dec 2011 03:37:55 +0000 (03:37 +0000)
source/blender/python/mathutils/mathutils.c
source/blender/python/mathutils/mathutils.h
source/blender/python/mathutils/mathutils_Color.c
source/blender/python/mathutils/mathutils_Euler.c
source/blender/python/mathutils/mathutils_Matrix.c
source/blender/python/mathutils/mathutils_Quaternion.c
source/blender/python/mathutils/mathutils_Vector.c

index 5587028d81291d3bd66785c26eecbd86467b2a2e..a656b0ab3e5ecae30e37e1e90db771faedecaac7 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 PyDoc_STRVAR(M_Mathutils_doc,
 "This module provides access to matrices, eulers, quaternions and vectors."
@@ -267,6 +268,18 @@ int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps)
        return 1;
 }
 
+/* dynstr as python string utility funcions, frees 'ds'! */
+PyObject *mathutils_dynstr_to_py(struct DynStr *ds)
+{
+       const int ds_len = BLI_dynstr_get_len(ds); /* space for \0 */
+       char *ds_buf     = PyMem_Malloc(ds_len + 1);
+       PyObject *ret;
+       BLI_dynstr_get_cstring_ex(ds, ds_buf);
+       BLI_dynstr_free(ds);
+       ret = PyUnicode_FromStringAndSize(ds_buf, ds_len);
+       PyMem_Free(ds_buf);
+       return ret;
+}
 
 /* Mathutils Callbacks */
 
index cd3548b9b825218757e32a4a985c1309047c9b34..2454e953949c25bcf4287bf2615202f011556d8c 100644 (file)
@@ -37,6 +37,8 @@
 
 /* Can cast different mathutils types to this, use for generic funcs */
 
+struct DynStr;
+
 extern char BaseMathObject_Wrapped_doc[];
 extern char BaseMathObject_Owner_doc[];
 
@@ -120,4 +122,7 @@ int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error
 
 int column_vector_multiplication(float rvec[4], VectorObject *vec, MatrixObject *mat);
 
+/* dynstr as python string utility funcions */
+PyObject *mathutils_dynstr_to_py(struct DynStr *ds);
+
 #endif /* MATHUTILS_H */
index e46dda7560f55a7d3bc3e11a7ab07714d526cda0..46486ec7f483f2e7c5b62462f5f87b65974422df 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define COLOR_SIZE 3
 
@@ -125,6 +126,21 @@ static PyObject *Color_repr(ColorObject * self)
        return ret;
 }
 
+static PyObject *Color_str(ColorObject * self)
+{
+       DynStr *ds;
+
+       if (BaseMath_ReadCallback(self) == -1)
+               return NULL;
+
+       ds= BLI_dynstr_new();
+
+       BLI_dynstr_appendf(ds, "<Color (r=%.4f, g=%.4f, b=%.4f) >",
+                          self->col[0], self->col[1], self->col[2]);
+
+       return mathutils_dynstr_to_py(ds); /* frees ds */
+}
+
 //------------------------tp_richcmpr
 //returns -1 execption, 0 false, 1 true
 static PyObject* Color_richcmpr(PyObject *a, PyObject *b, int op)
@@ -789,7 +805,7 @@ PyTypeObject color_Type = {
        &Color_AsMapping,                               //tp_as_mapping
        NULL,                                                   //tp_hash
        NULL,                                                   //tp_call
-       NULL,                                                   //tp_str
+       (reprfunc) Color_str,                   //tp_str
        NULL,                                                   //tp_getattro
        NULL,                                                   //tp_setattro
        NULL,                                                   //tp_as_buffer
index f44c0b82a2d4e05e054a4fe5691a93d0a63f7fac..b866311b398f918fb0fd1e42f4e05104c8d29269 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define EULER_SIZE 3
 
@@ -317,6 +318,21 @@ static PyObject *Euler_repr(EulerObject * self)
        return ret;
 }
 
+static PyObject *Euler_str(EulerObject * self)
+{
+       DynStr *ds;
+
+       if (BaseMath_ReadCallback(self) == -1)
+               return NULL;
+
+       ds= BLI_dynstr_new();
+
+       BLI_dynstr_appendf(ds, "<Euler (x=%.4f, y=%.4f, z=%.4f), order='%s' >",
+                          self->eul[0], self->eul[1], self->eul[2], euler_order_str(self));
+
+       return mathutils_dynstr_to_py(ds); /* frees ds */
+}
+
 static PyObject* Euler_richcmpr(PyObject *a, PyObject *b, int op)
 {
        PyObject *res;
@@ -635,7 +651,7 @@ PyTypeObject euler_Type = {
        &Euler_AsMapping,                               //tp_as_mapping
        NULL,                                                   //tp_hash
        NULL,                                                   //tp_call
-       NULL,                                                   //tp_str
+       (reprfunc) Euler_str,                   //tp_str
        NULL,                                                   //tp_getattro
        NULL,                                                   //tp_setattro
        NULL,                                                   //tp_as_buffer
index b98a6236719b114b723f4ed8e08d09084cb5aeb0..1ff2a893a50ff81309d5e22b036ea61fb851eeb5 100644 (file)
@@ -1325,11 +1325,10 @@ static PyObject *Matrix_repr(MatrixObject *self)
 static PyObject* Matrix_str(MatrixObject *self)
 {
        DynStr *ds;
-       char *ds_buf;
-       int ds_size;
 
-       int row, col, *maxsize;
-       PyObject *ret;
+       int maxsize[MATRIX_MAX_DIM];
+       int row, col;
+
        char dummy_buf[1];
 
        if (BaseMath_ReadCallback(self) == -1)
@@ -1337,8 +1336,6 @@ static PyObject* Matrix_str(MatrixObject *self)
 
        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;
@@ -1358,15 +1355,7 @@ static PyObject* Matrix_str(MatrixObject *self)
        }
        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;
+       return mathutils_dynstr_to_py(ds); /* frees ds */
 }
 
 static PyObject* Matrix_richcmpr(PyObject *a, PyObject *b, int op)
index 7ab9b7b07138094a4042f6aa520fc1a04cc779f0..27169e82b0fa89f0b7536d4725bf90d4fe33cdf7 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define QUAT_SIZE 4
 
@@ -493,6 +494,21 @@ static PyObject *Quaternion_repr(QuaternionObject *self)
        return ret;
 }
 
+static PyObject *Quaternion_str(QuaternionObject *self)
+{
+       DynStr *ds;
+
+       if (BaseMath_ReadCallback(self) == -1)
+               return NULL;
+
+       ds= BLI_dynstr_new();
+
+       BLI_dynstr_appendf(ds, "<Quaternion (w=%.4f, x=%.4f, y=%.4f, z=%.4f) >",
+                          self->quat[0], self->quat[1], self->quat[2], self->quat[3]);
+
+       return mathutils_dynstr_to_py(ds); /* frees ds */
+}
+
 static PyObject* Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
 {
        PyObject *res;
@@ -1167,7 +1183,7 @@ PyTypeObject quaternion_Type = {
        &Quaternion_AsMapping,                  //tp_as_mapping
        NULL,                                                           //tp_hash
        NULL,                                                           //tp_call
-       NULL,                                                           //tp_str
+       (reprfunc) Quaternion_str,                      //tp_str
        NULL,                                                           //tp_getattro
        NULL,                                                           //tp_setattro
        NULL,                                                           //tp_as_buffer
index aedf60e71918f11ab3ba1266a0855df1c5172a26..6249a340c3f9550f360f095525ca741b52b7bc08 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define MAX_DIMENSIONS 4
 
@@ -1171,6 +1172,29 @@ static PyObject *Vector_repr(VectorObject *self)
        return ret;
 }
 
+static PyObject *Vector_str(VectorObject *self)
+{
+       int i;
+
+       DynStr *ds;
+
+       if (BaseMath_ReadCallback(self) == -1)
+               return NULL;
+
+       ds= BLI_dynstr_new();
+
+       BLI_dynstr_append(ds, "<Vector (");
+
+       for (i = 0; i < self->size; i++) {
+               BLI_dynstr_appendf(ds, i ? ", %.4f" : "%.4f", self->vec[i]);
+       }
+
+       BLI_dynstr_append(ds, ") >");
+
+       return mathutils_dynstr_to_py(ds); /* frees ds */
+}
+
+
 /* Sequence Protocol */
 /* sequence length len(vector) */
 static int Vector_len(VectorObject *self)
@@ -2715,7 +2739,7 @@ PyTypeObject vector_Type = {
 
        NULL,                       /* hashfunc tp_hash; */
        NULL,                       /* ternaryfunc tp_call; */
-       NULL,                       /* reprfunc tp_str; */
+       (reprfunc)Vector_str,       /* reprfunc tp_str; */
        NULL,                       /* getattrofunc tp_getattro; */
        NULL,                       /* setattrofunc tp_setattro; */