I've tried to find a way to do this using handlers and events, etc. Essentially I need to know when an object is selected. This isn't a click event, or another action based on user input, but instead based on user selection.
I guess I could capture all clicks through a modal and use that to check selected objects, but that seems like overkill. Blender knows when something has been selected. I just don't know how to piggy back off of that.
This is the modified script from the examples I could find. While I want selection eventually, for now just wanted to get something that would work. Anyone know why this script won't work?
import bpy ob = bpy.data.objects['Cube'] def cube_location_callback(ob): # Do something here print("Cube has moved to ", ob.location) def subscribe_to_cube_loc_change(ob): if ob.type != 'MESH': return subscribe_to = bpy.context.object.location bpy.msgbus.subscribe_rna( key=subscribe_to, # owner of msgbus subcribe (for clearing later) owner=ob, # Args passed to callback function (tuple) args=(ob,), # Callback function for property update notify=cube_location_callback, ) subscribe_to_cube_loc_change(ob)
Since my original goal was to fire an even upon object selection, I wanted to update with a solution.
subscribe_to = bpy.types.LayerObjects, "active" bpy.msgbus.subscribe_rna( key = subscribe_to, owner = self, args = (self,), notify = wall_selected_callback, )
This taps into the active object selector and will callback whenever the active object changes. This is pretty handy.
It's not what I wanted originally, which was the ability to tie back into the object itself and only fire when that object is selected. If that were available then the object would automatically know what to do when selected. With this solution the script still needs to figure out what has been selected and what the next actions is. There may still be a way to register with the object itself, but I haven't got that far yet.
A lot of people recommend using handlers for this type of usage, but the only option for this is the bpy.app.handlers.depsgraph_update_pre/post command. This can fire off a lot of extra events if all you want is to know when an object has been selected. Plus handlers seem to revolve much more around file and animation operations. Using the above is a more targeted solution.