[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22173] branches/soc-2009-kazanbas/source: - re-wrote image exporting function renaming it from BKE_export_image to BKE_get_image_export_path because now it doesn 't copy

Arystanbek Dyussenov arystan.d at gmail.com
Mon Aug 3 14:02:40 CEST 2009


Revision: 22173
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22173
Author:   kazanbas
Date:     2009-08-03 14:02:40 +0200 (Mon, 03 Aug 2009)

Log Message:
-----------
- re-wrote image exporting function renaming it from BKE_export_image to BKE_get_image_export_path because now it doesn't copy 
files but only manipulates paths. It produces both ablsolute and relative paths. COLLADA exporter can now use it.
- re-wrote unit test for it, this is now more compact and readable
- RNA API Image.get_export_path takes a boolean arg to indicate whether a relative or absolute path should be returned. Python scripts 
can now use it.

Modified Paths:
--------------
    branches/soc-2009-kazanbas/source/blender/blenkernel/BKE_image.h
    branches/soc-2009-kazanbas/source/blender/blenkernel/intern/image.c
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_image_api.c
    branches/soc-2009-kazanbas/source/creator/tests/alltest.c

Modified: branches/soc-2009-kazanbas/source/blender/blenkernel/BKE_image.h
===================================================================
--- branches/soc-2009-kazanbas/source/blender/blenkernel/BKE_image.h	2009-08-03 11:12:17 UTC (rev 22172)
+++ branches/soc-2009-kazanbas/source/blender/blenkernel/BKE_image.h	2009-08-03 12:02:40 UTC (rev 22173)
@@ -156,9 +156,8 @@
 void BKE_image_merge(struct Image *dest, struct Image *source);
 
 /* copy image file to a directory rebuilding subdirectory structure */
-int BKE_export_image(struct Image *im, const char *dest_dir, char *out_path, int out_path_len);
+int BKE_get_image_export_path(struct Image *im, const char *dest_dir, char *abs, int abs_size, char *rel, int rel_size);
 
-
 #ifdef __cplusplus
 }
 #endif

Modified: branches/soc-2009-kazanbas/source/blender/blenkernel/intern/image.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/blenkernel/intern/image.c	2009-08-03 11:12:17 UTC (rev 22172)
+++ branches/soc-2009-kazanbas/source/blender/blenkernel/intern/image.c	2009-08-03 12:02:40 UTC (rev 22173)
@@ -2117,41 +2117,51 @@
 }
 
 /*
-  Copy an image to destination directory rebuilding subdirectory structure if needed.
-  Target image path is written to out_path.
-  Returns 1 on success, 0 otherwise.
+  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
+  - 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.
+  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.
+  - 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_export_image(Image *im, const char *dest_dir, char *out_path, int out_path_len)
+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;
 
-	out_path[0]= 0;
+	if (abs)
+		abs[0]= 0;
 
+	if (rel)
+		rel[0]= 0;
+
 	BLI_split_dirfile_basic(G.sce, blend_dir, NULL);
 
-	if (!strcmp(im->name, "") || im->type != IMA_TYPE_IMAGE) {
-		if (G.f & G_DEBUG) printf("invalid image type\n");
+	if (!strlen(im->name)) {
+		if (G.f & G_DEBUG) printf("Invalid image type.\n");
 		return 0;
 	}
 
@@ -2160,61 +2170,48 @@
 	/* expand "//" in filename and get absolute path */
 	BLI_convertstringcode(path, G.sce);
 
-	/* proceed only if image file exists */
-	if (!BLI_exists(path)) {
-		if (G.f & G_DEBUG) printf("%s doesn't exist\n", path);
-		return 0;
-	}
-
 	/* 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)) {
-			/* copy to dest_dir */
 			BLI_join_dirfile(dest_path, dest_dir, base);
 		}
 		/* "below" */
 		else {
-			char rel[FILE_MAX];
-
 			/* rel = image_path_dir - blend_dir */
-			BLI_strncpy(rel, dir + len, sizeof(rel));
-				
-			BLI_join_dirfile(dest_path, dest_dir, rel);
+			BLI_strncpy(rel_dir, dir + len, sizeof(rel_dir));
 
-			/* build identical directory structure under dest_dir */
-			BLI_recurdir_fileops(dest_path);
-
+			BLI_join_dirfile(dest_path, dest_dir, rel_dir);
 			BLI_join_dirfile(dest_path, dest_path, base);
 		}
 			
 	}
 	/* image is out of current directory */
 	else {
-		/* copy to dest_dir */
 		BLI_join_dirfile(dest_path, dest_dir, base);
 	}
 
-	if (G.f & G_DEBUG) printf("copying %s to %s\n", path, dest_path);
-
-	/* only copy if paths differ */
-	if (strcmp(path, dest_path)) {
-		if (BLI_copy_fileops(path, dest_path) != 0) {
-			if (G.f & G_DEBUG) printf("couldn't copy %s to %s\n", path, dest_path);
-			return 0;
-		}
+	/* only return 1 if paths differ */
+	if (!strcmp(path, dest_path)) {
+		if (G.f & G_DEBUG) printf("%s and %s are the same file\n", path, dest_path);
+		return 0;
 	}
-	else if (G.f & G_DEBUG){
-		printf("%s and %s are the same file\n", path, dest_path);
-	}
 
-	BLI_strncpy(out_path, dest_path, out_path_len);
+	if (abs)
+		BLI_strncpy(abs, dest_path, abs_size);
 
+	if (rel) {
+		strncat(rel, rel_dir, rel_size);
+		strncat(rel, base, rel_size);
+	}
+
 	return 1;
 }

Modified: branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_image_api.c
===================================================================
--- branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_image_api.c	2009-08-03 11:12:17 UTC (rev 22172)
+++ branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_image_api.c	2009-08-03 12:02:40 UTC (rev 22173)
@@ -41,12 +41,12 @@
 #include "BKE_utildefines.h"
 #include "BKE_image.h"
 
-static char *rna_Image_export(Image *image, char *dest_dir)
+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_export_image(image, dest_dir, path, length)) {
+	if (!BKE_get_image_export_path(image, dest_dir, rel ? NULL : path, length, rel ? path : NULL, length )) {
 		MEM_freeN(path);
 		return NULL;
 	}
@@ -61,11 +61,13 @@
 	FunctionRNA *func;
 	PropertyRNA *parm;
 
-	func= RNA_def_function(srna, "export", "rna_Image_export");
-	RNA_def_function_ui_description(func, "Copy image file to a directory rebuilding subdirectory structure.");
+	func= RNA_def_function(srna, "export", "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_string(func, "path", "", 0, "", "Absolute file path of copied image.");
+	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);
 }
 

Modified: branches/soc-2009-kazanbas/source/creator/tests/alltest.c
===================================================================
--- branches/soc-2009-kazanbas/source/creator/tests/alltest.c	2009-08-03 11:12:17 UTC (rev 22172)
+++ branches/soc-2009-kazanbas/source/creator/tests/alltest.c	2009-08-03 12:02:40 UTC (rev 22173)
@@ -19,48 +19,26 @@
 char bprogname[FILE_MAXDIR+FILE_MAXFILE];
 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
 
+typedef struct ImageTestResult {
+	char *path;
+	char *rel;
+	int ret;
+} ImageTestResult;
+
 typedef struct ImageTestData {
 	char *path; /* image filename */
-	char *expect_path; /* file path that we expect */
-	int type; /* image type */
-	int ret;  /* expected function return value */
-	int create_file; /* whether the file should be created */
+	ImageTestResult result[10];
 } ImageTestData;
 
-/* recursively deletes a directory only if it is under /tmp */
-static void delete_only_tmp(char *path, int dir) {
-#ifdef WIN32
-#else
-	if (!strncmp(path, "/tmp/", 5) && BLI_exists(path)) {
-		BLI_delete(path, dir, 1);
-	}
-#endif
-}
-
-static void touch_only_tmp(char *path) {
-#ifdef WIN32
-#else
-	if (!strncmp(path, "/tmp/", 5)) {
-		BLI_touch(path);
-	}
-#endif
-}
-
 /* check that BKE_copy_images manipulates paths correctly */
 START_TEST(test_copy_images)
 {
 	char **dir;
 	ImageTestData *test;
+	int i,j;
 
-	/* XXX Windows not tested */	
 #ifdef WIN32
-	static ImageTestData test_data[] = {
-		{"//bar/image.png", "C:\\Temp\\bar\\image.png"},
-		/* TODO add more */
-		{NULL, NULL},
-	};
-	
-	BLI_strncpy(G.sce, "C:\\Temp\untitled.blend", sizeof(G.sce));
+	/* TBD... */
 #else
 	/*
 	  XXX are these paths possible in image->name?:
@@ -70,77 +48,100 @@
 
 	  if so, BKE_copy_images currently doesn't support them!
 	 */
+
+	const char *blend_dir = "/home/user/foo";
+	char *dest_dir[] = {"/home/user/", "/home/user", "/home/user/export/", "/home/user/foo/", NULL};
+
 	static ImageTestData test_data[] = {
-		{"//bar/image.png", "/tmp/blender/dest/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"//image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"//textures/test/foo/bar/image.png", "/tmp/blender/dest/textures/test/foo/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"//textures/test/foo/bar/image.png", "", IMA_TYPE_MULTILAYER, 0, 1},
-		{"//./foo/bar/image.png", "/tmp/blender/dest/foo/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"//../foo/bar/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"/tmp/blender/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
-		/* expecting it to return 1 when src and dest are the same file */
-		{"/tmp/blender/foo/bar/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
-		{"/tmp/blender/dest/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
-		/* expecting empty path and 0 return value for non-existing files */
-		{"/tmp/blender/src/file-not-created", "", IMA_TYPE_IMAGE, 0, 0},
-		{"", "", IMA_TYPE_IMAGE, 0, 0},
-		{NULL, NULL},
-	};
 
-	char *dest_dir[] = {"/tmp/blender/dest/", "/tmp/blender/dest", NULL};
-	const char *blend_dir = "/tmp/blender/src";
-	
-	/* substitute G.sce */
-	BLI_snprintf(G.sce, sizeof(G.sce), "%s/untitled.blend", blend_dir);
-#endif
+		/* image path | [expected output path | corresponding relative path | expected return value] */
 
-	/* only delete files/directories under /tmp/ ! */
-	delete_only_tmp(blend_dir, 1);
+		/* relative, 0 level deep */
+		{"//image.png", {{"/home/user/image.png", "image.png", 1},
+						 {"/home/user/image.png", "image.png", 1},

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list