3

I would like to do a very simple thing but I am quite lost.

I am using a program called Blender and I want to write a script in python which open a .blend file but using the blender.app which is located in the same folder with the blend file, not with the blender.app which is located in Applications. (using Macosx)

So I was thinking that this should do the job...but instead it opens blender twice...

import os

path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open blender.app Import_mhx.blend")

I also tried this one

import os

path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open Import_mhx.blend")

but unfortunately it opens the .blend file with the default blender.app which is located in Applications...

any idea?

jdi
  • 90,542
  • 19
  • 167
  • 203
Dimitra Micha
  • 2,089
  • 8
  • 27
  • 31

2 Answers2

4

This cannot work since the system command gets executed in a subshell, and the chdir is only valid for that subshell. Replace the command by

os.system("open -a path/blender.app Import_mhx.blend")

or (much better)

subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),
                       "Import_mhx.blend"])
Philipp
  • 48,066
  • 12
  • 84
  • 109
1

Have you tried telling the open command to open it WITH a specific application?

open -a /path/to/blender.app /path/to/Import_mhx.blend

Your first attempt was on the right track but you were really telling open to just open two different things. Not one with the other.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • Well, the one that you suggested it didn't work...You are right. It opens two different things but both of them empty! – Dimitra Micha Mar 23 '12 at 23:50
  • it still opens two different blenders – Dimitra Micha Mar 23 '12 at 23:53
  • That makes no sense. Then why would @Philipp's answer be any different? It has nothing to do with os.system vs subprocess. I feel like you forgot to add the -a flag – jdi Mar 23 '12 at 23:54
  • I am confirming though that your issue was not including the -a flag in your attempt. My answer suggested using -a and it make your file open with that given application path. It works just the same as @Phillips answer, yet he added the subprocess which is the same thing really. – jdi Mar 23 '12 at 23:59
  • Well I tried it, using the first solution of philip and yours and it doesn't work actually. Am I missing smth here? – Dimitra Micha Mar 24 '12 at 00:02
  • You must be, because it works absolutely fine. Maybe something wrong with your paths in your command. I used absolute paths. – jdi Mar 24 '12 at 00:05
  • maybe it's because of the paths. – Dimitra Micha Mar 24 '12 at 00:11