From 969b4673c76696c872874f032c5535a1daf1eb84 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 10 Mar 2010 20:54:14 +0000 Subject: [PATCH] Python/RNA: added collection.move(from, to) for python defined collection properties. --- source/blender/makesrna/RNA_access.h | 1 + source/blender/makesrna/intern/rna_access.c | 28 +++++++++++++++++++++ source/blender/python/intern/bpy_rna.c | 22 ++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 89dd6a5c3e6..ae431beb1fe 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -742,6 +742,7 @@ void RNA_property_pointer_remove(PointerRNA *ptr, PropertyRNA *prop); 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); diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 4cc5ca6f9df..6bd99793933 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -2172,6 +2172,34 @@ int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key) 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; diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 5a114199005..552b187d097 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -2220,6 +2220,27 @@ static PyObject *pyrna_prop_remove(BPy_PropertyRNA *self, PyObject *value) 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 @@ static struct PyMethodDef pyrna_prop_collection_methods[] = { /* 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} };