Skip to main content
3 of 3
added 1044 characters in body

How to catch and respond to a scene addition event?

My add-on uses a number of dynamically registered panels, created and configured per scene. It needs to refresh/rebuild itself whenever the scene changes to represent the data in the active scene. I'm using a msgbus subscription upon bpy.types.Window.scene to respond to scene switches by destroying/rebuilding the UI, but this doesn't catch scene creation/deletion events.

In order to do that I've added an app handler for depsgraph_update_post, which fires for scene deletion events but unfortunately is not triggered when a new scene is created. When a new scene is added my UI is left in it's previous, now incorrect state, until the user does something to trigger a depsgraph update and my UI rebuilding code is executed. I've enabled various debug options in the console and monitored output during scene changes to try and find something I can hook into and leverage but I'm coming up empty handed. I'd rather not implement a recurrent timer-based check of the current scene to trigger a UI update, but that's what I'm now faced with.

Does anyone have any ideas or advice for use of msgbus and/or app handlers that would allow me to respond to scene creation events in a timely manner? Thanks in advance for your help, I am constantly amazed by this ingenuity, patience, and generosity of this community. Kudos!

Edited, adding context below:

The number of panels in the sidebar is determined by the user clicking Add/Remove operators in a static master panel. The number and configuration of these dynamic panels in a scene is stored in a property group in the scene. When the active scene changes the add-on destroys/unregisters the UI panels for the previously active scene, and creates/registers new panels according to the panel config of the newly active scene. It's trying to replicate the behaviour of the modifier stack, where selecting a new object destroys/creates modifier sub-panels according to object selection. Except instead of modifiers on objects, I'm doing this with panels per scene. This works nicely when switching back and forth between existing scenes, and it can catch the deletion of an existing scene by using the depsgraph update to trigger a UI refresh, but when a new scene is created it don't have any means of responding to this until the user does something like add/move an object to trigger a depsgraph update.