[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [37579] trunk/blender: IDProperty python module update

Campbell Barton ideasman42 at gmail.com
Fri Jun 17 07:45:46 CEST 2011


Revision: 37579
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=37579
Author:   campbellbarton
Date:     2011-06-17 05:45:46 +0000 (Fri, 17 Jun 2011)
Log Message:
-----------
IDProperty python module update
- add support for IDProp array slicing, but not resizing.
- rename array attribute type to typecode and use chars 'f', 'd', 'i' which match pythons array module. (was using int's which only have a meaning internally).
- rename function 'convert_to_pyobject' to 'to_dict' and 'to_list' for IDProp group and array types respectively.
- remove 'len' array attribute, calling len(array) is fine.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/rna_prop_ui.py
    trunk/blender/source/blender/python/generic/IDProp.c
    trunk/blender/source/blender/python/generic/IDProp.h
    trunk/blender/source/blender/python/generic/py_capi_utils.c
    trunk/blender/source/blender/python/generic/py_capi_utils.h
    trunk/blender/source/blender/python/intern/bpy_props.c
    trunk/blender/source/blender/python/intern/bpy_util.c

Modified: trunk/blender/release/scripts/modules/rna_prop_ui.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_prop_ui.py	2011-06-17 03:53:51 UTC (rev 37578)
+++ trunk/blender/release/scripts/modules/rna_prop_ui.py	2011-06-17 05:45:46 UTC (rev 37579)
@@ -111,12 +111,16 @@
             continue
 
         row = layout.row()
-        convert_to_pyobject = getattr(val, "convert_to_pyobject", None)
+        to_dict = getattr(val, "to_dict", None)
+        to_list = getattr(val, "to_list", None)
 
         val_orig = val
-        if convert_to_pyobject:
-            val_draw = val = val.convert_to_pyobject()
-            val_draw = str(val_draw)
+        if to_dict:
+            val = to_dict()
+            val_draw = str(val)
+        elif to_list:
+            val = to_list()
+            val_draw = str(val)
         else:
             val_draw = val
 
@@ -131,7 +135,7 @@
         row.label(text=key)
 
         # explicit exception for arrays
-        if convert_to_pyobject and not hasattr(val_orig, "len"):
+        if to_dict or to_list:
             row.label(text=val_draw)
         else:
             if key in rna_properties:

Modified: trunk/blender/source/blender/python/generic/IDProp.c
===================================================================
--- trunk/blender/source/blender/python/generic/IDProp.c	2011-06-17 03:53:51 UTC (rev 37578)
+++ trunk/blender/source/blender/python/generic/IDProp.c	2011-06-17 05:45:46 UTC (rev 37579)
@@ -45,26 +45,31 @@
 #include "py_capi_utils.h"
 #endif
 
-extern PyTypeObject IDArray_Type;
-extern PyTypeObject IDGroup_Iter_Type;
+extern PyTypeObject BPy_IDArray_Type;
+extern PyTypeObject BPy_IDGroup_Iter_Type;
+extern PyTypeObject BPy_IDGroup_Type;
 
 /*********************** ID Property Main Wrapper Stuff ***************/
 
-static PyObject *IDGroup_repr( BPy_IDProperty *self )
+/* use for both array and group */
+static long BPy_IDGroup_hash(BPy_IDProperty *self)
 {
-	return PyUnicode_FromFormat( "<bpy ID property from \"%s\">", self->id->name);
+	return _Py_HashPointer(self->prop);
 }
 
-extern PyTypeObject IDGroup_Type;
+static PyObject *BPy_IDGroup_repr(BPy_IDProperty *self)
+{
+	return PyUnicode_FromFormat( "<bpy id property from \"%s\">", self->id->name);
+}
 
 PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
 {
 	switch ( prop->type ) {
 		case IDP_STRING:
 #ifdef USE_STRING_COERCE
-			return PyC_UnicodeFromByte(prop->data.pointer);
+			return PyC_UnicodeFromByte(IDP_Array(prop));
 #else
-			return PyUnicode_FromString(prop->data.pointer);
+			return PyUnicode_FromString(IDP_Array(prop));
 #endif
 		case IDP_INT:
 			return PyLong_FromLong( (long)prop->data.val );
@@ -75,14 +80,14 @@
 		case IDP_GROUP:
 			/*blegh*/
 			{
-				BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &IDGroup_Type);
+				BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
 				group->id = id;
 				group->prop = prop;
 				return (PyObject*) group;
 			}
 		case IDP_ARRAY:
 			{
-				BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &IDArray_Type);
+				BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &BPy_IDArray_Type);
 				array->id = id;
 				array->prop = prop;
 				return (PyObject*) array;
@@ -135,13 +140,13 @@
 
 				st = _PyUnicode_AsString(value);
 				IDP_ResizeArray(prop, alloc_len);
-				memcpy(prop->data.pointer, st, alloc_len);
+				memcpy(IDP_Array(prop), st, alloc_len);
 				Py_XDECREF(value_coerce);
 			}
 #else
 			st = _PyUnicode_AsString(value);
 			IDP_ResizeArray(prop, strlen(st)+1);
-			strcpy(prop->data.pointer, st);
+			strcpy(IDP_Array(prop), st);
 #endif
 
 			return 0;
@@ -344,7 +349,7 @@
 			prop = IDP_New(IDP_ARRAY, val, name);
 			for (i=0; i<val.array.len; i++) {
 				item = PySequence_GetItem(ob, i);
-				((double*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
+				((double*)IDP_Array(prop))[i] = (float)PyFloat_AsDouble(item);
 				Py_DECREF(item);
 			}
 			break;
@@ -352,7 +357,7 @@
 			prop = IDP_New(IDP_ARRAY, val, name);
 			for (i=0; i<val.array.len; i++) {
 				item = PySequence_GetItem(ob, i);
-				((int*)prop->data.pointer)[i] = (int)PyLong_AsSsize_t(item);
+				((int*)IDP_Array(prop))[i] = (int)PyLong_AsSsize_t(item);
 				Py_DECREF(item);
 			}
 			break;
@@ -465,9 +470,9 @@
 	return BPy_Wrap_SetMapItem(self->prop, key, val);
 }
 
-static PyObject *BPy_IDGroup_SpawnIterator(BPy_IDProperty *self)
+static PyObject *BPy_IDGroup_iter(BPy_IDProperty *self)
 {
-	BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &IDGroup_Iter_Type);
+	BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &BPy_IDGroup_Iter_Type);
 	iter->group = self;
 	iter->mode = IDPROP_ITER_KEYS;
 	iter->cur = self->prop->data.group.first;
@@ -480,9 +485,9 @@
 	switch (prop->type) {
 		case IDP_STRING:
 #ifdef USE_STRING_COERCE
-			return PyC_UnicodeFromByte(prop->data.pointer);
+			return PyC_UnicodeFromByte(IDP_Array(prop));
 #else
-			return PyUnicode_FromString(prop->data.pointer);
+			return PyUnicode_FromString(IDP_Array(prop));
 #endif
 			break;
 		case IDP_FLOAT:
@@ -504,20 +509,37 @@
 				return NULL;
 			}
 
-			for (i=0; i<prop->len; i++) {
-				if (prop->subtype == IDP_FLOAT) {
-					PyList_SET_ITEM(seq, i,
-							PyFloat_FromDouble(((float*)prop->data.pointer)[i]));
+			switch(prop->subtype) {
+				case IDP_FLOAT:
+				{
+					float *array= (float*)IDP_Array(prop);
+					for (i=0; i<prop->len; i++) {
+						PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
+					}
+					break;
 				}
-				else if (prop->subtype == IDP_DOUBLE) {
-					PyList_SET_ITEM(seq, i,
-							PyFloat_FromDouble(((double*)prop->data.pointer)[i]));
+				case IDP_DOUBLE:
+				{
+					double *array= (double*)IDP_Array(prop);
+					for (i=0; i<prop->len; i++) {
+						PyList_SET_ITEM(seq, i, PyFloat_FromDouble(array[i]));
+					}
+					break;
 				}
-				else 	{
-					PyList_SET_ITEM(seq, i,
-							PyLong_FromLong(((int*)prop->data.pointer)[i]));
+				case IDP_INT:
+				{
+					int *array= (int*)IDP_Array(prop);
+					for (i=0; i<prop->len; i++) {
+						PyList_SET_ITEM(seq, i, PyLong_FromLong(array[i]));
+					}
+					break;
 				}
+				default:
+					PyErr_SetString(PyExc_RuntimeError, "invalid/corrupt array type!");
+					Py_DECREF(seq);
+					return NULL;
 			}
+
 			return seq;
 		}
 		case IDP_IDPARRAY:
@@ -595,7 +617,7 @@
 
 static PyObject *BPy_IDGroup_IterItems(BPy_IDProperty *self)
 {
-	BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &IDGroup_Iter_Type);
+	BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &BPy_IDGroup_Iter_Type);
 	iter->group = self;
 	iter->mode = IDPROP_ITER_ITEMS;
 	iter->cur = self->prop->data.group.first;
@@ -731,7 +753,7 @@
 	Py_RETURN_NONE;
 }
 
-static PyObject *BPy_IDGroup_ConvertToPy(BPy_IDProperty *self)
+static PyObject *BPy_IDGroup_to_dict(BPy_IDProperty *self)
 {
 	return BPy_IDGroup_MapDataToPy(self->prop);
 }
@@ -773,7 +795,7 @@
 		"updates the values in the group with the values of another or a dict"},
 	{"get", (PyCFunction)BPy_IDGroup_Get, METH_VARARGS,
 		"idprop.get(k[,d]) -> idprop[k] if k in idprop, else d.  d defaults to None"},
-	{"convert_to_pyobject", (PyCFunction)BPy_IDGroup_ConvertToPy, METH_NOARGS,
+	{"to_dict", (PyCFunction)BPy_IDGroup_to_dict, METH_NOARGS,
 		"return a purely python version of the group"},
 	{NULL, NULL, 0, NULL}
 };
@@ -792,16 +814,16 @@
 };
 
 static PyMappingMethods BPy_IDGroup_Mapping = {
-	(lenfunc)BPy_IDGroup_Map_Len, 			/*inquiry mp_length */
-	(binaryfunc)BPy_IDGroup_Map_GetItem,		/*binaryfunc mp_subscript */
+	(lenfunc)BPy_IDGroup_Map_Len, 		/*inquiry mp_length */
+	(binaryfunc)BPy_IDGroup_Map_GetItem,/*binaryfunc mp_subscript */
 	(objobjargproc)BPy_IDGroup_Map_SetItem,	/*objobjargproc mp_ass_subscript */
 };
 
-PyTypeObject IDGroup_Type = {
+PyTypeObject BPy_IDGroup_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	/*  For printing, in format "<module>.<name>" */
-	"Blender IDProperty",           /* char *tp_name; */
-	sizeof( BPy_IDProperty ),       /* int tp_basicsize; */
+	"Blender IDProperty",		/* char *tp_name; */
+	sizeof(BPy_IDProperty),		/* int tp_basicsize; */
 	0,                          /* tp_itemsize;  For allocation */
 
 	/* Methods to implement standard operations */
@@ -811,7 +833,7 @@
 	NULL,     /* getattrfunc tp_getattr; */
 	NULL,     /* setattrfunc tp_setattr; */
 	NULL,                       /* cmpfunc tp_compare; */
-	( reprfunc ) IDGroup_repr,     /* reprfunc tp_repr; */
+	(reprfunc)BPy_IDGroup_repr,     /* reprfunc tp_repr; */
 
 	/* Method suites for standard classes */
 
@@ -821,7 +843,7 @@
 
 	/* More standard operations (here for binary compatibility) */
 
-	NULL,                       /* hashfunc tp_hash; */
+	(hashfunc)BPy_IDGroup_hash, /* hashfunc tp_hash; */
 	NULL,                       /* ternaryfunc tp_call; */
 	NULL,                       /* reprfunc tp_str; */
 	NULL,                       /* getattrofunc tp_getattro; */
@@ -850,7 +872,7 @@
 
   /*** Added in release 2.2 ***/
 	/*   Iterators */
-	(getiterfunc)BPy_IDGroup_SpawnIterator, /* getiterfunc tp_iter; */
+	(getiterfunc)BPy_IDGroup_iter, /* getiterfunc tp_iter; */
 	NULL,                       /* iternextfunc tp_iternext; */
   /*** Attribute descriptor and subclassing stuff ***/
 	BPy_IDGroup_methods,        /* struct PyMethodDef *tp_methods; */
@@ -861,7 +883,7 @@
 /*********** Main external wrapping function *******/
 PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent)
 {
-	BPy_IDProperty *wrap = PyObject_New(BPy_IDProperty, &IDGroup_Type);
+	BPy_IDProperty *wrap = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
 	wrap->prop = prop;
 	wrap->parent = parent;
 	wrap->id = id;
@@ -872,36 +894,58 @@
 
 /********Array Wrapper********/
 
-static PyObject *IDArray_repr(BPy_IDArray *self)
+static PyTypeObject *idp_array_py_type(BPy_IDArray *self, short *is_double)
 {
-	return PyUnicode_FromFormat("(ID Array [%d])", self->prop->len);
+	switch (self->prop->subtype) {
+		case IDP_FLOAT:
+			*is_double= 0;
+			return &PyFloat_Type;
+		case IDP_DOUBLE:
+			*is_double= 1;
+			return &PyFloat_Type;
+		case IDP_INT:
+			*is_double= 0;
+			return &PyLong_Type;
+	}
+
+	*is_double= 0;
+	return NULL;
 }
 
+static PyObject *BPy_IDArray_repr(BPy_IDArray *self)
+{
+	return PyUnicode_FromFormat("<bpy id property array [%d]>", self->prop->len);
+}
 
 static PyObject *BPy_IDArray_GetType(BPy_IDArray *self)
 {
-	return PyLong_FromSsize_t( self->prop->subtype );
-}
+	switch(self->prop->subtype) {
+	case IDP_FLOAT:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list