How to refer to the active 3D view ?

Hello

I am new to Blender scripting and am trying to write a script which selects only the visible faces or vertices of a mesh, while rotating automatically that objet. I have some ideas for the aglorithm but am stuck with the basics of the API.

When I run the piece of script below from the script editor window in Blender (which is open beside the 3d view where my mesh is), the instructions supposed to configure the 3D view are not processed (those in the for loop). However, the other ones addressing the object directly are processed. Therefore it seems the reference to the 3D view is wrong. Any idea how to solve this ?

import bpy 
import mathutils 
bpy.ops.object.editmode_toggle()
areas = bpy.context.screen.areas 
for area in areas:     
if area.type == 'SpaceView3D' :
area.active_space.viewport_shade = 'SOLID'
area.active_space.use_occlude_geometry = True              

bpy.ops.mesh.select_mode(use_extend=False, use_expand=True, type='EDGE') 
bpy.ops.mesh.select_all(action='TOGGLE')

thx

Pierre


import bpy

#make a dict of areas keyed by their type
areas = {a.type:a for a in bpy.context.screen.areas}

# get the one we want, or None if not available
area = areas.get("VIEW_3D",None)

#if there is one of that kind
if area:
    space = area.spaces.active
    space.viewport_shade = "SOLID"




VIEW_3D is the enum for the type of the area, while SpaceView3D is the name for the class.

Many Thanks, it effectively works ! :yes: