3
$\begingroup$

I am new to scripting in Blender and I am having trouble attaching an app handler to keyframe changes during an animation.

Below is my Python code:

import bpy

def my_handler():
    bpy.data.objects["Cube"].location[0] += 0.5

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

I tried to call my_handler, which moves an object when the frame changes, manually or when the animation starts.

I have written the code in text editor. After I run script, nothing really happens. Please help me someone out there I couldn't find anything online.

$\endgroup$
  • $\begingroup$ As an addendum to answer below, for the most part the argument passed is the scene object. (is shown as dummy in load handler examples, where it has value None). For all other handlers the scene passed as argument def handler(scene): cube = scene.objects.get("Cube") will be the object if it is in the scene, None otherwise. Without this the object will move when changing frame on any scene. Also recommend something like cube.location.x = scene.frame_current * 0.5 to stop the object moving collectively further away every frame change. (or reset at a certain frame) $\endgroup$ – batFINGER Nov 18 '18 at 10:41
1
$\begingroup$

As we can see in the documentation at https://docs.blender.org/api/current/bpy.app.handlers.html :

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)

handlers need an argument.

You probably do not need it persistent, but you need the dummy part. This will work:

import bpy

def my_handler(dummy):
    bpy.data.objects["Cube"].location[0] += 0.5

bpy.app.handlers.frame_change_pre.append(my_handler)
$\endgroup$

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.