20

Is there a way to change text during an animation? Something like a readout that could show distance traveled. And have it displayed on the screen of the animation.

someonewithpc
  • 12,381
  • 6
  • 55
  • 89
user2850
  • 303
  • 1
  • 2
  • 5

1 Answers1

30

Building off the answer in how to animate string properties, here's a solution. You can insert the following code in the Text Editor and hit Run Script.

Example: display current frame

Assuming your text object is called 'Text', this will have it read the current frame:

import bpy

scene = bpy.context.scene obj = scene.objects['Text']

def recalculate_text(scene): obj.data.body = f'Current frame: {scene.frame_current}'

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

The last line just causes the recalculate_text function to be run each time the frame is changed (more on Application Handlers).

Example: distance travelled by object

To have a text showing the distance travelled by 'Cube':

import bpy

scene = bpy.context.scene obj = scene.objects['Text']

def recalculate_text(scene): x = scene.objects['Cube'].location[0] obj.data.body = f'Distance in x-direction: {x:.1f} meters'

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

where I've used Python's string formatting to make distance only display one decimal place.

Example: time elapsed

import bpy

scene = bpy.context.scene obj = scene.objects['Text']

def recalculate_text(scene): fps = scene.render.fps / scene.render.fps_base # actual framerate seconds_elapsed = scene.frame_current / fps obj.data.body = f'Elapsed time: {seconds_elapsed:.1f} seconds'

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

Demo video

Garrett
  • 6,596
  • 6
  • 47
  • 75