1
$\begingroup$

If i switch between solid and wireframe mode, i can see in the info window that these commands are invoked :

bpy.data.screens["Layout"].shading.type = 'WIREFRAME'
bpy.data.screens["Layout"].shading.type = 'SOLID'

Now i want to execute this command using python console. So i cut and paste the codes into python console but i got this error :

>>> bpy.data.screens["Layout"].shading.type = 'WIREFRAME'
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Screen' object has no attribute 'shading'

>>> bpy.data.screens["Layout"].shading.type = 'SOLID'
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Screen' object has no attribute 'shading'

Does anyone know why ? and what the correct script should i use in python console ?

Thanks

$\endgroup$
2
$\begingroup$

As to why the script generates an error: When directly clicking on a button on the screen Blender understands the context of which workspace (tabs across the top of the screen) and area (window within the screen) you are accessing. Your code fails to identify where exactly you want to apply the change.

import bpy

my_areas = bpy.context.workspace.screens[0].areas
my_shading = 'WIREFRAME'  # 'WIREFRAME' 'SOLID' 'MATERIAL' 'RENDERED'


for area in my_areas:
    for space in area.spaces:
        if space.type == 'VIEW_3D':
            space.shading.type = my_shading
$\endgroup$
1
$\begingroup$

This answer is basically the same as Ratt's, wrapped up as a function with a little more flexibility:

def set_shading_mode(mode="SOLID", screens=[]):
    """
    Performs an action analogous to clicking on the display/shade button of
    the 3D view. Mode is one of "RENDERED", "MATERIAL", "SOLID", "WIREFRAME".
    The change is applied to the given collection of bpy.data.screens.
    If none is given, the function is applied to bpy.context.screen (the
    active screen) only. E.g. set all screens to rendered mode:
      set_shading_mode("RENDERED", bpy.data.screens)
    """
    screens = screens if screens else [bpy.context.screen]
    for s in screens:
        for spc in s.areas:
            if spc.type == "VIEW_3D":
                spc.spaces[0].shading.type = mode
                break # we expect at most 1 VIEW_3D space

Hope helps someone!
Cheers,
Andres

$\endgroup$

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.