Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening .blend files using Blender's Python API

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)
like image 779
user3684240 Avatar asked Jan 21 '15 19:01

user3684240


1 Answers

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.

like image 186
sambler Avatar answered Oct 10 '22 06:10

sambler