Relative paths are going to be based on the directory that Blender started from, not from the directory where you opened your .blend file. Getting this to work will depends on the architecture of your script and what data you are loading from the “GUI_HUMAN” directory. Are those just data files that Python is reading, or are they .blend files that you want to load?
import some .mhx files that are read from the directory
save them in the same directory as .blend files
import these .blend files again
import some .obj files
and so on.
The thing is that I want to place the blender.app to the same folder, so that my total script is executed there…
I should also methion that my operation system is macos.
Most GUIs let you specify a “start-in directory” or “work directory”. I know that Windows and Linux do. If you set this on your Blender icon then it should use that directory as the base. You would set this directory to be your ‘path1’ directory and then you should be able to reference path2 the way you want.
Another option is to launch blender from the command line.
>cd <path1>
><full path to blender>/blender
That should do a similar trick and the base directory should then be whatever ‘path1’ was.
import os
import subprocess
# This should be the full path to your Blender executable.
blenderPath = "/cygdrive/c/Program Files/Blender Foundation/blender-2.62-release-windows32/blender.exe"
# This is the directory that you want to be your "current" directory when Blender starts
path1 = "/Users/user/Desktop/scenario/Blender"
# This makes makes it so your script is currently based at "path1"
os.chdir(path1)
subprocess.check_call([blenderPath, "Import_mhx.blend"])
When run, this will make the “current directory” for Blender be whatever is in “path1”. The “Import_mhx.blend” file should be in the “path1” directory. When that file is open, scripts should be able to refer to “…/GUI_HUMAN/EXPORTED_HUMANS”. (Note, you don’t need the “./” in the relative path.) You can run this script from any directory on your computer and it should still work the same.
yeah, you are right:)) Really thanks for your time:)
There is another small problem…
Blender seems to not be open…Although the .blend file seems to be opened, I don’t see blender.app open…
In the blenderPath I write the path of the blender executable (under mac)
path = os.getcwd()
os.system("cd path/")
print (path)
# This should be the full path to your Blender executable.
blenderPath = path + "/blender.app/Contents/macos/blender"
I am using MacOSX (lion). I figured that in windows it works…blender is the unix executable file and has no extension.
I use the same to run blender through terminal meaning
./blender -P script.py
but it is not what I want to do now, because I want to open a .blend file and use relative paths. Any idea?
Weird, I don’t know what the issue is then. I assume that MacOS would work basically the same as Linux and this would work under Linux. Without knowing more about how your system is setup there is not much more that I can help with.
So, is there smth I could tell you in order to give me some advice? Is there smth I could look for, except of course than unix executable files in mac?
I still suspect that you are not launching Blender from the correct directory or you are not changing your directory correctly from your script.
import os
import subprocess
# This should be the full path to your Blender executable.
blenderPath = "/cygdrive/c/Program Files/Blender Foundation/blender-2.62-release-windows32/blender.exe"
# This is the directory that you want to be your "current" directory when Blender starts
path1 = "/Users/user/Desktop/scenario/Blender"
# This makes makes it so your script is currently based at "path1"
os.chdir(path1)
subprocess.check_call([blenderPath, "Import_mhx.blend"])
Are you using this Python program? What makes this work correctly is using “os.chdir” instead of “os.system(“cd <directory>”)” as well as launching Blender by calling the executable directly with "subprocess.check_call([<blender exe>, “Import_mhx.blend”]) instead of relying on the shell by calling subprocess with an “open” command.
import os
import subprocess
path = os.getcwd()
print (path)
blenderPath = path + "/blender.app/Contents/macos/blender"
path1 = path
# This makes makes it so your script is currently based at "path1"
os.chdir(path1)
subprocess.check_call([ blenderPath, "Import_mhx.blend"])
Although, I tried it also with absolute path of blender executable…and it still is the same…
and it works…magically…
Isn’t there a way to make it with relevant paths…even this script? The problem is probably with this
path = os.getcwd() command? I want to make it an application that works with the whole program…
You need to go back and use my specific program. The changes that you made are causing you problems. Specifically:
path = path1 = os.getcwd()
This means that your call to:
os.chdir(path1) has no effect since it changes you to the same directory that you originally started from.
Another problem is that you are generating the path to your Blender executable based on appending some directories onto the ‘os.getcwd()’. This implies that this program will only run from 1 directory correctly. The only way it would work is if you have Blender installed a sub-directories of where you are storing your art assets. I guess that could work but it is not where I would install it.
Are you running the Python script from the command line or from an icon? I recommend running it from a command line as that will let you more easily control which directory you start from.
You say you have a file:
/Users/user/Desktop/scenario/Blender/Import_mhx.blend
And you want to reference:
/Users/user/Desktop/scenario/GUI_HUMAN/EXPORTED_HUMANS
Open a command prompt:
cd /Users/user/Desktop/scenario/Blender
python <scriptname>.py
I am running Python script from the IDLE, because finally I would like to use it in a bigger program…and I would like to be used in every platform and in every computer, that’s why I try to use relative paths.
If I do this,
Open a command prompt:
cd /Users/user/Desktop/scenario/Blender
python <scriptname>.py
do I have to still keep the absolute paths in my script??
How can I change them in relative ones?
There are some paths that make sense to keep absolute and some paths that make sense to be relative. Using an absolute path to launch Blender is probably not a painful restriction on the script. The advantage it brings is that you can run the script from any directory and it can still start Blender. Alternatively, you could add your Blender directory to your standard $PATH environment variable (I assume that MacOS has this given its BSD roots) and find your executable that way.
The absolute path to Blender has nothing to do with the relative paths that you want to manipulate and it the root of your problem. You keep trying to launch blender from a specific directory and this messes up your relative path when Blender is actually running.
You should probably keep your absolute path to Blender but all you have to do is ‘os.chdir’ to where you want your relative paths to be based. These are two totally separate issues.
if instead i want upload all image file from a directory and assign it to a each texture slot?? i’ve tried but it said that the acces to file are denied