0

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

Robert
  • 1,265
  • 1
  • 13
  • 29
  • By using an 'application handler': https://docs.blender.org/api/current/bpy.app.handlers.html#bpy.app.handlers.load_post – brockmann Sep 06 '19 at 19:31

1 Answers1

2

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()
brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 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. – Robert Gützkow Sep 06 '19 at 20:41
  • 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). – brockmann Sep 06 '19 at 21:06
  • No problem :D You're probably right with OP's intention. – Robert Gützkow Sep 06 '19 at 21:12
  • There are states in the add-on that change based on the current scene, but they are saved into the add-on preferences (so they are global). So if the user went to file -> new, and created a new scene, I would need to reset some of these states. It's a strange situation that probably doesn't arise very often (because states related to the scene can be stored inside the scene). – Robert Sep 06 '19 at 22:51
  • While I have you guys cornered, can you help me understand what the @ operator is doing here (with @persistent)? Is that similar to a pre-processor operator in C? Also, I'm currently using classes = (...) register, unregister = bpy.utils.register_classes_factory(classes). Although I barely understand what is going on in it. I'm a little lost with the registering aspects of add-on dev. – Robert Sep 06 '19 at 23:03
  • 1
    @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. – Robert Gützkow Sep 06 '19 at 23:14
  • Sorry, yes, I was referring to the creation of a new file or project. Not sure why I want to keep calling it the scene. As a game asset developer, the scene is just a place to sit my models. I would never need more than one, unless it was to export a different way. I assume this is why I have a hard time separating the two concepts in my head. Thank you for the help! – Robert Sep 06 '19 at 23:39
  • 2
    @Robert the @persistent is a decorator that marks callbacks which are kept even when a new project is loaded (https://docs.blender.org/api/current/bpy.app.handlers.html) – Robert Gützkow Sep 06 '19 at 23:41
  • Many thanks helping out here @rjg! I appreciate it, really. Cheers. – brockmann Sep 07 '19 at 16:34
  • 1
    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 – brockmann Sep 07 '19 at 16:42