added support for doubles to the id property code, and made the python code use them...
authorJoseph Eagar <joeedh@gmail.com>
Thu, 24 Jul 2008 19:22:17 +0000 (19:22 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Thu, 24 Jul 2008 19:22:17 +0000 (19:22 +0000)
source/blender/blenkernel/BKE_idprop.h
source/blender/blenkernel/intern/idprop.c
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/makesdna/DNA_ID.h
source/blender/python/api2_2x/IDProp.c

index 2d7d0e9286fceda6c1a1c8c83a6d7ac3ebf66da5..2274c54ad3bdea8a58207d1b4ac33e0901c5fc9c 100644 (file)
@@ -46,6 +46,7 @@ struct ID;
 typedef union {
        int i;
        float f;
+       double d;
        char *str;
        struct ID *id;
        struct {
index 2ef2f3a1b770ba095633b7ed5707a39811e5ffdc..b16f52571f63bbedb006f52154537ebdfa86ba29 100644 (file)
@@ -54,7 +54,8 @@ static char idp_size_table[] = {
        sizeof(float)*16, /*Matrix type, deprecated*/
        0, /*arrays don't have a fixed size*/
        sizeof(ListBase), /*Group type*/
-       sizeof(void*)
+       sizeof(void*),
+       sizeof(double)
 };
 
 
@@ -365,10 +366,14 @@ IDProperty *IDP_New(int type, IDPropertyTemplate val, char *name)
                        prop = MEM_callocN(sizeof(IDProperty), "IDProperty float");
                        *(float*)&prop->data.val = val.f;
                        break;
+               case IDP_DOUBLE:
+                       prop = MEM_callocN(sizeof(IDProperty), "IDProperty float");
+                       *(double*)&prop->data.val = val.d;
+                       break;          
                case IDP_ARRAY:
                {
-                       /*for now, we only support float and int arrays*/
-                       if (val.array.type == IDP_FLOAT || val.array.type == IDP_INT) {
+                       /*for now, we only support float and int and double arrays*/
+                       if (val.array.type == IDP_FLOAT || val.array.type == IDP_INT || val.array.type == IDP_DOUBLE) {
                                prop = MEM_callocN(sizeof(IDProperty), "IDProperty array");
                                prop->len = prop->totallen = val.array.len;
                                prop->subtype = val.array.type;
@@ -411,6 +416,10 @@ IDProperty *IDP_New(int type, IDPropertyTemplate val, char *name)
 
        prop->type = type;
        strncpy(prop->name, name, MAX_IDPROP_NAME);
+       
+       /*security null byte*/
+       prop->name[MAX_IDPROP_NAME-1] = 0;
+       
        return prop;
 }
 
index a93f1d91c77aa497ebfcc81cd5cfc75ba6ca742c..ad004dd5c8216509e59c967dec8cadf947351978 100644 (file)
@@ -1347,8 +1347,14 @@ void IDP_DirectLinkArray(IDProperty *prop, int switch_endian, void *fd)
        prop->data.pointer = newdataadr(fd, prop->data.pointer);
 
        if (switch_endian) {
-               for (i=0; i<prop->len; i++) {
-                       SWITCH_INT(((int*)prop->data.pointer)[i]);
+               if (prop->subtype != IDP_DOUBLE) {
+                       for (i=0; i<prop->len; i++) {
+                               SWITCH_INT(((int*)prop->data.pointer)[i]);
+                       }
+               } else {
+                       for (i=0; i<prop->len; i++) {
+                               SWITCH_LONGINT(((double*)prop->data.pointer)[i]);
+                       }
                }
        }
 }
@@ -1384,6 +1390,24 @@ void IDP_DirectLinkProperty(IDProperty *prop, int switch_endian, void *fd)
                        break;
                case IDP_ARRAY:
                        IDP_DirectLinkArray(prop, switch_endian, fd);
+                       break;
+               case IDP_DOUBLE:
+                       /*erg, stupid doubles.  since I'm storing them
+                        in the same field as int val; val2 in the
+                        IDPropertyData struct, they have to deal with
+                        endianness specifically
+                        
+                        in theory, val and val2 would've already been swapped
+                        if switch_endian is true, so we have to first unswap
+                        them then reswap them as a single 64-bit entity.
+                        */
+                       
+                       if (switch_endian) {
+                               SWITCH_INT(prop->data.val);
+                               SWITCH_INT(prop->data.val2);
+                               SWITCH_LONGINT(prop->data.val);
+                       }
+                       
                        break;
        }
 }
index b59dd851dfe462b4bcb8e3394a0ca905ff3a2257..73abf362d12d9d0875c8660ac76f01f488fab08b 100644 (file)
@@ -533,6 +533,7 @@ static void write_particlesettings(WriteData *wd, ListBase *idbase)
                if(part->id.us>0 || wd->current) {
                        /* write LibData */
                        writestruct(wd, ID_PA, "ParticleSettings", 1, part);
+                       if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
                        writestruct(wd, DATA, "PartDeflect", 1, part->pd);
                }
                part= part->id.next;
index 60050ea010ec4d84158f24dc7c562e22a4318d0a..3054e038ba2b114bc67937c499113001a11642a5 100644 (file)
@@ -46,7 +46,7 @@ struct ID;
 typedef struct IDPropertyData {
        void *pointer;
        ListBase group;
-       int val, pad;
+       int val, val2; /*note, we actually fit a double into these two ints*/
 } IDPropertyData;
 
 typedef struct IDProperty {
@@ -77,6 +77,7 @@ typedef struct IDProperty {
 /*the ID link property type hasn't been implemented yet, this will require
   some cleanup of blenkernel, most likely.*/
 #define IDP_ID         7
+#define IDP_DOUBLE     8
 
 /*add any future new id property types here.*/
 
index f60ebf8dee1991addbe82a1364b61edb7547ed93..4a51619aec417bbb45fed56aa52c3811ae094ebe 100644 (file)
@@ -60,6 +60,8 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
                        return PyInt_FromLong( (long)prop->data.val );
                case IDP_FLOAT:
                        return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) );
+               case IDP_DOUBLE:
+                       return PyFloat_FromDouble( (*(double*)(&prop->data.val)) );
                case IDP_GROUP:
                        /*blegh*/
                        {
@@ -128,7 +130,19 @@ int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
                        Py_XDECREF(value);
                        break;
                }
-
+               case IDP_DOUBLE:
+               {
+                       double dvalue;
+                       if (!PyNumber_Check(value))
+                               return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+                       value = PyNumber_Float(value);
+                       if (!value)
+                               return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+                       dvalue = (float) PyFloat_AsDouble(value);
+                       *(double*)&self->prop->data.val = dvalue;
+                       Py_XDECREF(value);
+                       break;
+               }
                default:
                        return EXPP_ReturnIntError(PyExc_AttributeError, "attempt to set read-only attribute!");
        }
@@ -204,8 +218,8 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje
        IDPropertyTemplate val = {0};
        
        if (PyFloat_Check(ob)) {
-               val.f = (float) PyFloat_AsDouble(ob);
-               prop = IDP_New(IDP_FLOAT, val, name);
+               val.d = PyFloat_AsDouble(ob);
+               prop = IDP_New(IDP_DOUBLE, val, name);
        } else if (PyInt_Check(ob)) {
                val.i = (int) PyInt_AsLong(ob);
                prop = IDP_New(IDP_INT, val, name);
@@ -223,7 +237,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje
                val.array.len = PySequence_Length(ob);
                for (i=0; i<val.array.len; i++) {
                        item = PySequence_GetItem(ob, i);
-                       if (PyFloat_Check(item)) val.array.type = IDP_FLOAT;
+                       if (PyFloat_Check(item)) val.array.type = IDP_DOUBLE;
                        else if (!PyInt_Check(item)) return "only floats and ints are allowed in ID property arrays";
                        Py_XDECREF(item);
                }
@@ -236,7 +250,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje
                                ((int*)prop->data.pointer)[i] = (int)PyInt_AsLong(item);
                        } else {
                                item = PyNumber_Float(item);
-                               ((float*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
+                               ((double*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
                        }
                        Py_XDECREF(item);
                }
@@ -334,6 +348,9 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
                case IDP_FLOAT:
                        return PyFloat_FromDouble(*((float*)&prop->data.val));
                        break;
+               case IDP_DOUBLE:
+                       return PyFloat_FromDouble(*((double*)&prop->data.val));
+                       break;
                case IDP_INT:
                        return PyInt_FromLong( (long)prop->data.val );
                        break;
@@ -347,12 +364,15 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
                                           "PyList_New() failed" );
                        
                        for (i=0; i<prop->len; i++) {
-                               if (prop->subtype == IDP_FLOAT)
+                               if (prop->subtype == IDP_FLOAT) {
                                                PyList_SetItem(seq, i,
                                                PyFloat_FromDouble(((float*)prop->data.pointer)[i]));
-                               
-                               else    PyList_SetItem(seq, i,
-                                               PyInt_FromLong(((int*)prop->data.pointer)[i]));
+                               } else if (prop->subtype == IDP_DOUBLE) {
+                                               PyList_SetItem(seq, i,
+                                               PyFloat_FromDouble(((double*)prop->data.pointer)[i]));                          
+                               } else  { PyList_SetItem(seq, i,
+                                                 PyInt_FromLong(((int*)prop->data.pointer)[i]));
+                               }
                        }
                        return seq;
                }
@@ -451,7 +471,7 @@ PyObject *BPy_IDGroup_GetKeys(BPy_IDProperty *self)
                /*set correct group length*/
                self->prop->len = i;
                
-               /*free the old list*/
+               /*free the list*/
                Py_DECREF(seq);
                
                /*call self again*/
@@ -688,6 +708,9 @@ PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index)
                case IDP_FLOAT:
                        return PyFloat_FromDouble( (double)(((float*)self->prop->data.pointer)[index]));
                        break;
+               case IDP_DOUBLE:
+                       return PyFloat_FromDouble( (((double*)self->prop->data.pointer)[index]));
+                       break;          
                case IDP_INT:
                        return PyInt_FromLong( (long)((int*)self->prop->data.pointer)[index] );
                        break;
@@ -700,7 +723,8 @@ int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *val)
 {
        int i;
        float f;
-
+       double d;
+       
        if (index < 0 || index >= self->prop->len)
                return EXPP_ReturnIntError( PyExc_RuntimeError,
                                "index out of range!");
@@ -717,6 +741,17 @@ int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *val)
                        ((float*)self->prop->data.pointer)[index] = f;
                        Py_XDECREF(val);
                        break;
+               case IDP_DOUBLE:
+                       if (!PyNumber_Check(val)) return EXPP_ReturnIntError( PyExc_TypeError,
+                               "expected a float");
+                       val = PyNumber_Float(val);
+                       if (!val) return EXPP_ReturnIntError( PyExc_TypeError,
+                               "expected a float");
+
+                       d = (double) PyFloat_AsDouble(val);
+                       ((double*)self->prop->data.pointer)[index] = d;
+                       Py_XDECREF(val);
+                       break;
                case IDP_INT:
                        if (!PyNumber_Check(val)) return EXPP_ReturnIntError( PyExc_TypeError,
                                "expected an int");