2
$\begingroup$

I'd like to receive a msgbus notification when the active scene is switched, or somehow otherwise capture this event and respond to it in my code.

I thought this would be a fairly simple msgbus subscription to bpy.context.scene, but my listener is not receiving anything from msgbus, not even errors. In fact I don't seem to be able to get msgbus to fire for anything in bpy.context. What I was hoping for was something like an active property for bpy.types.BlendDataScenes that I can hook msgbus into, but I've come up blank and the lack of any kind of output from msgbus has left me fumbling in the dark with mittens on.

Has anyone been here before?

$\endgroup$

1 Answer 1

7
$\begingroup$

Subscribe to some event that signals scene change.

As with How to get an event when an object is selected? find the code to change what you wish to notify.

In this case

Changing the scene

>>> C.window
<bpy_struct, Window at 0x7f842eee2f88>

>>> C.window.scene
bpy.data.scenes['Scene']

>>> C.window.scene = D.scenes['Scene.001']
>>> 

so in this case will try subscribing to the "scene" property of the Window class.

_I am not aware of anywhere that changing foo.scenes.active sets the context scene. For example there is no bpy.data.scenes.active.

Search for other msgbus examples for other subscription to try. AFAIK it is still the case that you cannot get notification of the instance object, so it is garnered from context.

import bpy
from mathutils import Vector, Matrix

handle = object()

# Triggers when window's scene is changed
subscribe_to = bpy.types.Window, "scene" # 



def notify_test(context):
    print(context.scene.name)

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=handle,
    args=(bpy.context,),
    notify=notify_test,
)

bpy.msgbus.publish_rna(key=subscribe_to)
$\endgroup$
2
  • $\begingroup$ to the rescue again, thank you! $\endgroup$ Dec 12, 2020 at 20:48
  • 2
    $\begingroup$ @batFINGER im curious.. how did you know this syntax? subscribe_to = bpy.types.Window, "scene" where is this documented? trying to find something to notify me when i select a keyframe in the Graph Editor. $\endgroup$ Jul 2, 2022 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .