3
$\begingroup$

I'm trying to use a method of my class as application handler for when the scene's frame is changed. For that I'm using bpy.app.handlers.frame_change_pre.append(my_handler). My approach is the same as described on the Blender documentation with the difference that my function is a method of my class:

import bpy
class MyClass(object):
    def my_handler(self, scene):
        print("Frame Change", scene.frame_current)
my_object = MyClass()
bpy.app.handlers.frame_change_pre.append(my_object.my_handler)

However, I am getting this error message:

TypeError: my_handler() takes 2 positional arguments but 3 were given

When I do my_object.my_handler(bpy.data.scenes[0]) I correctly get the frame number whenever I change the keyframe.

Where am I going wrong?

Why does the function get 3 arguments passed?

$\endgroup$

1 Answer 1

2
$\begingroup$

Add the depsgraph.

Since 2.8 most handlers take both a scene and a depsgraph argument, the latter is generally a kwarg.

If set up as a simple function this is fine

import bpy
# test clear
bpy.app.handlers.frame_change_pre.clear()

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

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

However if using a method of some instanced class, the way blender is wiring this up under the hood requires the extra argument defined for depsgraph. (Even if it is always None in FCPre)

Code below, same result as code above.

import bpy
# test clear
bpy.app.handlers.frame_change_pre.clear()

class MyClass(object):
    def my_handler(self, scene, depsgraph):
        print("Frame Change", scene.frame_current, depsgraph)

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

You must log in to answer this question.

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