How to use bpy.msgbus.clear_by_owner()?

Hello

quick question here:
when i made my msg_bus active listening for type.ParticleSettings, like below:


def msgbus_fct():
    print('MSGBUS: detected user interaction: "types.ParticleSettings"')

def msgbus_sub(owner):                                                                #args example: bpy.types.ParticleSettings
    bpy.msgbus.subscribe_rna(key=owner, owner=object(), args=(), notify=msgbus_fct,)  #subscribe
    bpy.msgbus.publish_rna(key=owner)                                                 #publish

print('Launching MSGBUS on "types.ParticleSettings"')
scatter_msgbus_sub(bpy.types.ParticleSettings)

it seem that

bpy.msgbus.clear_by_owner(bpy.types.ParticleSettings)

won’t remove the msgbus

what am i doing wrong here ?

clear_by_owner() works like this:

When you subscribe to rna events, an owner is required. The owner can be anything, but is a handle, or a receipt you use to reference a particular subscription.

When you call clear_by_owner and pass it the owner, any subscriptions made to this owner will be dropped.

In your example, you have owner=object():
You have no way of retrieving the owner because isn’t bound.
It’s like doing [].append(something), it doesn’t make sense.

Instead, do:

owner = object()
bpy.msgbus.subscribe_rna(key=owner, owner=owner ...
return owner

owner, the object you created with object(), is now a handle you need to store somewhere, so you can clear any subscriptions tied to it:

bpy.msgbus.clear_by_owner(owner)

Even better: You don’t have to create an object to pass as an owner. You can just use one of your python classes as owner.

1 Like