2
$\begingroup$

I know that you can register a python script to be exectued when a blend file is opened (if Autorun is checked in the Preferences). It works very well.

I would like to know if I can register a script to be run at other moments than loading. For instance:

  • run a script periodically
  • run a script before/after saving
$\endgroup$

1 Answer 1

1
$\begingroup$

You can use an 'Application Handler' to register code to run after certain events in blender, like rendering, saving, loading files and several other situations.

For example, in the code below, appending the function to the save_pre handler would print out "Saving File" just before the file is saved:

import bpy

def my_handler(scene):
    print("Saving File")

bpy.app.handlers.save_pre.append(my_handler)

If you wanted code to execute after the file is saved then you would append to save_post.

I am not too sure how you would execute code periodically, though there is a scene_update_pre and scene_update_post which runs whenever the scene needs to be updated that you could look into.

$\endgroup$
1
  • $\begingroup$ Thank you so much! Tried it and it works exactly as I want! $\endgroup$ Commented Sep 16, 2015 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .