# Changing the shading type in Python

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


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

• – batFINGER Apr 23 '19 at 11:22

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':


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):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"

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

def execute(self, context):
return {'FINISHED'}

def register():
bpy.utils.register_class(SimpleOperator)

def unregister():
bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
register()

# test call
#    bpy.ops.object.simple_operator()

• 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 – batFINGER Apr 23 '19 at 11:23
• @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. – Martynas Žiemys Apr 23 '19 at 11:40
• I'll add the code for an operator made from the examples to my answer. – Martynas Žiemys Apr 23 '19 at 11:40
• Basic OOP: A member is a class attribute in python terms. Start here: stackoverflow.com/a/12409963 @MartinZ – brockmann Apr 23 '19 at 12:36
• OK, thanks. That might help me with my lack of knowledge of the terminology. – Martynas Žiemys Apr 23 '19 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':