Blender  V3.3
bpy_rna_id_collection.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include <Python.h>
10 #include <stddef.h>
11 
12 #include "MEM_guardedalloc.h"
13 
14 #include "BLI_bitmap.h"
15 #include "BLI_utildefines.h"
16 
17 #include "BKE_global.h"
18 #include "BKE_lib_id.h"
19 #include "BKE_lib_query.h"
20 #include "BKE_main.h"
21 
22 #include "DNA_ID.h"
23 /* Those following are only to support hack of not listing some internal
24  * 'backward' pointers in generated user_map. */
25 #include "DNA_key_types.h"
26 #include "DNA_object_types.h"
27 
28 #include "WM_api.h"
29 #include "WM_types.h"
30 
31 #include "bpy_capi_utils.h"
32 #include "bpy_rna_id_collection.h"
33 
34 #include "../generic/py_capi_rna.h"
35 #include "../generic/py_capi_utils.h"
36 #include "../generic/python_utildefines.h"
37 
38 #include "RNA_access.h"
39 #include "RNA_enum_types.h"
40 #include "RNA_types.h"
41 
42 #include "bpy_rna.h"
43 
44 typedef struct IDUserMapData {
46  PyObject *py_id_curr;
48 
51 
53  PyObject *user_map;
55  bool is_subset;
57 
58 static int id_code_as_index(const short idcode)
59 {
60  return (int)*((ushort *)&idcode);
61 }
62 
63 static bool id_check_type(const ID *id, const BLI_bitmap *types_bitmap)
64 {
65  return BLI_BITMAP_TEST_BOOL(types_bitmap, id_code_as_index(GS(id->name)));
66 }
67 
69 {
70  ID **id_p = cb_data->id_pointer;
71 
72  if (*id_p) {
73  IDUserMapData *data = cb_data->user_data;
74  const int cb_flag = cb_data->cb_flag;
75 
76  if (data->types_bitmap) {
77  if (!id_check_type(*id_p, data->types_bitmap)) {
78  return IDWALK_RET_NOP;
79  }
80  }
81 
82  if (cb_flag & IDWALK_CB_LOOPBACK) {
83  /* We skip loop-back pointers like Key.from here,
84  * since it's some internal pointer which is not relevant info for py/API level. */
85  return IDWALK_RET_NOP;
86  }
87 
88  if (cb_flag & IDWALK_CB_EMBEDDED) {
89  /* We skip private pointers themselves, like root node trees, we'll 'link' their own ID
90  * pointers to their 'ID owner' instead. */
91  return IDWALK_RET_NOP;
92  }
93 
94  PyObject *key = pyrna_id_CreatePyObject(*id_p);
95 
96  PyObject *set;
97  if ((set = PyDict_GetItem(data->user_map, key)) == NULL) {
98  /* limit to key's added already */
99  if (data->is_subset) {
100  return IDWALK_RET_NOP;
101  }
102 
103  set = PySet_New(NULL);
104  PyDict_SetItem(data->user_map, key, set);
105  Py_DECREF(set);
106  }
107  Py_DECREF(key);
108 
109  if (data->py_id_curr == NULL) {
110  data->py_id_curr = pyrna_id_CreatePyObject(data->id_curr);
111  }
112 
113  PySet_Add(set, data->py_id_curr);
114  }
115 
116  return IDWALK_RET_NOP;
117 }
118 
119 PyDoc_STRVAR(bpy_user_map_doc,
120  ".. method:: user_map(subset, key_types, value_types)\n"
121  "\n"
122  " Returns a mapping of all ID data-blocks in current ``bpy.data`` to a set of all "
123  "datablocks using them.\n"
124  "\n"
125  " For list of valid set members for key_types & value_types, see: "
126  ":class:`bpy.types.KeyingSetPath.id_type`.\n"
127  "\n"
128  " :arg subset: When passed, only these data-blocks and their users will be "
129  "included as keys/values in the map.\n"
130  " :type subset: sequence\n"
131  " :arg key_types: Filter the keys mapped by ID types.\n"
132  " :type key_types: set of strings\n"
133  " :arg value_types: Filter the values in the set by ID types.\n"
134  " :type value_types: set of strings\n"
135  " :return: dictionary of :class:`bpy.types.ID` instances, with sets of ID's as "
136  "their values.\n"
137  " :rtype: dict\n");
138 static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
139 {
140 #if 0 /* If someone knows how to get a proper 'self' in that case... */
141  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
142  Main *bmain = pyrna->ptr.data;
143 #else
144  Main *bmain = G_MAIN; /* XXX Ugly, but should work! */
145 #endif
146  ListBase *lb;
147  ID *id;
148 
149  PyObject *subset = NULL;
150 
151  PyObject *key_types = NULL;
152  PyObject *val_types = NULL;
153  BLI_bitmap *key_types_bitmap = NULL;
154  BLI_bitmap *val_types_bitmap = NULL;
155 
156  PyObject *ret = NULL;
157 
158  IDUserMapData data_cb = {NULL};
159 
160  static const char *_keywords[] = {"subset", "key_types", "value_types", NULL};
161  static _PyArg_Parser _parser = {
162  "|$" /* Optional keyword only arguments. */
163  "O" /* `subset` */
164  "O!" /* `key_types` */
165  "O!" /* `value_types` */
166  ":user_map",
167  _keywords,
168  0,
169  };
170  if (!_PyArg_ParseTupleAndKeywordsFast(
171  args, kwds, &_parser, &subset, &PySet_Type, &key_types, &PySet_Type, &val_types)) {
172  return NULL;
173  }
174 
175  if (key_types) {
176  key_types_bitmap = pyrna_enum_bitmap_from_set(
177  rna_enum_id_type_items, key_types, sizeof(short), true, USHRT_MAX, "key types");
178  if (key_types_bitmap == NULL) {
179  goto error;
180  }
181  }
182 
183  if (val_types) {
184  val_types_bitmap = pyrna_enum_bitmap_from_set(
185  rna_enum_id_type_items, val_types, sizeof(short), true, USHRT_MAX, "value types");
186  if (val_types_bitmap == NULL) {
187  goto error;
188  }
189  }
190 
191  if (subset) {
192  PyObject *subset_fast = PySequence_Fast(subset, "user_map");
193  if (subset_fast == NULL) {
194  goto error;
195  }
196 
197  PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
198  Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
199 
200  data_cb.user_map = _PyDict_NewPresized(subset_len);
201  data_cb.is_subset = true;
202  for (; subset_len; subset_array++, subset_len--) {
203  PyObject *set = PySet_New(NULL);
204  PyDict_SetItem(data_cb.user_map, *subset_array, set);
205  Py_DECREF(set);
206  }
207  Py_DECREF(subset_fast);
208  }
209  else {
210  data_cb.user_map = PyDict_New();
211  }
212 
213  data_cb.types_bitmap = key_types_bitmap;
214 
215  FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
217  /* We cannot skip here in case we have some filter on key types... */
218  if (key_types_bitmap == NULL && val_types_bitmap != NULL) {
219  if (!id_check_type(id, val_types_bitmap)) {
220  break;
221  }
222  }
223 
224  if (!data_cb.is_subset &&
225  /* We do not want to pre-add keys of filtered out types. */
226  (key_types_bitmap == NULL || id_check_type(id, key_types_bitmap)) &&
227  /* We do not want to pre-add keys when we have filter on value types,
228  * but not on key types. */
229  (val_types_bitmap == NULL || key_types_bitmap != NULL)) {
230  PyObject *key = pyrna_id_CreatePyObject(id);
231  PyObject *set;
232 
233  /* We have to insert the key now,
234  * otherwise ID unused would be missing from final dict... */
235  if ((set = PyDict_GetItem(data_cb.user_map, key)) == NULL) {
236  set = PySet_New(NULL);
237  PyDict_SetItem(data_cb.user_map, key, set);
238  Py_DECREF(set);
239  }
240  Py_DECREF(key);
241  }
242 
243  if (val_types_bitmap != NULL && !id_check_type(id, val_types_bitmap)) {
244  continue;
245  }
246 
247  data_cb.id_curr = id;
250 
251  if (data_cb.py_id_curr) {
252  Py_DECREF(data_cb.py_id_curr);
253  data_cb.py_id_curr = NULL;
254  }
255  }
257  }
259 
260  ret = data_cb.user_map;
261 
262 error:
263  if (key_types_bitmap != NULL) {
264  MEM_freeN(key_types_bitmap);
265  }
266 
267  if (val_types_bitmap != NULL) {
268  MEM_freeN(val_types_bitmap);
269  }
270 
271  return ret;
272 }
273 
274 PyDoc_STRVAR(bpy_batch_remove_doc,
275  ".. method:: batch_remove(ids)\n"
276  "\n"
277  " Remove (delete) several IDs at once.\n"
278  "\n"
279  " WARNING: Considered experimental feature currently.\n"
280  "\n"
281  " Note that this function is quicker than individual calls to :func:`remove()` "
282  "(from :class:`bpy.types.BlendData`\n"
283  " ID collections), but less safe/versatile (it can break Blender, e.g. by removing "
284  "all scenes...).\n"
285  "\n"
286  " :arg ids: Iterables of IDs (types can be mixed).\n"
287  " :type subset: sequence\n");
288 static PyObject *bpy_batch_remove(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
289 {
290 #if 0 /* If someone knows how to get a proper 'self' in that case... */
291  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
292  Main *bmain = pyrna->ptr.data;
293 #else
294  Main *bmain = G_MAIN; /* XXX Ugly, but should work! */
295 #endif
296 
297  PyObject *ids = NULL;
298 
299  PyObject *ret = NULL;
300 
301  static const char *_keywords[] = {"ids", NULL};
302  static _PyArg_Parser _parser = {
303  "O" /* `ids` */
304  ":batch_remove",
305  _keywords,
306  0,
307  };
308  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &ids)) {
309  return ret;
310  }
311 
312  if (ids) {
313  BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
314 
315  PyObject *ids_fast = PySequence_Fast(ids, "batch_remove");
316  if (ids_fast == NULL) {
317  goto error;
318  }
319 
320  PyObject **ids_array = PySequence_Fast_ITEMS(ids_fast);
321  Py_ssize_t ids_len = PySequence_Fast_GET_SIZE(ids_fast);
322 
323  for (; ids_len; ids_array++, ids_len--) {
324  ID *id;
325  if (!pyrna_id_FromPyObject(*ids_array, &id)) {
326  PyErr_Format(
327  PyExc_TypeError, "Expected an ID type, not %.200s", Py_TYPE(*ids_array)->tp_name);
328  Py_DECREF(ids_fast);
329  goto error;
330  }
331 
332  id->tag |= LIB_TAG_DOIT;
333  }
334  Py_DECREF(ids_fast);
335 
337  /* Force full redraw, mandatory to avoid crashes when running this from UI... */
339  }
340  else {
341  goto error;
342  }
343 
344  Py_INCREF(Py_None);
345  ret = Py_None;
346 
347 error:
348  return ret;
349 }
350 
351 PyDoc_STRVAR(bpy_orphans_purge_doc,
352  ".. method:: orphans_purge()\n"
353  "\n"
354  " Remove (delete) all IDs with no user.\n"
355  "\n"
356  " :arg do_local_ids: Include unused local IDs in the deletion, defaults to True\n"
357  " :type do_local_ids: bool, optional\n"
358  " :arg do_linked_ids: Include unused linked IDs in the deletion, defaults to True\n"
359  " :type do_linked_ids: bool, optional\n"
360  " :arg do_recursive: Recursively check for unused IDs, ensuring no orphaned one "
361  "remain after a single run of that function, defaults to False\n"
362  " :type do_recursive: bool, optional\n"
363  " :return: The number of deleted IDs.\n");
364 static PyObject *bpy_orphans_purge(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
365 {
366 #if 0 /* If someone knows how to get a proper 'self' in that case... */
367  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
368  Main *bmain = pyrna->ptr.data;
369 #else
370  Main *bmain = G_MAIN; /* XXX Ugly, but should work! */
371 #endif
372 
373  int num_tagged[INDEX_ID_MAX] = {0};
374 
375  bool do_local_ids = true;
376  bool do_linked_ids = true;
377  bool do_recursive_cleanup = false;
378 
379  static const char *_keywords[] = {"do_local_ids", "do_linked_ids", "do_recursive", NULL};
380  static _PyArg_Parser _parser = {
381  "|" /* Optional arguments. */
382  "O&" /* `do_local_ids` */
383  "O&" /* `do_linked_ids` */
384  "O&" /* `do_recursive` */
385  ":orphans_purge",
386  _keywords,
387  0,
388  };
389  if (!_PyArg_ParseTupleAndKeywordsFast(args,
390  kwds,
391  &_parser,
393  &do_local_ids,
395  &do_linked_ids,
397  &do_recursive_cleanup)) {
398  return NULL;
399  }
400 
401  /* Tag all IDs to delete. */
403  bmain, LIB_TAG_DOIT, do_local_ids, do_linked_ids, do_recursive_cleanup, num_tagged);
404 
405  if (num_tagged[INDEX_ID_NULL] == 0) {
406  return PyLong_FromSize_t(0);
407  }
408 
409  const size_t num_datablocks_deleted = BKE_id_multi_tagged_delete(bmain);
410  /* Force full redraw, mandatory to avoid crashes when running this from UI... */
412 
413  return PyLong_FromSize_t(num_datablocks_deleted);
414 }
415 
417  "user_map",
418  (PyCFunction)bpy_user_map,
419  METH_STATIC | METH_VARARGS | METH_KEYWORDS,
420  bpy_user_map_doc,
421 };
423  "batch_remove",
424  (PyCFunction)bpy_batch_remove,
425  METH_STATIC | METH_VARARGS | METH_KEYWORDS,
426  bpy_batch_remove_doc,
427 };
429  "orphans_purge",
430  (PyCFunction)bpy_orphans_purge,
431  METH_STATIC | METH_VARARGS | METH_KEYWORDS,
432  bpy_orphans_purge_doc,
433 };
#define G_MAIN
Definition: BKE_global.h:267
void BKE_main_id_tag_all(struct Main *mainvar, int tag, bool value)
Definition: lib_id.c:930
size_t BKE_id_multi_tagged_delete(struct Main *bmain) ATTR_NONNULL()
void BKE_library_foreach_ID_link(struct Main *bmain, struct ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
Definition: lib_query.c:350
void BKE_lib_query_unused_ids_tag(struct Main *bmain, int tag, bool do_local_ids, bool do_linked_ids, bool do_tag_recursive, int *r_num_tagged)
Definition: lib_query.c:778
@ IDWALK_CB_LOOPBACK
Definition: BKE_lib_query.h:54
@ IDWALK_CB_EMBEDDED
Definition: BKE_lib_query.h:48
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:33
@ IDWALK_RET_NOP
Definition: BKE_lib_query.h:83
#define FOREACH_MAIN_LISTBASE_ID_END
Definition: BKE_main.h:336
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition: BKE_main.h:330
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition: BKE_main.h:341
#define BLI_BITMAP_TEST_BOOL(_bitmap, _index)
Definition: BLI_bitmap.h:74
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
unsigned short ushort
Definition: BLI_sys_types.h:68
#define UNUSED(x)
ID and Library types, which are fundamental for sdna.
@ INDEX_ID_NULL
Definition: DNA_ID.h:1057
@ INDEX_ID_MAX
Definition: DNA_ID.h:1058
@ LIB_TAG_DOIT
Definition: DNA_ID.h:707
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
#define NC_WINDOW
Definition: WM_types.h:325
static bool id_check_type(const ID *id, const BLI_bitmap *types_bitmap)
static int id_code_as_index(const short idcode)
static PyObject * bpy_orphans_purge(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
PyMethodDef BPY_rna_id_collection_batch_remove_method_def
struct IDUserMapData IDUserMapData
static int foreach_libblock_id_user_map_callback(LibraryIDLinkCallbackData *cb_data)
PyDoc_STRVAR(bpy_user_map_doc, ".. method:: user_map(subset, key_types, value_types)\n" "\n" " Returns a mapping of all ID data-blocks in current ``bpy.data`` to a set of all " "datablocks using them.\n" "\n" " For list of valid set members for key_types & value_types, see: " ":class:`bpy.types.KeyingSetPath.id_type`.\n" "\n" " :arg subset: When passed, only these data-blocks and their users will be " "included as keys/values in the map.\n" " :type subset: sequence\n" " :arg key_types: Filter the keys mapped by ID types.\n" " :type key_types: set of strings\n" " :arg value_types: Filter the values in the set by ID types.\n" " :type value_types: set of strings\n" " :return: dictionary of :class:`bpy.types.ID` instances, with sets of ID's as " "their values.\n" " :rtype: dict\n")
PyMethodDef BPY_rna_id_collection_orphans_purge_method_def
static PyObject * bpy_batch_remove(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
static PyObject * bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
PyMethodDef BPY_rna_id_collection_user_map_method_def
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition: bpy_rna.c:7646
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition: bpy_rna.c:7657
#define GS(x)
Definition: iris.c:225
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static void error(const char *str)
Definition: meshlaplacian.c:51
BLI_bitmap * pyrna_enum_bitmap_from_set(const EnumPropertyItem *items, PyObject *value, int type_size, bool type_convert_sign, int bitmap_size, const char *error_prefix)
Definition: py_capi_rna.c:70
int PyC_ParseBool(PyObject *o, void *p)
return ret
const EnumPropertyItem rna_enum_id_type_items[]
Definition: rna_ID.c:33
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:111
BLI_bitmap * types_bitmap
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
Definition: BKE_main.h:121
void * data
Definition: RNA_types.h:38
void WM_main_add_notifier(unsigned int type, void *reference)