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.
1 Answer
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)
-
$\begingroup$ Exactly what I was looking for! Both Time and distance. Thank you. $\endgroup$– user2850Commented Mar 27, 2014 at 11:31