2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * ***** END GPL LICENSE BLOCK *****
21 /** \file blender/blenkernel/intern/blendfile.c
24 * High level `.blend` file read/write,
25 * and functions for writing *partial* files (only selected data-blocks).
31 #include "MEM_guardedalloc.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
36 #include "BLI_listbase.h"
37 #include "BLI_string.h"
38 #include "BLI_path_util.h"
39 #include "BLI_utildefines.h"
41 #include "IMB_colormanagement.h"
43 #include "BKE_appdir.h"
44 #include "BKE_blender.h"
45 #include "BKE_blender_version.h"
46 #include "BKE_blendfile.h"
47 #include "BKE_bpath.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
51 #include "BKE_library.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_screen.h"
57 #include "BLO_readfile.h"
58 #include "BLO_writefile.h"
60 #include "RNA_access.h"
62 #include "RE_pipeline.h"
65 # include "BPY_extern.h"
68 /* -------------------------------------------------------------------- */
70 /** \name High Level `.blend` file read/write.
73 static bool clean_paths_visit_cb(void *UNUSED(userdata), char *path_dst, const char *path_src)
75 strcpy(path_dst, path_src);
76 BLI_path_native_slash(path_dst);
77 return !STREQ(path_dst, path_src);
80 /* make sure path names are correct for OS */
81 static void clean_paths(Main *main)
85 BKE_bpath_traverse_main(main, clean_paths_visit_cb, BKE_BPATH_TRAVERSE_SKIP_MULTIFILE, NULL);
87 for (scene = main->scene.first; scene; scene = scene->id.next) {
88 BLI_path_native_slash(scene->r.pic);
92 static bool wm_scene_is_visible(wmWindowManager *wm, Scene *scene)
95 for (win = wm->windows.first; win; win = win->next) {
96 if (win->screen->scene == scene) {
104 * Context matching, handle no-ui case
106 * \note this is called on Undo so any slow conversion functions here
107 * should be avoided or check (mode != LOAD_UNDO).
109 * \param bfd: Blend file data, freed by this function on exit.
110 * \param filepath: File path or identifier.
112 static void setup_app_data(
113 bContext *C, BlendFileData *bfd,
114 const char *filepath, ReportList *reports)
116 Main *bmain = G.main; /* Valid usage */
117 Scene *curscene = NULL;
118 const bool is_startup = (bfd->filename[0] == '\0');
119 const bool recover = (G.fileflags & G_FILE_RECOVER) != 0;
126 /* may happen with library files - UNDO file should never have NULL cursccene... */
127 if (ELEM(NULL, bfd->curscreen, bfd->curscene)) {
128 BKE_report(reports, RPT_WARNING, "Library file, loading empty scene");
131 else if (BLI_listbase_is_empty(&bfd->main->screen)) {
134 else if ((G.fileflags & G_FILE_NO_UI) && (is_startup == false)) {
141 /* Free all render results, without this stale data gets displayed after loading files */
142 if (mode != LOAD_UNDO) {
143 RE_FreeAllRenderResults();
146 /* Only make filepaths compatible when loading for real (not undo) */
147 if (mode != LOAD_UNDO) {
148 clean_paths(bfd->main);
151 /* XXX here the complex windowmanager matching */
153 /* no load screens? */
154 if (mode != LOAD_UI) {
155 /* Logic for 'track_undo_scene' is to keep using the scene which the active screen has,
156 * as long as the scene associated with the undo operation is visible in one of the open windows.
158 * - 'curscreen->scene' - scene the user is currently looking at.
159 * - 'bfd->curscene' - scene undo-step was created in.
161 * This means users can have 2+ windows open and undo in both without screens switching.
162 * But if they close one of the screens,
163 * undo will ensure that the scene being operated on will be activated
164 * (otherwise we'd be undoing on an off-screen scene which isn't acceptable).
167 bScreen *curscreen = NULL;
168 bool track_undo_scene;
170 /* comes from readfile.c */
171 SWAP(ListBase, bmain->wm, bfd->main->wm);
172 SWAP(ListBase, bmain->screen, bfd->main->screen);
174 /* we re-use current screen */
175 curscreen = CTX_wm_screen(C);
176 /* but use new Scene pointer */
177 curscene = bfd->curscene;
179 track_undo_scene = (mode == LOAD_UNDO && curscreen && curscene && bfd->main->wm.first);
181 if (curscene == NULL) {
182 curscene = bfd->main->scene.first;
184 /* empty file, we add a scene to make Blender work */
185 if (curscene == NULL) {
186 curscene = BKE_scene_add(bfd->main, "Empty");
189 if (track_undo_scene) {
190 /* keep the old (free'd) scene, let 'blo_lib_link_screen_restore'
191 * replace it with 'curscene' if its needed */
194 /* and we enforce curscene to be in current screen */
196 /* can run in bgmode */
197 curscreen->scene = curscene;
201 /* BKE_blender_globals_clear will free G.main, here we can still restore pointers */
202 blo_lib_link_screen_restore(bfd->main, curscreen, curscene);
203 /* curscreen might not be set when loading without ui (see T44217) so only re-assign if available */
205 curscene = curscreen->scene;
208 if (track_undo_scene) {
209 wmWindowManager *wm = bfd->main->wm.first;
210 if (wm_scene_is_visible(wm, bfd->curscene) == false) {
211 curscene = bfd->curscene;
212 curscreen->scene = curscene;
213 BKE_screen_view3d_scene_sync(curscreen);
218 /* free G.main Main database */
219 // CTX_wm_manager_set(C, NULL);
220 BKE_blender_globals_clear();
222 /* clear old property update cache, in case some old references are left dangling */
223 RNA_property_update_cache_free();
225 bmain = G.main = bfd->main;
227 CTX_data_main_set(C, bmain);
230 /* only here free userdef themes... */
231 BKE_blender_userdef_data_set_and_free(bfd->user);
234 /* Security issue: any blend file could include a USER block.
236 * Currently we load prefs from BLENDER_STARTUP_FILE and later on load BLENDER_USERPREF_FILE,
237 * to load the preferences defined in the users home dir.
239 * This means we will never accidentally (or maliciously)
240 * enable scripts auto-execution by loading a '.blend' file.
242 U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE;
245 /* case G_FILE_NO_UI or no screens in file */
246 if (mode != LOAD_UI) {
247 /* leave entire context further unaltered? */
248 CTX_data_scene_set(C, curscene);
251 /* Keep state from preferences. */
252 const int fileflags_skip = G_FILE_FLAGS_RUNTIME;
253 G.fileflags = (G.fileflags & fileflags_skip) | (bfd->fileflags & ~fileflags_skip);
254 CTX_wm_manager_set(C, bmain->wm.first);
255 CTX_wm_screen_set(C, bfd->curscreen);
256 CTX_data_scene_set(C, bfd->curscene);
257 CTX_wm_area_set(C, NULL);
258 CTX_wm_region_set(C, NULL);
259 CTX_wm_menu_set(C, NULL);
260 curscene = bfd->curscene;
263 /* this can happen when active scene was lib-linked, and doesn't exist anymore */
264 if (CTX_data_scene(C) == NULL) {
265 /* in case we don't even have a local scene, add one */
266 if (!bmain->scene.first)
267 BKE_scene_add(bmain, "Empty");
269 CTX_data_scene_set(C, bmain->scene.first);
270 CTX_wm_screen(C)->scene = CTX_data_scene(C);
271 curscene = CTX_data_scene(C);
274 BLI_assert(curscene == CTX_data_scene(C));
277 /* special cases, override loaded flags: */
278 if (G.f != bfd->globalf) {
279 const int flags_keep = (G_SWAP_EXCHANGE | G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
280 bfd->globalf = (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
287 /* let python know about new main */
288 BPY_context_update(C);
291 /* FIXME: this version patching should really be part of the file-reading code,
292 * but we still get too many unrelated data-corruption crashes otherwise... */
293 if (bmain->versionfile < 250)
294 do_versions_ipos_to_animato(bmain);
296 bmain->recovered = 0;
298 /* startup.blend or recovered startup */
299 if (bfd->filename[0] == 0) {
300 bmain->name[0] = '\0';
302 else if (recover && G.relbase_valid) {
303 /* in case of autosave or quit.blend, use original filename instead
304 * use relbase_valid to make sure the file is saved, else we get <memory2> in the filename */
305 filepath = bfd->filename;
306 bmain->recovered = 1;
308 /* these are the same at times, should never copy to the same location */
309 if (bmain->name != filepath)
310 BLI_strncpy(bmain->name, filepath, FILE_MAX);
313 /* baseflags, groups, make depsgraph, etc */
314 /* first handle case if other windows have different scenes visible */
315 if (mode == LOAD_UI) {
316 wmWindowManager *wm = bmain->wm.first;
321 for (win = wm->windows.first; win; win = win->next) {
322 if (win->screen && win->screen->scene) /* zealous check... */
323 if (win->screen->scene != curscene)
324 BKE_scene_set_background(bmain, win->screen->scene);
328 BKE_scene_set_background(bmain, curscene);
330 if (mode != LOAD_UNDO) {
331 RE_FreeAllPersistentData();
332 IMB_colormanagement_check_file_config(bmain);
339 static int handle_subversion_warning(Main *main, ReportList *reports)
341 if (main->minversionfile > BLENDER_VERSION ||
342 (main->minversionfile == BLENDER_VERSION &&
343 main->minsubversionfile > BLENDER_SUBVERSION))
345 BKE_reportf(reports, RPT_ERROR, "File written by newer Blender binary (%d.%d), expect loss of data!",
346 main->minversionfile, main->minsubversionfile);
352 int BKE_blendfile_read(
353 bContext *C, const char *filepath,
354 ReportList *reports, int skip_flags)
357 int retval = BKE_BLENDFILE_READ_OK;
359 /* don't print user-pref loading */
360 if (strstr(filepath, BLENDER_STARTUP_FILE) == NULL) {
361 printf("Read blend: %s\n", filepath);
364 bfd = BLO_read_from_file(filepath, reports, skip_flags);
367 retval = BKE_BLENDFILE_READ_OK_USERPREFS;
370 if (0 == handle_subversion_warning(bfd->main, reports)) {
371 BKE_main_free(bfd->main);
374 retval = BKE_BLENDFILE_READ_FAIL;
377 setup_app_data(C, bfd, filepath, reports);
381 BKE_reports_prependf(reports, "Loading '%s' failed: ", filepath);
383 return (bfd ? retval : BKE_BLENDFILE_READ_FAIL);
386 bool BKE_blendfile_read_from_memory(
387 bContext *C, const void *filebuf, int filelength,
388 ReportList *reports, int skip_flags, bool update_defaults)
392 bfd = BLO_read_from_memory(filebuf, filelength, reports, skip_flags);
395 BLO_update_defaults_startup_blend(bfd->main);
396 setup_app_data(C, bfd, "<memory2>", reports);
399 BKE_reports_prepend(reports, "Loading failed: ");
402 return (bfd != NULL);
405 /* memfile is the undo buffer */
406 bool BKE_blendfile_read_from_memfile(
407 bContext *C, struct MemFile *memfile,
408 ReportList *reports, int skip_flags)
410 Main *bmain = CTX_data_main(C);
413 bfd = BLO_read_from_memfile(bmain, BKE_main_blendfile_path(bmain), memfile, reports, skip_flags);
415 /* remove the unused screens and wm */
416 while (bfd->main->wm.first)
417 BKE_libblock_free(bfd->main, bfd->main->wm.first);
418 while (bfd->main->screen.first)
419 BKE_libblock_free(bfd->main, bfd->main->screen.first);
421 setup_app_data(C, bfd, "<memory1>", reports);
424 BKE_reports_prepend(reports, "Loading failed: ");
427 return (bfd != NULL);
431 * Utility to make a file 'empty' used for startup to optionally give an empty file.
434 void BKE_blendfile_read_make_empty(bContext *C)
436 Main *bmain = CTX_data_main(C);
438 ListBase *lbarray[MAX_LIBARRAY];
442 a = set_listbasepointers(bmain, lbarray);
444 id = lbarray[a]->first;
446 if (ELEM(GS(id->name), ID_SCE, ID_SCR, ID_WM)) {
449 while ((id = lbarray[a]->first)) {
450 BKE_libblock_delete(bmain, id);
456 /* only read the userdef from a .blend */
457 UserDef *BKE_blendfile_userdef_read(const char *filepath, ReportList *reports)
460 UserDef *userdef = NULL;
462 bfd = BLO_read_from_file(filepath, reports, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF);
467 BKE_main_free(bfd->main);
475 UserDef *BKE_blendfile_userdef_read_from_memory(
476 const void *filebuf, int filelength,
480 UserDef *userdef = NULL;
482 bfd = BLO_read_from_memory(filebuf, filelength, reports, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF);
487 BKE_main_free(bfd->main);
491 BKE_reports_prepend(reports, "Loading failed: ");
499 * Only write the userdef in a .blend
502 bool BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
504 Main *mainb = MEM_callocN(sizeof(Main), "empty main");
507 if (BLO_write_file(mainb, filepath, G_FILE_USERPREFS, reports, NULL)) {
517 * Only write the userdef in a .blend, merging with the existing blend file.
520 * \note In the future we should re-evaluate user preferences,
521 * possibly splitting out system/hardware specific prefs.
523 bool BKE_blendfile_userdef_write_app_template(const char *filepath, ReportList *reports)
525 /* if it fails, overwrite is OK. */
526 UserDef *userdef_default = BKE_blendfile_userdef_read(filepath, NULL);
527 if (userdef_default == NULL) {
528 return BKE_blendfile_userdef_write(filepath, reports);
531 BKE_blender_userdef_app_template_data_swap(&U, userdef_default);
532 bool ok = BKE_blendfile_userdef_write(filepath, reports);
533 BKE_blender_userdef_app_template_data_swap(&U, userdef_default);
534 BKE_blender_userdef_data_free(userdef_default, false);
535 MEM_freeN(userdef_default);
543 /* -------------------------------------------------------------------- */
545 /** \name Partial `.blend` file save.
548 void BKE_blendfile_write_partial_begin(Main *bmain_src)
550 BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);
553 void BKE_blendfile_write_partial_tag_ID(ID *id, bool set)
556 id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
559 id->tag &= ~(LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT);
563 static void blendfile_write_partial_cb(void *UNUSED(handle), Main *UNUSED(bmain), void *vid)
567 /* only tag for need-expand if not done, prevents eternal loops */
568 if ((id->tag & LIB_TAG_DOIT) == 0)
569 id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
571 if (id->lib && (id->lib->id.tag & LIB_TAG_DOIT) == 0)
572 id->lib->id.tag |= LIB_TAG_DOIT;
579 bool BKE_blendfile_write_partial(
580 Main *bmain_src, const char *filepath, const int write_flags, ReportList *reports)
582 Main *bmain_dst = MEM_callocN(sizeof(Main), "copybuffer");
583 ListBase *lbarray_dst[MAX_LIBARRAY], *lbarray_src[MAX_LIBARRAY];
586 void *path_list_backup = NULL;
587 const int path_list_flag = (BKE_BPATH_TRAVERSE_SKIP_LIBRARY | BKE_BPATH_TRAVERSE_SKIP_MULTIFILE);
589 /* This is needed to be able to load that file as a real one later
590 * (otherwise main->name will not be set at read time). */
591 BLI_strncpy(bmain_dst->name, bmain_src->name, sizeof(bmain_dst->name));
593 if (write_flags & G_FILE_RELATIVE_REMAP) {
594 path_list_backup = BKE_bpath_list_backup(bmain_src, path_list_flag);
597 BLO_main_expander(blendfile_write_partial_cb);
598 BLO_expand_main(NULL, bmain_src);
600 /* move over all tagged blocks */
601 set_listbasepointers(bmain_src, lbarray_src);
602 a = set_listbasepointers(bmain_dst, lbarray_dst);
605 ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
607 for (id = lb_src->first; id; id = nextid) {
609 if (id->tag & LIB_TAG_DOIT) {
610 BLI_remlink(lb_src, id);
611 BLI_addtail(lb_dst, id);
617 /* save the buffer */
618 retval = BLO_write_file(bmain_dst, filepath, write_flags, reports, NULL);
620 /* move back the main, now sorted again */
621 set_listbasepointers(bmain_src, lbarray_dst);
622 a = set_listbasepointers(bmain_dst, lbarray_src);
625 ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
627 while ((id = BLI_pophead(lb_src))) {
628 BLI_addtail(lb_dst, id);
629 id_sort_by_name(lb_dst, id);
633 MEM_freeN(bmain_dst);
635 if (path_list_backup) {
636 BKE_bpath_list_restore(bmain_src, path_list_flag, path_list_backup);
637 BKE_bpath_list_free(path_list_backup);
643 void BKE_blendfile_write_partial_end(Main *bmain_src)
645 BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);