[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31717] trunk/blender/source/blender/ python/intern: - new subclass for ID-Property based collections - this way add/remove/ move functions will only be shown for types that support it.

Campbell Barton ideasman42 at gmail.com
Thu Sep 2 08:35:00 CEST 2010


Revision: 31717
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31717
Author:   campbellbarton
Date:     2010-09-02 08:35:00 +0200 (Thu, 02 Sep 2010)

Log Message:
-----------
- new subclass for ID-Property based collections - this way add/remove/move functions will only be shown for types that support it.
- moved array attributes into array properties - saves 8 bytes per allocated non-array property.

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_array.c
    trunk/blender/source/blender/python/intern/bpy_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.h

Modified: trunk/blender/source/blender/python/intern/bpy_array.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_array.c	2010-09-02 06:28:03 UTC (rev 31716)
+++ trunk/blender/source/blender/python/intern/bpy_array.c	2010-09-02 06:35:00 UTC (rev 31717)
@@ -457,10 +457,10 @@
 }
 #endif
 
-PyObject *pyrna_py_from_array_index(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
+PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
 {
 	int totdim, arraydim, arrayoffset, dimsize[MAX_ARRAY_DIMENSION], i, len;
-	BPy_PropertyRNA *ret= NULL;
+	BPy_PropertyArrayRNA *ret= NULL;
 
 	arraydim= self ? self->arraydim : 0;
 	arrayoffset = self ? self->arrayoffset : 0;
@@ -478,7 +478,7 @@
 	totdim= RNA_property_array_dimension(ptr, prop, dimsize);
 
 	if (arraydim + 1 < totdim) {
-		ret= (BPy_PropertyRNA*)pyrna_prop_CreatePyObject(ptr, prop);
+		ret= (BPy_PropertyArrayRNA*)pyrna_prop_CreatePyObject(ptr, prop);
 		ret->arraydim= arraydim + 1;
 
 		/* arr[3][4][5]

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2010-09-02 06:28:03 UTC (rev 31716)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2010-09-02 06:35:00 UTC (rev 31717)
@@ -57,8 +57,8 @@
 #include "../generic/py_capi_utils.h"
 
 static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, ParameterList *parms, void *data, PyObject *value, const char *error_prefix);
-static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
-static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self);
+static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
+static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self);
 static Py_ssize_t pyrna_prop_collection_length(BPy_PropertyRNA *self);
 static short pyrna_rotation_euler_order_get(PointerRNA *ptr, PropertyRNA **prop_eul_order, short order_fallback);
 
@@ -501,7 +501,7 @@
 		if(type==PROP_COLLECTION) {
 			len= pyrna_prop_collection_length(self);
 		} else if (RNA_property_array_check(&self->ptr, self->prop)) {
-			len= pyrna_prop_array_length(self);
+			len= pyrna_prop_array_length((BPy_PropertyArrayRNA *)self);
 		}
 
 		if(len != -1)
@@ -1072,7 +1072,7 @@
 			}
 
 			/* another exception, allow to pass a collection as an RNA property */
-			if(Py_TYPE(value)==&pyrna_prop_collection_Type) {
+			if(Py_TYPE(value)==&pyrna_prop_collection_Type) { /* ok to ignore idprop collections */
 				PointerRNA c_ptr;
 				BPy_PropertyRNA *value_prop= (BPy_PropertyRNA *)value;
 				if(RNA_property_collection_type_get(&value_prop->ptr, value_prop->prop, &c_ptr)) {
@@ -1211,12 +1211,12 @@
 	return 0;
 }
 
-static PyObject * pyrna_prop_to_py_index(BPy_PropertyRNA *self, int index)
+static PyObject * pyrna_prop_array_to_py_index(BPy_PropertyArrayRNA *self, int index)
 {
 	return pyrna_py_from_array_index(self, &self->ptr, self->prop, index);
 }
 
-static int pyrna_py_to_prop_index(BPy_PropertyRNA *self, int index, PyObject *value)
+static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, PyObject *value)
 {
 	int ret = 0;
 	int totdim;
@@ -1286,7 +1286,7 @@
 }
 
 //---------------sequence-------------------------------------------
-static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self)
+static Py_ssize_t pyrna_prop_array_length(BPy_PropertyArrayRNA *self)
 {
 	if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
 		return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
@@ -1338,14 +1338,14 @@
 	return NULL;
 }
 
-static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyRNA *self, int keynum)
+static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum)
 {
 	int len= pyrna_prop_array_length(self);
 
 	if(keynum < 0) keynum += len;
 
 	if(keynum >= 0 && keynum < len)
-		return pyrna_prop_to_py_index(self, keynum);
+		return pyrna_prop_array_to_py_index(self, keynum);
 
 	PyErr_Format(PyExc_IndexError, "bpy_prop_array[index]: index %d out of range", keynum);
 	return NULL;
@@ -1386,10 +1386,10 @@
 }
 
 /* TODO - dimensions
- * note: could also use pyrna_prop_to_py_index(self, count) in a loop but its a lot slower
+ * note: could also use pyrna_prop_array_to_py_index(self, count) in a loop but its a lot slower
  * since at the moment it reads (and even allocates) the entire array for each index.
  */
-static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
+static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
 {
 	int count, totdim;
 
@@ -1399,7 +1399,7 @@
 
 	if (totdim > 1) {
 		for (count = start; count < stop; count++)
-			PyList_SET_ITEM(list, count - start, pyrna_prop_to_py_index(self, count));
+			PyList_SET_ITEM(list, count - start, pyrna_prop_array_to_py_index(self, count));
 	}
 	else {
 		switch (RNA_property_type(prop)) {
@@ -1497,7 +1497,7 @@
 	}
 }
 
-static PyObject *pyrna_prop_array_subscript(BPy_PropertyRNA *self, PyObject *key)
+static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject *key)
 {
 	/*if (PyUnicode_Check(key)) {
 		return pyrna_prop_array_subscript_str(self, _PyUnicode_AsString(key));
@@ -1532,7 +1532,7 @@
 	}
 }
 
-/* could call (pyrna_py_to_prop_index(self, i, value) in a loop but it is slow */
+/* could call (pyrna_py_to_prop_array_index(self, i, value) in a loop but it is slow */
 static int prop_subscript_ass_array_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length, PyObject *value_orig)
 {
 	PyObject *value;
@@ -1635,20 +1635,20 @@
 
 }
 
-static int prop_subscript_ass_array_int(BPy_PropertyRNA *self, Py_ssize_t keynum, PyObject *value)
+static int prop_subscript_ass_array_int(BPy_PropertyArrayRNA *self, Py_ssize_t keynum, PyObject *value)
 {
 	int len= pyrna_prop_array_length(self);
 
 	if(keynum < 0) keynum += len;
 
 	if(keynum >= 0 && keynum < len)
-		return pyrna_py_to_prop_index(self, keynum, value);
+		return pyrna_py_to_prop_array_index(self, keynum, value);
 
 	PyErr_SetString(PyExc_IndexError, "bpy_prop_array[index] = value: index out of range");
 	return -1;
 }
 
-static int pyrna_prop_array_ass_subscript( BPy_PropertyRNA *self, PyObject *key, PyObject *value )
+static int pyrna_prop_array_ass_subscript( BPy_PropertyArrayRNA *self, PyObject *key, PyObject *value )
 {
 	/* char *keyname = NULL; */ /* not supported yet */
 	int ret= -1;
@@ -2710,7 +2710,7 @@
 }
 
 /* odd case, we need to be able return a python method from a tp_getset */
-static PyObject *pyrna_prop_add(BPy_PropertyRNA *self)
+static PyObject *pyrna_prop_collection_idprop_add(BPy_PropertyRNA *self)
 {
 	PointerRNA r_ptr;
 
@@ -2724,7 +2724,7 @@
 	}
 }
 
-static PyObject *pyrna_prop_remove(BPy_PropertyRNA *self, PyObject *value)
+static PyObject *pyrna_prop_collection_idprop_remove(BPy_PropertyRNA *self, PyObject *value)
 {
 	PyObject *ret;
 	int key= PyLong_AsSsize_t(value);
@@ -2745,7 +2745,7 @@
 	return ret;
 }
 
-static PyObject *pyrna_prop_move(BPy_PropertyRNA *self, PyObject *args)
+static PyObject *pyrna_prop_collection_idprop_move(BPy_PropertyRNA *self, PyObject *args)
 {
 	PyObject *ret;
 	int key=0, pos=0;
@@ -3191,7 +3191,7 @@
 
 /* A bit of a kludge, make a list out of a collection or array,
  * then return the lists iter function, not especially fast but convenient for now */
-PyObject *pyrna_prop_array_iter(BPy_PropertyRNA *self)
+PyObject *pyrna_prop_array_iter(BPy_PropertyArrayRNA *self)
 {
 	/* Try get values from a collection */
 	PyObject *ret;
@@ -3288,11 +3288,13 @@
 	{"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, NULL},
 	
 	{"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
+	{NULL, NULL, 0, NULL}
+};
 
-	/* moved into a getset */
-	{"add", (PyCFunction)pyrna_prop_add, METH_NOARGS, NULL},
-	{"remove", (PyCFunction)pyrna_prop_remove, METH_O, NULL},
-	{"move", (PyCFunction)pyrna_prop_move, METH_VARARGS, NULL},
+static struct PyMethodDef pyrna_prop_collection_idprop_methods[] = {
+	{"add", (PyCFunction)pyrna_prop_collection_idprop_add, METH_NOARGS, NULL},
+	{"remove", (PyCFunction)pyrna_prop_collection_idprop_remove, METH_O, NULL},
+	{"move", (PyCFunction)pyrna_prop_collection_idprop_move, METH_VARARGS, NULL},
 	{NULL, NULL, 0, NULL}
 };
 
@@ -3903,7 +3905,7 @@
 PyTypeObject pyrna_prop_array_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_prop_array",		/* tp_name */
-	sizeof( BPy_PropertyRNA ),			/* tp_basicsize */
+	sizeof( BPy_PropertyArrayRNA ),			/* tp_basicsize */
 	0,			/* tp_itemsize */
 	/* methods */
 	NULL,						/* tp_dealloc */
@@ -4060,6 +4062,87 @@
 	NULL
 };
 
+/* only for add/remove/move methods */
+PyTypeObject pyrna_prop_collection_idprop_Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"bpy_prop_collection_idprop",		/* tp_name */
+	sizeof( BPy_PropertyRNA ),			/* tp_basicsize */
+	0,			/* tp_itemsize */
+	/* methods */
+	NULL,						/* tp_dealloc */
+	NULL,                       /* printfunc tp_print; */
+	NULL,						/* getattrfunc tp_getattr; */
+	NULL,                       /* setattrfunc tp_setattr; */
+	NULL,						/* tp_compare */ /* DEPRECATED in python 3.0! */
+	NULL, /* subclassed */		/* tp_repr */
+
+	/* Method suites for standard classes */
+
+	NULL,   /* PyNumberMethods *tp_as_number; */
+	NULL,	/* PySequenceMethods *tp_as_sequence; */
+	NULL,	/* PyMappingMethods *tp_as_mapping; */
+
+	/* More standard operations (here for binary compatibility) */
+
+	NULL,						/* hashfunc tp_hash; */
+	NULL,                       /* ternaryfunc tp_call; */
+	NULL,                       /* reprfunc tp_str; */
+
+	/* will only use these if this is a subtype of a py class */
+	NULL,						/* getattrofunc tp_getattro; */
+	NULL,						/* setattrofunc tp_setattro; */
+
+	/* Functions to access object as input/output buffer */
+	NULL,                       /* PyBufferProcs *tp_as_buffer; */
+
+  /*** Flags to define presence of optional/expanded features ***/
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,         /* long tp_flags; */
+
+	NULL,						/*  char *tp_doc;  Documentation string */

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list