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.

  • $\begingroup$ What is the bl_context of your addon? It should draw or poll if the scene changes, i think. $\endgroup$
    – lemon
    Dec 20 '20 at 16:33
  • $\begingroup$ blender.stackexchange.com/questions/13569 $\endgroup$ Dec 20 '20 at 16:54
  • 1
    $\begingroup$ This is super hacky and not a direct answer to your question, but I've worked around similar things in the past by registering more panels than I need, and using the poll method to hide the extras. The other thing I've done, as you mentioned, is using a timer. Unfortunately I've never worked with scene handlers so I can't offer anything better. $\endgroup$ Dec 20 '20 at 21:08
  • 1
    $\begingroup$ Quentin, what proposed @ChrisHayes works. Don't you have some scene properties to rely on in order to trigerr a timer? $\endgroup$
    – lemon
    Dec 21 '20 at 11:17
  • 1
    $\begingroup$ @QuentinWalker, you can fire a timer only once from poll, just in order to allow your code to modify the scene as you need. So no recurrent timer calls. $\endgroup$
    – lemon
    Dec 21 '20 at 13:13

Your Answer

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

Browse other questions tagged or ask your own question.