__str__ functions for other mathutils types

This commit is contained in:
2011-12-20 03:37:55 +00:00
parent 32b23b9f74
commit 3d8ee28750
7 changed files with 98 additions and 19 deletions

View 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 */

View 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 */

View 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

View 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

View 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)

View 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

View 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; */