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, but I have not checked exactly how you would do that.:
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):
context.space_data.shading.type = "MATERIAL"
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()