Using bpy.msgbus.subscribe_rna

Hello, i developed an addon with the use of bpy.msgbus.subscribe_rna like a handler to call a function in my script whenever a specified property, in this case whenever the render engine changes, changes.
My question is how can i make it persistent like it is used to do with the handlers? Adding @persistent above the function that gets called DOESN’T make it persistent.
Here are the pieces of code:

# some code

# function that gets called whenever the render engine changes
@persistent
def function(*args):
    print("Test")

# other code

def register():

    # registering some classes here

    # definition msgbus to update the function whenever the render engine updates
    subscribe_to = bpy.types.RenderSettings, "engine"
    bpy.types.Scene.something = object()
    bpy.msgbus.subscribe_rna(
        key = subscribe_to,
        owner = bpy.types.Scene.something,
        args = (),
        notify = function
        )

This is what the terminal prints after i:

  • Created a project, enabled my addon and here the msgbus was working
  • Saved the preferences with the addon enabled, saved the blend file and closed Blender
  • Then i reopened the blend file (and the addon was enabled as i saved the preferences before), but now the msgbus is not working anymore
2 Likes

You should use bpy.app.handlers to subscribe on file load.

1 Like

subscribe_to = bpy.types.Object, “location”

not working if I using manipulator or G … works only if change location from ui input text fields (N panel)

some workaround?

Ok, i got it working! Thank you!
Another question, how can i hide these messages i get in the terminal when i enable the addon?
No errors, just messages:

From just a glance, they look like debug prints, which if true, implies they will eventually go away.
An alternative is they are logging messages which means they are controlled by a setting.
tl;dr: I have no practical idea.

Is this going to make into 2.80 API?

@nacioss how did you tdo to make it persistent at tge end ?
with an handler ? because it seem that neither

there’s a set(‘PERSISTENT’) args within the fuction

#SUBSCRIBE RNA
bpy.msgbus.subscribe_rna(data, owner, args, notify, options=set()
.. function:: subscribe_rna(data, owner, args, notify, options=set())
:arg key: Represents the type of data being subscribed to
   Arguments include
   - :class:`bpy.types.Property` instance.
   - :class:`bpy.types.Struct` type.
   - (:class:`bpy.types.Struct`, str) type and property name.
:type key: Muliple
:arg owner: Handle for this subscription (compared by identity).
:type owner: Any type.
:arg options: Change the behavior of the subscriber.
   - ``PERSISTENT`` when set, the subscriber will be kept when remapping ID data.
:type options: set of str.
Returns a new vector int property definition.

also, how to verify if the msgbus is not active yet before subscribing to rna ? and how do clear an active msg bus ?

didn’t figurate how to use bpy.msgbus.clear_by_owner() as a logical args like the rna bpy.type didn’t work out

for example :
bpy.msgbus.clear_by_owner(bpy.types.ParticleSettings)

there’s so little information about msgbus. it’s quite useful