Example #1
0
void python_script_error_jump(const char *filepath, int *lineno, int *offset)
{
  PyObject *exception, *value;
  PyTracebackObject *tb;

  *lineno = -1;
  *offset = 0;

  PyErr_Fetch(&exception, &value, (PyObject **)&tb);

  if (exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
    /* no traceback available when SyntaxError.
     * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
    PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
    PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */

    if (value) { /* should always be true */
      PyObject *message;
      PyObject *filename_py, *text_py;

      if (parse_syntax_error(value, &message, &filename_py, lineno, offset, &text_py)) {
        const char *filename = _PyUnicode_AsString(filename_py);
        /* python adds a '/', prefix, so check for both */
        if ((BLI_path_cmp(filename, filepath) == 0) ||
            ((filename[0] == '\\' || filename[0] == '/') &&
             BLI_path_cmp(filename + 1, filepath) == 0)) {
          /* good */
        }
        else {
          *lineno = -1;
        }
      }
      else {
        *lineno = -1;
      }
    }
  }
  else {
    PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
    PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
    PyErr_Print();

    for (tb = (PyTracebackObject *)PySys_GetObject("last_traceback");
         tb && (PyObject *)tb != Py_None;
         tb = tb->tb_next) {
      PyObject *coerce;
      const char *tb_filepath = traceback_filepath(tb, &coerce);
      const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
                         ((tb_filepath[0] == '\\' || tb_filepath[0] == '/') &&
                          BLI_path_cmp(tb_filepath + 1, filepath) == 0));
      Py_DECREF(coerce);

      if (match) {
        *lineno = tb->tb_lineno;
        /* used to break here, but better find the inner most line */
      }
    }
  }
}
Example #2
0
static void write_history(void)
{
	struct RecentFile *recent, *next_recent;
	char name[FILE_MAX];
	const char *user_config_dir;
	FILE *fp;
	int i;

	/* no write history for recovered startup files */
	if (G.main->name[0] == 0)
		return;
	
	/* will be NULL in background mode */
	user_config_dir = BLI_get_folder_create(BLENDER_USER_CONFIG, NULL);
	if (!user_config_dir)
		return;

	BLI_make_file_string("/", name, user_config_dir, BLENDER_HISTORY_FILE);

	recent = G.recent_files.first;
	/* refresh recent-files.txt of recent opened files, when current file was changed */
	if (!(recent) || (BLI_path_cmp(recent->filepath, G.main->name) != 0)) {
		fp = BLI_fopen(name, "w");
		if (fp) {
			/* add current file to the beginning of list */
			recent = (RecentFile *)MEM_mallocN(sizeof(RecentFile), "RecentFile");
			recent->filepath = BLI_strdup(G.main->name);
			BLI_addhead(&(G.recent_files), recent);
			/* write current file to recent-files.txt */
			fprintf(fp, "%s\n", recent->filepath);
			recent = recent->next;
			i = 1;
			/* write rest of recent opened files to recent-files.txt */
			while ((i < U.recent_files) && (recent)) {
				/* this prevents to have duplicities in list */
				if (BLI_path_cmp(recent->filepath, G.main->name) != 0) {
					fprintf(fp, "%s\n", recent->filepath);
					recent = recent->next;
				}
				else {
					next_recent = recent->next;
					MEM_freeN(recent->filepath);
					BLI_freelinkN(&(G.recent_files), recent);
					recent = next_recent;
				}
				i++;
			}
			fclose(fp);
		}

		/* also update most recent files on System */
		GHOST_addToSystemRecentFiles(G.main->name);
	}
}
Example #3
0
static void file_panel_category(const bContext *C, Panel *pa, FSMenuCategory category, short *nr, int icon, int allow_delete)
{
	SpaceFile *sfile = CTX_wm_space_file(C);
	uiBlock *block;
	uiBut *but;
	uiLayout *box, *col;
	struct FSMenu *fsmenu = fsmenu_get();
	int i, nentries = fsmenu_get_nentries(fsmenu, category);

	/* reset each time */
	*nr = -1;

	/* hide if no entries */
	if (nentries == 0)
		return;

	/* layout */
	uiLayoutSetAlignment(pa->layout, UI_LAYOUT_ALIGN_LEFT);
	block = uiLayoutGetBlock(pa->layout);
	box = uiLayoutBox(pa->layout);
	col = uiLayoutColumn(box, TRUE);

	for (i = 0; i < nentries; ++i) {
		char dir[FILE_MAX];
		char temp[FILE_MAX];
		uiLayout *layout = uiLayoutRow(col, FALSE);
		char *entry;
		
		entry = fsmenu_get_entry(fsmenu, category, i);
		
		/* set this list item as active if we have a match */
		if (sfile->params) {
			if (BLI_path_cmp(sfile->params->dir, entry) == 0) {
				*nr = i;
			}
		}

		/* create nice bookmark name, shows last directory in the full path currently */
		BLI_strncpy(temp, entry, FILE_MAX);
		BLI_add_slash(temp);
		BLI_getlastdir(temp, dir, FILE_MAX);
		BLI_del_slash(dir);

		if (dir[0] == 0)
			BLI_strncpy(dir, entry, FILE_MAX);

		/* create list item */
		but = uiDefIconTextButS(block, LISTROW, 0, icon, dir, 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, nr, 0, i, 0, 0, entry);
		uiButSetFunc(but, file_panel_cb, entry, NULL);
		uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
		uiButSetFlag(but, UI_ICON_LEFT | UI_TEXT_LEFT);

		/* create delete button */
		if (allow_delete && fsmenu_can_save(fsmenu, category, i)) {
			uiBlockSetEmboss(block, UI_EMBOSSN);
			uiItemIntO(layout, "", ICON_X, "FILE_OT_delete_bookmark", "index", i);
			uiBlockSetEmboss(block, UI_EMBOSS);
		}
	}
}
Example #4
0
bSound *BKE_sound_new_file_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
{
	bSound *sound;
	char str[FILE_MAX], strtest[FILE_MAX];

	BLI_strncpy(str, filepath, sizeof(str));
	BLI_path_abs(str, bmain->name);

	/* first search an identical filepath */
	for (sound = bmain->sound.first; sound; sound = sound->id.next) {
		BLI_strncpy(strtest, sound->name, sizeof(sound->name));
		BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &sound->id));

		if (BLI_path_cmp(strtest, str) == 0) {
			sound->id.us++;  /* officially should not, it doesn't link here! */
			if (r_exists)
				*r_exists = true;
			return sound;
		}
	}

	if (r_exists)
		*r_exists = false;
	return BKE_sound_new_file(bmain, filepath);
}
Example #5
0
/**
 * Run after saving a file to refresh the #BLENDER_HISTORY_FILE list.
 */
static void wm_history_file_update(void)
{
	RecentFile *recent;

	/* no write history for recovered startup files */
	if (G.main->name[0] == 0)
		return;

	recent = G.recent_files.first;
	/* refresh recent-files.txt of recent opened files, when current file was changed */
	if (!(recent) || (BLI_path_cmp(recent->filepath, G.main->name) != 0)) {

		recent = wm_file_history_find(G.main->name);
		if (recent) {
			BLI_remlink(&G.recent_files, recent);
		}
		else {
			RecentFile *recent_next;
			for (recent = BLI_findlink(&G.recent_files, U.recent_files - 1); recent; recent = recent_next) {
				recent_next = recent->next;
				wm_history_file_free(recent);
			}
			recent = wm_history_file_new(G.main->name);
		}

		/* add current file to the beginning of list */
		BLI_addhead(&(G.recent_files), recent);

		/* write current file to recent-files.txt */
		wm_history_file_write();

		/* also update most recent files on System */
		GHOST_addToSystemRecentFiles(G.main->name);
	}
}
Example #6
0
static void filelist_read_library(struct FileList* filelist)
{
	if (!filelist) return;
	BLI_cleanup_dir(G.main->name, filelist->dir);
	filelist_from_library(filelist);
	if (!filelist->libfiledata) {
		int num;
		struct direntry *file;

		BLI_make_exist(filelist->dir);
		filelist_read_dir(filelist);
		file = filelist->filelist;
		for (num=0; num<filelist->numfiles; num++, file++) {
			if (BLO_has_bfile_extension(file->relname)) {
				char name[FILE_MAX];

				BLI_join_dirfile(name, sizeof(name), filelist->dir, file->relname);

				/* prevent current file being used as acceptable dir */
				if (BLI_path_cmp(G.main->name, name) != 0) {
					file->type &= ~S_IFMT;
					file->type |= S_IFDIR;
				}
			}
		}
	}
}
Example #7
0
Main* KX_BlenderSceneConverter::GetMainDynamicPath(const char *path)
{
	for (vector<Main*>::iterator it=m_DynamicMaggie.begin(); !(it==m_DynamicMaggie.end()); it++)
		if (BLI_path_cmp((*it)->name, path) == 0)
			return *it;
	
	return NULL;
}
Example #8
0
static DirBLF *blf_dir_find(const char *path)
{
	DirBLF *p;
	
	p = global_font_dir.first;
	while (p) {
		if (BLI_path_cmp(p->path, path) == 0)
			return p;
		p = p->next;
	}
	return NULL;
}
int UI_iconfile_get_index(const char *filename)
{
	IconFile *ifile;
	ListBase *list = &(iconfilelist);
	
	for (ifile = list->first; ifile; ifile = ifile->next) {
		if (BLI_path_cmp(filename, ifile->filename) == 0) {
			return ifile->index;
		}
	}
	
	return 0;
}
Example #10
0
int folderlist_clear_next(struct SpaceFile *sfile)
{
	struct FolderList *folder;

	// if there is no folder_next there is nothing we can clear
	if (!sfile->folders_next)
		return 0;

	// if previous_folder, next_folder or refresh_folder operators are executed it doesn't clear folder_next
	folder = sfile->folders_prev->last;
	if ((!folder) ||(BLI_path_cmp(folder->foldername, sfile->params->dir) == 0))
		return 0;

	// eventually clear flist->folders_next
	return 1;
}
Example #11
0
static int editsource_text_edit(bContext *C,
                                wmOperator *op,
                                const char filepath[FILE_MAX],
                                const int line)
{
  struct Main *bmain = CTX_data_main(C);
  Text *text;

  /* Developers may wish to copy-paste to an external editor. */
  printf("%s:%d\n", filepath, line);

  for (text = bmain->texts.first; text; text = text->id.next) {
    if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
      break;
    }
  }

  if (text == NULL) {
    text = BKE_text_load(bmain, filepath, BKE_main_blendfile_path(bmain));
    id_us_ensure_real(&text->id);
  }

  if (text == NULL) {
    BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath);
    return OPERATOR_CANCELLED;
  }
  else {
    /* naughty!, find text area to set, not good behavior
     * but since this is a dev tool lets allow it - campbell */
    ScrArea *sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
    if (sa) {
      SpaceText *st = sa->spacedata.first;
      st->text = text;
    }
    else {
      BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2);
    }

    txt_move_toline(text, line - 1, false);
    WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text);
  }

  return OPERATOR_FINISHED;
}
Example #12
0
void folderlist_pushdir(ListBase *folderlist, const char *dir)
{
	struct FolderList *folder, *previous_folder;
	previous_folder = folderlist->last;

	/* check if already exists */
	if (previous_folder && previous_folder->foldername) {
		if (BLI_path_cmp(previous_folder->foldername, dir) == 0) {
			return;
		}
	}

	/* create next folder element */
	folder = MEM_mallocN(sizeof(*folder), __func__);
	folder->foldername = BLI_strdup(dir);

	/* add it to the end of the list */
	BLI_addtail(folderlist, folder);
}
Example #13
0
File: bpy.c Project: BHCLL/blendocv
static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
{
	struct BPathIterator *bpi;
	PyObject *list, *st; /* stupidly big string to be safe */
	/* be sure there is low chance of the path being too short */
	char filepath_expanded[1024];
	const char *lib;

	int absolute= 0;
	static const char *kwlist[]= {"absolute", NULL};

	if (!PyArg_ParseTupleAndKeywords(args, kw, "|i:blend_paths", (char **)kwlist, &absolute))
		return NULL;

	list= PyList_New(0);

	for (BLI_bpathIterator_init(&bpi, G.main, G.main->name, 0); !BLI_bpathIterator_isDone(bpi); BLI_bpathIterator_step(bpi)) {
		/* build the list */
		if (absolute) {
			BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
		}
		else {
			lib= BLI_bpathIterator_getLib(bpi);
			if (lib && (BLI_path_cmp(lib, BLI_bpathIterator_getBasePath(bpi)))) { /* relative path to the library is NOT the same as our blendfile path, return an absolute path */
				BLI_bpathIterator_getPathExpanded(bpi, filepath_expanded);
			}
			else {
				BLI_bpathIterator_getPath(bpi, filepath_expanded);
			}
		}
		st= PyUnicode_DecodeFSDefault(filepath_expanded);

		PyList_Append(list, st);
		Py_DECREF(st);
	}

	BLI_bpathIterator_free(bpi);

	return list;
}
Example #14
0
static int editsource_text_edit(bContext *C, wmOperator *op,
                                char filepath[240], int line)
{
	struct Main *bmain= CTX_data_main(C);
	Text *text;

	for (text=bmain->text.first; text; text=text->id.next) {
		if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
			break;
		}
	}

	if (text == NULL) {
		text= add_text(filepath, bmain->name);
	}

	if (text == NULL) {
		BKE_reportf(op->reports, RPT_WARNING,
		            "file: '%s' can't be opened", filepath);
		return OPERATOR_CANCELLED;
	}
	else {
		/* naughty!, find text area to set, not good behavior
		 * but since this is a dev tool lets allow it - campbell */
		ScrArea *sa= BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TEXT, 0);
		if(sa) {
			SpaceText *st= sa->spacedata.first;
			st->text= text;
		}
		else {
			BKE_reportf(op->reports, RPT_INFO,
			            "See '%s' in the text editor", text->id.name + 2);
		}

		txt_move_toline(text, line - 1, FALSE);
		WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
	}

	return OPERATOR_FINISHED;
}
Example #15
0
void folderlist_pushdir(ListBase* folderlist, const char *dir)
{
	struct FolderList *folder, *previous_folder;
	previous_folder = folderlist->last;

	// check if already exists
	if (previous_folder && previous_folder->foldername) {
		if (BLI_path_cmp(previous_folder->foldername, dir)==0) {
			return;
		}
	}

	// create next folder element
	folder = (FolderList*)MEM_mallocN(sizeof(FolderList),"FolderList");
	folder->foldername = (char*)MEM_mallocN(sizeof(char)*(strlen(dir)+1), "foldername");
	folder->foldername[0] = '\0';

	BLI_strncpy(folder->foldername, dir, FILE_MAXDIR);

	// add it to the end of the list
	BLI_addtail(folderlist, folder);
}
Example #16
0
void ImagesExporter::export_UV_Image(Image *image, bool use_copies) 
{
	std::string name(id_name(image));
	std::string translated_name(translate_id(name));
	bool not_yet_exported = find(mImages.begin(), mImages.end(), translated_name) == mImages.end();

	if (not_yet_exported) {

		ImBuf *imbuf       = BKE_image_acquire_ibuf(image, NULL, NULL);
		if (!imbuf) {
			fprintf(stderr, "Collada export: image does not exist:\n%s\n", image->name);
			return;
		}

		bool  is_dirty     = (imbuf->userflags & IB_BITMAPDIRTY) != 0;

		ImageFormatData imageFormat;
		BKE_imbuf_to_image_format(&imageFormat, imbuf);

		short image_source = image->source;
		bool  is_generated = image_source == IMA_SRC_GENERATED;
		bool  is_packed    = image->packedfile != NULL;

		char export_path[FILE_MAX];
		char source_path[FILE_MAX];
		char export_dir[FILE_MAX];
		char export_file[FILE_MAX];

		// Destination folder for exported assets
		BLI_split_dir_part(this->export_settings->filepath, export_dir, sizeof(export_dir));

		if (is_generated || is_dirty || use_copies || is_packed) {

			// make absolute destination path

			BLI_strncpy(export_file, name.c_str(), sizeof(export_file));
			BKE_image_path_ensure_ext_from_imformat(export_file, &imageFormat);

			BLI_join_dirfile(export_path, sizeof(export_path), export_dir, export_file);

			// make dest directory if it doesn't exist
			BLI_make_existing_file(export_path);
		}

		if (is_generated || is_dirty || is_packed) {

			// This image in its current state only exists in Blender memory.
			// So we have to export it. The export will keep the image state intact,
			// so the exported file will not be associated with the image.

			if (BKE_imbuf_write_as(imbuf, export_path, &imageFormat, true) == 0) {
				fprintf(stderr, "Collada export: Cannot export image to:\n%s\n", export_path);
				return;
			}
			BLI_strncpy(export_path, export_file, sizeof(export_path));
		}
		else {

			// make absolute source path
			BLI_strncpy(source_path, image->name, sizeof(source_path));
			BLI_path_abs(source_path, G.main->name);
			BLI_cleanup_path(NULL, source_path);

			if (use_copies) {
			
				// This image is already located on the file system.
				// But we want to create copies here.
				// To move images into the same export directory.
				// Note: If an image is already located in the export folder,
				// then skip the copy (as it would result in a file copy error).

				if (BLI_path_cmp(source_path, export_path) != 0) {
					if (BLI_copy(source_path, export_path) != 0) {
						fprintf(stderr, "Collada export: Cannot copy image:\n source:%s\ndest :%s\n", source_path, export_path);
						return;
					}
				}

				BLI_strncpy(export_path, export_file, sizeof(export_path));

			}
			else {

				// Do not make any copies, but use the source path directly as reference
				// to the original image

				BLI_strncpy(export_path, source_path, sizeof(export_path));
			}
		}

		COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(export_path)), translated_name, translated_name); /* set name also to mNameNC. This helps other viewers import files exported from Blender better */
		img.add(mSW);
		fprintf(stdout, "Collada export: Added image: %s\n", export_file);
		mImages.push_back(translated_name);

		BKE_image_release_ibuf(image, imbuf, NULL);
	}
}
Example #17
0
int WM_write_file(bContext *C, const char *target, int fileflags, ReportList *reports, int copy)
{
	Library *li;
	int len;
	char filepath[FILE_MAX];

	int *thumb = NULL;
	ImBuf *ibuf_thumb = NULL;

	len = strlen(target);
	
	if (len == 0) {
		BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
		return -1;
	}

	if (len >= FILE_MAX) {
		BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
		return -1;
	}
 
	BLI_strncpy(filepath, target, FILE_MAX);
	BLI_replace_extension(filepath, FILE_MAX, ".blend");
	/* don't use 'target' anymore */
	
	/* send the OnSave event */
	for (li = G.main->library.first; li; li = li->id.next) {
		if (BLI_path_cmp(li->filepath, filepath) == 0) {
			BKE_reportf(reports, RPT_ERROR, "Can't overwrite used library '%.240s'", filepath);
			return -1;
		}
	}

	/* blend file thumbnail */
	/* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */
	if (U.flag & USER_SAVE_PREVIEWS) {
		ibuf_thumb = blend_file_thumb(CTX_data_scene(C), CTX_wm_screen(C), &thumb);
	}

	BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_PRE);

	/* operator now handles overwrite checks */

	if (G.fileflags & G_AUTOPACK) {
		packAll(G.main, reports);
	}
	
	ED_object_exit_editmode(C, EM_DO_UNDO);
	ED_sculpt_force_update(C);

	/* don't forget not to return without! */
	WM_cursor_wait(1);
	
	fileflags |= G_FILE_HISTORY; /* write file history */

	if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) {
		if (!copy) {
			G.relbase_valid = 1;
			BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));  /* is guaranteed current file */
	
			G.save_over = 1; /* disable untitled.blend convention */
		}

		if (fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
		else G.fileflags &= ~G_FILE_COMPRESS;
		
		if (fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY;
		else G.fileflags &= ~G_FILE_AUTOPLAY;

		/* prevent background mode scripts from clobbering history */
		if (!G.background) {
			write_history();
		}

		BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_POST);

		/* run this function after because the file cant be written before the blend is */
		if (ibuf_thumb) {
			IMB_thumb_delete(filepath, THB_FAIL); /* without this a failed thumb overrides */
			ibuf_thumb = IMB_thumb_create(filepath, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb);
			IMB_freeImBuf(ibuf_thumb);
		}

		if (thumb) MEM_freeN(thumb);
	}
	else {
		if (ibuf_thumb) IMB_freeImBuf(ibuf_thumb);
		if (thumb) MEM_freeN(thumb);
		
		WM_cursor_wait(0);
		return -1;
	}

	WM_cursor_wait(0);
	
	return 0;
}
Example #18
0
/**
 * \see #wm_homefile_write_exec wraps #BLO_write_file in a similar way.
 */
int wm_file_write(bContext *C, const char *filepath, int fileflags, ReportList *reports)
{
	Library *li;
	int len;
	int ret = -1;
	BlendThumbnail *thumb, *main_thumb;
	ImBuf *ibuf_thumb = NULL;

	len = strlen(filepath);
	
	if (len == 0) {
		BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
		return ret;
	}

	if (len >= FILE_MAX) {
		BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
		return ret;
	}
	
	/* Check if file write permission is ok */
	if (BLI_exists(filepath) && !BLI_file_is_writable(filepath)) {
		BKE_reportf(reports, RPT_ERROR, "Cannot save blend file, path '%s' is not writable", filepath);
		return ret;
	}
 
	/* note: used to replace the file extension (to ensure '.blend'),
	 * no need to now because the operator ensures,
	 * its handy for scripts to save to a predefined name without blender editing it */
	
	/* send the OnSave event */
	for (li = G.main->library.first; li; li = li->id.next) {
		if (BLI_path_cmp(li->filepath, filepath) == 0) {
			BKE_reportf(reports, RPT_ERROR, "Cannot overwrite used library '%.240s'", filepath);
			return ret;
		}
	}

	/* Call pre-save callbacks befores writing preview, that way you can generate custom file thumbnail... */
	BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_PRE);

	/* blend file thumbnail */
	/* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */
	/* Main now can store a .blend thumbnail, usefull for background mode or thumbnail customization. */
	main_thumb = thumb = CTX_data_main(C)->blen_thumb;
	if ((U.flag & USER_SAVE_PREVIEWS) && BLI_thread_is_main()) {
		ibuf_thumb = blend_file_thumb(CTX_data_scene(C), CTX_wm_screen(C), &thumb);
	}

	/* operator now handles overwrite checks */

	if (G.fileflags & G_AUTOPACK) {
		packAll(G.main, reports, false);
	}

	/* don't forget not to return without! */
	WM_cursor_wait(1);
	
	ED_editors_flush_edits(C, false);

	fileflags |= G_FILE_HISTORY; /* write file history */

	/* first time saving */
	/* XXX temp solution to solve bug, real fix coming (ton) */
	if ((G.main->name[0] == '\0') && !(fileflags & G_FILE_SAVE_COPY)) {
		BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));
	}

	/* XXX temp solution to solve bug, real fix coming (ton) */
	G.main->recovered = 0;
	
	if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) {
		const bool do_history = (G.background == false) && (CTX_wm_manager(C)->op_undo_depth == 0);

		if (!(fileflags & G_FILE_SAVE_COPY)) {
			G.relbase_valid = 1;
			BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));  /* is guaranteed current file */
	
			G.save_over = 1; /* disable untitled.blend convention */
		}

		BKE_BIT_TEST_SET(G.fileflags, fileflags & G_FILE_COMPRESS, G_FILE_COMPRESS);
		BKE_BIT_TEST_SET(G.fileflags, fileflags & G_FILE_AUTOPLAY, G_FILE_AUTOPLAY);

		/* prevent background mode scripts from clobbering history */
		if (do_history) {
			wm_history_file_update();
		}

		BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_POST);

		/* run this function after because the file cant be written before the blend is */
		if (ibuf_thumb) {
			IMB_thumb_delete(filepath, THB_FAIL); /* without this a failed thumb overrides */
			ibuf_thumb = IMB_thumb_create(filepath, THB_LARGE, THB_SOURCE_BLEND, ibuf_thumb);
		}

		ret = 0;  /* Success. */
	}

	if (ibuf_thumb) {
		IMB_freeImBuf(ibuf_thumb);
	}
	if (thumb && thumb != main_thumb) {
		MEM_freeN(thumb);
	}

	WM_cursor_wait(0);

	return ret;
}
Example #19
0
/**
 * Produce image export path.
 * 
 * Returns:
 * 0        if image filename is empty or if destination path
 *          matches image path (i.e. both are the same file).
 * 2        if source is identical to destination.
 * 1        if rebase was successful
 * -------------------------------------------------------------
 * Hint: Trailing slash in dest_dir is optional.
 *
 * Logic:
 *
 * - if an image is "below" current .blend file directory:
 *   rebuild the same dir structure in dest_dir
 *
 *   Example: 
 *   src : //textures/foo/bar.png
 *   dest: [dest_dir]/textures/foo/bar.png.
 *
 * - if an image is not "below" current .blend file directory,
 *   disregard it's path and copy it into the destination  
 *   directory.
 *
 *   Example:
 *   src : //../foo/bar.png becomes
 *   dest: [dest_dir]/bar.png.
 *
 * This logic ensures that all image paths are relative and
 * that a user gets his images in one place. It'll also provide
 * consistent behavior across exporters.
 * IMPORTANT NOTE: If base_dir contains an empty string, then
 * this function returns wrong results!
 * XXX: test on empty base_dir and return an error ?
 */
int BLI_rebase_path(char *abs, size_t abs_len, char *rel, size_t rel_len, const char *base_dir, const char *src_dir, const char *dest_dir)
{
	char path[FILE_MAX];
	char dir[FILE_MAX];
	char base[FILE_MAX];
	char blend_dir[FILE_MAX];   /* directory, where current .blend file resides */
	char dest_path[FILE_MAX];
	char rel_dir[FILE_MAX];
	int len;

	if (abs)
		abs[0] = 0;

	if (rel)
		rel[0] = 0;

	BLI_split_dir_part(base_dir, blend_dir, sizeof(blend_dir));

	if (src_dir[0] == '\0')
		return BLI_REBASE_NO_SRCDIR;

	BLI_strncpy(path, src_dir, sizeof(path));

	/* expand "//" in filename and get absolute path */
	BLI_path_abs(path, base_dir);

	/* get the directory part */
	BLI_split_dirfile(path, dir, base, sizeof(dir), sizeof(base));

	len = strlen(blend_dir);

	rel_dir[0] = 0;

	/* if image is "below" current .blend file directory */
	if (!BLI_path_ncmp(path, blend_dir, len)) {

		/* if image is _in_ current .blend file directory */
		if (BLI_path_cmp(dir, blend_dir) == 0) {
			BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base);
		}
		/* "below" */
		else {
			/* rel = image_path_dir - blend_dir */
			BLI_strncpy(rel_dir, dir + len, sizeof(rel_dir));

			BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, rel_dir);
			BLI_join_dirfile(dest_path, sizeof(dest_path), dest_path, base);
		}

	}
	/* image is out of current directory */
	else {
		BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base);
	}

	if (abs)
		BLI_strncpy(abs, dest_path, abs_len);

	if (rel) {
		strncat(rel, rel_dir, rel_len);
		strncat(rel, base, rel_len);
	}

	/* return 2 if (src == dest) */
	if (BLI_path_cmp(path, dest_path) == 0) {
		// if (G.debug & G_DEBUG) printf("%s and %s are the same file\n", path, dest_path);
		return BLI_REBASE_IDENTITY;
	}

	return BLI_REBASE_OK;
}
Example #20
0
int wm_file_write(bContext *C, const char *filepath, int fileflags, ReportList *reports)
{
	Library *li;
	int len;
	int *thumb = NULL;
	ImBuf *ibuf_thumb = NULL;

	len = strlen(filepath);
	
	if (len == 0) {
		BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
		return -1;
	}

	if (len >= FILE_MAX) {
		BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
		return -1;
	}
 
	/* note: used to replace the file extension (to ensure '.blend'),
	 * no need to now because the operator ensures,
	 * its handy for scripts to save to a predefined name without blender editing it */
	
	/* send the OnSave event */
	for (li = G.main->library.first; li; li = li->id.next) {
		if (BLI_path_cmp(li->filepath, filepath) == 0) {
			BKE_reportf(reports, RPT_ERROR, "Cannot overwrite used library '%.240s'", filepath);
			return -1;
		}
	}

	/* blend file thumbnail */
	/* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */
	if ((U.flag & USER_SAVE_PREVIEWS) && BLI_thread_is_main()) {
		ibuf_thumb = blend_file_thumb(CTX_data_scene(C), CTX_wm_screen(C), &thumb);
	}

	BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_PRE);

	/* operator now handles overwrite checks */

	if (G.fileflags & G_AUTOPACK) {
		packAll(G.main, reports);
	}

	ED_object_editmode_load(CTX_data_edit_object(C));
	ED_sculpt_force_update(C);

	/* don't forget not to return without! */
	WM_cursor_wait(1);
	
	fileflags |= G_FILE_HISTORY; /* write file history */

	/* first time saving */
	/* XXX temp solution to solve bug, real fix coming (ton) */
	if ((G.main->name[0] == '\0') && !(fileflags & G_FILE_SAVE_COPY)) {
		BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));
	}

	/* XXX temp solution to solve bug, real fix coming (ton) */
	G.main->recovered = 0;
	
	if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) {
		if (!(fileflags & G_FILE_SAVE_COPY)) {
			G.relbase_valid = 1;
			BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));  /* is guaranteed current file */
	
			G.save_over = 1; /* disable untitled.blend convention */
		}

		if (fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
		else G.fileflags &= ~G_FILE_COMPRESS;
		
		if (fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY;
		else G.fileflags &= ~G_FILE_AUTOPLAY;

		/* prevent background mode scripts from clobbering history */
		if (!G.background) {
			write_history();
		}

		BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_POST);

		/* run this function after because the file cant be written before the blend is */
		if (ibuf_thumb) {
			IMB_thumb_delete(filepath, THB_FAIL); /* without this a failed thumb overrides */
			ibuf_thumb = IMB_thumb_create(filepath, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb);
			IMB_freeImBuf(ibuf_thumb);
		}

		if (thumb) MEM_freeN(thumb);
	}
	else {
		if (ibuf_thumb) IMB_freeImBuf(ibuf_thumb);
		if (thumb) MEM_freeN(thumb);
		
		WM_cursor_wait(0);
		return -1;
	}

	WM_cursor_wait(0);
	
	return 0;
}