6
$\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$

4 Answers 4

11
$\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$
0
8
$\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=None):
    """
    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 or [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
$\endgroup$
1
$\begingroup$

it is nice to support 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$
0
$\begingroup$

The previous answers did not work for me. I found out while working on Goo package, the codes below always point to "Scripting" screen and do not list all screens such as Layout, Modeling..etc. That maybe because context is always pointing to the recent active screen - I am not sure maybe someone can correct me.

bpy.context.screen.areas. # results in giving bpy.data.screens[Scripting].areas

I solved this issue by accessing the memory instead through data.

for area in bpy.data.screens[3].areas: 
    if area.type == 'VIEW_3D':
       for space in area.spaces: 
           if space.type == 'VIEW_3D':
              space.shading.type = 'RENDERED'

How did I figure out 'screens[3]'? On the python console window, if change the number between square bracket you will get the screens name (the first row tab).

enter image description here

bpy.data.screens[0] # Animation
bpy.data.screens[1] # Compositing
bpy.data.screens[2] # Geometry Nodes
bpy.data.screens[3] # Layout
$\endgroup$
1
  • 1
    $\begingroup$ I think what you want rather is to iterate through bpy.data.workspaces eg for ws in bpy.data.workspaces: for screen in ws.screens: ... $\endgroup$
    – Gorgious
    Commented Jun 29, 2022 at 16:44

You must log in to answer this question.

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