Py API: Add orphans_purge helper to bpy.data.

Much more convinient than trying to use outliner operator...
This commit is contained in:
2020-02-13 17:46:57 +01:00
parent 92e41bb1a8
commit dadabf5cf3
2 changed files with 48 additions and 0 deletions

View File

@@ -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):

View File

@@ -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;
}