Running script on every frame of Animation

I am making a procedural material. I wanted to animate the scale value. I wanted to animate in such a way that the scale value changes to the frame number every time the frame number is a multiple of 10, so I scripted the following:

import bpy

frame_num = bpy.context.scene.frame_current
if frame_num % 10 == 0:
    bpy.data.materials["noise"].node_tree.nodes["White Noise Texture"].inputs[1].default_value = frame_num

When I Run the script it works as intended. But when I do not know how to link this script to the Animation. How do I run the script for each frame?

yep, just use an app.handler for frame change. there’s one for before the frame changes and one for after it changes.
https://docs.blender.org/api/current/bpy.app.handlers.html#bpy.app.handlers.frame_change_post

It’s also possible to create custom python scripts for drivers:

1 Like

I updated the script to the following:

import bpy

def trans_texture():
    frame_num = bpy.context.scene.frame_current
    if frame_num % 10 == 0:
        bpy.data.materials["noise"].node_tree.nodes["White Noise Texture"].inputs[1].default_value = frame_num

bpy.app.handlers.frame_change_post.append(trans_texture)

But it does not read the script after every frame. What am I doing wrong here?

are you not getting an exception? app handlers require one positional argument (scene) and according to your code you haven’t supplied it.

take a look at the example on that same page I linked

1 Like