Ubuntu 18.04 with Blender 2.79

The goal is to load a 3D Model into Blender and create/save a .PNG file.

Here is my script:

def preprocess(file):                                                                                   

    # remove mesh Cube                                                                                  
    if "Cube" in bpy.data.meshes:                                                                       
        mesh = bpy.data.meshes["Cube"]                                                                  
        print("removing mesh", mesh)                                                                    

    # import the 3D object                                                                              
    imported_object = bpy.ops.import_scene.obj(filepath=file)                                           

    # change the view to make certain the entire 3D object is viewable                                  
    for area in bpy.context.screen.areas:                                                               
        if area.type == 'VIEW_3D':                                                                      
            for region in area.regions:                                                                 
                if region.type == 'WINDOW':                                                             
                    override = {'area': area, 'region': region} 

    # Deselect all so selection highlights are not passed to .png                                                                                      


    bpy.data.images['Render Result'].save_render(filepath="scripted.png")                               

This script works until I get to the line:


This line appears to have no effect whatsoever.

When I am run the above command from the console manually by going to the Render -> OpenGL Render Image menu, Blender renders the object as expected and the console view is updated with a 'Render Result' view.

From what I have learned about the API, some commands must be executed within a certain context. It seems like I need to let the script know which viewport is the active viewport, but this is purely speculation on my part.

Any advice on how to render this 3D object via script would be appreciated.


1 Answer 1


The operator works correctly and will render your image. However with the default arguments it won't switch to the UV/Image Editor. If you intend to replicate the behavior of Render > OpenGL Render Image then you will have to change the execution context. In general buttons in the user interface use 'INVOKE_DEFAULT' as execution context.


Additionally you can use the write_still=True argument to save the file. It will write the file to the path specified in bpy.context.scene.render.filepath. The last line in your script could then be removed.

  • $\begingroup$ Thank you for this answer. In addition, I learned the rendered image is based on the Camera view. My object was not in view of the Camera and I kept getting images of just the background color. $\endgroup$ Commented Feb 1, 2020 at 13:52
  • $\begingroup$ @learningtofly The actual final render (Cycles, Eevee, BI) with bpy.ops.render.render() is from the camera's perspective, bpy.ops.render.opengl() should match your view from the 3D viewport. If my answer solved your problem, please consider marking it as accepted by clicking on the checkmark. $\endgroup$ Commented Feb 1, 2020 at 13:55
  • $\begingroup$ Checked! Apologies for the oversight. Thanks again for the answer. $\endgroup$ Commented Feb 3, 2020 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .