15
$\begingroup$

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.

$\endgroup$
18
$\begingroup$

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 = 'Current frame: ' + str(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 = 'Distance in x-direction: {0:.1f} meters'.format(x)

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
    obj.data.body = 'Elapsed time: {0:.1f} seconds'.format(scene.frame_current/fps)

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

Demo video

$\endgroup$
  • $\begingroup$ Exactly what I was looking for! Both Time and distance. Thank you. $\endgroup$ – user2850 Mar 27 '14 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.