I have a load_post handler to run some code at startup:
@persistent
def update_prefix(scene):
# some function
print("chapters Loaded")
def register():
bpy.app.handlers.load_post.append(update_prefix)
It works fine with the blender startup file, but when loading a file it runs twice and gives a PyContext error in the console:
chapters Loaded
Read blend: C:\Users\User\Desktop\File.blend
PyContext 'window' not found
PyContext 'window' not found
PyContext 'blend_data' not found
PyContext 'region' not found
PyContext 'area' not found
PyContext 'screen' not found
PyContext 'screen' not found
PyContext 'area' not found
chapters Loaded
How do I get it to only run once or am I missing something?
EDIT:
Even if I just add this to \startup
import bpy
from bpy.app.handlers import persistent
@persistent
def update_prefix(scene):
print("Hello World")
def register():
bpy.app.handlers.load_post.append(update_prefix)
I still get:
Hello World
Read blend: C:\Users\User\Desktop\File.blend
Hello World
PyContext 'window' not found
PyContext 'window' not found
PyContext 'blend_data' not found
PyContext 'region' not found
PyContext 'area' not found
PyContext 'screen' not found
PyContext 'screen' not found
PyContext 'area' not found
This isn't even running a function or operator, it's just printing.
I don't have 2 scenes in the .blend.
if update_prefix in bpy.app.handlers.load_post: return doesn't seem to do anything. Shouldn't the console read:
Read blend: C:\Users\User\Desktop\File.blend
Hello World
if update_prefix in bpy.app.handlers.load_post: return
in yourregister()
function. $\endgroup$ – dr. Sybren Jun 10 '18 at 9:31[f for f in bpy.app.handlers.load_pre if f.__name__.startswith("update_prefix")]
from the handlers ensures no "hangers on". Does "some function" involve context members? (It looks like you are calling an operator from a handler.. do you have two scenes?) When registering: context is restricted and some members are not available. Or in the case of running in headless mode from cli, UI members like screen and area will be None. $\endgroup$ – batFINGER Jun 10 '18 at 9:55