Blender V5.3
bpy_library_load.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
15
16#include <Python.h>
17#include <cstddef>
18
19#include "BLI_linklist.hh"
21#include "BLI_path_utils.hh"
22#include "BLI_string.hh"
23#include "BLI_utildefines.hh"
24
25#include "BKE_asset.hh"
26#include "BKE_blender_version.h"
28#include "BKE_context.hh"
29#include "BKE_idtype.hh"
30#include "BKE_lib_id.hh"
31#include "BKE_main.hh"
32#include "BKE_report.hh"
33
34#include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
35
36#include "BLO_readfile.hh"
37
38#include "bpy_capi_utils.hh"
39#include "bpy_library.hh"
40
42#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
44
45/* nifty feature. swap out strings for RNA data */
46#define USE_RNA_DATABLOCKS
47
48#ifdef USE_RNA_DATABLOCKS
49# include "RNA_access.hh"
50# include "bpy_rna.hh"
51#endif
52
53namespace blender {
54
55/* -------------------------------------------------------------------- */
58
63static uint32_t bool_flag_pair_as_flag(const BoolFlagPair *bool_flags, int bool_flags_num)
64{
65 uint32_t flag = 0;
66 for (int i = 0; i < bool_flags_num; i++) {
67 BLI_assert(bool_flags[i].flag);
68 if (bool_flags[i].value) {
69 flag |= bool_flags[i].flag;
70 }
71 }
72 return flag;
73}
74
76
77/* Named tuple to store BLODataBlockInfo::Library data. */
78static PyTypeObject bpy_lib_LibraryType;
79
80static PyStructSequence_Field bpy_lib_LibraryType_fields[] = {
81 {"filepath",
82 "Filepath to the library file, may be relative to the blendfile being loaded"
83 "\n\n:type: str"},
84 {"is_archive",
85 "Whether the Library data-block is an archive one (used as namespace for packed linked "
86 "data-blocks)"
87 "\n\n:type: bool"},
88 {nullptr},
89};
90
91static PyStructSequence_Desc bpy_lib_LibraryType_desc = {
92 /*name*/ "bpy_lib_library",
93 /*doc*/ "Contains information about libraries present in the loaded blendfile",
95 /*n_in_sequence*/ ARRAY_SIZE(bpy_lib_LibraryType_fields) - 1,
96};
97
98static PyObject *make_library_info(BLODataBlockInfo &id_info)
99{
100 PyObject *library_info;
101 int pos = 0;
102
103 library_info = PyStructSequence_New(&bpy_lib_LibraryType);
104 if (library_info == nullptr) {
105 return nullptr;
106 }
107
108 PyStructSequence_SET_ITEM(
109 library_info, pos++, PyC_UnicodeFromBytes(id_info.library_data.filepath));
110 PyStructSequence_SET_ITEM(
111 library_info,
112 pos++,
113 PyBool_FromLong((id_info.library_data.flag & LIBRARY_FLAG_IS_ARCHIVE) != 0));
114
115 BLI_assert(pos == bpy_lib_LibraryType_desc.n_in_sequence);
116
117 if (PyErr_Occurred()) [[unlikely]] {
118 Py_DECREF(library_info);
119 return nullptr;
120 }
121 return library_info;
122}
123
127static constexpr Py_ssize_t bpy_library_dict_num = INDEX_ID_MAX + 1;
128
163
164static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw);
165static PyObject *bpy_lib_enter(BPy_Library *self);
166static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args);
167static PyObject *bpy_lib_dir(BPy_Library *self);
168
169#ifdef __GNUC__
170# ifdef __clang__
171# pragma clang diagnostic push
172# pragma clang diagnostic ignored "-Wcast-function-type"
173# else
174# pragma GCC diagnostic push
175# pragma GCC diagnostic ignored "-Wcast-function-type"
176# endif
177#endif
178
179static PyMethodDef bpy_lib_methods[] = {
180 {"__enter__", reinterpret_cast<PyCFunction>(bpy_lib_enter), METH_NOARGS},
181 {"__exit__", reinterpret_cast<PyCFunction>(bpy_lib_exit), METH_VARARGS},
182 {"__dir__", reinterpret_cast<PyCFunction>(bpy_lib_dir), METH_NOARGS},
183 {nullptr} /* sentinel */
184};
185
186#ifdef __GNUC__
187# ifdef __clang__
188# pragma clang diagnostic pop
189# else
190# pragma GCC diagnostic pop
191# endif
192#endif
193
194static void bpy_lib_dealloc(BPy_Library *self)
195{
196 Py_XDECREF(self->dict);
197 Py_TYPE(self)->tp_free(self);
198}
199
200static PyTypeObject bpy_lib_Type = {
201 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
202 /*tp_name*/ "bpy_lib",
203 /*tp_basicsize*/ sizeof(BPy_Library),
204 /*tp_itemsize*/ 0,
205 /*tp_dealloc*/ reinterpret_cast<destructor>(bpy_lib_dealloc),
206 /*tp_vectorcall_offset*/ 0,
207 /*tp_getattr*/ nullptr,
208 /*tp_setattr*/ nullptr,
209 /*tp_as_async*/ nullptr,
210 /*tp_repr*/ nullptr,
211 /*tp_as_number*/ nullptr,
212 /*tp_as_sequence*/ nullptr,
213 /*tp_as_mapping*/ nullptr,
214 /*tp_hash*/ nullptr,
215 /*tp_call*/ nullptr,
216 /*tp_str*/ nullptr,
217 /*tp_getattro*/ PyObject_GenericGetAttr,
218 /*tp_setattro*/ nullptr,
219 /*tp_as_buffer*/ nullptr,
220 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
221 /*tp_doc*/ nullptr,
222 /*tp_traverse*/ nullptr,
223 /*tp_clear*/ nullptr,
224 /*tp_richcompare*/ nullptr,
225 /*tp_weaklistoffset*/ 0,
226 /*tp_iter*/ nullptr,
227 /*tp_iternext*/ nullptr,
228 /*tp_methods*/ bpy_lib_methods,
229 /*tp_members*/ nullptr,
230 /*tp_getset*/ nullptr,
231 /*tp_base*/ nullptr,
232 /*tp_dict*/ nullptr,
233 /*tp_descr_get*/ nullptr,
234 /*tp_descr_set*/ nullptr,
235 /*tp_dictoffset*/ offsetof(BPy_Library, dict),
236 /*tp_init*/ nullptr,
237 /*tp_alloc*/ nullptr,
238 /*tp_new*/ nullptr,
239 /*tp_free*/ nullptr,
240 /*tp_is_gc*/ nullptr,
241 /*tp_bases*/ nullptr,
242 /*tp_mro*/ nullptr,
243 /*tp_cache*/ nullptr,
244 /*tp_subclasses*/ nullptr,
245 /*tp_weaklist*/ nullptr,
246 /*tp_del*/ nullptr,
247 /*tp_version_tag*/ 0,
248 /*tp_finalize*/ nullptr,
249 /*tp_vectorcall*/ nullptr,
250};
251
253 /* Wrap. */
254 bpy_lib_load_doc,
255 ".. method:: load("
256 "filepath, "
257 "*, "
258 "link=False, "
259 "pack=False, "
260 "relative=False, "
261 "set_fake=False, "
262 "recursive=False, "
263 "reuse_local_id=False, "
264 "assets_only=False, "
265 "clear_asset_data=False, "
266 "create_liboverrides=False, "
267 "reuse_liboverrides=False, "
268 "create_liboverrides_runtime=False)\n"
269 "\n"
270 " Returns a context manager which exposes a pair of library objects (input and output)\n"
271 " on entering.\n"
272 "\n"
273 " The input contains the data-blocks available in the loaded blend-file library and should\n"
274 " be treated as read-only.\n"
275 " Data-block names added to the output are linked/appended when the context exits.\n"
276 "\n"
277 " Each library object contains:\n"
278 " - Attributes matching ``bpy.data``, which are lists of strings representing linkable\n"
279 " data-blocks.\n"
280 " - A ``libraries`` attribute, which for the input is a list of all other libraries\n"
281 " used by the loaded one, as named tuples (``filepath``, ``is_archive``). The\n"
282 " filepath may be absolute, or relative to the loaded blend-file. The output\n"
283 " ``libraries`` attribute is always ``None``.\n"
284 " - A ``version`` attribute, representing the version of the loaded library blend-file\n"
285 " (for the input) or the version of the current Blender (for the output).\n"
286 "\n"
287 " Notes:\n"
288 " - Not all data-block types are linkable (e.g. WindowManager, Library, ...).\n"
289 " - Packed linked data-blocks are not linkable and are not listed in the input\n"
290 " ``libraries`` attribute.\n"
291 "\n"
292 " :param filepath: The path to a blend file.\n"
293 " :type filepath: str | bytes\n"
294 " :param link: When False reference to the original file is lost.\n"
295 " :type link: bool\n"
296 " :param pack: If True, and ``link`` is also True, pack linked data-blocks into the current "
297 "blend-file.\n"
298 " :type pack: bool\n"
299 " :param relative: When True the path is stored relative to the open blend file.\n"
300 " :type relative: bool\n"
301 " :param set_fake: If True, set fake user on appended IDs.\n"
302 " :type set_fake: bool\n"
303 " :param recursive: If True, also make indirect dependencies of appended libraries local.\n"
304 " :type recursive: bool\n"
305 " :param reuse_local_id: If True,"
306 "try to re-use previously appended matching ID on new append.\n"
307 " :type reuse_local_id: bool\n"
308 " :param assets_only: If True, only list data-blocks marked as assets.\n"
309 " :type assets_only: bool\n"
310 " :param clear_asset_data: If True, "
311 "clear the asset data on append (it is always kept for linked data).\n"
312 " :type clear_asset_data: bool\n"
313 " :param create_liboverrides: If True and ``link`` is True, liboverrides will\n"
314 " be created for linked data.\n"
315 " :type create_liboverrides: bool\n"
316 " :param reuse_liboverrides: If True and ``create_liboverride`` is True,\n"
317 " search for existing liboverride first.\n"
318 " :type reuse_liboverrides: bool\n"
319 " :param create_liboverrides_runtime: If True and ``create_liboverride`` is True,\n"
320 " create (or search for existing) runtime liboverride.\n"
321 " :type create_liboverrides_runtime: bool\n");
322static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw)
323{
324 Main *bmain_base = CTX_data_main(BPY_context_get());
325 Main *bmain = static_cast<Main *>(self->ptr->data); /* Typically #G_MAIN */
327 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
328
329 /* #BPy_Library::flag
330 *
331 * - #BLO_LIBLINK_OBDATA_INSTANCE: The caller must manage instancing.
332 * - #BLO_LIBLINK_COLLECTION_INSTANCE: The caller must manage instancing.
333 */
334 struct {
335 BoolFlagPair is_link = {false, FILE_LINK};
336 BoolFlagPair is_pack = {false, BLO_LIBLINK_PACK};
337 BoolFlagPair is_relative = {false, FILE_RELPATH};
339 BoolFlagPair recursive = {false, BLO_LIBLINK_APPEND_RECURSIVE};
340 BoolFlagPair reuse_local_id = {false, BLO_LIBLINK_APPEND_LOCAL_ID_REUSE};
341 BoolFlagPair assets_only = {false, FILE_ASSETS_ONLY};
342 BoolFlagPair clear_asset_data = {false, BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR};
343 } flag_vars;
344
345 bool create_liboverrides = false;
346
347 /* #BPy_Library::liboverride_flags */
348 struct {
350 BoolFlagPair create_liboverrides_runtime = {false, BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME};
351 } liboverride_flag_vars;
352
353 static const char *_keywords[] = {
354 "filepath",
355 "link",
356 "pack",
357 "relative",
358 "set_fake",
359 "recursive",
360 "reuse_local_id",
361 "assets_only",
362 "clear_asset_data",
363 "create_liboverrides",
364 "reuse_liboverrides",
365 "create_liboverrides_runtime",
366 nullptr,
367 };
368 static _PyArg_Parser _parser = {
369 "O&" /* `filepath` */
370 /* Optional, keyword only arguments. */
371 "|$"
372 "O&" /* `link` */
373 "O&" /* `pack` */
374 "O&" /* `relative` */
375 "O&" /* `set_fake` */
376 "O&" /* `recursive` */
377 "O&" /* `reuse_local_id` */
378 "O&" /* `assets_only` */
379 "O&" /* `clear_asset_data` */
380 "O&" /* `create_liboverrides` */
381 "O&" /* `reuse_liboverrides` */
382 "O&" /* `create_liboverrides_runtime` */
383 ":load",
384 _keywords,
385 nullptr,
386 };
387 if (!_PyArg_ParseTupleAndKeywordsFast(args,
388 kw,
389 &_parser,
391 &filepath_data,
393 &flag_vars.is_link,
395 &flag_vars.is_pack,
397 &flag_vars.is_relative,
399 &flag_vars.set_fake,
401 &flag_vars.recursive,
403 &flag_vars.reuse_local_id,
405 &flag_vars.assets_only,
407 &flag_vars.clear_asset_data,
409 &create_liboverrides,
411 &liboverride_flag_vars.reuse_liboverrides,
413 &liboverride_flag_vars.create_liboverrides_runtime))
414 {
415 return nullptr;
416 }
417
418 const char *blendfile_path = BKE_main_blendfile_path(bmain);
419 char filepath_rel[FILE_MAX];
420 char filepath_abs[FILE_MAX];
421
422 STRNCPY(filepath_rel, filepath_data.value);
423 STRNCPY(filepath_abs, filepath_rel);
424 BLI_path_abs(filepath_abs, blendfile_path);
425 Py_XDECREF(filepath_data.value_coerce);
426
427 if (blendfile_path[0]) {
428 /* NOTE: intentionally leave `filepath_abs` and only use normalizing for comparison.
429 * It's important that this comparison matches read-files logic for matching paths.
430 * See the logic inside #BKE_blendfile_link.
431 *
432 * This means it's not necessary to check if the paths are *actually* the same.
433 * It's possible to load from this file if a user makes a symbolic-link - for example.
434 * See #140929. */
435 char filepath_abs_normalized[FILE_MAX];
436 STRNCPY(filepath_abs_normalized, filepath_abs);
437 BLI_path_normalize(filepath_abs_normalized);
438 if (BLI_path_cmp(filepath_abs_normalized, blendfile_path) == 0) {
439 PyErr_SetString(PyExc_ValueError, "Cannot load from the current blend file.");
440 return nullptr;
441 }
442 }
443
444 if (flag_vars.is_link.value) {
445 /* Link. */
446 if (flag_vars.set_fake.value) {
447 PyErr_SetString(PyExc_ValueError, "`link` must be False if `set_fake` is True");
448 return nullptr;
449 }
450 if (flag_vars.recursive.value) {
451 PyErr_SetString(PyExc_ValueError, "`link` must be False if `recursive` is True");
452 return nullptr;
453 }
454 if (flag_vars.reuse_local_id.value) {
455 PyErr_SetString(PyExc_ValueError, "`link` must be False if `reuse_local_id` is True");
456 return nullptr;
457 }
458 if (flag_vars.clear_asset_data.value) {
459 PyErr_SetString(PyExc_ValueError, "`link` must be False if `clear_asset_data` is True");
460 return nullptr;
461 }
462 }
463 else {
464 /* Append. */
465 if (create_liboverrides) {
466 PyErr_SetString(PyExc_ValueError, "`link` is False but `create_liboverrides` is True");
467 return nullptr;
468 }
469 if (flag_vars.is_pack.value) {
470 PyErr_SetString(PyExc_ValueError, "`pack` must be False if `link` is False");
471 return nullptr;
472 }
473 }
474
475 if (create_liboverrides) {
476 /* Library overrides. */
477 if (flag_vars.is_pack.value) {
478 PyErr_SetString(PyExc_ValueError, "`create_liboverrides` must be False if `pack` is True");
479 return nullptr;
480 }
481 }
482 else {
483 /* Library overrides (disabled). */
484 if (liboverride_flag_vars.reuse_liboverrides.value) {
485 PyErr_SetString(PyExc_ValueError,
486 "`create_liboverrides` is False but `reuse_liboverrides` is True");
487 return nullptr;
488 }
489 if (liboverride_flag_vars.create_liboverrides_runtime.value) {
490 PyErr_SetString(PyExc_ValueError,
491 "`create_liboverrides` is False but `create_liboverrides_runtime` is True");
492 return nullptr;
493 }
494 }
495
496 ret = PyObject_New(BPy_Library, &bpy_lib_Type);
497
498 STRNCPY(ret->relpath, filepath_rel);
499 STRNCPY(ret->abspath, filepath_abs);
500
501 ret->bmain = bmain;
502 ret->bmain_is_temp = (bmain != bmain_base);
503
504 ret->blo_handle = nullptr;
505
506 ret->flag = bool_flag_pair_as_flag(reinterpret_cast<const BoolFlagPair *>(&flag_vars),
507 sizeof(flag_vars) / sizeof(BoolFlagPair));
508
509 ret->create_liboverrides = create_liboverrides;
510 ret->liboverride_flags = create_liboverrides ?
512 reinterpret_cast<const BoolFlagPair *>(&liboverride_flag_vars),
513 sizeof(liboverride_flag_vars) / sizeof(BoolFlagPair))) :
515
516 ret->dict = _PyDict_NewPresized(bpy_library_dict_num);
517
518 return reinterpret_cast<PyObject *>(ret);
519}
520
521static PyObject *_bpy_ids_info(BPy_Library *self, int blocktype)
522{
523 int ids_info_num;
525 self->blo_handle, blocktype, (self->flag & FILE_ASSETS_ONLY) != 0, &ids_info_num);
526 PyObject *list = PyList_New(ids_info_num);
527
528 if (ids_info) {
529 int i = 0;
530 for (LinkNode *l = ids_info; l; l = l->next, i++) {
531 BLODataBlockInfo *id_info = static_cast<BLODataBlockInfo *>(l->link);
532 if (blocktype == ID_LI) {
533 PyList_SET_ITEM(list, i, make_library_info(*id_info));
534 continue;
535 }
536 PyList_SET_ITEM(list, i, PyUnicode_FromString(id_info->name));
537 }
538 /* free linklist *and* each node's data */
539 BLI_linklist_free(ids_info, [](void *link) -> void {
540 BLODataBlockInfo *id_info = static_cast<BLODataBlockInfo *>(link);
541 if (id_info->free_asset_data) {
543 }
544 MEM_delete(id_info);
545 });
546 }
547
548 return list;
549}
550
551static PyObject *bpy_lib_enter(BPy_Library *self)
552{
553 /* The dummy returned as `data_from` has no `bmain` to link into.
554 * A non-null `blo_handle` means this context is already entered, re-entering
555 * would leak the handle. */
556 if ((self->bmain == nullptr) || (self->blo_handle != nullptr)) [[unlikely]] {
557 PyErr_SetString(PyExc_RuntimeError, "This context manager cannot be entered");
558 return nullptr;
559 }
560
561 ReportList *reports = &self->reports;
562 BlendFileReadReport *bf_reports = &self->bf_reports;
563
565 memset(bf_reports, 0, sizeof(*bf_reports));
566 bf_reports->reports = reports;
567
568 self->blo_handle = BLO_blendhandle_from_file(self->abspath, bf_reports);
569
570 if (self->blo_handle == nullptr) {
571 if (BPy_reports_to_error(reports, PyExc_IOError, true) != -1) {
572 PyErr_Format(PyExc_IOError, "load: %s failed to open blend file", self->abspath);
573 }
574 return nullptr;
575 }
576
577 PyObject *dict_src = _PyDict_NewPresized(bpy_library_dict_num);
578 PyObject *dict_dst = self->dict; /* Only for convenience (always `self->dict`). */
579 int dict_num_offset = 0;
580
581 int i = 0, code;
582 while ((code = BKE_idtype_idcode_iter_step(&i))) {
583 const bool is_library = (code == ID_LI);
584 if (!is_library && !BKE_idtype_idcode_is_linkable(code)) {
585 dict_num_offset += 1;
586 continue;
587 }
588 const char *name_plural = BKE_idtype_idcode_to_name_plural(code);
589 PyObject *str = PyUnicode_FromString(name_plural);
590 PyObject *item;
591
592 /* Do not add 'libraries' entry in output data, this would make no sense currently. */
593 if (is_library) {
594 PyDict_SetItem(dict_dst, str, Py_None);
595 }
596 else {
597 PyDict_SetItem(dict_dst, str, item = PyList_New(0));
598 Py_DECREF(item);
599 }
600 PyDict_SetItem(dict_src, str, item = _bpy_ids_info(self, code));
601 Py_DECREF(item);
602
603 Py_DECREF(str);
604 }
605
606 /* Create a dummy. */
607 BPy_Library *self_src = PyObject_New(BPy_Library, &bpy_lib_Type);
608 STRNCPY(self_src->relpath, self->relpath);
609 STRNCPY(self_src->abspath, self->abspath);
610
611 /* Library blend-file version. */
612 {
613 PyObject *version;
614 PyObject *identifier = PyUnicode_FromString("version");
615 int3 blendfile_version;
616
617 /* Source. */
618 blendfile_version = BLO_blendhandle_get_version(self->blo_handle);
619 version = PyC_Tuple_PackArray_I32(&blendfile_version[0], 3);
620 PyDict_SetItem(dict_src, identifier, version);
621 Py_DECREF(version);
622
623 /* Destination. */
624 blendfile_version = int3(
626 version = PyC_Tuple_PackArray_I32(&blendfile_version[0], 3);
627 PyDict_SetItem(dict_dst, identifier, version);
628 Py_DECREF(version);
629
630 Py_DECREF(identifier);
631 }
632
633 self_src->bmain = nullptr;
634 self_src->bmain_is_temp = false;
635 self_src->blo_handle = nullptr;
636 self_src->flag = 0;
637 self_src->create_liboverrides = false;
639 self_src->dict = dict_src; /* owns the dict */
640
641 /* While it's not a bug if the sizes differ, the size is expected to match.
642 * Ensure `bpy_library_dict_num` gets updated when members are added. */
643 BLI_assert(PyDict_GET_SIZE(self_src->dict) + dict_num_offset == bpy_library_dict_num);
644 BLI_assert(PyDict_GET_SIZE(self->dict) + dict_num_offset == bpy_library_dict_num);
645 UNUSED_VARS_NDEBUG(dict_num_offset);
646
647 BKE_reports_clear(reports);
648
649 /* Return a pair. */
650 PyObject *ret = PyTuple_New(2);
651 PyTuple_SET_ITEMS(ret, (PyObject *)self_src, Py_NewRef((PyObject *)self));
652 return ret;
653}
654
656 const char *name_plural,
657 const char *idname)
658{
659 PyObject *exc, *val, *tb;
660 PyErr_Fetch(&exc, &val, &tb);
661 if (PyErr_WarnFormat(PyExc_UserWarning,
662 1,
663 "load: '%s' does not contain %s[\"%s\"]",
664 self->abspath,
665 name_plural,
666 idname))
667 {
668 /* Spurious errors can appear at shutdown */
669 if (PyErr_ExceptionMatches(PyExc_Warning)) {
670 PyErr_WriteUnraisable(reinterpret_cast<PyObject *>(self));
671 }
672 }
673 PyErr_Restore(exc, val, tb);
674}
675
676static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
677{
678 PyObject *exc, *val, *tb;
679 PyErr_Fetch(&exc, &val, &tb);
680 if (PyErr_WarnFormat(PyExc_UserWarning,
681 1,
682 "load: '%s' expected a string type, not a %.200s",
683 self->abspath,
684 Py_TYPE(item)->tp_name))
685 {
686 /* Spurious errors can appear at shutdown */
687 if (PyErr_ExceptionMatches(PyExc_Warning)) {
688 PyErr_WriteUnraisable(reinterpret_cast<PyObject *>(self));
689 }
690 }
691 PyErr_Restore(exc, val, tb);
692}
693
700
704{
705 /* Since `bpy_lib_exit` loops over all ID types, all items in `lapp_context` end up being looped
706 * over for each ID type, so when it does not match the item can simply be skipped: it either has
707 * already been processed, or will be processed in a later loop. */
708 if (BKE_blendfile_link_append_context_item_idcode_get(lapp_context, item) != data.idcode) {
709 return true;
710 }
711
712 const int py_list_index = POINTER_AS_INT(
714 ID *new_id = BKE_blendfile_link_append_context_item_newid_get(lapp_context, item);
715 ID *liboverride_id = data.py_library->create_liboverrides ?
717 item) :
718 nullptr;
719
720 BLI_assert(py_list_index < data.py_list_size);
721
722 /* Fully invalid items (which got set to `Py_None` already in first loop of `bpy_lib_exit`)
723 * should never be accessed here, since their index should never be set to any item in
724 * `lapp_context`. */
725 PyObject *item_src = PyList_GET_ITEM(data.py_list, py_list_index);
726 BLI_assert(item_src != Py_None);
727
728 PyObject *py_item;
729 if (liboverride_id != nullptr) {
730 PointerRNA newid_ptr = RNA_id_pointer_create(liboverride_id);
731 py_item = pyrna_struct_CreatePyObject(&newid_ptr);
732 }
733 else if (new_id != nullptr) {
734 PointerRNA newid_ptr = RNA_id_pointer_create(new_id);
735 py_item = pyrna_struct_CreatePyObject(&newid_ptr);
736 }
737 else {
738 const char *item_idname = PyUnicode_AsUTF8(item_src);
739 const char *idcode_name_plural = BKE_idtype_idcode_to_name_plural(data.idcode);
740
741 bpy_lib_exit_warn_idname(data.py_library, idcode_name_plural, item_idname);
742
743 py_item = Py_NewRef(Py_None);
744 }
745
746 PyList_SET_ITEM(data.py_list, py_list_index, py_item);
747
748 Py_DECREF(item_src);
749
750 return true;
751}
752
753static PyObject *bpy_lib_exit(BPy_Library *self, PyObject * /*args*/)
754{
755 /* Members used below are only initialized by `__enter__`. */
756 if (self->blo_handle == nullptr) [[unlikely]] {
757 PyErr_SetString(PyExc_RuntimeError, "Context manager not entered");
758 return nullptr;
759 }
760
761 Main *bmain = self->bmain;
762 const bool do_pack = ((self->flag & BLO_LIBLINK_PACK) != 0);
763 const bool do_append = ((self->flag & FILE_LINK) == 0);
764 const bool create_liboverrides = self->create_liboverrides;
765 /* Code in #bpy_lib_load should have raised exception in case of incompatible parameter values.
766 */
767 BLI_assert(!do_append || !create_liboverrides);
768
770
771 /* here appending/linking starts */
772 const int id_tag_extra = self->bmain_is_temp ? int(ID_TAG_TEMP_MAIN) : 0;
773 LibraryLink_Params liblink_params;
774 BLO_library_link_params_init(&liblink_params, bmain, self->flag, id_tag_extra);
775
777 &liblink_params);
778 /* NOTE: Transfers the ownership of the `blo_handle` to the `lapp_context`. */
780 self->blo_handle = nullptr;
781
782 int idcode_step = 0;
783 short idcode;
784 while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) {
785 if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) {
786 continue;
787 }
788
789 const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode);
790 PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
791 // printf("lib: %s\n", name_plural);
792 if (ls == nullptr || !PyList_Check(ls)) {
793 continue;
794 }
795
796 const Py_ssize_t size = PyList_GET_SIZE(ls);
797 if (size == 0) {
798 continue;
799 }
800
801 /* loop */
802 for (Py_ssize_t i = 0; i < size; i++) {
803 PyObject *item_src = PyList_GET_ITEM(ls, i);
804 const char *item_idname = PyUnicode_AsUTF8(item_src);
805
806 // printf(" %s\n", item_idname);
807
808 /* NOTE: index of item in py list is stored in userdata pointer, so that it can be found
809 * later on to replace the ID name by the actual ID pointer. */
810 if (item_idname != nullptr) {
812 lapp_context, item_idname, idcode, POINTER_FROM_INT(i));
814 }
815 else {
816 /* XXX, could complain about this */
817 bpy_lib_exit_warn_type(self, item_src);
818 PyErr_Clear();
819
820#ifdef USE_RNA_DATABLOCKS
821 /* We can replace the item immediately with `None`. */
822 PyObject *py_item = Py_NewRef(Py_None);
823 PyList_SET_ITEM(ls, i, py_item);
824 Py_DECREF(item_src);
825#endif
826 }
827 }
828 }
829
831
832 BKE_blendfile_link(lapp_context, nullptr);
833 if (do_pack) {
834 BKE_blendfile_link_pack(lapp_context, nullptr);
835 }
836 else if (do_append) {
837 BKE_blendfile_append(lapp_context, nullptr);
838 }
839 else if (create_liboverrides) {
840 BKE_blendfile_override(lapp_context, self->liboverride_flags, nullptr);
841 }
842
844
845/* If enabled, replace named items in given lists by the final matching new ID pointer. */
846#ifdef USE_RNA_DATABLOCKS
847 idcode_step = 0;
848 while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) {
849 if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) {
850 continue;
851 }
852 const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode);
853 PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
854 // printf("lib: %s\n", name_plural);
855 if (ls == nullptr || !PyList_Check(ls)) {
856 continue;
857 }
858
859 const Py_ssize_t size = PyList_GET_SIZE(ls);
860 if (size == 0) {
861 continue;
862 }
863
864 /* Loop over linked items in `lapp_context` to find matching python one in the list, and
865 * replace them with proper ID pointer. */
867 iter_data.idcode = idcode;
868 iter_data.py_library = self;
869 iter_data.py_list = ls;
870 iter_data.py_list_size = size;
872 lapp_context,
873 [&iter_data](BlendfileLinkAppendContext *lapp_context,
874 BlendfileLinkAppendContextItem *item) -> bool {
875 return bpy_lib_exit_lapp_context_items_cb(lapp_context, item, iter_data);
876 },
878 }
879#endif // USE_RNA_DATABLOCKS
880
883
885
886 Py_RETURN_NONE;
887}
888
889static PyObject *bpy_lib_dir(BPy_Library *self)
890{
891 return PyDict_Keys(self->dict);
892}
893
894#ifdef __GNUC__
895# ifdef __clang__
896# pragma clang diagnostic push
897# pragma clang diagnostic ignored "-Wcast-function-type"
898# else
899# pragma GCC diagnostic push
900# pragma GCC diagnostic ignored "-Wcast-function-type"
901# endif
902#endif
903
905 "load",
906 reinterpret_cast<PyCFunction>(bpy_lib_load),
907 METH_VARARGS | METH_KEYWORDS,
908 bpy_lib_load_doc,
909};
910
911#ifdef __GNUC__
912# ifdef __clang__
913# pragma clang diagnostic pop
914# else
915# pragma GCC diagnostic pop
916# endif
917#endif
918
920{
921 if (PyType_Ready(&bpy_lib_Type) < 0) {
922 return -1;
923 }
924
925 PyStructSequence_InitType(&bpy_lib_LibraryType, &bpy_lib_LibraryType_desc);
926
927 return 0;
928}
929
930} // namespace blender
#define BLENDER_FILE_SUBVERSION
#define BLENDER_FILE_VERSION
#define FILE_MAX
#define BLI_path_cmp
external readfile function prototypes.
#define INDEX_ID_MAX
Definition DNA_ID.h:1362
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define POINTER_AS_INT(i)
#define offsetof(t, d)
#define str(s)
BMesh Iterators.
int3 BLO_blendhandle_get_version(const BlendHandle *bh)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
static PyObject * bpy_lib_enter(BPy_Library *self)
LinkNode * BLO_blendhandle_get_datablock_info(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_info_items)
BlendfileLinkAppendContext * BKE_blendfile_link_append_context_new(LibraryLink_Params *params)
char * STRNCPY(char(&dst)[N], const char *src)
int PyC_ParseBool(PyObject *o, void *p)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
static uint32_t bool_flag_pair_as_flag(const BoolFlagPair *bool_flags, int bool_flags_num)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
PointerRNA RNA_id_pointer_create(ID *id)
void BKE_blendfile_link_append_context_free(BlendfileLinkAppendContext *lapp_context)
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition asset.cc:36
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")
BlendfileLinkAppendContextItem * BKE_blendfile_link_append_context_item_add(BlendfileLinkAppendContext *lapp_context, const char *idname, short idcode, void *userdata)
static PyTypeObject bpy_lib_LibraryType
void BKE_blendfile_link_append_context_item_library_index_enable(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, int library_index)
void BKE_reports_clear(ReportList *reports)
Definition report.cc:118
static PyTypeObject bpy_lib_Type
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
@ BLO_LIBLINK_APPEND_LOCAL_ID_REUSE
@ BLO_LIBLINK_APPEND_RECURSIVE
@ BLO_LIBLINK_APPEND_SET_FAKEUSER
@ BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR
PyObject * PyC_UnicodeFromBytes(const char *str)
void BLO_library_link_params_init(LibraryLink_Params *params, Main *bmain, int flag, int id_tag_extra)
Definition readfile.cc:5747
ID * BKE_blendfile_link_append_context_item_newid_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
ID * BKE_blendfile_link_append_context_item_liboverrideid_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
short BKE_blendfile_link_append_context_item_idcode_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 3 > int3
static PyObject * bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw)
void * BKE_blendfile_link_append_context_item_userdata_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
struct bContext * BPY_context_get()
short BKE_idtype_idcode_iter_step(int *idtype_index)
Definition idtype.cc:483
BMesh const char void * data
int BPY_library_load_type_ready()
void BKE_blendfile_link_append_context_finalize(BlendfileLinkAppendContext *lapp_context)
@ BKE_LIBLINK_OVERRIDE_USE_EXISTING_LIBOVERRIDES
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
const char * BKE_idtype_idcode_to_name_plural(short idcode)
Definition idtype.cc:268
static PyObject * make_library_info(BLODataBlockInfo &id_info)
static PyObject * bpy_lib_exit(BPy_Library *self, PyObject *args)
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
@ LIBRARY_FLAG_IS_ARCHIVE
PyObject * PyC_Tuple_PackArray_I32(const int *array, uint len)
static PyObject * bpy_lib_dir(BPy_Library *self)
void BKE_blendfile_append(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
static PyObject * _bpy_ids_info(BPy_Library *self, int blocktype)
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
void BKE_blendfile_link_pack(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc)
Main * CTX_data_main(const bContext *C)
void BKE_blendfile_link_append_context_library_add(BlendfileLinkAppendContext *lapp_context, const char *libname, BlendHandle *blo_handle)
@ ID_TAG_TEMP_MAIN
Definition DNA_ID.h:974
@ ID_TAG_PRE_EXISTING
Definition DNA_ID.h:929
static PyMethodDef bpy_lib_methods[]
static PyStructSequence_Desc bpy_lib_LibraryType_desc
static void bpy_lib_dealloc(BPy_Library *self)
@ BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT
static PyStructSequence_Field bpy_lib_LibraryType_fields[]
void BKE_blendfile_link(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
void BKE_blendfile_link_append_context_item_foreach(BlendfileLinkAppendContext *lapp_context, FunctionRef< bool(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)> callback_function, eBlendfileLinkAppendForeachItemFlag flag)
static constexpr Py_ssize_t bpy_library_dict_num
@ RPT_PRINT_HANDLED_BY_OWNER
Definition BKE_report.hh:62
PyMethodDef BPY_library_load_method_def
static bool bpy_lib_exit_lapp_context_items_cb(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, LibExitLappContextItemsIterData &data)
void BKE_blendfile_link_append_context_init_done(BlendfileLinkAppendContext *lapp_context)
void BKE_blendfile_override(BlendfileLinkAppendContext *lapp_context, const eBKELibLinkOverride flags, ReportList *reports)
header-only compatibility defines.
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
BLODataBlockInfo::Library library_data
BlendFileReadReport bf_reports
PyObject_HEAD char relpath[FILE_MAX]
eBKELibLinkOverride liboverride_flags
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:153