diff --git a/source/blender/blenloader/intern/readfile.cc b/source/blender/blenloader/intern/readfile.cc index 862650ac1ad..196ad3ca5e8 100644 --- a/source/blender/blenloader/intern/readfile.cc +++ b/source/blender/blenloader/intern/readfile.cc @@ -3094,6 +3094,31 @@ static void read_libblock_undo_restore_at_old_address(FileData *fd, Main *main, BLI_addtail(new_lb, id_old); BLI_addtail(old_lb, id); + + /* In case a library has been re-read, it has added already its own split main to the new Main + * (see #direct_link_library code). + * + * Since we are replacing it with the 'id_old' address, we need to update that Main::curlib + * pointer accordingly. + * + * Note that: + * - This code is only for undo, and on undo we do not re-read regular libraries, only archive + * ones for packed data. + * - The new split main should still be empty at this stage (this code and adding the split + * Main in #direct_link_library are part of the same #read_libblock call). + */ + if (GS(id_old->name) == ID_LI) { + Library *lib_old = blender::id_cast(id_old); + Library *lib = blender::id_cast(id); + BLI_assert(lib_old->flag & LIBRARY_FLAG_IS_ARCHIVE); + + for (Main *bmain_iter : *fd->bmain->split_mains) { + if (bmain_iter->curlib == lib) { + BLI_assert(BKE_main_is_empty(bmain_iter)); + bmain_iter->curlib = lib_old; + } + } + } } static bool read_libblock_undo_restore(