Select all faces visible from camera

There is a strange behavior in my script which I cannot explain. Run the script once, I get an error. But with no action in between, running the script a second time, it does what I expect it to do: selecting all faces that are visible from active camera.

Here is my script:

import bpy

def view3d_find():
    # returns first 3d view, normally we get from context
    for area in bpy.context.window.screen.areas:
        if area.type == 'VIEW_3D':
            v3d = area.spaces[0]
            rv3d = v3d.region_3d
            for region in area.regions:
                if region.type == 'WINDOW':
                    return region, rv3d
    return None, None

def view3d_camera_border(scene):
    obj =
    cam =

    #frame = cam.view_frame(scene)
    frame =

    # move from object-space into world-space 
    frame = [obj.matrix_world @ v for v in frame]

    # move into pixelspace
    from bpy_extras.view3d_utils import location_3d_to_region_2d
    region, rv3d = view3d_find()
    print(region, rv3d)
    frame_px = [location_3d_to_region_2d(region, rv3d, v) for v in frame]
    return frame_px

def getView3dAreaAndRegion(context):
    for area in context.screen.areas:
        if area.type == "VIEW_3D":
            for region in area.regions:
                if region.type == "WINDOW":
                    return area, region

def select_border(context, corners, extend=True):
    return True

def getOverride(context):
    view3dArea, view3dRegion = getView3dAreaAndRegion(context)
    override = context.copy()
    override['area'] = view3dArea
    override['region'] = view3dRegion
    return override

def getCorners(cam_corners):
    returns xmin,xmax,ymin,ymax
    return [cam_corners[2][0],cam_corners[0][0],cam_corners[2][1],cam_corners[0][1]]

def doStuff():
    # go to editmode
    vertex, edge, face = False, False, True
    bpy.context.tool_settings.mesh_select_mode = (vertex, edge, face)

    # set camera view (both approaches do the same)
    area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
    area.spaces[0].region_3d.view_perspective = 'CAMERA'

    # get camera borders in view coordinates
    cam_corners = getCorners(view3d_camera_border(bpy.context.scene))
    # select stuff in borders
    select_border(bpy.context, cam_corners, extend = False)


print("now ready for splitting")



UPDATE: I get the feeling that the amount of faces that is selected depends on the distance of the mesh to the camera and the size of the faces. This phenomenon not only occurs when selecting faces via the script but also when selecting with mouse. I tried to give examples in the image.

help us help you. tell us what the error is.

Funny thing is: I don’t get an error anymore… wanted to copy the error for you and loaded a mesh, run the script, and it worked. Thanks anyway and if someone wants to use it, feel free :smiley:

You can try this solution:

Whether my add-on can help depends on whether you want to select geometry based on what is visible on the screen or based on which faces are not turning their back to the camera.
For the latter, yes, it can help. But for the first scenario, no.

1 Like