Absolute Paths in Blender

Hello,

I am trying to make some absolute paths work in blender 2.57, but there seems to be a problem or I can’t understand smth.

I have a .blend file that is saved in

path1 = “/Users/user/Desktop/scenario/Blender”

and I want to go read all the files in the directory

path2 = “/Users/user/Desktop/scenario/GUI_HUMAN/EXPORTED_HUMANS”

so I use this path

path = “./…/GUI_HUMAN/EXPORTED_HUMANS”

but this doesn’t seem to work…
I have also tried some other solutions with absolute paths, but they seem to be wrong.

Is there another way to define paths in blender?

Thanks in advance,
Mitsaki

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?

Well, the purpose is

  1. import some .mhx files that are read from the directory
  2. save them in the same directory as .blend files
  3. import these .blend files again
  4. 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.

Hello again,

I have written a small script which opens a blend file (whose paths I want to make relative)


import os
import subprocess

path = os.getcwd()
os.system("cd path/")
subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),"Import_mhx.blend"])

so now I suppose that I can use relative paths right?
But it seems that I can’t…I am really confused…

It looks like you are almost there.


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"

right? Strange thing…

No problem!

I just edited the last post, Really strange what happens. The application is not opened…

Which OS are you using? Is your Blender program really just called “blender” or does it have an extension? I did this under Windows XP with Cygwin.

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.

Hello,

I am using this program

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…

but!!! I tried it with absolute paths

import os
import subprocess

blenderPath = "/Users/user/Desktop/scenario/Blender/blender.app/Contents/MacOS/blender"
path1 = "/Users/user/Desktop/scenario/Blender"

os.chdir(path1)

subprocess.check_call([ blenderPath, "Animation.blend"])

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

That should be it.

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.

import os
import subprocess

blenderPath = "./blender.app/Contents/MacOS/blender"

path1 = "./"

os.chdir(path1)

subprocess.check_call([ blenderPath, "Animation.blend"])

Really thanks for your effort. It was quite more simple than I thought.
Both blender and relative paths inside .blend file work perfectly:)

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