2
$\begingroup$

I have been working on my project for Blender for quite some time and have reached the end of this small section, but I want to add a toggle button to the GUI that toggles if backface culling is off or not, I have tried using the command:

bpy.context.space_data.shading.show_backface_culling = True

and the opposite but when I run it in the python script, it says:

"SpaceTextEditor" object has no attribute shading

I know it's probably something really simple.

$\endgroup$
1
  • $\begingroup$ Don't know whether this helps as I don't know python in Blender: If you hover over the checkbox with Python tooltips enabled in Preferences > Interface > Display, the tooltip shows "bpy.data.materials["Material"].use_backface_culling". $\endgroup$
    – John Eason
    Commented Apr 26, 2023 at 23:39

1 Answer 1

1
$\begingroup$

When you run bpy.context.space_data in the TextEditor, the context will be within Text Editor by default, and thus space_data will be of type SpaceTextEditor. When you run that code line in the Python Console, the context will be within the Python Console by default, and thus space_data will be of type SpaceConsole. But the attribute shading is part of the SpaceView3D class so you have to override the context to be within the 3D Viewport which you can do by using the following script:

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

if len(areas) <= 0:
    raise Exception(f"Make sure an Area of type {area_type} is open or visible in your screen!")

with bpy.context.temp_override(area=areas[0]):
    bpy.context.space_data.shading.show_backface_culling = False
$\endgroup$
2
  • 1
    $\begingroup$ I like that you went ahead and added a check for the area's existence. $\endgroup$
    – Lauloque
    Commented Apr 27, 2023 at 4:31
  • 1
    $\begingroup$ thanks! yeah new users may get confused when they run a script and it didn't work, just because the area was not there in their workspace. Also easy to debug a problem :) $\endgroup$
    – Harry McKenzie
    Commented Apr 27, 2023 at 4:33

You must log in to answer this question.

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