40#include "RNA_prototypes.hh"
49 PyErr_Format(PyExc_TypeError,
50 "Expected a StructRNA of type BlendData, not %.200s",
51 Py_TYPE(obj)->tp_name);
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);
62 return static_cast<Main *
>(pyrna->
ptr->data);
81 return int(*(
reinterpret_cast<ushort *
>(
const_cast<short *
>(&idcode))));
97 if (
data->types_bitmap) {
118 if ((set = PyDict_GetItem(
data->user_map, key)) ==
nullptr) {
120 if (
data->is_subset) {
125 set = PySet_New(
nullptr);
126 PyDict_SetItem(
data->user_map, key, set);
131 if (
data->py_id_curr ==
nullptr) {
135 PySet_Add(set,
data->py_id_curr);
144 ".. method:: user_map(*, subset=None, key_types=None, value_types=None)\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"
149 " For list of valid set members for key_types & value_types, see: "
150 ":class:`bpy.types.KeyingSetPath.id_type`.\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)
171 PyObject *subset = Py_None;
173 PyObject *key_types =
nullptr;
175 PyObject *val_types =
nullptr;
180 PyObject *
ret =
nullptr;
184 static const char *_keywords[] = {
"subset",
"key_types",
"value_types",
nullptr};
185 static _PyArg_Parser _parser = {
194 if (!_PyArg_ParseTupleAndKeywordsFast(args,
209 if (key_types_bitmap ==
nullptr) {
217 if (val_types_bitmap ==
nullptr) {
222 if (subset != Py_None) {
223 PyObject *subset_fast = PySequence_Fast(subset,
"user_map");
224 if (subset_fast ==
nullptr) {
228 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
229 Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
231 data_cb.
user_map = _PyDict_NewPresized(subset_len);
233 for (; subset_len; subset_array++, subset_len--) {
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);
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);
250 Py_DECREF(subset_fast);
261 if (key_types_bitmap ==
nullptr && val_types_bitmap !=
nullptr) {
269 (key_types_bitmap ==
nullptr ||
id_check_type(
id, key_types_bitmap)) &&
272 (val_types_bitmap ==
nullptr || key_types_bitmap !=
nullptr))
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);
287 if (val_types_bitmap !=
nullptr && !
id_check_type(
id, val_types_bitmap)) {
307 if (key_types_bitmap !=
nullptr) {
308 MEM_delete(key_types_bitmap);
310 if (val_types_bitmap !=
nullptr) {
311 MEM_delete(val_types_bitmap);
337 const char *path_src)
340 PyObject *id_file_path_set =
data.id_file_path_set;
344 if (path_src && *path_src) {
346 PySet_Add(id_file_path_set, path);
356 PyObject *id_file_path_set =
data.id_file_path_set;
360 PySet_Add(id_file_path_set, path);
369 bpy_file_path_map_doc,
370 ".. method:: file_path_map(*, subset=None, key_types=None, include_libraries=False)\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"
375 " For list of valid set members for key_types, see: "
376 ":class:`bpy.types.KeyingSetPath.id_type`.\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");
396 PyObject *subset = Py_None;
398 PyObject *key_types =
nullptr;
400 PyObject *include_libraries =
nullptr;
403 PyObject *
ret =
nullptr;
408 static const char *_keywords[] = {
"subset",
"key_types",
"include_libraries",
nullptr};
409 static _PyArg_Parser _parser = {
418 if (!_PyArg_ParseTupleAndKeywordsFast(args,
433 if (key_types_bitmap ==
nullptr) {
438 bpath_data.
bmain = bmain;
442 bpath_data.
user_data = &filepathmap_data;
446 if (subset != Py_None) {
447 PyObject *subset_fast = PySequence_Fast(subset,
"subset");
448 if (subset_fast ==
nullptr) {
452 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
453 Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
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)) {
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);
476 filepathmap_data.
id =
id;
479 Py_DECREF(subset_fast);
489 if (key_types_bitmap && !
id_check_type(
id, key_types_bitmap)) {
499 filepathmap_data.
id =
id;
510 if (key_types_bitmap !=
nullptr) {
511 MEM_delete(key_types_bitmap);
549 "Skip paths of linked IDs"},
554 "Skip paths when their matching data is packed"},
556 "SKIP_WEAK_REFERENCES",
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"},
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 "
573 "Reload data when the path is edited"},
578 "Resolve tokens within a virtual filepath to a single, concrete, filepath. Currently only "
579 "used for UDIM tiles"},
584 "Expand template tokens in virtual file paths and invoke the callback once for each "
585 "concrete file. Currently only used for UDIM tiles"},
590 "Expand image and volume sequences, invoking the callback once per file on disk"},
595 "Visit cache files, like texture cache files associated with images"},
596 {0,
nullptr, 0,
nullptr,
nullptr},
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 "
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 "
635 return PyBool_FromLong(self->
is_cache);
640 bpy_file_path_meta_is_readonly_doc,
641 "True when the path is read-only and can not be edited.\n"
653 bpy_file_path_meta_is_expanded_doc,
658 bpy_file_path_meta_is_cache_doc,
663 bpy_file_path_meta_is_readonly_doc,
670 Py_TYPE(self)->tp_free(
reinterpret_cast<PyObject *
>(self));
675 bpy_file_path_meta_doc,
676 "Metadata about a file path visited by :class:`bpy.types.BlendData.file_path_foreach`.\n");
678 PyVarObject_HEAD_INIT(
nullptr, 0)
698 bpy_file_path_meta_doc,
731 if (self ==
nullptr) {
737 return reinterpret_cast<PyObject *
>(self);
742 const size_t path_dst_maxncpy,
743 const char *path_src)
747 if (
data.seen_error) {
752 if (!path_src || !path_src[0]) {
759 PyObject *args = PyTuple_New(3);
763 PyObject *py_path_src = PyUnicode_FromString(path_src);
769 PyObject *
result = PyObject_CallObject(
data.visit_path_fn, args);
776 data.seen_error =
true;
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\"",
793 data.seen_error =
true;
798 Py_ssize_t replacement_path_length = 0;
799 PyObject *value_coerce =
nullptr;
801 result, &replacement_path_length, &value_coerce);
804 if (
STREQ(replacement_path, path_src)) {
805 Py_XDECREF(value_coerce);
811 PyErr_Format(PyExc_RuntimeError,
812 "visit_path_fn() changed a read-only path \"%s\" for owner_id=\"%s\"",
815 data.seen_error =
true;
816 Py_XDECREF(value_coerce);
825 path_dst, replacement_path, std::min(path_dst_maxncpy,
size_t(replacement_path_length + 1)));
827 Py_XDECREF(value_coerce);
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"
838 " Call ``visit_path_fn`` for the file paths used by all ID data-blocks in current "
841 " For list of valid set members for visit_types, see: "
842 ":class:`bpy.types.KeyingSetPath.id_type`.\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 "
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");
866 PyObject *visit_path_fn =
nullptr;
867 PyObject *subset = Py_None;
868 PyObject *visit_types =
nullptr;
870 std::unique_ptr<BLI_bitmap, MEM_smart_ptr_deleter<BLI_bitmap>> visit_types_bitmap;
871 PyObject *py_flags =
nullptr;
876 static const char *_keywords[] = {
"visit_path_fn",
"subset",
"visit_types",
"flags",
nullptr};
877 static _PyArg_Parser _parser = {
883 ":file_path_foreach",
887 if (!_PyArg_ParseTupleAndKeywordsFast(args,
894 &visit_types_or_none,
904 if (visit_types_bitmap_rawptr ==
nullptr) {
907 visit_types_bitmap.reset(visit_types_bitmap_rawptr);
915 reinterpret_cast<int *
>(&bpath_data.
flag),
922 bpath_data.
bmain = bmain;
924 bpath_data.
user_data = &filepathforeach_data;
929 if (subset != Py_None) {
931 PyObject *subset_fast = PySequence_Fast(subset,
"subset");
936 PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast);
937 const Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast);
939 PyObject *subset_item = subset_array[
index];
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);
954 Py_DECREF(subset_fast);
958 Py_DECREF(subset_fast);
966 if (visit_types_bitmap && !
id_check_type(
id, visit_types_bitmap.get())) {
987 bpy_batch_remove_doc,
988 ".. method:: batch_remove(ids)\n"
990 " Remove (delete) several IDs at once.\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 "
997 " :param ids: Sequence of IDs (types can be mixed).\n"
998 " :type ids: Sequence[:class:`bpy.types.ID`]\n");
1006 PyObject *ids =
nullptr;
1008 static const char *_keywords[] = {
"ids",
nullptr};
1009 static _PyArg_Parser _parser = {
1015 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &ids)) {
1023 PyObject *ids_fast = PySequence_Fast(ids,
"batch_remove");
1024 if (ids_fast ==
nullptr) {
1028 PyObject **ids_array = PySequence_Fast_ITEMS(ids_fast);
1029 Py_ssize_t ids_len = PySequence_Fast_GET_SIZE(ids_fast);
1031 for (; ids_len; ids_array++, ids_len--) {
1035 PyExc_TypeError,
"Expected an ID type, not %.200s", Py_TYPE(*ids_array)->tp_name);
1036 Py_DECREF(ids_fast);
1040 ids_to_delete.
add(
id);
1042 Py_DECREF(ids_fast);
1053 bpy_orphans_purge_doc,
1054 ".. method:: orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=False)\n"
1056 " Remove (delete) all IDs with no user.\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"
1079 static const char *_keywords[] = {
"do_local_ids",
"do_linked_ids",
"do_recursive",
nullptr};
1080 static _PyArg_Parser _parser = {
1089 if (!_PyArg_ParseTupleAndKeywordsFast(args,
1106 return PyLong_FromSize_t(0);
1113 return PyLong_FromSize_t(num_datablocks_deleted);
1118# pragma clang diagnostic push
1119# pragma clang diagnostic ignored "-Wcast-function-type"
1121# pragma GCC diagnostic push
1122# pragma GCC diagnostic ignored "-Wcast-function-type"
1129 METH_VARARGS | METH_KEYWORDS,
1135 METH_VARARGS | METH_KEYWORDS,
1136 bpy_file_path_map_doc,
1139 "file_path_foreach",
1141 METH_VARARGS | METH_KEYWORDS,
1142 bpy_file_path_foreach_doc,
1147 METH_VARARGS | METH_KEYWORDS,
1148 bpy_batch_remove_doc,
1153 METH_VARARGS | METH_KEYWORDS,
1154 bpy_orphans_purge_doc,
1159# pragma clang diagnostic pop
1161# pragma GCC diagnostic pop
#define FOREACH_MAIN_LISTBASE_ID_END
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
#define FOREACH_MAIN_LISTBASE_END
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
#define BLI_BITMAP_TEST_BOOL(_bitmap, _index)
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Read Blender memory allocation and freeing.
#define PYRNA_STRUCT_CHECK_OBJ(obj)
#define BPy_StructRNA_Check(v)
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[]
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 ¶meters)
int pyrna_enum_bitfield_from_set(const EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
PyObject * pyrna_id_CreatePyObject(ID *id)
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 *)
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)
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)
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)
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
@ BKE_BPATH_FOREACH_PATH_EXPAND_CACHES
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
@ BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE
@ BKE_BPATH_TRAVERSE_SKIP_WEAK_REFERENCES
@ BKE_BPATH_FOREACH_PATH_EXPAND_SEQUENCES
@ BKE_BPATH_FOREACH_PATH_RELOAD_EDITED
@ BKE_BPATH_FOREACH_PATH_RESOLVE_TOKEN
@ BKE_BPATH_FOREACH_PATH_SKIP_LINKED
PyMethodDef BPY_rna_id_collection_file_path_map_method_def
static Main * pyrna_bmain_FromPyObject(PyObject *obj)
static PyObject * bpy_file_path_meta_get_is_readonly(BPy_FilePathMeta *self, void *)
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_EMBEDDED_NOT_OWNING
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)
const char * PyC_UnicodeAsBytesAndSize(PyObject *py_str, Py_ssize_t *r_size, PyObject **r_coerce)
header-only compatibility defines.
#define PyTuple_SET_ITEMS(op_arg,...)
BPathForeachPathFunctionCallback callback_function
PyObject_HEAD std::optional< PointerRNA > ptr
PyObject * id_file_path_set
BLI_bitmap * types_bitmap
std::array< int, INDEX_ID_MAX > num_total
LibraryForeachIDCallbackFlag cb_flag