0

在我之前的问题Open a file from a specific program from python中,我了解了如何使用 subprocess 以使用.blend此代码从特定文件路径打开程序(Blender)——好吧,一个特定文件。

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

论坛上一个人的帮助下,我想在.blend文件中使用相对路径,所以我以这种方式更改了代码(对于 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"])

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

结果:

  1. 在 Windows 中,它工作正常。
  2. 在 Mac 上,结果是文件打开了,但程序似乎没有打开。我认为这很奇怪。

问题:

  1. 为了打开它,我应该为搅拌机(UNIX 可执行文件)放置任何扩展名吗?
  2. 有没有其他方法可以让我正确打开程序,但也可以在.blend文件中使用相对路径?
4

1 回答 1

0
import os
import subprocess

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

path1 = "./"

os.chdir(path1)

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

两个搅拌机都可以完美打开,并且 .blend 文件中的相对路径可以正常工作:)

于 2012-03-30T09:52:15.690 回答