0
$\begingroup$

I am actually trying to open a blend file from a script and I have to make a copy of the scene so that I can make some modifications in the new scene. However, after I run

bpy.ops.wm.open_mainfile(filepath = 'a saved .blend file path')
bpy.ops.scene.new.(type = 'LINK_COPY')

blender crashes and leaves an error of 'EXCEPTION_ACCESS_VIOLATION'. I believe that the context could not have been set and this problem is showing up but I have not been able to figure out how to get rid of this error. My script is actually long but these two line of code is sufficient to reproduce this problem for anybody.

What would I need to do before trying to make a copy of the scene?

$\endgroup$

1 Answer 1

1
$\begingroup$

The problem can be solved by using a persistent application handler that is executed once the project file has been fully loaded. bpy.app.handler.load_post allows you to append functions that shall be executed once the loading is completed. The annotation @persistent is required in order to keep the application handler registered even though a different project is loaded. This is required since normally application handlers are freed once a new project is loaded.

import bpy
from bpy.app.handlers import persistent


@persistent
def load_post(dummy):
    bpy.ops.scene.new(type="LINK_COPY")
    bpy.app.handlers.load_post.remove(load_post)

bpy.app.handlers.load_post.append(load_post)
bpy.ops.wm.open_mainfile(filepath="/path/to/your/file.blend")
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .