[Bf-blender-cvs] [dadabf5cf3b] master: Py API: Add `orphans_purge` helper to `bpy.data`.

Bastien Montagne noreply at git.blender.org
Thu Feb 13 17:55:11 CET 2020


Commit: dadabf5cf3babfbda5075bd7093909ca01655b9e
Author: Bastien Montagne
Date:   Thu Feb 13 17:46:57 2020 +0100
Branches: master
https://developer.blender.org/rBdadabf5cf3babfbda5075bd7093909ca01655b9e

Py API: Add `orphans_purge` helper to `bpy.data`.

Much more convinient than trying to use outliner operator...

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

M	release/scripts/modules/bpy_types.py
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 4c47b5a5fb6..a30f9d1dd1d 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -29,6 +29,7 @@ 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
+bpy_types.BlendData.orphans_purge = _bpy._rna_id_collection_orphans_purge
 
 
 class Context(StructRNA):
diff --git a/source/blender/python/intern/bpy_rna_id_collection.c b/source/blender/python/intern/bpy_rna_id_collection.c
index e1d5c358ba5..ca5cb287401 100644
--- a/source/blender/python/intern/bpy_rna_id_collection.c
+++ b/source/blender/python/intern/bpy_rna_id_collection.c
@@ -348,6 +348,43 @@ error:
   return ret;
 }
 
+PyDoc_STRVAR(bpy_orphans_purge_doc,
+             ".. method:: orphans_purge()\n"
+             "\n"
+             "   Remove (delete) all IDs with no user.\n"
+             "\n"
+             "   WARNING: Considered experimental feature currently.\n");
+static PyObject *bpy_orphans_purge(PyObject *UNUSED(self),
+                                   PyObject *UNUSED(args),
+                                   PyObject *UNUSED(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
+
+  ID *id;
+  FOREACH_MAIN_ID_BEGIN (bmain, id) {
+    if (id->us == 0) {
+      id->tag |= LIB_TAG_DOIT;
+    }
+    else {
+      id->tag &= ~LIB_TAG_DOIT;
+    }
+  }
+  FOREACH_MAIN_ID_END;
+
+  BKE_id_multi_tagged_delete(bmain);
+  /* Force full redraw, mandatory to avoid crashes when running this from UI... */
+  WM_main_add_notifier(NC_WINDOW, NULL);
+
+  Py_INCREF(Py_None);
+
+  return Py_None;
+}
+
 int BPY_rna_id_collection_module(PyObject *mod_par)
 {
   static PyMethodDef user_map = {
@@ -365,5 +402,15 @@ int BPY_rna_id_collection_module(PyObject *mod_par)
   PyModule_AddObject(
       mod_par, "_rna_id_collection_batch_remove", PyCFunction_New(&batch_remove, NULL));
 
+  static PyMethodDef orphans_purge = {
+      "orphans_purge",
+      (PyCFunction)bpy_orphans_purge,
+      METH_VARARGS | METH_KEYWORDS,
+      bpy_orphans_purge_doc,
+  };
+
+  PyModule_AddObject(
+      mod_par, "_rna_id_collection_orphans_purge", PyCFunction_New(&orphans_purge, NULL));
+
   return 0;
 }



More information about the Bf-blender-cvs mailing list