0
$\begingroup$

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)                                                                    
        bpy.data.meshes.remove(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} 
                    bpy.ops.view3d.view_all(override)                                                   

    # Deselect all so selection highlights are not passed to .png                                                                                      
    bpy.ops.object.select_all(action='TOGGLE')                                                          


    bpy.ops.render.opengl()                                                                             

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

This script works until I get to the line:

bpy.ops.render.opengl()

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.

| improve this question | | | | |
$\endgroup$
0
$\begingroup$

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.

bpy.ops.render.opengl('INVOKE_DEFAULT')

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.

| improve this answer | | | | |
$\endgroup$
  • $\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$ – learningtofly Feb 1 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$ – Robert Gützkow Feb 1 at 13:55
  • $\begingroup$ Checked! Apologies for the oversight. Thanks again for the answer. $\endgroup$ – learningtofly Feb 3 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.