remove rna functions image.get_export_path() and image.get_abs_filename(), filename functions should not be spesific to images.

rename BKE_get_image_export_path() to BKE_rebase_path() and take a filename arg rather then an image.

obj export file copy was also not working because of a missing import.
This commit is contained in:
Campbell Barton 2010-02-26 11:50:59 +00:00
parent 3ea627245b
commit 878d8b44ee
9 changed files with 122 additions and 163 deletions

@ -1293,16 +1293,18 @@ def write(filename, batch_objects = None, \
file.write('\n\t}') file.write('\n\t}')
def copy_image(image): def copy_image(image):
fn = bpy.utils.expandpath(image.filename)
rel = image.get_export_path(basepath, True) fn_strip = os.path.basename(fn)
base = os.path.basename(rel)
if EXP_IMAGE_COPY: if EXP_IMAGE_COPY:
absp = image.get_export_path(basepath, False) rel = fn_strip
if not os.path.exists(absp): fn_abs_dest = os.path.join(basepath, fn_strip)
shutil.copy(image.get_abs_filename(), absp) 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) # tex is an Image (Arystan)
def write_video(texname, tex): def write_video(texname, tex):

@ -44,6 +44,7 @@ will be exported as mesh data.
# import math # import math
import os import os
import time import time
import shutil
import bpy import bpy
import Mathutils import Mathutils
@ -76,12 +77,15 @@ def write_mtl(scene, filename, copy_images):
dest_dir = os.path.dirname(filename) dest_dir = os.path.dirname(filename)
def copy_image(image): 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: if copy_images:
abspath = image.get_export_path(dest_dir, False) rel = fn_strip
if not os.path.exists(abs_path): fn_abs_dest = os.path.join(dest_dir, fn_strip)
shutil.copy(image.get_abs_filename(), abs_path) if not os.path.exists(fn_abs_dest):
shutil.copy(fn, fn_abs_dest)
else:
rel = fn
return rel return rel

@ -794,7 +794,7 @@ class x3d_class:
pic = tex.image pic = tex.image
# using .expandpath just in case, os.path may not expect // # 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].image
# pic = alltextures[i].getImage() # pic = alltextures[i].getImage()

@ -130,9 +130,6 @@ void BKE_image_assign_ibuf(struct Image *ima, struct ImBuf *ibuf);
/* called on frame change or before render */ /* called on frame change or before render */
void BKE_image_user_calc_frame(struct ImageUser *iuser, int cfra, int fieldnr); 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 */ /* fix things in ImageUser when new image gets assigned */
void BKE_image_user_new_image(struct Image *ima, struct ImageUser *iuser); void BKE_image_user_new_image(struct Image *ima, struct ImageUser *iuser);

@ -2260,104 +2260,3 @@ void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
if(iuser->ok==0) iuser->ok= 1; 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;
}

@ -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(char *string, char *dir, char *file);
void BLI_split_dirfile_basic(const 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); 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); void BLI_getlastdir(const char* dir, char *last, int maxlen);
int BLI_testextensie(const char *str, const char *ext); 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); void BLI_uniquename(struct ListBase *list, void *vlink, const char defname[], char delim, short name_offs, short len);

@ -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) static int add_win32_extension(char *name)
{ {
int retval = 0; int retval = 0;

@ -1435,9 +1435,9 @@ public:
BLI_split_dirfile_basic(mfilename, dir, NULL); 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 // make absolute source path
BLI_strncpy(src, image->name, sizeof(src)); BLI_strncpy(src, image->name, sizeof(src));

@ -45,24 +45,6 @@
#include "MEM_guardedalloc.h" #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) static void rna_Image_save(Image *image, bContext *C, ReportList *reports, char *path, Scene *scene)
{ {
ImBuf *ibuf; 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 #else
void RNA_api_image(StructRNA *srna) void RNA_api_image(StructRNA *srna)
@ -110,21 +81,6 @@ void RNA_api_image(StructRNA *srna)
FunctionRNA *func; FunctionRNA *func;
PropertyRNA *parm; 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"); func= RNA_def_function(srna, "save", "rna_Image_save");
RNA_def_function_ui_description(func, "Save image to a specific path."); RNA_def_function_ui_description(func, "Save image to a specific path.");
RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS); RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);