Opening .blend files using Blender's Python API

喜你入骨 提交于 2021-02-07 05:47:19

问题


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)

回答1:


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 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.

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)

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.

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.



来源:https://stackoverflow.com/questions/28075599/opening-blend-files-using-blenders-python-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!