使用Blender的Python API打开.blend文件

使用Blender的Python API打开.blend文件

问题描述:

我正在尝试为Blender 2.73创建一个自动构建系统,该系统读取具有很多路径的XML文件,一个接一个地打开文件,然后呈现它们.

I'm trying to make an automated build system for Blender 2.73 which reads XML files with lots of paths, opens the files one by another and then renders them.

我正在使用以下代码来打开:

I'm using the following code in order to open:

bpy.ops.wm.open_mainfile("file_path")

我的问题是出现以下错误:

My problem is that I get the following error:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "<BLENDER_PATH>/scripts/modules/bpy/ops.py", line 186, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
TypeError: Calling operator "bpy.ops.wm.open_mainfile" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIE)

操作员调用的问题是它不接受位置参数,您需要为每个参数命名-

The issue with your operator call is that it doesn't accept positional arguments, you need to name each argument -

bpy.ops.wm.open_mainfile(filepath="file_path")

Blender一次只允许一个打开的文件,当您打开另一个Blend文件时,现有数据会从ram中清除掉,这通常包括您正在运行的脚本.

Blender only allows one open file at a time, when you open another blend file the existing data is flushed out of ram, this normally includes the script you are running.

如果您查看 bpy.app.handlers ,您可以将处理程序设置为持久性,因为它在加载新的混合文件后将保留在内存中.这样可以让您在打开新的Blend文件后运行代码.

If you have a look at bpy.app.handlers, you can setup a handler to be persistent, in that it will remain in memory after loading a new blend file. This can allow you to run your code after the new blend file is opened.

import bpy
from bpy.app.handlers import persistent

@persistent
def load_handler(dummy):
    print("Load Handler:", bpy.data.filepath)

bpy.app.handlers.load_post.append(load_handler)

您可能还需要考虑在Blender之外进行主要工作,循环浏览每个文件,并告诉Blender

You may also want to consider doing the main work outside of blender, loop through each file and tell blender to open and render each file.

blender --background thefile.blend -a

将基于混合文件中的设置渲染动画.

will render an animation based on settings in the blend file.

要获得更多控制,您还可以指定在打开混合文件后运行的python脚本.这个问题可以为您扩展这个问题.

For more control you can also specify a python script to be run once the blend file is opened. This question can expand on that for you.