[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15739] trunk/blender/source/blender: added support for doubles to the id property code, and made the python code use them by default

Joseph Eagar joeedh at gmail.com
Thu Jul 24 21:22:17 CEST 2008


Revision: 15739
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15739
Author:   joeedh
Date:     2008-07-24 21:22:17 +0200 (Thu, 24 Jul 2008)

Log Message:
-----------
added support for doubles to the id property code, and made the python code use them by default

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_idprop.h
    trunk/blender/source/blender/blenkernel/intern/idprop.c
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/blenloader/intern/writefile.c
    trunk/blender/source/blender/makesdna/DNA_ID.h
    trunk/blender/source/blender/python/api2_2x/IDProp.c

Modified: trunk/blender/source/blender/blenkernel/BKE_idprop.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_idprop.h	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/blenkernel/BKE_idprop.h	2008-07-24 19:22:17 UTC (rev 15739)
@@ -46,6 +46,7 @@
 typedef union {
 	int i;
 	float f;
+	double d;
 	char *str;
 	struct ID *id;
 	struct {

Modified: trunk/blender/source/blender/blenkernel/intern/idprop.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/idprop.c	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/blenkernel/intern/idprop.c	2008-07-24 19:22:17 UTC (rev 15739)
@@ -54,7 +54,8 @@
 	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 @@
 			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 @@
 
 	prop->type = type;
 	strncpy(prop->name, name, MAX_IDPROP_NAME);
+	
+	/*security null byte*/
+	prop->name[MAX_IDPROP_NAME-1] = 0;
+	
 	return prop;
 }
 

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2008-07-24 19:22:17 UTC (rev 15739)
@@ -1347,8 +1347,14 @@
 	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]);
+			}
 		}
 	}
 }
@@ -1385,6 +1391,24 @@
 		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;
 	}
 }
 

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2008-07-24 19:22:17 UTC (rev 15739)
@@ -533,6 +533,7 @@
 		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;

Modified: trunk/blender/source/blender/makesdna/DNA_ID.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_ID.h	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/makesdna/DNA_ID.h	2008-07-24 19:22:17 UTC (rev 15739)
@@ -46,7 +46,7 @@
 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 @@
 /*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.*/
 

Modified: trunk/blender/source/blender/python/api2_2x/IDProp.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/IDProp.c	2008-07-24 19:12:30 UTC (rev 15738)
+++ trunk/blender/source/blender/python/api2_2x/IDProp.c	2008-07-24 19:22:17 UTC (rev 15739)
@@ -60,6 +60,8 @@
 			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 @@
 			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 @@
 	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 @@
 		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 @@
 				((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 @@
 		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 @@
 					   "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 @@
 		/*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 @@
 		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 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 @@
 			((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");





More information about the Bf-blender-cvs mailing list