Run Function on Scene Created

Basically I want my code to run every time I create a scene. I know that the messagebus system is probably the way to go, but I can’t get it to work. I can detect when the active scene is changed like this:

def callback_scenechange():
  print("changed Scene")

owner = object()
subscribe_to = bpy.types.Window, "scene"

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=owner,
    args=(),
    notify=callback_scenechange,
    options={"PERSISTENT"}
)

But this does not trigger whe I create a NEW scene.
Help would be appreciated :slight_smile:

I did not try this but maybe use

@persistent
def callback_scenechange():
  print("changed Scene")

There are also

bpy.app.handlers.load_post
on loading a new blend file (after)

bpy.app.handlers.load_pre
on loading a new blend file (before)

thanks but this doesn’t trigger when creating a new scene - just when loading a blendfile.

closing this because I decided to go with viewlayers instead (carries the same problems though)

Interesting. In theory, a new scene is just loading the default scene.