2
$\begingroup$

I wanna run a function each frame. So I create a new script and type in the following:

import bpy

def my_handler(scene):
    print("Frame Change", scene.frame_current)

print("test")
bpy.app.handlers.frame_change_pre.append(my_handler)

The problem is, that it simply doesn't work. When I hit "Run Script", "test" is written to the console, but nothing else happens then.

$\endgroup$
3
  • 2
    $\begingroup$ The hander "fires" when you change frames. What happens when you scrub frames or animate? $\endgroup$
    – batFINGER
    Mar 27, 2018 at 15:21
  • $\begingroup$ Oh, I thought it fires each frame of blender... can I do that somehow? $\endgroup$ Mar 27, 2018 at 15:53
  • 2
    $\begingroup$ I found it: bpy.app.handlers.scene_update_post.append(my_handler) $\endgroup$ Mar 27, 2018 at 16:13

1 Answer 1

2
$\begingroup$

From link below: "By default handlers are freed when loading new files. In some cases you may want the handler to stay running across multiple files (when the handler is part of an add-on for example)."
You may need to use the persistent decorator:

import bpy
from bpy.app.handlers import persistent

@persistent
def my_handler(scene):
    print("Frame Change", scene.frame_current)

print("test")
bpy.app.handlers.frame_change_pre.append(my_handler)

see https://docs.blender.org/api/blender_python_api_2_60_6/bpy.app.handlers.html

$\endgroup$

You must log in to answer this question.

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