0
$\begingroup$

Very simple question from a very new add-on developer. How would one go about detecting when a scene is started fresh or loaded from file?

For example, if you have some add-on preference variable that is related to the scene in some way, you may want to process that variable when the scene is dramatically changed. I believe this would primarily happen when a new scene is started or when one is loaded from file. Anyone know of a simple way to detect when that happens?

Thanks for any information

$\endgroup$
2
$\begingroup$

You can use application handlers which can be executed before or after specific operations. For your case there is a load_post handler, kicking in after the blend file is loaded and even a load_pre handler kicking in before the file is loading. Simple add-on example using bpy.app.handlers.load_post handler:

bl_info = {
    "name": "Load Handler",
    "author": "Your Name Here",
    "version": (0, 1),
    "blender": (2, 80, 0),
    "category": "Development"
}

import bpy
from bpy.app.handlers import persistent


@persistent
def load_post_handler(dummy):
    print("Event: load_post", bpy.data.filepath)

def register():
    bpy.app.handlers.load_post.append(load_post_handler)

def unregister():    
    bpy.app.handlers.load_post.remove(load_post_handler)

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ This wouldn't be triggered when a new scene is created though. I think that would only work with the depsgraph_update_post handler and a check whether the number of scenes have changed. $\endgroup$ – Robert Gützkow Sep 6 at 20:41
  • $\begingroup$ Right, thanks @rjg! I think the OP wants to load a 'file' rather. We will see, I keep you posted :D Otherwise just add an answer, I would appreciate it (at least). $\endgroup$ – brockmann Sep 6 at 21:06
  • 1
    $\begingroup$ @Robert BTW what you're describing is the creation of a new project. Scenes are a different concept, you can have multiple scenes in a project. $\endgroup$ – Robert Gützkow Sep 6 at 23:14
  • 2
    $\begingroup$ @Robert the @persistent is a decorator that marks callbacks which are kept even when a new project is loaded (docs.blender.org/api/current/bpy.app.handlers.html) $\endgroup$ – Robert Gützkow Sep 6 at 23:41
  • 1
    $\begingroup$ When it comes to addon development, I'd suggest start here: How to create a custom UI? - There you also can find a pretty sweet template to start with, easy... @Robert $\endgroup$ – brockmann Sep 7 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.