Operators and properties are often context sensitive. It means they need to be executed from specific context (screen area). Your example needs to be run from View_3D
context, so it cannot run from the Python Console.
Luckily, there is a way to access different screen areas and get their types
and properties. In your case use_occlude_geometry
is a property of SpaceView3D, thus only accessible from that space:
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
if context.active_object.mode == 'EDIT':
area.spaces[0].use_occlude_geometry = True
You can paste and run the above example in Python Console as a multi-line command or run it from the Text Editor as usual.
However, best practice for Addons is writing your own Operator to match the context:
import bpy
class OccludeGeometryToggle(bpy.types.Operator):
"""Toggle Occlude Geometry Option in 3D View"""
bl_idname = "view3d.toggle_occlude_geometry"
bl_label = "Occlude Geometry Toggle"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.active_object.mode == 'EDIT'
def execute(self, context):
context.space_data.use_occlude_geometry = \
not context.space_data.use_occlude_geometry
return {'FINISHED'}
def register():
bpy.utils.register_class(OccludeGeometryToggle)
def unregister():
bpy.utils.unregister_class(OccludeGeometryToggle)
if __name__ == "__main__":
register()
Once the operator is registered, press Space in Edit Mode (since the poll() method checks whether the active object is in the correct mode) and type "Occlude Geometry Tog..."
.