Update object at every change ( frame, other objects )

Hi,
I have an object A.
I use a script to build an object B, depending on A’s position in space.
I would like to run my script automatically every time A’s position is changed ( manually or by animation) to update B.

So I understand I need to use a handler like this :

import bpy
from bpy.app.handlers import persistent

@persistent
def load_handler(dummy):
    print("Load Handler:", bpy.data.filepath)

bpy.app.handlers.load_post.append(load_handler)

Source

… except that’s it is not the right handler. depsgraph_update_post may be the one but I am not sure.

For this, I need help …

Another important point : If I add this structure to my script, I will have to run it once and it will run at every change on objects … untill … untill what ? How can it be stopped ?

Thanks !

you’re correct, depsgraph post is what you want.

as for stopping it- if you change scenes it will stop automatically unless you use the @bpy.app.handlers.persistent decorator. Otherwise, to stop it just use depsgraph_update_post.remove(handler_callback), the app handlers are just lists.

Hi Testure, Thanks for your answer.
What exactly do you mean by “Change scenes” ?
… does depsgraph_update_post stop being activated if the scene is changed or … if I load another scene ? (Sorry, a dummy question probably)

yeah, sorry… overloaded terminology. if you close the currently active blend file and load a new one, any active app.handlers will be cleared out unless they are flagged as persistent.

… don’t be sorry, you’re saving my a*µ … only kindness here is overloaded. :smiley:
Well, the following code runs. ( The “Done !” is displayed. )
Then, when I try to select any object, Blender crashes (it quits.)

def update_scene_handler(something):
        do_the_job
bpy.app.handlers.depsgraph_update_post.append(update_scene_handler)
print("Done !!")

Any idea why this crash occurs ?

I tried something lighter : using only frame_change_pre.
Blender calculates successfully the meshes on preview … but crashes on render

I wonder if using keyframes is not a better option … :slight_smile: