Updating text with frame_change handlers won't work after save & load the blend file

Hi all! I’m trying to animate text. Here’s a quick example of my code:

import bpy

# delete default cube
bpy.ops.object.delete(use_global=False)

# add text
text_curve = bpy.data.curves.new(type="FONT",name="text curve")
text_obj = bpy.data.objects.new("Text", text_curve)
bpy.context.scene.collection.objects.link(text_obj)

def update(self):
    text = bpy.data.objects['Text']
    frame = bpy.context.scene.frame_current
    
    if frame <= 10:
        text.data.body = 'new text'
    elif frame > 10:
        text.data.body = 'newer text'
               
def register():
     bpy.app.handlers.frame_change_post.append(update)

register()

Now this code works fine the first time you run the script. The problem is, if you save, close then reopen the blend file, the update no longer works, and the text stays in the same state when you save.

Is there a way to make the updating behavior persistence on closing & loading the files? I want to use render farms because rendering on my laptop takes too long, so I need the reopened blend file to work properly.

It’s strange that you were able to find bpy.app.handlers.frame_change_post but somehow missed bpy.app.handlers.persistent :slight_smile:

persistent is a decorator, so just use it above your update function like this:

@persistent
def update(self):
   # the rest of your code

Hi, thanks for the reply. I did try the persistent decorator but it didn’t seem to do anything for me. I tried in both 2.82a and the latest 2.83.

hi, you could try registering the script to autoload…
from text editor > text > register, but script should be named *.py
you also need to enable ‘auto run scripts’ from preferences
I don’t know if this is enabled in render farms

1 Like

Thanks so much for asking this question. I have wondered about “text on the fly” but never looked into it.

So, I have a question.

How do you change the font with code?

I tried text.data.font.name = ‘CourierNewPSMT’

Any advice? Thanks

UPDATE (changing font)

found it! It is :

new_font = ‘C:\Windows\Fonts\ARESSENCE.ttf’ #file location of new font

text.data.font = bpy.data.fonts.load(new_font)

So I’ve found a walk around based on @liero suggestion.

First thing I found is that you can use Google Colab and use their GPU to render for free. I used this juypter notebook from this video.

Since juypter notebook gives access to shell command, what you can do is to remove the text in your blend file, put the animate text part in a python script, and use the command line tool to open blend file, execute python script to animate text on the spot, and render the scene.

So take the notebook earlier for example, you can add the code to the “setgpu.py” script (expand the first cell to modify it), and then run the following command to render:

!sudo ./blender/blender -b '/gdrive/My Drive/[BLENDER FILE]' -P setgpu.py -o '/gdrive/My Drive/[OUTPUT]/' -f 1

Note the order of the argument is important, you must first open the blender file (’-b’) THEN run the script (’-P’).