Fix for [#7866] Relative Path to library from command line
authorCampbell Barton <ideasman42@gmail.com>
Fri, 28 Dec 2007 21:16:00 +0000 (21:16 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 28 Dec 2007 21:16:00 +0000 (21:16 +0000)
http://projects.blender.org/tracker/index.php?func=detail&aid=7866&group_id=9&atid=125

where linked relative blend files would not load when the absolute path was not given.
Solved by constructing the absolute path from the command line argument given.

source/creator/creator.c

index 6a780553607734f293d16335577a164e9fa3cfc1..e5abe425468f6e61c7adf087eeae4b3d97449558 100644 (file)
@@ -687,9 +687,50 @@ int main(int argc, char **argv)
                                break;
                        }
                }
-               else {                  
+               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);
+                       
+                       /* 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) {
-                               BKE_read_file(argv[a], NULL);
+                               BKE_read_file(filename, NULL);
                                sound_initialize_sounds();
 
                                /* happens for the UI on file reading too */
@@ -698,8 +739,8 @@ int main(int argc, char **argv)
                        } else {
                                /* we are not running in background mode here, but start blender in UI mode with 
                                   a file - this should do everything a 'load file' does */
-                               BIF_read_file(argv[a]);
-                       }                       
+                               BIF_read_file(filename);
+                       }
                }
        }