How to animate the render size? I have a lot of camera background images for reference in an architectural project. I made an animation where, for each frame:


The camera position is defined, corresponding to one of the images
the same image is used as viewport background. Images have been grouped in a video, and the video is used as a camera background instead.


This way I can change my reference image and match the point of view by just changing the current frame.

However some of the images are in portrait mode, some in landscape mode, and the render size ("resolution") has to be switched accordingly, frame by frame.



This works manually, but I'd like to do this automatically.

Example: Selecting frame 30 to set the (animated) camera viewpoint to some actual picture which was shot in portrait mode and inserted in frame 30 of the background video:

  

The 30rd background video frame is stretched because the "resolution" is not set accordingly. Is there a bypass?
I think the only way to do that is to use a python script to adjust and render each frame.

import bpy

res_landscape = [1920,1080]
res_portrait = [1080,1920]

frame_res = {
    1: res_landscape,
    2: res_landscape,
    3: res_portrait,
    4: res_portrait,
    5: res_landscape,
    }

scn = bpy.context.scene

for f,r in frame_res.items():
    scn.frame_set(f)
    scn.render.resolution_x = r[0]
    scn.render.resolution_y = r[1]
    scn.render.filepath = '//renders/image{:03}.jpg'.format(f)
    bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)