diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-12-20 07:37:55 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-12-20 07:37:55 +0400 |
commit | 3d8ee287509f3a0dac535914a7193397217b006c (patch) | |
tree | 2a2fc72b6151fd21bef9c29248ee2b59da443044 /source | |
parent | 32b23b9f746b9bec4023da1d2ef6085fbbde9ae7 (diff) |
__str__ functions for other mathutils types
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/mathutils/mathutils.c | 13 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils.h | 5 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Color.c | 18 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Euler.c | 18 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 19 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Quaternion.c | 18 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_Vector.c | 26 |
7 files changed, 98 insertions, 19 deletions
diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c index 5587028d812..a656b0ab3e5 100644 --- a/source/blender/python/mathutils/mathutils.c +++ b/source/blender/python/mathutils/mathutils.c @@ -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 */ diff --git a/source/blender/python/mathutils/mathutils.h b/source/blender/python/mathutils/mathutils.h index cd3548b9b82..2454e953949 100644 --- a/source/blender/python/mathutils/mathutils.h +++ b/source/blender/python/mathutils/mathutils.h @@ -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 */ diff --git a/source/blender/python/mathutils/mathutils_Color.c b/source/blender/python/mathutils/mathutils_Color.c index e46dda7560f..46486ec7f48 100644 --- a/source/blender/python/mathutils/mathutils_Color.c +++ b/source/blender/python/mathutils/mathutils_Color.c @@ -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 diff --git a/source/blender/python/mathutils/mathutils_Euler.c b/source/blender/python/mathutils/mathutils_Euler.c index f44c0b82a2d..b866311b398 100644 --- a/source/blender/python/mathutils/mathutils_Euler.c +++ b/source/blender/python/mathutils/mathutils_Euler.c @@ -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 diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index b98a6236719..1ff2a893a50 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -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) diff --git a/source/blender/python/mathutils/mathutils_Quaternion.c b/source/blender/python/mathutils/mathutils_Quaternion.c index 7ab9b7b0713..27169e82b0f 100644 --- a/source/blender/python/mathutils/mathutils_Quaternion.c +++ b/source/blender/python/mathutils/mathutils_Quaternion.c @@ -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 diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c index aedf60e7191..6249a340c3f 100644 --- a/source/blender/python/mathutils/mathutils_Vector.c +++ b/source/blender/python/mathutils/mathutils_Vector.c @@ -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; */ |