46#define USE_RNA_DATABLOCKS
48#ifdef USE_RNA_DATABLOCKS
66 for (
int i = 0; i < bool_flags_num; i++) {
68 if (bool_flags[i].value) {
69 flag |= bool_flags[i].
flag;
82 "Filepath to the library file, may be relative to the blendfile being loaded"
85 "Whether the Library data-block is an archive one (used as namespace for packed linked "
93 "Contains information about libraries present in the loaded blendfile",
100 PyObject *library_info;
104 if (library_info ==
nullptr) {
108 PyStructSequence_SET_ITEM(
110 PyStructSequence_SET_ITEM(
117 if (PyErr_Occurred()) [[unlikely]] {
118 Py_DECREF(library_info);
171# pragma clang diagnostic push
172# pragma clang diagnostic ignored "-Wcast-function-type"
174# pragma GCC diagnostic push
175# pragma GCC diagnostic ignored "-Wcast-function-type"
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},
188# pragma clang diagnostic pop
190# pragma GCC diagnostic pop
196 Py_XDECREF(self->
dict);
197 Py_TYPE(self)->tp_free(self);
201 PyVarObject_HEAD_INIT(
nullptr, 0)
217 PyObject_GenericGetAttr,
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"
270 " Returns a context manager which exposes a pair of library objects (input and output)\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"
277 " Each library object contains:\n"
278 " - Attributes matching ``bpy.data``, which are lists of strings representing linkable\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"
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"
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 "
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");
325 Main *bmain =
static_cast<Main *
>(self->
ptr->data);
345 bool create_liboverrides =
false;
351 } liboverride_flag_vars;
353 static const char *_keywords[] = {
363 "create_liboverrides",
364 "reuse_liboverrides",
365 "create_liboverrides_runtime",
368 static _PyArg_Parser _parser = {
387 if (!_PyArg_ParseTupleAndKeywordsFast(args,
397 &flag_vars.is_relative,
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))
423 STRNCPY(filepath_abs, filepath_rel);
427 if (blendfile_path[0]) {
435 char filepath_abs_normalized[
FILE_MAX];
436 STRNCPY(filepath_abs_normalized, filepath_abs);
438 if (
BLI_path_cmp(filepath_abs_normalized, blendfile_path) == 0) {
439 PyErr_SetString(PyExc_ValueError,
"Cannot load from the current blend file.");
444 if (flag_vars.is_link.value) {
446 if (flag_vars.set_fake.value) {
447 PyErr_SetString(PyExc_ValueError,
"`link` must be False if `set_fake` is True");
450 if (flag_vars.recursive.value) {
451 PyErr_SetString(PyExc_ValueError,
"`link` must be False if `recursive` is True");
454 if (flag_vars.reuse_local_id.value) {
455 PyErr_SetString(PyExc_ValueError,
"`link` must be False if `reuse_local_id` is True");
458 if (flag_vars.clear_asset_data.value) {
459 PyErr_SetString(PyExc_ValueError,
"`link` must be False if `clear_asset_data` is True");
465 if (create_liboverrides) {
466 PyErr_SetString(PyExc_ValueError,
"`link` is False but `create_liboverrides` is True");
469 if (flag_vars.is_pack.value) {
470 PyErr_SetString(PyExc_ValueError,
"`pack` must be False if `link` is False");
475 if (create_liboverrides) {
477 if (flag_vars.is_pack.value) {
478 PyErr_SetString(PyExc_ValueError,
"`create_liboverrides` must be False if `pack` is True");
484 if (liboverride_flag_vars.reuse_liboverrides.value) {
485 PyErr_SetString(PyExc_ValueError,
486 "`create_liboverrides` is False but `reuse_liboverrides` is True");
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");
502 ret->bmain_is_temp = (bmain != bmain_base);
504 ret->blo_handle =
nullptr;
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))) :
518 return reinterpret_cast<PyObject *
>(
ret);
526 PyObject *
list = PyList_New(ids_info_num);
532 if (blocktype ==
ID_LI) {
536 PyList_SET_ITEM(
list, i, PyUnicode_FromString(id_info->
name));
556 if ((self->
bmain ==
nullptr) || (self->
blo_handle !=
nullptr)) [[unlikely]] {
557 PyErr_SetString(PyExc_RuntimeError,
"This context manager cannot be entered");
565 memset(bf_reports, 0,
sizeof(*bf_reports));
572 PyErr_Format(PyExc_IOError,
"load: %s failed to open blend file", self->
abspath);
578 PyObject *dict_dst = self->
dict;
579 int dict_num_offset = 0;
583 const bool is_library = (code ==
ID_LI);
585 dict_num_offset += 1;
589 PyObject *
str = PyUnicode_FromString(name_plural);
594 PyDict_SetItem(dict_dst,
str, Py_None);
597 PyDict_SetItem(dict_dst,
str, item = PyList_New(0));
614 PyObject *identifier = PyUnicode_FromString(
"version");
615 int3 blendfile_version;
620 PyDict_SetItem(dict_src, identifier, version);
624 blendfile_version =
int3(
627 PyDict_SetItem(dict_dst, identifier, version);
630 Py_DECREF(identifier);
633 self_src->
bmain =
nullptr;
639 self_src->
dict = dict_src;
650 PyObject *
ret = PyTuple_New(2);
656 const char *name_plural,
659 PyObject *exc, *val, *tb;
660 PyErr_Fetch(&exc, &val, &tb);
661 if (PyErr_WarnFormat(PyExc_UserWarning,
663 "load: '%s' does not contain %s[\"%s\"]",
669 if (PyErr_ExceptionMatches(PyExc_Warning)) {
670 PyErr_WriteUnraisable(
reinterpret_cast<PyObject *
>(self));
673 PyErr_Restore(exc, val, tb);
678 PyObject *exc, *val, *tb;
679 PyErr_Fetch(&exc, &val, &tb);
680 if (PyErr_WarnFormat(PyExc_UserWarning,
682 "load: '%s' expected a string type, not a %.200s",
684 Py_TYPE(item)->tp_name))
687 if (PyErr_ExceptionMatches(PyExc_Warning)) {
688 PyErr_WriteUnraisable(
reinterpret_cast<PyObject *
>(self));
691 PyErr_Restore(exc, val, tb);
715 ID *liboverride_id =
data.py_library->create_liboverrides ?
725 PyObject *item_src = PyList_GET_ITEM(
data.py_list, py_list_index);
729 if (liboverride_id !=
nullptr) {
733 else if (new_id !=
nullptr) {
738 const char *item_idname = PyUnicode_AsUTF8(item_src);
743 py_item = Py_NewRef(Py_None);
746 PyList_SET_ITEM(
data.py_list, py_list_index, py_item);
756 if (self->
blo_handle ==
nullptr) [[unlikely]] {
757 PyErr_SetString(PyExc_RuntimeError,
"Context manager not entered");
767 BLI_assert(!do_append || !create_liboverrides);
790 PyObject *ls = PyDict_GetItemString(self->
dict, name_plural);
792 if (ls ==
nullptr || !PyList_Check(ls)) {
796 const Py_ssize_t size = PyList_GET_SIZE(ls);
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);
810 if (item_idname !=
nullptr) {
820#ifdef USE_RNA_DATABLOCKS
822 PyObject *py_item = Py_NewRef(Py_None);
823 PyList_SET_ITEM(ls, i, py_item);
836 else if (do_append) {
839 else if (create_liboverrides) {
846#ifdef USE_RNA_DATABLOCKS
853 PyObject *ls = PyDict_GetItemString(self->
dict, name_plural);
855 if (ls ==
nullptr || !PyList_Check(ls)) {
859 const Py_ssize_t size = PyList_GET_SIZE(ls);
867 iter_data.
idcode = idcode;
891 return PyDict_Keys(self->
dict);
896# pragma clang diagnostic push
897# pragma clang diagnostic ignored "-Wcast-function-type"
899# pragma GCC diagnostic push
900# pragma GCC diagnostic ignored "-Wcast-function-type"
907 METH_VARARGS | METH_KEYWORDS,
913# pragma clang diagnostic pop
915# pragma GCC diagnostic pop
#define BLENDER_FILE_SUBVERSION
#define BLENDER_FILE_VERSION
external readfile function prototypes.
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define POINTER_AS_INT(i)
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)
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)
void BKE_reports_init(ReportList *reports, int flag)
void BKE_asset_metadata_free(AssetMetaData **asset_data)
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)
static PyTypeObject bpy_lib_Type
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
@ 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)
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()
short BKE_blendfile_link_append_context_item_idcode_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
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)
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
@ BKE_LIBLINK_OVERRIDE_INIT
@ BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
const char * BKE_idtype_idcode_to_name_plural(short idcode)
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)
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)
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
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.
#define PyTuple_SET_ITEMS(op_arg,...)
AssetMetaData * asset_data
BLODataBlockInfo::Library library_data
BlendFileReadReport bf_reports
PyObject_HEAD char relpath[FILE_MAX]
eBKELibLinkOverride liboverride_flags
PyObject_HEAD std::optional< PointerRNA > ptr