1
$\begingroup$

Let's assume we have a simple scene with a rotating cube. I am looking for a Python way to put additional info in the right bottom corner (like rotation angle for example) on the rendered video that I have as a result. So I need to write the cube rotation angle on each frame or each keyframe.

I was trying to use something like this:

bpy.context.scene.render.stamp_note_text = str(cube_obj.rotation_euler[2])

but this cannot add metadata to each frame.

Here is my simple scene code.

import bpy
from math import radians, degrees, pi, cos



def clear_scene(fps=24):
    for coll in bpy.data.collections:
        if coll:
            obs = [o for o in coll.objects]
            while obs:
                bpy.data.objects.remove(obs.pop())

        bpy.data.collections.remove(coll)

    for obj in bpy.data.objects:
        bpy.data.objects.remove(obj)
        

    for material in bpy.data.materials:
        bpy.data.materials.remove(material, do_unlink=True)

    bpy.context.scene.render.filepath = '/home/vkudak/test_cube'
    bpy.context.scene.render.ffmpeg.format = 'MPEG4'
    bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
    bpy.context.scene.render.ffmpeg.constant_rate_factor = 'LOWEST'
    bpy.context.scene.render.resolution_x = 500
    bpy.context.scene.render.resolution_y = 500
#    640×360   426x240
    bpy.context.scene.render.ffmpeg.format = 'MPEG4'
    bpy.context.scene.render.fps=fps
    
    bpy.context.scene.use_gravity = False

    
    bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = (0, 0, 0, 1)
    




if __name__ == "__main__":
    FPS = 5
    
    clear_scene(fps=FPS)
    
    
    bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', 
                                    location=(0, 0, 0), scale=(1, 1, 1))
    cube_obj = bpy.context.active_object
    cube_obj.name = "Cube"
    
    
    TIME = 60
    times = list(range(0, TIME+1))
 

    bpy.context.scene.frame_end = TIME*FPS

    
    ##### Create CAMERA
    cam_data = bpy.data.cameras.new('camera')
    cam = bpy.data.objects.new('camera', cam_data)
    bpy.context.collection.objects.link(cam)
    # add camera to scene
    scene = bpy.context.scene
    scene.camera=cam
    cam.location=(7, -7, 5)
    cam.data.lens=50


    
    # tracking
    cam_constraint = cam.constraints.new(type='TRACK_TO')
    cam_constraint.target=cube_obj
    
    
    # Add light
    bpy.ops.object.light_add(type='POINT', radius=1, align='WORLD', 
                                            location=(6, 5, 2), scale=(1, 1, 1))
    bpy.context.object.data.energy = 300

    
#   period of self rotation
    rot_period = 10


    frame_num = 0
    for t in times:
                
        bpy.context.scene.frame_set(frame_num)
        
        cube_obj.rotation_euler[2] += radians(360/rot_period) #sat_axis_rot 
        cube_obj.keyframe_insert(data_path="rotation_euler", index=2)
        frame_num += FPS
        

#    # Iterate over all the objects animation function curves
    for fc in cube_obj.animation_data.action.fcurves:
        fc.extrapolation = 'LINEAR' # Set extrapolation type



```
$\endgroup$
3
  • 2
    $\begingroup$ what about adding a simple text, parent that to the camera and then change the text via python? should be easy $\endgroup$
    – Chris
    Jun 12 at 17:20
  • $\begingroup$ @Chris You might add that as an answer, as it seems a reasonable solution. $\endgroup$
    – Phrogz
    Jun 12 at 17:35
  • $\begingroup$ @Chris I have such thoughts, but I change camera focus during video rendering, so this method will require to change also text size. I was searching for another way to do it $\endgroup$
    – Viktor
    Jun 12 at 18:22

0

You must log in to answer this question.

Browse other questions tagged .