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?

2
  • you're actually cd'ing into a folder called path inside the current directory, is this a typo? Commented Mar 23, 2012 at 23:26
  • @agf Well, I tried with this one, but still it opens two blenders. Commented Mar 23, 2012 at 23:48

2 Answers 2

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"])
Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

Well, the one that you suggested it didn't work...You are right. It opens two different things but both of them empty!
it still opens two different blenders
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
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.
Well I tried it, using the first solution of philip and yours and it doesn't work actually. Am I missing smth here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.