[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16532] trunk/blender/source: added BLI_convertstringcwd, used so command line blendfiles and python scripts can be relative to the current path .

Campbell Barton ideasman42 at gmail.com
Mon Sep 15 03:33:03 CEST 2008


Revision: 16532
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16532
Author:   campbellbarton
Date:     2008-09-15 03:32:53 +0200 (Mon, 15 Sep 2008)

Log Message:
-----------
added BLI_convertstringcwd, used so command line blendfiles and python scripts can be relative to the current path.
- was alredy doing this for blendfiles, but better to have in its own function.

header_text.c - renamed PATH_MAX, was defined by system includes.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_blenlib.h
    trunk/blender/source/blender/blenlib/intern/util.c
    trunk/blender/source/blender/python/BPY_extern.h
    trunk/blender/source/blender/python/BPY_interface.c
    trunk/blender/source/blender/src/header_text.c
    trunk/blender/source/creator/creator.c

Modified: trunk/blender/source/blender/blenlib/BLI_blenlib.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_blenlib.h	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/blender/blenlib/BLI_blenlib.h	2008-09-15 01:32:53 UTC (rev 16532)
@@ -151,6 +151,7 @@
 	 */
 int BLI_convertstringcode(char *path, const char *basepath);
 int BLI_convertstringframe(char *path, int frame);
+int BLI_convertstringcwd(char *path);
 
 void BLI_makestringcode(const char *relfile, char *file);
 

Modified: trunk/blender/source/blender/blenlib/intern/util.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/util.c	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/blender/blenlib/intern/util.c	2008-09-15 01:32:53 UTC (rev 16532)
@@ -1211,6 +1211,49 @@
 	return wasrelative;
 }
 
+
+/*
+ * Should only be done with command line paths.
+ * this is NOT somthing blenders internal paths support like the // prefix
+ */
+int BLI_convertstringcwd(char *path)
+{
+	int wasrelative = 1;
+	int filelen = strlen(path);
+	
+#ifdef WIN32
+	if (filelen >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
+		wasrelative = 0;
+#else
+	if (filelen >= 2 && path[0] == '/')
+		wasrelative = 0;
+#endif
+	
+	if (wasrelative==1) {
+		char cwd[FILE_MAXDIR + FILE_MAXFILE];
+		BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
+		
+		if (cwd[0] == '\0') {
+			printf( "Could not get the current working directory - $PWD for an unknown reason.");
+		} else {
+			/* uses the blend path relative to cwd important for loading relative linked files.
+			*
+			* cwd should contain c:\ etc on win32 so the relbase can be NULL
+			* relbase being NULL also prevents // being misunderstood as relative to the current
+			* blend file which isnt a feature we want to use in this case since were dealing
+			* with a path from the command line, rather then from inside Blender */
+			
+			char origpath[FILE_MAXDIR + FILE_MAXFILE];
+			BLI_strncpy(origpath, path, FILE_MAXDIR + FILE_MAXFILE);
+			
+			BLI_make_file_string(NULL, path, cwd, origpath); 
+		}
+	}
+	
+	return wasrelative;
+}
+
+
 /* copy di to fi, filename only */
 void BLI_splitdirstring(char *di, char *fi)
 {

Modified: trunk/blender/source/blender/python/BPY_extern.h
===================================================================
--- trunk/blender/source/blender/python/BPY_extern.h	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/blender/python/BPY_extern.h	2008-09-15 01:32:53 UTC (rev 16532)
@@ -94,7 +94,7 @@
 	int BPY_menu_do_python( short menutype, int event );
 	int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short modifiers );
 	int BPY_menu_invoke( struct BPyMenu *pym, short menutype );
-	void BPY_run_python_script( char *filename );
+	void BPY_run_python_script( const char *filename );
 	int BPY_run_script(struct Script *script);
 	void BPY_free_compiled_text( struct Text *text );
 

Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/blender/python/BPY_interface.c	2008-09-15 01:32:53 UTC (rev 16532)
@@ -720,14 +720,24 @@
 * automatically. The script can be a file or a Blender Text in the current 
 * .blend.
 ****************************************************************************/
-void BPY_run_python_script( char *fn )
+void BPY_run_python_script( const char *fn )
 {
+	char filename[FILE_MAXDIR + FILE_MAXFILE];
 	Text *text = NULL;
 	int is_blender_text = 0;
+	
+	BLI_strncpy(filename, fn, FILE_MAXDIR + FILE_MAXFILE);
+	
+	if (!BLI_exists(filename))
+		BLI_convertstringcwd(filename);
+		
+	if (!BLI_exists(filename)) {	/* if there's no such filename ... */
+		/* try an already existing Blender Text.
+		 * use 'fn' rather then filename for this since were looking for
+		 * internal text
+		 */
+		text = G.main->text.first;
 
-	if (!BLI_exists(fn)) {	/* if there's no such filename ... */
-		text = G.main->text.first;	/* try an already existing Blender Text */
-
 		while (text) {
 			if (!strcmp(fn, text->id.name + 2)) break;
 			text = text->id.next;
@@ -741,11 +751,14 @@
 	}
 
 	else {
-		text = add_text(fn);
+		/* use filename here since we know it exists,
+		 * 'fn' may have been a relative path
+		 */
+		text = add_text(filename);
 
 		if (text == NULL) {
 			printf("\nError in BPY_run_python_script:\n"
-				"couldn't create Blender text from %s\n", fn);
+				"couldn't create Blender text from \"%s\"\n", filename);
 		/* Chris: On Windows if I continue I just get a segmentation
 		 * violation.  To get a baseline file I exit here. */
 		exit(2);
@@ -762,13 +775,8 @@
 		/* We can't simply free the text, since the script might have called
 		 * Blender.Load() to load a new .blend, freeing previous data.
 		 * So we check if the pointer is still valid. */
-		Text *txtptr = G.main->text.first;
-		while (txtptr) {
-			if (txtptr == text) {
-				free_libblock(&G.main->text, text);
-				break;
-			}
-			txtptr = txtptr->id.next;
+		if (BLI_findindex(&G.main->text, text) != -1) {
+			free_libblock(&G.main->text, text);
 		}
 	}
 }

Modified: trunk/blender/source/blender/src/header_text.c
===================================================================
--- trunk/blender/source/blender/src/header_text.c	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/blender/src/header_text.c	2008-09-15 01:32:53 UTC (rev 16532)
@@ -862,14 +862,14 @@
 }
 
 /* header */
-#define PATH_MAX	260
+#define HEADER_PATH_MAX	260
 void text_buttons(void)
 {
 	uiBlock *block;
 	SpaceText *st= curarea->spacedata.first;
 	Text *text;
 	short xco, xmax;
-	char naam[256], fname[PATH_MAX], headtxt[PATH_MAX+17];
+	char naam[256], fname[HEADER_PATH_MAX], headtxt[HEADER_PATH_MAX+17];
 	int len;
 	
 	if (st==NULL || st->spacetype != SPACE_TEXT) return;
@@ -961,8 +961,8 @@
 	if (text) {
 		if (text->name) {
 			len = strlen(text->name);
-			if (len > PATH_MAX-1)
-				len = PATH_MAX-1;
+			if (len > HEADER_PATH_MAX-1)
+				len = HEADER_PATH_MAX-1;
 			strncpy(fname, text->name, len);
 			fname[len]='\0';
 			if (text->flags & TXT_ISDIRTY)

Modified: trunk/blender/source/creator/creator.c
===================================================================
--- trunk/blender/source/creator/creator.c	2008-09-15 00:57:11 UTC (rev 16531)
+++ trunk/blender/source/creator/creator.c	2008-09-15 01:32:53 UTC (rev 16532)
@@ -772,45 +772,11 @@
 		else {
 			
 			/* Make the path absolute because its needed for relative linked blends to be found */
-			int abs = 0;
-			int filelen;
-			char cwd[FILE_MAXDIR + FILE_MAXFILE];
 			char filename[FILE_MAXDIR + FILE_MAXFILE];
-			cwd[0] = filename[0] = '\0';
 			
 			BLI_strncpy(filename, argv[a], sizeof(filename));
-			filelen = strlen(filename);
+			BLI_convertstringcwd(filename);
 			
-			/* relative path checks, could do more tests here... */
-#ifdef WIN32
-			/* Account for X:/ and X:\ - should be enough */
-			if (filelen >= 3 && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
-				abs = 1;
-#else
-			if (filelen >= 2 && filename[0] == '/')
-				abs = 1	;
-#endif
-			if (!abs) {
-				BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
-				
-				if (cwd[0] == '\0') {
-					printf(
-					"Could not get the current working directory - $PWD for an unknown reason.\n\t"
-					"Relative linked files will not load if the entire blend path is not used.\n\t"
-					"The 'Play' button may also fail.\n"
-					);
-				} else {
-					/* uses the blend path relative to cwd important for loading relative linked files.
-					*
-					* cwd should contain c:\ etc on win32 so the relbase can be NULL
-					* relbase being NULL also prevents // being misunderstood as relative to the current
-					* blend file which isnt a feature we want to use in this case since were dealing
-					* with a path from the command line, rather then from inside Blender */
-					
-					BLI_make_file_string(NULL, filename, cwd, argv[a]); 
-				}
-			}
-			
 			if (G.background) {
 				int retval = BKE_read_file(filename, NULL);
 				sound_initialize_sounds();





More information about the Bf-blender-cvs mailing list