Do many OpenGL renders in script - issue

I am currently writing a simple script, the idea is as follows: for every camera currently selected perform following actions:

  • set camera as active (it involves switching to given camera view in the viewport)
  • do OpenGL render from given camera view
  • save render to dir
  • proceed to next camera.

The problem is that the script doesn’t work as expected because OpenGL renderer doesn’t “notice” the viewport view change during execution. I’ve tried adding “bpy.ops.wm.redraw_timer(type=‘DRAW_WIN_SWAP’, iterations=1)” in the middle of the loop in order to update viewport but with no luck. Any ideas?

Here is the code:


import os
import bpy

context = bpy.context

override = None

for area in context.screen.areas:
    if area.type == 'VIEW_3D':
        override = context.copy()
        override['area'] = area
        break
        
assert override is not None

renderNum = 1

savePath = context.scene.render.filepath
renderNames = ''

for obj in context.selected_objects:
    if obj.type != 'CAMERA':
        continue
    
    context.scene.objects.active = obj
    bpy.ops.view3d.object_as_camera(override)
    
    renderName = str(renderNum) + '.png'
    context.scene.render.filepath = os.path.join(savePath, renderName)
    
    bpy.ops.render.opengl(write_still=True)
    renderNum += 1
    
context.scene.render.filepath = savePath

I had the same issue on a similar script. To solve it, I update the viewport changing the frame with the same one and it worked for me.

bpy.context.scene.frame_current = bpy.context.scene.frame_current

Unfortunately this tip didn’t work in my case. I finally figured it out using timer with modal operator. I created simple add-on over this solution. You can read about it here.