diff --git a/release/scripts/io/export_fbx.py b/release/scripts/io/export_fbx.py index 35ea4153e5c..e1c7f68c557 100644 --- a/release/scripts/io/export_fbx.py +++ b/release/scripts/io/export_fbx.py @@ -1293,16 +1293,18 @@ def write(filename, batch_objects = None, \ file.write('\n\t}') def copy_image(image): - - rel = image.get_export_path(basepath, True) - base = os.path.basename(rel) + fn = bpy.utils.expandpath(image.filename) + fn_strip = os.path.basename(fn) if EXP_IMAGE_COPY: - absp = image.get_export_path(basepath, False) - if not os.path.exists(absp): - shutil.copy(image.get_abs_filename(), absp) + rel = fn_strip + fn_abs_dest = os.path.join(basepath, fn_strip) + if not os.path.exists(fn_abs_dest): + shutil.copy(fn, fn_abs_dest) + else: + rel = os.path.relpath(fn, basepath) - return (rel, base) + return (rel, fn_strip) # tex is an Image (Arystan) def write_video(texname, tex): diff --git a/release/scripts/io/export_obj.py b/release/scripts/io/export_obj.py index e3b6715d288..2d039bfd45c 100644 --- a/release/scripts/io/export_obj.py +++ b/release/scripts/io/export_obj.py @@ -44,6 +44,7 @@ will be exported as mesh data. # import math import os import time +import shutil import bpy import Mathutils @@ -76,12 +77,15 @@ def write_mtl(scene, filename, copy_images): dest_dir = os.path.dirname(filename) def copy_image(image): - rel = image.get_export_path(dest_dir, True) - + fn = bpy.utils.expandpath(image.filename) + fn_strip = os.path.basename(fn) if copy_images: - abspath = image.get_export_path(dest_dir, False) - if not os.path.exists(abs_path): - shutil.copy(image.get_abs_filename(), abs_path) + rel = fn_strip + fn_abs_dest = os.path.join(dest_dir, fn_strip) + if not os.path.exists(fn_abs_dest): + shutil.copy(fn, fn_abs_dest) + else: + rel = fn return rel diff --git a/release/scripts/io/export_x3d.py b/release/scripts/io/export_x3d.py index 61a2e8220e2..f1ab8cd3de7 100644 --- a/release/scripts/io/export_x3d.py +++ b/release/scripts/io/export_x3d.py @@ -794,7 +794,7 @@ class x3d_class: pic = tex.image # using .expandpath just in case, os.path may not expect // - basename = os.path.basename(pic.get_abs_filename()) + basename = os.path.basename(bpy.utils.expandpath(pic.filename)) pic = alltextures[i].image # pic = alltextures[i].getImage() diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h index 153cf3f300e..6b22b10cf24 100644 --- a/source/blender/blenkernel/BKE_image.h +++ b/source/blender/blenkernel/BKE_image.h @@ -130,9 +130,6 @@ void BKE_image_assign_ibuf(struct Image *ima, struct ImBuf *ibuf); /* called on frame change or before render */ void BKE_image_user_calc_frame(struct ImageUser *iuser, int cfra, int fieldnr); -/* produce image export path */ -int BKE_get_image_export_path(struct Image *im, const char *dest_dir, char *abs, int abs_size, char *rel, int rel_size); - /* fix things in ImageUser when new image gets assigned */ void BKE_image_user_new_image(struct Image *ima, struct ImageUser *iuser); diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index d6edb068fa0..accadb3d434 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -2260,104 +2260,3 @@ void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr) if(iuser->ok==0) iuser->ok= 1; } } - -/* - Produce image export path. - - Fails returning 0 if image filename is empty or if destination path - matches image path (i.e. both are the same file). - - 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 - - For example //textures/foo/bar.png becomes - [dest_dir]/textures/foo/bar.png. - - - if an image is not "below" current .blend file directory, - disregard it's path and copy it in the same directory where 3D file - goes. - - For example //../foo/bar.png becomes [dest_dir]/bar.png. - - This logic will help ensure that all image paths are relative and - that a user gets his images in one place. It'll also provide - consistent behaviour across exporters. - */ -int BKE_get_image_export_path(struct Image *im, const char *dest_dir, char *abs, int abs_size, char *rel, int rel_size) -{ - 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_dirfile_basic(G.sce, blend_dir, NULL); - - if (!strlen(im->name)) { - if (G.f & G_DEBUG) printf("Invalid image type.\n"); - return 0; - } - - BLI_strncpy(path, im->name, sizeof(path)); - - /* expand "//" in filename and get absolute path */ - BLI_convertstringcode(path, G.sce); - - /* get the directory part */ - BLI_split_dirfile_basic(path, dir, base); - - len= strlen(blend_dir); - - rel_dir[0] = 0; - - /* if image is "below" current .blend file directory */ - if (!strncmp(path, blend_dir, len)) { - - /* if image is _in_ current .blend file directory */ - if (!strcmp(dir, blend_dir)) { - BLI_join_dirfile(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, dest_dir, rel_dir); - BLI_join_dirfile(dest_path, dest_path, base); - } - - } - /* image is out of current directory */ - else { - BLI_join_dirfile(dest_path, dest_dir, base); - } - - if (abs) - BLI_strncpy(abs, dest_path, abs_size); - - if (rel) { - strncat(rel, rel_dir, rel_size); - strncat(rel, base, rel_size); - } - - /* return 2 if src=dest */ - if (!strcmp(path, dest_path)) { - if (G.f & G_DEBUG) printf("%s and %s are the same file\n", path, dest_path); - return 2; - } - - return 1; -} - diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 9e494de5379..9b4084aa172 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -59,6 +59,7 @@ void BLI_make_existing_file(char *name); void BLI_split_dirfile(char *string, char *dir, char *file); void BLI_split_dirfile_basic(const char *string, char *dir, char *file); void BLI_join_dirfile(char *string, const char *dir, const char *file); +int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char *base_dir, const char *src_dir, const char *dest_dir); void BLI_getlastdir(const char* dir, char *last, int maxlen); int BLI_testextensie(const char *str, const char *ext); void BLI_uniquename(struct ListBase *list, void *vlink, const char defname[], char delim, short name_offs, short len); diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 1bf321ec1bc..fe43960b770 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1320,6 +1320,106 @@ void BLI_join_dirfile(char *string, const char *dir, const char *file) } } + +/* + Produce image export path. + + Fails returning 0 if image filename is empty or if destination path + matches image path (i.e. both are the same file). + + 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 + + For example //textures/foo/bar.png becomes + [dest_dir]/textures/foo/bar.png. + + - if an image is not "below" current .blend file directory, + disregard it's path and copy it in the same directory where 3D file + goes. + + For example //../foo/bar.png becomes [dest_dir]/bar.png. + + This logic will help ensure that all image paths are relative and + that a user gets his images in one place. It'll also provide + consistent behaviour across exporters. + */ +int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, 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_dirfile_basic(base_dir, blend_dir, NULL); + + if (src_dir[0]=='\0') + return 0; + + BLI_strncpy(path, src_dir, sizeof(path)); + + /* expand "//" in filename and get absolute path */ + BLI_convertstringcode(path, base_dir); + + /* get the directory part */ + BLI_split_dirfile_basic(path, dir, base); + + len= strlen(blend_dir); + + rel_dir[0] = 0; + + /* if image is "below" current .blend file directory */ + if (!strncmp(path, blend_dir, len)) { + + /* if image is _in_ current .blend file directory */ + if (!strcmp(dir, blend_dir)) { + BLI_join_dirfile(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, dest_dir, rel_dir); + BLI_join_dirfile(dest_path, dest_path, base); + } + + } + /* image is out of current directory */ + else { + BLI_join_dirfile(dest_path, dest_dir, base); + } + + if (abs) + BLI_strncpy(abs, dest_path, abs_size); + + if (rel) { + strncat(rel, rel_dir, rel_size); + strncat(rel, base, rel_size); + } + + /* return 2 if src=dest */ + if (!strcmp(path, dest_path)) { + // if (G.f & G_DEBUG) printf("%s and %s are the same file\n", path, dest_path); + return 2; + } + + return 1; +} + + static int add_win32_extension(char *name) { int retval = 0; diff --git a/source/blender/collada/DocumentExporter.cpp b/source/blender/collada/DocumentExporter.cpp index 4b35499fb62..3e1898a64d9 100644 --- a/source/blender/collada/DocumentExporter.cpp +++ b/source/blender/collada/DocumentExporter.cpp @@ -1435,9 +1435,9 @@ public: BLI_split_dirfile_basic(mfilename, dir, NULL); - BKE_get_image_export_path(image, dir, abs, sizeof(abs), rel, sizeof(rel)); + BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.sce, image->name, dir); - if (strlen(abs)) { + if (abs[0] != '\0') { // make absolute source path BLI_strncpy(src, image->name, sizeof(src)); diff --git a/source/blender/makesrna/intern/rna_image_api.c b/source/blender/makesrna/intern/rna_image_api.c index e394fd4f710..e87ec02daea 100644 --- a/source/blender/makesrna/intern/rna_image_api.c +++ b/source/blender/makesrna/intern/rna_image_api.c @@ -45,24 +45,6 @@ #include "MEM_guardedalloc.h" -/* - User should check if returned path exists before copying a file there. - - TODO: it would be better to return a (abs, rel) tuple. -*/ -static char *rna_Image_get_export_path(Image *image, char *dest_dir, int rel) -{ - int length = FILE_MAX; - char *path= MEM_callocN(length, "image file path"); - - if (!BKE_get_image_export_path(image, dest_dir, rel ? NULL : path, length, rel ? path : NULL, length )) { - MEM_freeN(path); - return NULL; - } - - return path; -} - static void rna_Image_save(Image *image, bContext *C, ReportList *reports, char *path, Scene *scene) { ImBuf *ibuf; @@ -92,17 +74,6 @@ static void rna_Image_save(Image *image, bContext *C, ReportList *reports, char } } -char *rna_Image_get_abs_filename(Image *image, bContext *C) -{ - char *filename= MEM_callocN(FILE_MAX, "Image.get_abs_filename()"); - - BLI_strncpy(filename, image->name, FILE_MAXDIR + FILE_MAXFILE); - BLI_convertstringcode(filename, CTX_data_main(C)->name); - BLI_convertstringframe(filename, CTX_data_scene(C)->r.cfra, 0); - - return filename; -} - #else void RNA_api_image(StructRNA *srna) @@ -110,21 +81,6 @@ void RNA_api_image(StructRNA *srna) FunctionRNA *func; PropertyRNA *parm; - func= RNA_def_function(srna, "get_export_path", "rna_Image_get_export_path"); - RNA_def_function_ui_description(func, "Produce image export path."); - parm= RNA_def_string(func, "dest_dir", "", 0, "", "Destination directory."); - RNA_def_property_flag(parm, PROP_REQUIRED); - parm= RNA_def_boolean(func, "get_rel_path", 1, "", "Return relative path if True."); - RNA_def_property_flag(parm, PROP_REQUIRED); - parm= RNA_def_string(func, "path", "", 0, "", "Absolute export path."); - RNA_def_function_return(func, parm); - - func= RNA_def_function(srna, "get_abs_filename", "rna_Image_get_abs_filename"); - RNA_def_function_ui_description(func, "Get absolute filename."); - RNA_def_function_flag(func, FUNC_USE_CONTEXT); - parm= RNA_def_string_file_path(func, "abs_filename", NULL, 0, "", "Image/movie absolute filename."); - RNA_def_function_return(func, parm); - func= RNA_def_function(srna, "save", "rna_Image_save"); RNA_def_function_ui_description(func, "Save image to a specific path."); RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);