I'm calling Blender from the command line with blender --python script.py
, and in that script I'm rendering the scene and saving as an image. I tried this:
import bpy
bpy.ops.render.render()
bpy.ops.image.save_as(filepath="render.png")
but this produces RuntimeError: Operator bpy.ops.image.save_as.poll() failed, context is incorrect
. I believe the correct way to address this is to override the context as shown in an answer to a previous question of mine. Something like this:
import bpy
for area in bpy.context.screen.areas:
if area.type == "IMAGE_EDITOR":
image_editor = area
break
else:
raise RuntimeError("IMAGE_EDITOR area not found")
with bpy.context.temp_override(area=image_editor):
bpy.ops.render.render()
bpy.ops.image.save_as(filepath="render.png")
But the image editor is not in bpy.context.screen.areas
, so the RuntimeError is triggered. Any ideas on how to do this properly?