2
$\begingroup$

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.

EDIT

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)

FOLLOW UP

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.

$\endgroup$
  • $\begingroup$ devtalk.blender.org/t/… $\endgroup$ – Blender Dadaist Sep 5 at 9:17
  • $\begingroup$ Interesting... not heard of the msgbus but seems like the right area to catch events. Also looks like a bit of dark magic involved and not sure if the examples out there still work for 2.8. I edited my post with the code I tried but can't get to work yet. $\endgroup$ – Sam Vimes Sep 5 at 17:02
3
$\begingroup$

You have to pass "location" (string) to path_resolve():

import bpy

# Callback function for location changes
def obj_location_callback(ob):
    # Do something here
    print('Object "{}" changed its location to: {}: '.format(
        ob.name, ob.location)
        )


# Subscribe to the context object (mesh)
def subscribe_to_obj_loc(ob):
    if ob.type != 'MESH':
        return

    subscribe_to = ob.path_resolve("location", False)

    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=obj_location_callback,
    )

# Ensure only meshes are passed to this function
subscribe_to_obj_loc(bpy.context.object)
$\endgroup$
  • $\begingroup$ Hi. This is very interesting (did not know that 'msgbus'). That means that all that have rna path can benefit for an update callback, that's it? Is there some documentation somewhere or described roadmap about it? Thanks. And additionally 'owner' is the object? I mean clear will clear all callbacks? $\endgroup$ – lemon Sep 5 at 17:48
  • 1
    $\begingroup$ New to me too @lemon :P ... Yep looks like that. Still on it and try to figure out how to get rid of it by using bpy.msgbus.clear_by_owner(ob), also I don't get why it doesn't work when grabbing the object... $\endgroup$ – brockmann Sep 5 at 18:17
  • 1
    $\begingroup$ Found a more complete example using a 'handle': developer.blender.org/P563 @lemon $\endgroup$ – brockmann Sep 5 at 20:11
  • $\begingroup$ I've updated script the reflect the changes, which appears to be the correct answer. However I can't get my script to report any feedback. I assume just by moving the object it should callback the location? $\endgroup$ – Sam Vimes Sep 6 at 17:51
  • $\begingroup$ To make it work, run it, bring up the N-Panel and drag any location slider... What do you think is wrong (not correct) on my answer? The only difference I see is that you are not using ob.path_resolve(loc... but this returns the python path of the property (loc) so it's even safer for general usage (other properties) at the moment. @SamVimes $\endgroup$ – brockmann Sep 6 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.