Run Python Function When Object is Selected

What’s the best way to run a function when an object is selected?

You might want to check this thread

Thanks, I’ll give this a try.

def obj_selected_callback():
    # Do something here.

subscribe_to = bpy.types.LayerObjects, "active"
bpy.types.Scene.coater_object_selection_updater = object()
bpy.msgbus.subscribe_rna(key=subscribe_to, owner=bpy.types.Scene.coater_object_selection_updater, args=(), notify=obj_selected_callback)

worth pointing out that this will not work with bulk selection methods, such as the marquee selection, lasso, or circle select since none of those methods change the active object and thus will not trigger a msgbus notify. Also probably worth mentioning that LayerObjects has a ‘selected’ property but subscribing to it is pointless since it’s a prop_collection and won’t trigger notifies.