Blender V5.3
bpy_rna_id_collection.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <Python.h>
12#include <cstddef>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_bitmap.hh"
17#include "BLI_string.hh"
18
19#include "BKE_bpath.hh"
20#include "BKE_global.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_lib_query.hh"
23#include "BKE_main.hh"
24
25#include "DNA_ID.h"
26/* Those following are only to support hack of not listing some internal
27 * 'backward' pointers in generated user_map. */
28
29#include "WM_api.hh"
30#include "WM_types.hh"
31
33
36#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
38
39#include "RNA_enum_types.hh"
40#include "RNA_prototypes.hh"
41
42#include "bpy_rna.hh"
43
44namespace blender {
45
46static Main *pyrna_bmain_FromPyObject(PyObject *obj)
47{
48 if (!BPy_StructRNA_Check(obj)) {
49 PyErr_Format(PyExc_TypeError,
50 "Expected a StructRNA of type BlendData, not %.200s",
51 Py_TYPE(obj)->tp_name);
52 return nullptr;
53 }
54 BPy_StructRNA *pyrna = reinterpret_cast<BPy_StructRNA *>(obj);
56 if (!(pyrna->ptr && pyrna->ptr->type == RNA_BlendData && *pyrna->ptr)) {
57 PyErr_Format(PyExc_TypeError,
58 "Expected a StructRNA of type BlendData, not %.200s",
59 Py_TYPE(pyrna)->tp_name);
60 return nullptr;
61 }
62 return static_cast<Main *>(pyrna->ptr->data);
63}
64
67 PyObject *py_id_curr;
69
72
74 PyObject *user_map;
77};
78
79static int id_code_as_index(const short idcode)
80{
81 return int(*(reinterpret_cast<ushort *>(const_cast<short *>(&idcode))));
82}
83
84static bool id_check_type(const ID *id, const BLI_bitmap *types_bitmap)
85{
86 return BLI_BITMAP_TEST_BOOL(types_bitmap, id_code_as_index(id->id_type()));
87}
88
90{
91 ID **id_p = cb_data->id_pointer;
92
93 if (*id_p) {
94 IDUserMapData *data = static_cast<IDUserMapData *>(cb_data->user_data);
95 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
96
97 if (data->types_bitmap) {
98 if (!id_check_type(*id_p, data->types_bitmap)) {
99 return IDWALK_RET_NOP;
100 }
101 }
102
103 if (cb_flag & IDWALK_CB_LOOPBACK) {
104 /* We skip loop-back pointers like Key.from here,
105 * since it's some internal pointer which is not relevant info for py/API level. */
106 return IDWALK_RET_NOP;
107 }
108
110 /* We skip private pointers themselves, like root node trees, we'll 'link' their own ID
111 * pointers to their 'ID owner' instead. */
112 return IDWALK_RET_NOP;
113 }
114
115 PyObject *key = pyrna_id_CreatePyObject(*id_p);
116
117 PyObject *set;
118 if ((set = PyDict_GetItem(data->user_map, key)) == nullptr) {
119 /* limit to key's added already */
120 if (data->is_subset) {
121 Py_DECREF(key);
122 return IDWALK_RET_NOP;
123 }
124
125 set = PySet_New(nullptr);
126 PyDict_SetItem(data->user_map, key, set);
127 Py_DECREF(set);
128 }
129 Py_DECREF(key);
130
131 if (data->py_id_curr == nullptr) {
132 data->py_id_curr = pyrna_id_CreatePyObject(data->id_curr);
133 }
134
135 PySet_Add(set, data->py_id_curr);
136 }
137
138 return IDWALK_RET_NOP;
139}
140
142 /* Wrap. */
143 bpy_user_map_doc,
144 ".. method:: user_map(*, subset=None, key_types=None, value_types=None)\n"
145 "\n"
146 " Returns a mapping of all ID data-blocks in current ``bpy.data`` to a set of all "
147 "data-blocks using them.\n"
148 "\n"
149 " For list of valid set members for key_types & value_types, see: "
150 ":class:`bpy.types.KeyingSetPath.id_type`.\n"
151 "\n"
152 " :param subset: When passed, only these data-blocks and their users will be "
153 "included as keys/values in the map.\n"
154 " :type subset: Sequence[:class:`bpy.types.ID`] | None\n"
155 " :param key_types: Filter the keys mapped by ID types.\n"
156 " :type key_types: set[str] | None\n"
157 " :param value_types: Filter the values in the set by ID types.\n"
158 " :type value_types: set[str] | None\n"
159 " :return: dictionary that maps data-blocks ID's to their users.\n"
160 " :rtype: dict[:class:`bpy.types.ID`, set[:class:`bpy.types.ID`]]\n");
161static PyObject *bpy_user_map(PyObject *self, PyObject *args, PyObject *kwds)
162{
163 Main *bmain = pyrna_bmain_FromPyObject(self);
164 if (!bmain) {
165 return nullptr;
166 }
167
168 ListBaseT<ID> *lb;
169 ID *id;
170
171 PyObject *subset = Py_None;
172
173 PyObject *key_types = nullptr;
174 PyC_TypeOrNone key_types_or_none = {&PySet_Type, &key_types};
175 PyObject *val_types = nullptr;
176 PyC_TypeOrNone val_types_or_none = {&PySet_Type, &val_types};
177 BLI_bitmap *key_types_bitmap = nullptr;
178 BLI_bitmap *val_types_bitmap = nullptr;
179
180 PyObject *ret = nullptr;
181
182 IDUserMapData data_cb = {nullptr};
183
184 static const char *_keywords[] = {"subset", "key_types", "value_types", nullptr};
185 static _PyArg_Parser _parser = {
186 "|$" /* Optional, keyword only arguments. */
187 "O" /* `subset` */
188 "O&" /* `key_types` */
189 "O&" /* `value_types` */
190 ":user_map",
191 _keywords,
192 nullptr,
193 };
194 if (!_PyArg_ParseTupleAndKeywordsFast(args,
195 kwds,
196 &_parser,
197 &subset,
199 &key_types_or_none,
201 &val_types_or_none))
202 {
203 return nullptr;
204 }
205
206 if (key_types) {
207 key_types_bitmap = pyrna_enum_bitmap_from_set(
208 rna_enum_id_type_items, key_types, sizeof(short), true, USHRT_MAX, "key types");
209 if (key_types_bitmap == nullptr) {
210 goto error;
211 }
212 }
213
214 if (val_types) {
215 val_types_bitmap = pyrna_enum_bitmap_from_set(
216 rna_enum_id_type_items, val_types, sizeof(short), true, USHRT_MAX, "value types");
217 if (val_types_bitmap == nullptr) {
218 goto error;
219 }
220 }
221
222 if (subset != Py_None) {
223 PyObject *subset_fast = PySequence_Fast(subset, "user_map");
224 if (subset_fast == nullptr) {
225 goto error;
226 }
227
228 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
229 Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
230
231 data_cb.user_map = _PyDict_NewPresized(subset_len);
232 data_cb.is_subset = true;
233 for (; subset_len; subset_array++, subset_len--) {
234 ID *id;
235 if (!pyrna_id_FromPyObject(*subset_array, &id)) {
236 PyErr_Format(PyExc_TypeError,
237 "Expected an ID type in `subset` iterable, not %.200s",
238 Py_TYPE(*subset_array)->tp_name);
239 Py_DECREF(subset_fast);
240 Py_DECREF(data_cb.user_map);
241 goto error;
242 }
243
244 if (!PyDict_Contains(data_cb.user_map, *subset_array)) {
245 PyObject *set = PySet_New(nullptr);
246 PyDict_SetItem(data_cb.user_map, *subset_array, set);
247 Py_DECREF(set);
248 }
249 }
250 Py_DECREF(subset_fast);
251 }
252 else {
253 data_cb.user_map = PyDict_New();
254 }
255
256 data_cb.types_bitmap = key_types_bitmap;
257
258 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
260 /* We cannot skip here in case we have some filter on key types... */
261 if (key_types_bitmap == nullptr && val_types_bitmap != nullptr) {
262 if (!id_check_type(id, val_types_bitmap)) {
263 break;
264 }
265 }
266
267 if (!data_cb.is_subset &&
268 /* We do not want to pre-add keys of filtered out types. */
269 (key_types_bitmap == nullptr || id_check_type(id, key_types_bitmap)) &&
270 /* We do not want to pre-add keys when we have filter on value types,
271 * but not on key types. */
272 (val_types_bitmap == nullptr || key_types_bitmap != nullptr))
273 {
274 PyObject *key = pyrna_id_CreatePyObject(id);
275 PyObject *set;
276
277 /* We have to insert the key now,
278 * otherwise ID unused would be missing from final dict... */
279 if ((set = PyDict_GetItem(data_cb.user_map, key)) == nullptr) {
280 set = PySet_New(nullptr);
281 PyDict_SetItem(data_cb.user_map, key, set);
282 Py_DECREF(set);
283 }
284 Py_DECREF(key);
285 }
286
287 if (val_types_bitmap != nullptr && !id_check_type(id, val_types_bitmap)) {
288 continue;
289 }
290
291 data_cb.id_curr = id;
293 nullptr, id, foreach_libblock_id_user_map_callback, &data_cb, IDWALK_NOP);
294
295 if (data_cb.py_id_curr) {
296 Py_DECREF(data_cb.py_id_curr);
297 data_cb.py_id_curr = nullptr;
298 }
299 }
301 }
303
304 ret = data_cb.user_map;
305
306error:
307 if (key_types_bitmap != nullptr) {
308 MEM_delete(key_types_bitmap);
309 }
310 if (val_types_bitmap != nullptr) {
311 MEM_delete(val_types_bitmap);
312 }
313
314 return ret;
315}
316
318 /* Data unchanged for the whole process. */
319
321 PyObject *file_path_map;
322
325
326 /* Data modified for each processed ID. */
327
332};
333
335 char * /*path_dst*/,
336 size_t /*path_dst_maxncpy*/,
337 const char *path_src)
338{
339 IDFilePathMapData &data = *static_cast<IDFilePathMapData *>(bpath_data->user_data);
340 PyObject *id_file_path_set = data.id_file_path_set;
341
342 BLI_assert(data.id == bpath_data->owner_id);
343
344 if (path_src && *path_src) {
345 PyObject *path = PyC_UnicodeFromBytes(path_src);
346 PySet_Add(id_file_path_set, path);
347 Py_DECREF(path);
348 }
349 return false;
350}
351
353{
354 IDFilePathMapData &data = *static_cast<IDFilePathMapData *>(bpath_data.user_data);
355 ID *id = data.id;
356 PyObject *id_file_path_set = data.id_file_path_set;
357
358 if (data.include_libraries && ID_IS_LINKED(id)) {
359 PyObject *path = PyC_UnicodeFromBytes(id->lib->filepath);
360 PySet_Add(id_file_path_set, path);
361 Py_DECREF(path);
362 }
363
364 BKE_bpath_foreach_path_id(&bpath_data, id);
365}
366
368 /* Wrap. */
369 bpy_file_path_map_doc,
370 ".. method:: file_path_map(*, subset=None, key_types=None, include_libraries=False)\n"
371 "\n"
372 " Returns a mapping of all ID data-blocks in current ``bpy.data`` to a set of all "
373 "file paths used by them.\n"
374 "\n"
375 " For list of valid set members for key_types, see: "
376 ":class:`bpy.types.KeyingSetPath.id_type`.\n"
377 "\n"
378 " :param subset: When given, only these data-blocks and their used file paths "
379 "will be included as keys/values in the map.\n"
380 " :type subset: Sequence[:class:`bpy.types.ID`] | None\n"
381 " :param key_types: When given, filter the keys mapped by ID types. "
382 "Ignored if ``subset`` is also given.\n"
383 " :type key_types: set[str] | None\n"
384 " :param include_libraries: Include library file paths of linked data. False by default.\n"
385 " :type include_libraries: bool\n"
386 " :return: dictionary of :class:`bpy.types.ID` instances, with sets of file path "
387 "strings as their values.\n"
388 " :rtype: dict[:class:`bpy.types.ID`, set[str]]\n");
389static PyObject *bpy_file_path_map(PyObject *self, PyObject *args, PyObject *kwds)
390{
391 Main *bmain = pyrna_bmain_FromPyObject(self);
392 if (!bmain) {
393 return nullptr;
394 }
395
396 PyObject *subset = Py_None;
397
398 PyObject *key_types = nullptr;
399 PyC_TypeOrNone key_types_or_none = {&PySet_Type, &key_types};
400 PyObject *include_libraries = nullptr;
401 BLI_bitmap *key_types_bitmap = nullptr;
402
403 PyObject *ret = nullptr;
404
405 IDFilePathMapData filepathmap_data{};
406 BPathForeachPathData bpath_data{};
407
408 static const char *_keywords[] = {"subset", "key_types", "include_libraries", nullptr};
409 static _PyArg_Parser _parser = {
410 "|$" /* Optional, keyword only arguments. */
411 "O" /* `subset` */
412 "O&" /* `key_types` */
413 "O!" /* `include_libraries` */
414 ":file_path_map",
415 _keywords,
416 nullptr,
417 };
418 if (!_PyArg_ParseTupleAndKeywordsFast(args,
419 kwds,
420 &_parser,
421 &subset,
423 &key_types_or_none,
424 &PyBool_Type,
425 &include_libraries))
426 {
427 return nullptr;
428 }
429
430 if (key_types) {
431 key_types_bitmap = pyrna_enum_bitmap_from_set(
432 rna_enum_id_type_items, key_types, sizeof(short), true, USHRT_MAX, "key types");
433 if (key_types_bitmap == nullptr) {
434 goto error;
435 }
436 }
437
438 bpath_data.bmain = bmain;
440 /* TODO: needs to be controllable from caller (add more options to the API). */
442 bpath_data.user_data = &filepathmap_data;
443
444 filepathmap_data.include_libraries = (include_libraries == Py_True);
445
446 if (subset != Py_None) {
447 PyObject *subset_fast = PySequence_Fast(subset, "subset");
448 if (subset_fast == nullptr) {
449 goto error;
450 }
451
452 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
453 Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
454
455 filepathmap_data.file_path_map = _PyDict_NewPresized(subset_len);
456 for (; subset_len; subset_array++, subset_len--) {
457 if (PyDict_Contains(filepathmap_data.file_path_map, *subset_array)) {
458 continue;
459 }
460
461 ID *id;
462 if (!pyrna_id_FromPyObject(*subset_array, &id)) {
463 PyErr_Format(PyExc_TypeError,
464 "Expected an ID type in `subset` iterable, not %.200s",
465 Py_TYPE(*subset_array)->tp_name);
466 Py_DECREF(subset_fast);
467 Py_DECREF(filepathmap_data.file_path_map);
468 goto error;
469 }
470
471 filepathmap_data.id_file_path_set = PySet_New(nullptr);
472 PyDict_SetItem(
473 filepathmap_data.file_path_map, *subset_array, filepathmap_data.id_file_path_set);
474 Py_DECREF(filepathmap_data.id_file_path_set);
475
476 filepathmap_data.id = id;
477 foreach_id_file_path_map(bpath_data);
478 }
479 Py_DECREF(subset_fast);
480 }
481 else {
482 ListBaseT<ID> *lb;
483 ID *id;
484 filepathmap_data.file_path_map = PyDict_New();
485
486 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
488 /* We can skip here in case we have some filter on key types. */
489 if (key_types_bitmap && !id_check_type(id, key_types_bitmap)) {
490 break;
491 }
492
493 PyObject *key = pyrna_id_CreatePyObject(id);
494 filepathmap_data.id_file_path_set = PySet_New(nullptr);
495 PyDict_SetItem(filepathmap_data.file_path_map, key, filepathmap_data.id_file_path_set);
496 Py_DECREF(filepathmap_data.id_file_path_set);
497 Py_DECREF(key);
498
499 filepathmap_data.id = id;
500 foreach_id_file_path_map(bpath_data);
501 }
503 }
505 }
506
507 ret = filepathmap_data.file_path_map;
508
509error:
510 if (key_types_bitmap != nullptr) {
511 MEM_delete(key_types_bitmap);
512 }
513
514 return ret;
515}
516
526 PyObject *visit_path_fn;
527
534};
535
543 /* BKE_BPATH_FOREACH_PATH_ABSOLUTE is not included here, as its only use is to initialize a
544 * field in BPathForeachPathData that is not used by the callback. */
546 "SKIP_LINKED",
547 0,
548 "Skip Linked",
549 "Skip paths of linked IDs"},
551 "SKIP_PACKED",
552 0,
553 "Skip Packed",
554 "Skip paths when their matching data is packed"},
556 "SKIP_WEAK_REFERENCES",
557 0,
558 "Skip Weak References",
559 "Skip weak reference paths. Those paths are typically 'nice to have' extra information, but "
560 "are not used as actual source of data by the current .blend file"},
562 "SKIP_MULTIFILE",
563 0,
564 "Skip Multi-file",
565 "Skip paths where a single dir is used with an array of files, eg. sequence strip images or "
566 "point-caches. In this case only the first file path is processed. This is needed for "
567 "directory manipulation callbacks which might otherwise modify the same directory multiple "
568 "times"},
570 "RELOAD_EDITED",
571 0,
572 "Reload Edited",
573 "Reload data when the path is edited"},
575 "RESOLVE_TOKEN",
576 0,
577 "Resolve Tokens",
578 "Resolve tokens within a virtual filepath to a single, concrete, filepath. Currently only "
579 "used for UDIM tiles"},
581 "EXPAND_TOKENS",
582 0,
583 "Expand Tokens",
584 "Expand template tokens in virtual file paths and invoke the callback once for each "
585 "concrete file. Currently only used for UDIM tiles"},
587 "EXPAND_SEQUENCES",
588 0,
589 "Expand Sequences",
590 "Expand image and volume sequences, invoking the callback once per file on disk"},
592 "EXPAND_CACHES",
593 0,
594 "Expand Caches",
595 "Visit cache files, like texture cache files associated with images"},
596 {0, nullptr, 0, nullptr, nullptr},
597};
598
608 PyObject_HEAD
612};
613
615 /* Wrap. */
616 bpy_file_path_meta_is_expanded_doc,
617 "True when the path was expanded from a UDIM tile or sequence frame. These paths can not be "
618 "edited.\n"
619 "\n"
620 ":type: bool\n");
621static PyObject *bpy_file_path_meta_get_is_expanded(BPy_FilePathMeta *self, void * /*closure*/)
622{
623 return PyBool_FromLong(self->is_expanded);
624}
625
627 /* Wrap. */
628 bpy_file_path_meta_is_cache_doc,
629 "True when the path is a cache file, like the image texture cache. These paths can not be "
630 "edited.\n"
631 "\n"
632 ":type: bool\n");
633static PyObject *bpy_file_path_meta_get_is_cache(BPy_FilePathMeta *self, void * /*closure*/)
634{
635 return PyBool_FromLong(self->is_cache);
636}
637
639 /* Wrap. */
640 bpy_file_path_meta_is_readonly_doc,
641 "True when the path is read-only and can not be edited.\n"
642 "\n"
643 ":type: bool\n");
644static PyObject *bpy_file_path_meta_get_is_readonly(BPy_FilePathMeta *self, void * /*closure*/)
645{
646 return PyBool_FromLong(self->is_readonly);
647}
648
649static PyGetSetDef bpy_file_path_meta_getset[] = {
650 {"is_expanded",
651 reinterpret_cast<getter>(bpy_file_path_meta_get_is_expanded),
652 nullptr,
653 bpy_file_path_meta_is_expanded_doc,
654 nullptr},
655 {"is_cache",
656 reinterpret_cast<getter>(bpy_file_path_meta_get_is_cache),
657 nullptr,
658 bpy_file_path_meta_is_cache_doc,
659 nullptr},
660 {"is_readonly",
661 reinterpret_cast<getter>(bpy_file_path_meta_get_is_readonly),
662 nullptr,
663 bpy_file_path_meta_is_readonly_doc,
664 nullptr},
665 {nullptr},
666};
667
669{
670 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
671}
672
674 /* Wrap. */
675 bpy_file_path_meta_doc,
676 "Metadata about a file path visited by :class:`bpy.types.BlendData.file_path_foreach`.\n");
677static PyTypeObject BPyFilePathMeta_Type = {
678 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
679 /*tp_name*/ "BlendDataPathMeta",
680 /*tp_basicsize*/ sizeof(BPy_FilePathMeta),
681 /*tp_itemsize*/ 0,
682 /*tp_dealloc*/ reinterpret_cast<destructor>(bpy_file_path_meta_dealloc),
683 /*tp_vectorcall_offset*/ 0,
684 /*tp_getattr*/ nullptr,
685 /*tp_setattr*/ nullptr,
686 /*tp_as_async*/ nullptr,
687 /*tp_repr*/ nullptr,
688 /*tp_as_number*/ nullptr,
689 /*tp_as_sequence*/ nullptr,
690 /*tp_as_mapping*/ nullptr,
691 /*tp_hash*/ nullptr,
692 /*tp_call*/ nullptr,
693 /*tp_str*/ nullptr,
694 /*tp_getattro*/ nullptr,
695 /*tp_setattro*/ nullptr,
696 /*tp_as_buffer*/ nullptr,
697 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
698 /*tp_doc*/ bpy_file_path_meta_doc,
699 /*tp_traverse*/ nullptr,
700 /*tp_clear*/ nullptr,
701 /*tp_richcompare*/ nullptr,
702 /*tp_weaklistoffset*/ 0,
703 /*tp_iter*/ nullptr,
704 /*tp_iternext*/ nullptr,
705 /*tp_methods*/ nullptr,
706 /*tp_members*/ nullptr,
707 /*tp_getset*/ bpy_file_path_meta_getset,
708 /*tp_base*/ nullptr,
709 /*tp_dict*/ nullptr,
710 /*tp_descr_get*/ nullptr,
711 /*tp_descr_set*/ nullptr,
712 /*tp_dictoffset*/ 0,
713 /*tp_init*/ nullptr,
714 /*tp_alloc*/ nullptr,
715 /* Prevent users from creating instances directly. */
716 /*tp_new*/ nullptr,
717};
718
720{
721 if (PyType_Ready(&BPyFilePathMeta_Type) < 0) {
722 return nullptr;
723 }
724 return reinterpret_cast<PyObject *>(&BPyFilePathMeta_Type);
725}
726
728{
729 BPy_FilePathMeta *self = reinterpret_cast<BPy_FilePathMeta *>(
731 if (self == nullptr) {
732 return nullptr;
733 }
734 self->is_expanded = bpath_data->is_expanded;
735 self->is_cache = bpath_data->is_cache;
736 self->is_readonly = bpath_data->is_readonly;
737 return reinterpret_cast<PyObject *>(self);
738}
739
741 char *path_dst,
742 const size_t path_dst_maxncpy,
743 const char *path_src)
744{
745 IDFilePathForeachData &data = *static_cast<IDFilePathForeachData *>(bpath_data->user_data);
746
747 if (data.seen_error) {
748 /* The Python interpreter is already set up for reporting an exception, so don't touch it. */
749 return false;
750 }
751
752 if (!path_src || !path_src[0]) {
753 return false;
754 }
755 BLI_assert(path_dst);
756
757 /* Construct the callback function parameters. */
758 PointerRNA id_ptr = RNA_id_pointer_create(bpath_data->owner_id);
759 PyObject *args = PyTuple_New(3);
760 /* args[0]: */
761 PyObject *py_owner_id = pyrna_struct_CreatePyObject(&id_ptr);
762 /* args[1]: */
763 PyObject *py_path_src = PyUnicode_FromString(path_src);
764 /* args[2]: */
765 PyObject *py_path_meta = bpy_file_path_meta_CreatePyObject(bpath_data);
766 PyTuple_SET_ITEMS(args, py_owner_id, py_path_src, py_path_meta);
767
768 /* Call the Python callback function. */
769 PyObject *result = PyObject_CallObject(data.visit_path_fn, args);
770
771 /* Done with the function arguments. */
772 Py_DECREF(args);
773 args = nullptr;
774
775 if (result == nullptr) {
776 data.seen_error = true;
777 return false;
778 }
779
780 if (result == Py_None) {
781 /* Nothing to do. */
782 Py_DECREF(result);
783 return false;
784 }
785
786 if (!PyUnicode_Check(result)) {
787 PyErr_Format(PyExc_TypeError,
788 "visit_path_fn() should return a string or None, but returned %s for "
789 "owner_id=\"%s\" and file_path=\"%s\"",
790 Py_TYPE(result)->tp_name,
791 bpath_data->owner_id->name,
792 path_src);
793 data.seen_error = true;
794 Py_DECREF(result);
795 return false;
796 }
797
798 Py_ssize_t replacement_path_length = 0;
799 PyObject *value_coerce = nullptr;
800 const char *replacement_path = PyC_UnicodeAsBytesAndSize(
801 result, &replacement_path_length, &value_coerce);
802
803 /* Path was unchanged, treat as not edited. */
804 if (STREQ(replacement_path, path_src)) {
805 Py_XDECREF(value_coerce);
806 Py_DECREF(result);
807 return false;
808 }
809
810 if (bpath_data->is_readonly) {
811 PyErr_Format(PyExc_RuntimeError,
812 "visit_path_fn() changed a read-only path \"%s\" for owner_id=\"%s\"",
813 path_src,
814 bpath_data->owner_id->name);
815 data.seen_error = true;
816 Py_XDECREF(value_coerce);
817 Py_DECREF(result);
818 return false;
819 }
820
821 /* Copy the returned string back into the path.
822 * BLI_strncpy wants buffer size, but PyC_UnicodeAsBytesAndSize reports string
823 * length, hence the +1. */
825 path_dst, replacement_path, std::min(path_dst_maxncpy, size_t(replacement_path_length + 1)));
826
827 Py_XDECREF(value_coerce);
828 Py_DECREF(result);
829 return true;
830}
831
833 /* Wrap. */
834 bpy_file_path_foreach_doc,
835 ".. method:: file_path_foreach(visit_path_fn, *, subset=None, visit_types=None, "
836 "flags={'SKIP_PACKED', 'SKIP_WEAK_REFERENCES'})\n"
837 "\n"
838 " Call ``visit_path_fn`` for the file paths used by all ID data-blocks in current "
839 "``bpy.data``.\n"
840 "\n"
841 " For list of valid set members for visit_types, see: "
842 ":class:`bpy.types.KeyingSetPath.id_type`.\n"
843 "\n"
844 " :param visit_path_fn: function that takes three parameters: the data-block, a file path, "
845 "and a :class:`bpy.types.BlendDataPathMeta` metadata object. "
846 "The function should return either ``None`` or a ``str``. In the latter case, the visited "
847 "file path will be replaced with the returned string.\n"
848 " :type visit_path_fn: Callable[[:class:`bpy.types.ID`, str, "
849 ":class:`bpy.types.BlendDataPathMeta`], str|None]\n"
850 " :param subset: When given, only these data-blocks and their used file paths "
851 "will be visited.\n"
852 " :type subset: set[:class:`bpy.types.ID`] | None\n"
853 " :param visit_types: When given, only visit data-blocks of these types. Ignored if "
854 "``subset`` is also given.\n"
855 " :type visit_types: set[str] | None\n"
856 " :type flags: set[str]\n"
857 " :param flags: Set of flags that influence which data-blocks are visited. See "
858 ":ref:`rna_enum_file_path_foreach_flag_items`.\n");
859static PyObject *bpy_file_path_foreach(PyObject *self, PyObject *args, PyObject *kwds)
860{
861 Main *bmain = pyrna_bmain_FromPyObject(self);
862 if (!bmain) {
863 return nullptr;
864 }
865
866 PyObject *visit_path_fn = nullptr;
867 PyObject *subset = Py_None;
868 PyObject *visit_types = nullptr;
869 PyC_TypeOrNone visit_types_or_none = {&PySet_Type, &visit_types};
870 std::unique_ptr<BLI_bitmap, MEM_smart_ptr_deleter<BLI_bitmap>> visit_types_bitmap;
871 PyObject *py_flags = nullptr;
872
873 IDFilePathForeachData filepathforeach_data{};
874 BPathForeachPathData bpath_data{};
875
876 static const char *_keywords[] = {"visit_path_fn", "subset", "visit_types", "flags", nullptr};
877 static _PyArg_Parser _parser = {
878 "O!" /* `visit_path_fn` */
879 "|$" /* Optional, keyword only arguments. */
880 "O" /* `subset` */
881 "O&" /* `visit_types` */
882 "O!" /* `flags` */
883 ":file_path_foreach",
884 _keywords,
885 nullptr,
886 };
887 if (!_PyArg_ParseTupleAndKeywordsFast(args,
888 kwds,
889 &_parser,
890 &PyFunction_Type,
891 &visit_path_fn,
892 &subset,
894 &visit_types_or_none,
895 &PySet_Type,
896 &py_flags))
897 {
898 return nullptr;
899 }
900
901 if (visit_types) {
902 BLI_bitmap *visit_types_bitmap_rawptr = pyrna_enum_bitmap_from_set(
903 rna_enum_id_type_items, visit_types, sizeof(short), true, USHRT_MAX, "visit_types");
904 if (visit_types_bitmap_rawptr == nullptr) {
905 return nullptr;
906 }
907 visit_types_bitmap.reset(visit_types_bitmap_rawptr);
908 }
909
910 /* Parse the flags, start with sensible defaults. */
912 if (py_flags) {
914 py_flags,
915 reinterpret_cast<int *>(&bpath_data.flag),
916 "flags") == -1)
917 {
918 return nullptr;
919 }
920 }
921
922 bpath_data.bmain = bmain;
924 bpath_data.user_data = &filepathforeach_data;
925
926 filepathforeach_data.visit_path_fn = visit_path_fn;
927 filepathforeach_data.seen_error = false;
928
929 if (subset != Py_None) {
930 /* Visit the given subset of IDs. */
931 PyObject *subset_fast = PySequence_Fast(subset, "subset");
932 if (!subset_fast) {
933 return nullptr;
934 }
935
936 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
937 const Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
938 for (Py_ssize_t index = 0; index < subset_len; index++) {
939 PyObject *subset_item = subset_array[index];
940
941 ID *id;
942 if (!pyrna_id_FromPyObject(subset_item, &id)) {
943 PyErr_Format(PyExc_TypeError,
944 "Expected an ID type in `subset` iterable, not %.200s",
945 Py_TYPE(subset_item)->tp_name);
946 Py_DECREF(subset_fast);
947 return nullptr;
948 }
949
950 BKE_bpath_foreach_path_id(&bpath_data, id);
951 if (filepathforeach_data.seen_error) {
952 /* Whatever triggered this error should have already set up the Python
953 * interpreter for producing an exception. */
954 Py_DECREF(subset_fast);
955 return nullptr;
956 }
957 }
958 Py_DECREF(subset_fast);
959 }
960 else {
961 /* Visit all IDs, filtered by type if necessary. */
962 ListBaseT<ID> *lb;
963 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
964 ID *id;
966 if (visit_types_bitmap && !id_check_type(id, visit_types_bitmap.get())) {
967 break;
968 }
969
970 BKE_bpath_foreach_path_id(&bpath_data, id);
971 if (filepathforeach_data.seen_error) {
972 /* Whatever triggered this error should have already set up the Python
973 * interpreter for producing an exception. */
974 return nullptr;
975 }
976 }
978 }
980 }
981
982 Py_RETURN_NONE;
983}
984
986 /* Wrap. */
987 bpy_batch_remove_doc,
988 ".. method:: batch_remove(ids)\n"
989 "\n"
990 " Remove (delete) several IDs at once.\n"
991 "\n"
992 " Note that this function is quicker than individual calls to :func:`remove()` "
993 "(from :class:`bpy.types.BlendData`\n"
994 " ID collections), but less safe/versatile (it can break Blender, e.g. by removing "
995 "all scenes...).\n"
996 "\n"
997 " :param ids: Sequence of IDs (types can be mixed).\n"
998 " :type ids: Sequence[:class:`bpy.types.ID`]\n");
999static PyObject *bpy_batch_remove(PyObject *self, PyObject *args, PyObject *kwds)
1000{
1001 Main *bmain = pyrna_bmain_FromPyObject(self);
1002 if (!bmain) {
1003 return nullptr;
1004 }
1005
1006 PyObject *ids = nullptr;
1007
1008 static const char *_keywords[] = {"ids", nullptr};
1009 static _PyArg_Parser _parser = {
1010 "O" /* `ids` */
1011 ":batch_remove",
1012 _keywords,
1013 nullptr,
1014 };
1015 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &ids)) {
1016 return nullptr;
1017 }
1018
1019 if (!ids) {
1020 return nullptr;
1021 }
1022
1023 PyObject *ids_fast = PySequence_Fast(ids, "batch_remove");
1024 if (ids_fast == nullptr) {
1025 return nullptr;
1026 }
1027
1028 PyObject **ids_array = PySequence_Fast_ITEMS(ids_fast);
1029 Py_ssize_t ids_len = PySequence_Fast_GET_SIZE(ids_fast);
1030 Set<ID *> ids_to_delete;
1031 for (; ids_len; ids_array++, ids_len--) {
1032 ID *id;
1033 if (!pyrna_id_FromPyObject(*ids_array, &id)) {
1034 PyErr_Format(
1035 PyExc_TypeError, "Expected an ID type, not %.200s", Py_TYPE(*ids_array)->tp_name);
1036 Py_DECREF(ids_fast);
1037 return nullptr;
1038 }
1039
1040 ids_to_delete.add(id);
1041 }
1042 Py_DECREF(ids_fast);
1043
1044 BKE_id_multi_delete(bmain, ids_to_delete);
1045 /* Force full redraw, mandatory to avoid crashes when running this from UI... */
1047
1048 Py_RETURN_NONE;
1049}
1050
1052 /* Wrap. */
1053 bpy_orphans_purge_doc,
1054 ".. method:: orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=False)\n"
1055 "\n"
1056 " Remove (delete) all IDs with no user.\n"
1057 "\n"
1058 " :param do_local_ids: Include unused local IDs in the deletion, defaults to True\n"
1059 " :type do_local_ids: bool\n"
1060 " :param do_linked_ids: Include unused linked IDs in the deletion, defaults to True\n"
1061 " :type do_linked_ids: bool\n"
1062 " :param do_recursive: Recursively check for unused IDs, ensuring no orphaned one "
1063 "remain after a single run of that function, defaults to False\n"
1064 " :type do_recursive: bool\n"
1065 " :return: The number of deleted IDs.\n"
1066 " :rtype: int\n");
1067static PyObject *bpy_orphans_purge(PyObject *self, PyObject *args, PyObject *kwds)
1068{
1069 Main *bmain = pyrna_bmain_FromPyObject(self);
1070 if (!bmain) {
1071 return nullptr;
1072 }
1073
1074 LibQueryUnusedIDsData unused_ids_data;
1075 unused_ids_data.do_local_ids = true;
1076 unused_ids_data.do_linked_ids = true;
1077 unused_ids_data.do_recursive = false;
1078
1079 static const char *_keywords[] = {"do_local_ids", "do_linked_ids", "do_recursive", nullptr};
1080 static _PyArg_Parser _parser = {
1081 "|" /* Optional arguments. */
1082 "O&" /* `do_local_ids` */
1083 "O&" /* `do_linked_ids` */
1084 "O&" /* `do_recursive` */
1085 ":orphans_purge",
1086 _keywords,
1087 nullptr,
1088 };
1089 if (!_PyArg_ParseTupleAndKeywordsFast(args,
1090 kwds,
1091 &_parser,
1093 &unused_ids_data.do_local_ids,
1095 &unused_ids_data.do_linked_ids,
1097 &unused_ids_data.do_recursive))
1098 {
1099 return nullptr;
1100 }
1101
1102 /* Tag all IDs to delete. */
1103 BKE_lib_query_unused_ids_tag(bmain, ID_TAG_DOIT, unused_ids_data);
1104
1105 if (unused_ids_data.num_total[INDEX_ID_NULL] == 0) {
1106 return PyLong_FromSize_t(0);
1107 }
1108
1109 const size_t num_datablocks_deleted = BKE_id_multi_tagged_delete(bmain);
1110 /* Force full redraw, mandatory to avoid crashes when running this from UI... */
1112
1113 return PyLong_FromSize_t(num_datablocks_deleted);
1114}
1115
1116#ifdef __GNUC__
1117# ifdef __clang__
1118# pragma clang diagnostic push
1119# pragma clang diagnostic ignored "-Wcast-function-type"
1120# else
1121# pragma GCC diagnostic push
1122# pragma GCC diagnostic ignored "-Wcast-function-type"
1123# endif
1124#endif
1125
1127 "user_map",
1128 reinterpret_cast<PyCFunction>(bpy_user_map),
1129 METH_VARARGS | METH_KEYWORDS,
1130 bpy_user_map_doc,
1131};
1133 "file_path_map",
1134 reinterpret_cast<PyCFunction>(bpy_file_path_map),
1135 METH_VARARGS | METH_KEYWORDS,
1136 bpy_file_path_map_doc,
1137};
1139 "file_path_foreach",
1140 reinterpret_cast<PyCFunction>(bpy_file_path_foreach),
1141 METH_VARARGS | METH_KEYWORDS,
1142 bpy_file_path_foreach_doc,
1143};
1145 "batch_remove",
1146 reinterpret_cast<PyCFunction>(bpy_batch_remove),
1147 METH_VARARGS | METH_KEYWORDS,
1148 bpy_batch_remove_doc,
1149};
1151 "orphans_purge",
1152 reinterpret_cast<PyCFunction>(bpy_orphans_purge),
1153 METH_VARARGS | METH_KEYWORDS,
1154 bpy_orphans_purge_doc,
1155};
1156
1157#ifdef __GNUC__
1158# ifdef __clang__
1159# pragma clang diagnostic pop
1160# else
1161# pragma GCC diagnostic pop
1162# endif
1163#endif
1164
1165} // namespace blender
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
#define BLI_BITMAP_TEST_BOOL(_bitmap, _index)
Definition BLI_bitmap.hh:73
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
Read Blender memory allocation and freeing.
#define NC_WINDOW
Definition WM_types.hh:388
#define STREQ(a, b)
#define PYRNA_STRUCT_CHECK_OBJ(obj)
Definition bpy_rna.hh:81
#define BPy_StructRNA_Check(v)
Definition bpy_rna.hh:76
bool add(const Key &key)
Definition BLI_set.hh:248
#define USHRT_MAX
BMesh Iterators.
static int id_code_as_index(const short idcode)
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool foreach_id_file_path_map_callback(BPathForeachPathData *bpath_data, char *, size_t, const char *path_src)
static PyObject * bpy_file_path_map(PyObject *self, PyObject *args, PyObject *kwds)
static void foreach_id_file_path_map(BPathForeachPathData &bpath_data)
static void bpy_file_path_meta_dealloc(BPy_FilePathMeta *self)
const EnumPropertyItem rna_enum_id_type_items[]
Definition rna_ID.cc:33
static PyObject * bpy_file_path_foreach(PyObject *self, PyObject *args, PyObject *kwds)
int PyC_ParseBool(PyObject *o, void *p)
void BKE_lib_query_unused_ids_tag(Main *bmain, int tag, LibQueryUnusedIDsData &parameters)
int pyrna_enum_bitfield_from_set(const EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition bpy_rna.cc:8978
PyMethodDef BPY_rna_id_collection_orphans_purge_method_def
PointerRNA RNA_id_pointer_create(ID *id)
PyMethodDef BPY_rna_id_collection_batch_remove_method_def
const EnumPropertyItem rna_enum_file_path_foreach_flag_items[]
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * bpy_file_path_meta_get_is_expanded(BPy_FilePathMeta *self, void *)
unsigned short ushort
Definition BKE_text.h:56
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)
static bool foreach_id_file_path_foreach_callback(BPathForeachPathData *bpath_data, char *path_dst, const size_t path_dst_maxncpy, const char *path_src)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
PyObject * PyC_UnicodeFromBytes(const char *str)
PyObject * BPyInit_blend_data_path_meta_type()
static PyObject * bpy_file_path_meta_get_is_cache(BPy_FilePathMeta *self, void *)
static PyObject * bpy_user_map(PyObject *self, PyObject *args, PyObject *kwds)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
static bool id_check_type(const ID *id, const BLI_bitmap *types_bitmap)
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8988
size_t BKE_id_multi_tagged_delete(Main *bmain, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
static PyObject * bpy_batch_remove(PyObject *self, PyObject *args, PyObject *kwds)
void BKE_bpath_foreach_path_id(BPathForeachPathData *bpath_data, ID *id)
Definition bpath.cc:77
PyMethodDef BPY_rna_id_collection_user_map_method_def
static PyTypeObject BPyFilePathMeta_Type
static PyGetSetDef bpy_file_path_meta_getset[]
@ BKE_BPATH_FOREACH_PATH_EXPAND_TOKENS
Definition BKE_bpath.hh:87
@ BKE_BPATH_FOREACH_PATH_EXPAND_CACHES
Definition BKE_bpath.hh:97
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
Definition BKE_bpath.hh:49
@ BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE
Definition BKE_bpath.hh:76
@ BKE_BPATH_TRAVERSE_SKIP_WEAK_REFERENCES
Definition BKE_bpath.hh:62
@ BKE_BPATH_FOREACH_PATH_EXPAND_SEQUENCES
Definition BKE_bpath.hh:92
@ BKE_BPATH_FOREACH_PATH_RELOAD_EDITED
Definition BKE_bpath.hh:81
@ BKE_BPATH_FOREACH_PATH_RESOLVE_TOKEN
Definition BKE_bpath.hh:54
@ BKE_BPATH_FOREACH_PATH_SKIP_LINKED
Definition BKE_bpath.hh:47
PyMethodDef BPY_rna_id_collection_file_path_map_method_def
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
static Main * pyrna_bmain_FromPyObject(PyObject *obj)
static PyObject * bpy_file_path_meta_get_is_readonly(BPy_FilePathMeta *self, void *)
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
static PyObject * bpy_orphans_purge(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject * bpy_file_path_meta_CreatePyObject(const BPathForeachPathData *bpath_data)
LibraryForeachIDCallbackFlag
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_EMBEDDED
static int foreach_libblock_id_user_map_callback(LibraryIDLinkCallbackData *cb_data)
size_t BKE_id_multi_delete(Main *bmain, Set< ID * > &ids_to_delete, const BKEIDDeleteOptions &options={})
PyMethodDef BPY_rna_id_collection_file_path_foreach_method_def
int PyC_ParseTypeOrNone(PyObject *o, void *p)
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
const char * PyC_UnicodeAsBytesAndSize(PyObject *py_str, Py_ssize_t *r_size, PyObject **r_coerce)
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
BPathForeachPathFunctionCallback callback_function
Definition BKE_bpath.hh:124
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:133
char name[258]
Definition DNA_ID.h:433
std::array< int, INDEX_ID_MAX > num_total
LibraryForeachIDCallbackFlag cb_flag