Search code examples
macosexecutablerelative-pathblender-2.50

Opening Blender (a program) from a specific filepath, relative paths, Unix executable


In my previous question, Open a file from a specific program from python, I found out how to use subprocess in order to open a program (Blender) — well, a specific .blend file — from a specific file path with this code.

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"])

With the help of a guy at a forum, I wanted to use relative paths inside the .blend file, so I changed the code in this way (for Windows)

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"])

and for Mac,

import os
import subprocess

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"

# 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"])

Results:

  1. In Windows, it works fine.
  2. On Macs, the result is that the file is opened, but the program seems not to be opened. It is quite strange, I think.

Questions:

  1. Is there any extension that I should place for the blender (UNIX executable file) in order for it to open?
  2. Is there any other way that I can do it in order to open the program correctly, but also be able to use relative paths inside .blend files?

Solution

  • import os
    import subprocess
    
    blenderPath = "./blender.app/Contents/MacOS/blender"
    
    path1 = "./"
    
    os.chdir(path1)
    
    subprocess.check_call([ blenderPath, "Animation.blend"])
    

    Both blender opens perfectly and relative paths inside .blend file work ok:)