[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42751] trunk/blender/source/blender/ python/mathutils: __str__ functions for other mathutils types

Campbell Barton ideasman42 at gmail.com
Tue Dec 20 04:38:08 CET 2011


Revision: 42751
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42751
Author:   campbellbarton
Date:     2011-12-20 03:37:55 +0000 (Tue, 20 Dec 2011)
Log Message:
-----------
__str__ functions for other mathutils types

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils.c
    trunk/blender/source/blender/python/mathutils/mathutils.h
    trunk/blender/source/blender/python/mathutils/mathutils_Color.c
    trunk/blender/source/blender/python/mathutils/mathutils_Euler.c
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
    trunk/blender/source/blender/python/mathutils/mathutils_Quaternion.c
    trunk/blender/source/blender/python/mathutils/mathutils_Vector.c

Modified: trunk/blender/source/blender/python/mathutils/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -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 @@
 	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 */
 

Modified: trunk/blender/source/blender/python/mathutils/mathutils.h
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.h	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils.h	2011-12-20 03:37:55 UTC (rev 42751)
@@ -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 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 */

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Color.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Color.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Color.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -32,6 +32,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define COLOR_SIZE 3
 
@@ -125,6 +126,21 @@
 	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 @@
 	&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

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Euler.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Euler.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Euler.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -36,6 +36,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define EULER_SIZE 3
 
@@ -317,6 +318,21 @@
 	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 @@
 	&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

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -1325,11 +1325,10 @@
 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 @@
 
 	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 @@
 	}
 	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)

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Quaternion.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Quaternion.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Quaternion.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -35,6 +35,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define QUAT_SIZE 4
 
@@ -493,6 +494,21 @@
 	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 @@
 	&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

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Vector.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Vector.c	2011-12-20 03:11:56 UTC (rev 42750)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Vector.c	2011-12-20 03:37:55 UTC (rev 42751)
@@ -35,6 +35,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_dynstr.h"
 
 #define MAX_DIMENSIONS 4
 
@@ -1171,6 +1172,29 @@
 	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 @@
 
 	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; */
 




More information about the Bf-blender-cvs mailing list