11
$\begingroup$

I am trying to change the viewport shading to "RENDERED" with python, but I can't seem to figure out how.

It seems like bpy.types.SpaceView3D.viewport_shade = "RENDERED" should do it, but this doesn't work and doesn't even give a context error.

$\endgroup$
2

3 Answers 3

16
$\begingroup$

You have to specify which 3D view you want to access.

For example, this will iterate through all the areas in the current screen and set any 3D views to rendered shading:

import bpy

for area in bpy.context.screen.areas: # iterate through areas in current screen
    if area.type == 'VIEW_3D':
        for space in area.spaces: # iterate through spaces in current VIEW_3D area
            if space.type == 'VIEW_3D': # check if space is a 3D view
                space.viewport_shade = 'RENDERED' # set the viewport shading to rendered

Update

Alternatively, you could also do the following:

import bpy

area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
space = next(space for space in area.spaces if space.type == 'VIEW_3D')
space.viewport_shade = 'RENDERED'  # set the viewport shading

Instead of looping through the whole bpy.context.screen.areas, etc, the next() method returns area as soon as it finds area.type == 'VIEW_3D'. This doesn't really affect performance for this example but it's a better way of achieving the same result. Plus, it looks neater!

$\endgroup$
4
  • $\begingroup$ thanks! I guessed it was space compliant, because you can't activate it from anywhere but there. I just couldn't find a list of spaces. Thanks alot! $\endgroup$ Oct 13, 2014 at 3:18
  • $\begingroup$ is there a way to do this in the game engine? say when you start the game it will always go into the specified shading instead of what ever shading you had when editing the game. $\endgroup$
    – kanuki
    Dec 7, 2018 at 9:57
  • $\begingroup$ I'm getting 'SpaceView3D' object has no attribute 'viewport_shade' in 2.80. There is a shading attribute, but it is read-only. $\endgroup$ Dec 19, 2019 at 21:02
  • 2
    $\begingroup$ blender.stackexchange.com/a/124427/68286 works - uses space.shading.type $\endgroup$ Dec 19, 2019 at 21:10
3
$\begingroup$

Updated answer for 2.8+ and multi-window

import bpy


for window in bpy.context.window_manager.windows:
    for area in window.screen.areas: # iterate through areas in current screen
        if area.type == 'VIEW_3D':
            for space in area.spaces: # iterate through spaces in current VIEW_3D area
                if space.type == 'VIEW_3D': # check if space is a 3D view
                    space.shading.type = 'SOLID'

$\endgroup$
1
$\begingroup$

context did not work for me. Check this out if you are facing the same issue.

Blender 2.8 : python code to switch shading mode (between wireframe and solid mode)

$\endgroup$

You must log in to answer this question.

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