diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2019-01-16 13:58:11 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2019-01-16 14:02:37 +0300 |
commit | 514e53bb8023775c0abb5c238633b45fe24afae2 (patch) | |
tree | 738bf280bcf33c802f613856b25aa747fb90067a /source/blender/python/intern/bpy_rna_id_collection.c | |
parent | ce6d20b54e3077e8d57103de3e7933069a2d54e7 (diff) |
Expose batch IDs deletion in python API.
Follow-up to previous commit.
Diffstat (limited to 'source/blender/python/intern/bpy_rna_id_collection.c')
-rw-r--r-- | source/blender/python/intern/bpy_rna_id_collection.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/source/blender/python/intern/bpy_rna_id_collection.c b/source/blender/python/intern/bpy_rna_id_collection.c index d2d08a37d81..3fc12d4cc54 100644 --- a/source/blender/python/intern/bpy_rna_id_collection.c +++ b/source/blender/python/intern/bpy_rna_id_collection.c @@ -35,6 +35,7 @@ #include "BLI_bitmap.h" #include "BKE_global.h" +#include "BKE_library.h" #include "BKE_library_query.h" #include "BKE_main.h" @@ -289,6 +290,79 @@ error: } +PyDoc_STRVAR(bpy_batch_remove_doc, +".. method:: batch_remove(ids=(id1, id2, ...))\n" +"\n" +" Remove (delete) several IDs at once.\n" +"\n" +" WARNING: Considered experimental feature currently.\n" +"\n" +" Note that this function is quicker than individual calls to :func:`remove()` (from :class:`bpy.types.BlendData`\n" +" ID collections), but less safe/versatile (it can break Blender, e.g. by removing all scenes...).\n" +"\n" +" :arg ids: Iterables of IDs (types can be mixed).\n" +" :type subset: sequence\n" +); +static PyObject *bpy_batch_remove(PyObject *UNUSED(self), PyObject *args, PyObject *kwds) +{ +#if 0 /* If someone knows how to get a proper 'self' in that case... */ + BPy_StructRNA *pyrna = (BPy_StructRNA *)self; + Main *bmain = pyrna->ptr.data; +#else + Main *bmain = G_MAIN; /* XXX Ugly, but should work! */ +#endif + + PyObject *ids = NULL; + + PyObject *ret = NULL; + + static const char *_keywords[] = {"ids", NULL}; + static _PyArg_Parser _parser = {"O:user_map", _keywords, 0}; + if (!_PyArg_ParseTupleAndKeywordsFast( + args, kwds, &_parser, + &ids)) + { + return ret; + } + + if (ids) { + BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false); + + PyObject *ids_fast = PySequence_Fast(ids, "batch_remove"); + if (ids_fast == NULL) { + goto error; + } + + PyObject **ids_array = PySequence_Fast_ITEMS(ids_fast); + Py_ssize_t ids_len = PySequence_Fast_GET_SIZE(ids_fast); + + for (; ids_len; ids_array++, ids_len--) { + ID *id; + if (!pyrna_id_FromPyObject(*ids_array, &id)) { + PyErr_Format(PyExc_TypeError, + "Expected an ID type, not %.200s", + Py_TYPE(*ids_array)->tp_name); + Py_DECREF(ids_fast); + goto error; + } + + id->tag |= LIB_TAG_DOIT; + } + Py_DECREF(ids_fast); + + BKE_id_multi_tagged_delete(bmain); + } + else { + goto error; + } + + Py_INCREF(Py_None); + ret = Py_None; + +error: + return ret; +} + int BPY_rna_id_collection_module(PyObject *mod_par) { static PyMethodDef user_map = { @@ -296,5 +370,10 @@ int BPY_rna_id_collection_module(PyObject *mod_par) PyModule_AddObject(mod_par, "_rna_id_collection_user_map", PyCFunction_New(&user_map, NULL)); + static PyMethodDef batch_remove = { + "batch_remove", (PyCFunction)bpy_batch_remove, METH_VARARGS | METH_KEYWORDS, bpy_batch_remove_doc}; + + PyModule_AddObject(mod_par, "_rna_id_collection_batch_remove", PyCFunction_New(&batch_remove, NULL)); + return 0; } |