3
$\begingroup$

This is probably a no-brainer, but somehow I can't find the solution with google. How do I execute a function once, if an object is clicked?

import bpy

def something(obj)
  print(obj.name)
  return

...
if anyObjClicked:
  something(context.scene.active_object)

Result should be:

Cube

and not

Cube
Cube
Cube
Cube
...
$\endgroup$
2
$\begingroup$

Blender's event system doesn't expose an event that triggers just once when an Object is selected (December 2015) - You aren't missing anything obvious.

You could make an event handler that listens for scene updates and only fires a custom function call once if the active_object changes between calls (these scene updates can be high frequency, 30 times per second).

read: bpy.app.handlers.html see bpy.app.handlers.scene_update_pre (or ..._post)

The docs have sample code which you can extrapolate.

ps. this assumes your use case doesn't include clicking the same object a number of times in sequence.

$\endgroup$
  • $\begingroup$ Thanks for clarifying. The idea of monitoring the active object for a change is the solution for me! I'll figure the rest with some boolean triggers. Thank you! $\endgroup$ – MSchmits Dec 6 '15 at 9:10

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.