Updating text object in Blender 2.81 using python I'm trying to update multiple text objects by testing it with changing the current frame (Please note this will be replaced with different field variables with decimal values in it) but instead of replacing the number it writes over it and creates several font objects.

How can I fix this so it replaces the previous value and doesn't create all those font objects?



import bpy

scene = bpy.context.scene

def recalculate_text(self):
    font_curve = bpy.data.curves.new(type="FONT",name="Font Curve")
    font_curve.body = 'Current Frame: ' + str(scene.frame_current) 
    font_obj = bpy.data.objects.update("Font Object", font_curve)
    bpy.context.scene.collection.objects.link(font_obj)

    font_curve2 = bpy.data.curves.new(type="FONT",name="Font Curve")
    font_curve2.body = '2nd Field value: ' + str(scene.frame_current)
    font_obj2 = bpy.data.objects.update("Font Object", font_curve2)
    bpy.context.scene.collection.objects.link(font_obj2)


def register():
    bpy.app.handlers.frame_change_post.append(recalculate_text)

def unregister():
    bpy.app.handlers.frame_change_post.remove(recalculate_text)

register()


PS: I know I could do this with Animation nodes but I'm trying to get better at scripting.
The function recalculate_text which you're having run every frame, is also setup to create a new object every time. Pull the object creation out of the def function. Check this out.

import bpy

scene = bpy.context.scene

font_curve = bpy.data.curves.new(type="FONT",name="Font Curve")
font_curve.body = 'Current Frame: ' + str(scene.frame_current) 

font_obj = bpy.data.objects.new("Font Object", font_curve)

scene.collection.objects.link(font_obj)

def recalculate_text(scene):
    font_curve.body = 'Current Frame: ' + str(scene.frame_current) 

def register():
    bpy.app.handlers.frame_change_post.append(recalculate_text)

def unregister():
    bpy.app.handlers.frame_change_post.remove(recalculate_text)

register()