0
$\begingroup$

I want when a user is selecting/deselecting an object to do something.

I'm working on an alignment tool (free to use) and I manage to make almost everything work.

If you select several objects and click align x or rotate x, it will align rotation/scale/dimension/move

But I can't figure out how to hook into select/deselect event so that I can automatically align objects based on what is checked.

$\endgroup$

1 Answer 1

0
$\begingroup$

I'm not going to claim this is the correct way; but it will work

import bpy


@bpy.app.handlers.persistent
def _depsgraph_update(scene):
    print(bpy.context.selected_objects)


bpy.app.handlers.depsgraph_update_post.append(_depsgraph_update)
# bpy.app.handlers.depsgraph_update_post.remove(_depsgraph_update)

The other thing you can try; is listening for the active object changing - on the basis that you can't select without changing the active object. Note you won't get a change notification on undo

bpy.msgbus.subscribe_rna(
     key=(bpy.types.LayerObjects, "active"),
     owner=object(),
     args=tuple(),
     notify=callback,
)
$\endgroup$
2
  • $\begingroup$ Thank you, I will go with the first approach $\endgroup$ Aug 29, 2020 at 13:01
  • 1
    $\begingroup$ IMO not good practice to mix context and handlers. The context scene isn't necessarily the one being handled by the handler. Look over the scene objects collection. $\endgroup$
    – batFINGER
    Aug 30, 2020 at 15:08

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.