[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27400] trunk/blender/source/blender: Python/RNA: added collection.move(from, to) for python defined

Brecht Van Lommel brecht at blender.org
Wed Mar 10 21:54:14 CET 2010


Revision: 27400
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27400
Author:   blendix
Date:     2010-03-10 21:54:14 +0100 (Wed, 10 Mar 2010)

Log Message:
-----------
Python/RNA: added collection.move(from, to) for python defined
collection properties.

Modified Paths:
--------------
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_access.h	2010-03-10 20:33:57 UTC (rev 27399)
+++ trunk/blender/source/blender/makesrna/RNA_access.h	2010-03-10 20:54:14 UTC (rev 27400)
@@ -742,6 +742,7 @@
 void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr);
 int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key);
 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop);
+int RNA_property_collection_move(PointerRNA *ptr, PropertyRNA *prop, int key, int pos);
 
 /* copy/reset */
 int RNA_property_copy(PointerRNA *ptr, PointerRNA *fromptr, PropertyRNA *prop, int index);

Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c	2010-03-10 20:33:57 UTC (rev 27399)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c	2010-03-10 20:54:14 UTC (rev 27400)
@@ -2172,6 +2172,34 @@
 	return 0;
 }
 
+int RNA_property_collection_move(PointerRNA *ptr, PropertyRNA *prop, int key, int pos)
+{
+	IDProperty *idprop;
+
+	if((idprop=rna_idproperty_check(&prop, ptr))) {
+		IDProperty tmp, *array;
+		int len;
+
+		len= idprop->len;
+		array= IDP_IDPArray(idprop);
+
+		if(key >= 0 && key < len && pos >= 0 && pos < len && key != pos) {
+			memcpy(&tmp, &array[key], sizeof(IDProperty));
+			if(pos < key)
+				memmove(array+pos+1, array+pos, sizeof(IDProperty)*(key - pos));
+			else
+				memmove(array+key, array+key+1, sizeof(IDProperty)*(pos - key));
+			memcpy(&array[pos], &tmp, sizeof(IDProperty));
+		}
+
+		return 1;
+	}
+	else if(prop->flag & PROP_IDPROPERTY)
+		return 1;
+
+	return 0;
+}
+
 void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
 {
 	IDProperty *idprop;

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2010-03-10 20:33:57 UTC (rev 27399)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2010-03-10 20:54:14 UTC (rev 27400)
@@ -2220,6 +2220,27 @@
 	return ret;
 }
 
+static PyObject *pyrna_prop_move(BPy_PropertyRNA *self, PyObject *args)
+{
+	PyObject *ret;
+	int key=0, pos=0;
+
+	if (!PyArg_ParseTuple(args, "ii", &key, &pos)) {
+		PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move(): expected two ints as arguments");
+		return NULL;
+	}
+
+	if(!RNA_property_collection_move(&self->ptr, self->prop, key, pos)) {
+		PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move() not supported for this collection");
+		return NULL;
+	}
+
+	ret = Py_None;
+	Py_INCREF(ret);
+
+	return ret;
+}
+
 static PyObject *pyrna_struct_get_id_data(BPy_StructRNA *self)
 {
 	if(self->ptr.id.data) {
@@ -2714,6 +2735,7 @@
 	/* 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},
 	{NULL, NULL, 0, NULL}
 };
 





More information about the Bf-blender-cvs mailing list