change mathutils 'repr' functions to closer match input
authorCampbell Barton <ideasman42@gmail.com>
Mon, 19 Apr 2010 22:02:53 +0000 (22:02 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 19 Apr 2010 22:02:53 +0000 (22:02 +0000)
source/blender/python/generic/mathutils_color.c
source/blender/python/generic/mathutils_euler.c
source/blender/python/generic/mathutils_matrix.c
source/blender/python/generic/mathutils_quat.c
source/blender/python/generic/mathutils_vector.c
source/blender/python/intern/bpy_rna.c

index d44515d1551cb6576a92f35f440f9ed584e58741..564afbdd038ec7bcdcc411c2c02f50eb38ef9573 100644 (file)
@@ -104,13 +104,22 @@ static PyObject *Color_copy(ColorObject * self, PyObject *args)
 //print the object to screen
 static PyObject *Color_repr(ColorObject * self)
 {
-       char str[64];
+       PyObject *r, *g, *b, *ret;
 
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       sprintf(str, "[%.6f, %.6f, %.6f](color)", self->col[0], self->col[1], self->col[2]);
-       return PyUnicode_FromString(str);
+       r= PyFloat_FromDouble(self->col[0]);
+       g= PyFloat_FromDouble(self->col[1]);
+       b= PyFloat_FromDouble(self->col[2]);
+
+       ret= PyUnicode_FromFormat("Color(%R, %R, %R)", r, g, b);
+
+       Py_DECREF(r);
+       Py_DECREF(g);
+       Py_DECREF(b);
+
+       return ret;
 }
 //------------------------tp_richcmpr
 //returns -1 execption, 0 false, 1 true
index 04f80dd41160a6375315ede44988abb42f1560d2..c347478bce8dc2f565f6ec93728a5332212b10d1 100644 (file)
@@ -314,13 +314,22 @@ static PyObject *Euler_copy(EulerObject * self, PyObject *args)
 //print the object to screen
 static PyObject *Euler_repr(EulerObject * self)
 {
-       char str[64];
+       PyObject *x, *y, *z, *ret;
 
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       sprintf(str, "[%.6f, %.6f, %.6f](euler)", self->eul[0], self->eul[1], self->eul[2]);
-       return PyUnicode_FromString(str);
+       x= PyFloat_FromDouble(self->eul[0]);
+       y= PyFloat_FromDouble(self->eul[1]);
+       z= PyFloat_FromDouble(self->eul[2]);
+
+       ret= PyUnicode_FromFormat("Euler(%R, %R, %R)", x, y, z);
+
+       Py_DECREF(x);
+       Py_DECREF(y);
+       Py_DECREF(z);
+
+       return ret;
 }
 //------------------------tp_richcmpr
 //returns -1 execption, 0 false, 1 true
index 0ce5641b07cb9f09a45825df284f3e57a3f0eedb..ddc14f83218b6deae320e7d44c068c15ba8f6f5a 100644 (file)
@@ -723,27 +723,25 @@ PyObject *Matrix_copy(MatrixObject * self)
 static PyObject *Matrix_repr(MatrixObject * self)
 {
        int x, y;
-       char buffer[48], str[1024];
+       char str[1024]="Matrix((", *str_p;
 
        if(!BaseMath_ReadCallback(self))
                return NULL;
-       
-       BLI_strncpy(str,"",1024);
+
+       str_p= &str[8];
+
        for(x = 0; x < self->colSize; x++){
-               sprintf(buffer, "[");
-               strcat(str,buffer);
                for(y = 0; y < (self->rowSize - 1); y++) {
-                       sprintf(buffer, "%.6f, ", self->matrix[y][x]);
-                       strcat(str,buffer);
+                       str_p += sprintf(str_p, "%f, ", self->matrix[y][x]);
                }
                if(x < (self->colSize-1)){
-                       sprintf(buffer, "%.6f](matrix [row %d])\n", self->matrix[y][x], x);
-                       strcat(str,buffer);
-               }else{
-                       sprintf(buffer, "%.6f](matrix [row %d])", self->matrix[y][x], x);
-                       strcat(str,buffer);
+                       str_p += sprintf(str_p, "%f), (", self->matrix[y][x]);
+               }
+               else{
+                       str_p += sprintf(str_p, "%f)", self->matrix[y][x]);
                }
        }
+       strcat(str_p, ")");
 
        return PyUnicode_FromString(str);
 }
index 38fb2ae49039ffdee34517a0453b1032c9ab9af2..3ee44b0378bc6f01cacaff98519aa9f0ae0396a9 100644 (file)
@@ -351,13 +351,24 @@ static PyObject *Quaternion_copy(QuaternionObject * self)
 //print the object to screen
 static PyObject *Quaternion_repr(QuaternionObject * self)
 {
-       char str[64];
+       PyObject *w, *x, *y, *z, *ret;
 
        if(!BaseMath_ReadCallback(self))
                return NULL;
 
-       sprintf(str, "[%.6f, %.6f, %.6f, %.6f](quaternion)", self->quat[0], self->quat[1], self->quat[2], self->quat[3]);
-       return PyUnicode_FromString(str);
+       w= PyFloat_FromDouble(self->quat[0]);
+       x= PyFloat_FromDouble(self->quat[1]);
+       y= PyFloat_FromDouble(self->quat[2]);
+       z= PyFloat_FromDouble(self->quat[3]);
+
+       ret= PyUnicode_FromFormat("Quaternion(%R, %R, %R, %R)", w, x, y, z);
+
+       Py_DECREF(w);
+       Py_DECREF(x);
+       Py_DECREF(y);
+       Py_DECREF(z);
+
+       return ret;
 }
 //------------------------tp_richcmpr
 //returns -1 execption, 0 false, 1 true
index e013a3583930f4c3646bcb90d25e506b7e8277ff..1f51724c731af3b179e7c1044ed110e8169c945f 100644 (file)
@@ -704,26 +704,33 @@ static PyObject *Vector_copy(VectorObject * self)
   print the object to screen */
 static PyObject *Vector_repr(VectorObject * self)
 {
+       PyObject *axis[4], *ret;
        int i;
-       char buffer[48], str[1024];
 
        if(!BaseMath_ReadCallback(self))
                return NULL;
-       
-       BLI_strncpy(str,"[",1024);
-       for(i = 0; i < self->size; i++){
-               if(i < (self->size - 1)){
-                       sprintf(buffer, "%.6f, ", self->vec[i]);
-                       strcat(str,buffer);
-               }else{
-                       sprintf(buffer, "%.6f", self->vec[i]);
-                       strcat(str,buffer);
-               }
+
+       for(i = 0; i < self->size; i++)
+               axis[i] = PyFloat_FromDouble(self->vec[i]);
+
+       switch(self->size) {
+       case 2:
+               ret= PyUnicode_FromFormat("Vector(%R, %R)", axis[0], axis[1]);
+               break;
+       case 3:
+               ret= PyUnicode_FromFormat("Vector(%R, %R, %R)", axis[0], axis[1], axis[2]);
+               break;
+       case 4:
+               ret= PyUnicode_FromFormat("Vector(%R, %R, %R, %R)", axis[0], axis[1], axis[2], axis[3]);
+               break;
        }
-       strcat(str, "](vector)");
 
-       return PyUnicode_FromString(str);
+       for(i = 0; i < self->size; i++)
+               Py_DECREF(axis[i]);
+
+       return ret;
 }
+
 /*---------------------SEQUENCE PROTOCOLS------------------------
   ----------------------------len(object)------------------------
   sequence length*/
index 47c208608a41a8c598b72b8162eaef13839d707a..7a0bde4fddb0e050f8df8e2484b694d5ede8b960 100644 (file)
@@ -1346,7 +1346,7 @@ static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, in
                return -1;
        }
 
-       if(!(value=PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: type is not a sequence"))) {
+       if(!(value=PySequence_Fast(value_orig, "bpy_prop_array[slice] = value: assignment is not a sequence type"))) {
                return -1;
        }