[Bf-blender-cvs] [514e53bb802] master: Expose batch IDs deletion in python API.

Bastien Montagne noreply at git.blender.org
Wed Jan 16 12:03:03 CET 2019


Commit: 514e53bb8023775c0abb5c238633b45fe24afae2
Author: Bastien Montagne
Date:   Wed Jan 16 11:58:11 2019 +0100
Branches: master
https://developer.blender.org/rB514e53bb8023775c0abb5c238633b45fe24afae2

Expose batch IDs deletion in python API.

Follow-up to previous commit.

===================================================================

M	release/scripts/modules/bpy_types.py
M	source/blender/python/intern/bpy_library_write.c
M	source/blender/python/intern/bpy_rna_id_collection.c

===================================================================

diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py
index 47f132027d2..f12b259362a 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -28,6 +28,7 @@ StructMetaPropGroup = bpy_types.bpy_struct_meta_idprop
 bpy_types.BlendDataLibraries.load = _bpy._library_load
 bpy_types.BlendDataLibraries.write = _bpy._library_write
 bpy_types.BlendData.user_map = _bpy._rna_id_collection_user_map
+bpy_types.BlendData.batch_remove = _bpy._rna_id_collection_batch_remove
 
 
 class Context(StructRNA):
diff --git a/source/blender/python/intern/bpy_library_write.c b/source/blender/python/intern/bpy_library_write.c
index 07a81a3bddb..69dcfdb9455 100644
--- a/source/blender/python/intern/bpy_library_write.c
+++ b/source/blender/python/intern/bpy_library_write.c
@@ -134,7 +134,7 @@ static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject
 
 			if (!pyrna_id_FromPyObject(key, &id_store->id)) {
 				PyErr_Format(PyExc_TypeError,
-				             "Expected and ID type, not %.200s",
+				             "Expected an ID type, not %.200s",
 				             Py_TYPE(key)->tp_name);
 				ret = NULL;
 				goto finally;
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;
 }



More information about the Bf-blender-cvs mailing list