5
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ There's an example of changing viewport shading in: Is it possible to change viewport draw mode when navigating?. $\endgroup$ – Aldrik Aug 26 '13 at 23:18
  • 1
    $\begingroup$ @gandalf3: The two lines below commented conditional must be indented (within first if area.type's block). Their current state made them executed for every area type, not what you intended. $\endgroup$ – Adhi Aug 27 '13 at 1:52
  • $\begingroup$ @Adhi ah, thanks. I didn't realize that VIEW_3D is SpaceView3D.. it works now :) $\endgroup$ – gandalf3 Aug 27 '13 at 2:29
5
$\begingroup$

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'
$\endgroup$
  • 1
    $\begingroup$ I'd use spaces.active just to make sure, but it'd be good too if first item in any collection is always the active one. $\endgroup$ – Adhi Aug 26 '13 at 23:37
  • $\begingroup$ @Adhi Docs say it is so. ;) $\endgroup$ – Aldrik Aug 27 '13 at 0:00
  • $\begingroup$ @Aldrik though this may be the implementation it's still (IMO) semantically better to use spaces.active because that's what you logically care about - the internal representation isn't really important. It also makes it more clear what you're doing (e.g., any reader of the code would be momentarily distracted exactly as was Adhi) and thus more maintainable. $\endgroup$ – wchargin Oct 24 '13 at 4:15

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.