I'm wanting to change the shader type of the viewport, up the top left of blender 2.8 it has the icons: Wireframe/Solid/LookDev/Rendered

I want to set this in python, when I mouse over it shows: bpy.data.screens["Modeling"].shader.type

So I tried

bpy.data.screens["Modeling"].shader.type = 'MATERIAL'

but this didn't work, I also tried:

shading = context.space_data.shading
    shading.type = 'MATERIAL'

but neither worked, bot said that type 'SpaceProperties has no attribute Shading'


2 Answers 2


You need to get the space you want to change. If you run it from the Text Editor or Python Console, context will point to the Text Editor's or Python Console's spaces. You can loop through all areas and their spaces and change all the ones of type VIEW_3D:

import bpy

for area in bpy.context.screen.areas: 
    if area.type == 'VIEW_3D':
        for space in area.spaces: 
            if space.type == 'VIEW_3D':
                space.shading.type = 'MATERIAL'

Or if you write an operator and execute it when the mouse is over the 3d viewport you should be able to get the current space from the context:

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        context.space_data.shading.type = "MATERIAL"
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

    # test call
#    bpy.ops.object.simple_operator()
  • 1
    $\begingroup$ space = area.spaces.active is the space of same type as area. The context member if run in 3d viewport would be context.space_data $\endgroup$
    – batFINGER
    Commented Apr 23, 2019 at 11:23
  • $\begingroup$ @batFINGER I fail to follow. What do mean by 'context member'? Are you saying that if the mouse is over 3d viewport the area he needs would be bpy.context.space_data? I thought so, I just did not check, because I am lazy. $\endgroup$ Commented Apr 23, 2019 at 11:40
  • $\begingroup$ I'll add the code for an operator made from the examples to my answer. $\endgroup$ Commented Apr 23, 2019 at 11:40
  • $\begingroup$ Basic OOP: A member is a class attribute in python terms. Start here: stackoverflow.com/a/12409963 @MartinZ $\endgroup$
    – brockmann
    Commented Apr 23, 2019 at 12:36
  • $\begingroup$ OK, thanks. That might help me with my lack of knowledge of the terminology. $\endgroup$ Commented Apr 23, 2019 at 12:48

Thanks, I ended up with the following solution:

for area in bpy.context.screen.areas: 
        if area.type == 'VIEW_3D':
            space = area.spaces.active
            if space.type == 'VIEW_3D':
                space.shading.type = 'MATERIAL'

You must log in to answer this question.

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