Changing settings of the 3D view with Python I am a beginner with python, and prompted by my previous question, I was trying to set viewport shading to Solid and View Only Render to True with python. (The goal being to get the script in that answer to work entirely from python without any need for interaction with the GUI)

however, simply typing show_only_render= True or viewport_shade= 'SOLID' in the console results in nothing happening.

From what I can understand from a lot of searching, is that the cursor needs to be in the 3D view for that object to be active.

It is suggested here to loop through all the areas to do this.

I tried doing this and setting the view to camera, shading to solid, and view only render to true.

However, what actually happens is that the view is set to camera, and the image is rendered without changing view only render to True.

Modified version of the script originally written by Adhi in my previous question, with changes suggested by Aldrik in the answer below:

import bpy
import sys                      # read argument from sys.argv
from bpy.app.handlers import persistent

@persistent
def do_render_opengl(dummy):
    for area in bpy.context.screen.areas:
      if area.type == 'VIEW_3D':
        area.spaces[0].region_3d.view_perspective = 'CAMERA'
      #if area.type == 'SpaceView3D' :
      area.spaces[0].show_only_render = True
      area.spaces[0].viewport_shade = 'SOLID'
    bpy.data.scenes[0].render.filepath="/tmp/glrender.png"
    bpy.ops.render.opengl(animation=False, write_still=True, view_context=True)
    bpy.ops.wm.quit_blender()

bpy.app.handlers.load_post.append(do_render_opengl)




Update:
I have changed the script with Aldrik's improvements, but when I run it with
blender -P gl_render.py example.blend, It returns: 

File "/home/gandalf3/Blender/Opengl_render.py", line 11, in do_render_opengl
    area.spaces[0].show_only_render = True
AttributeError: 'SpaceInfo' object has no attribute 'show_only_render'


It seems that it's still not in the right context?
show_only_render and viewport_shade are attributes of the SpaceView3D. So for your usage example you'd make the following change:

-      if area.type == 'SpaceView3D' :
-        show_only_render = True
-        viewport_shade = 'SOLID'
+        area.spaces[0].show_only_render = True
+        area.spaces[0].viewport_shade = 'SOLID'