3
$\begingroup$

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?

text

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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()
$\endgroup$
1
  • $\begingroup$ @.....................Thanks! $\endgroup$
    – Rick T
    Commented Jan 31, 2020 at 2:58

You must log in to answer this question.

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