1

I want to select the front face, but I don’t want to select the back face

bpy.ops.view3d.select_box(xmin=0,xmax=area.width,ymin=0,ymax=area.height,wait_for_input=False,mode='SET')

enter image description here

actual:

enter image description here

hope:

enter image description here

How to ignore the back? The backside culling is useless.

Martynas Žiemys
  • 24,274
  • 2
  • 34
  • 77
zeronofreya
  • 359
  • 1
  • 8

1 Answers1

3

This will select the front-facing faces of the selected object.

Conditions :

  • You must be in OBJECT mode.
  • Your object must be a MESH object.
  • You workspace must contain at least a 3D viewport editor.

You'll have to learn a bit about the dot product : https://en.wikipedia.org/wiki/Dot_product

import bpy
import mathutils

mesh = bpy.context.object.data # Retrieve the mesh normals = [p.normal for p in mesh.polygons] # Compute polygon normals

Compute the view direction

up = mathutils.Vector((0.0, 0.0, 1.0)) view3d_area = next(a for a in bpy.context.screen.areas if a.ui_type == 'VIEW_3D') trans_world = (view3d_area.spaces.active.region_3d.view_matrix.inverted()).to_3x3() @ up trans_world.normalize()

Unselect the mesh elements

for v in mesh.vertices: v.select = False for e in mesh.edges: e.select = False

for i, normal in enumerate(normals): # Select the front facing faces mesh.polygons[i].select = trans_world.dot(normal) >= 0

enter image description here

Related Automated way to make "Select interior faces" ignore select faces that are visible to the camera?

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • 1
    Might require either converting view vector to local space of mesh, or face normals to global space https://blender.stackexchange.com/a/119342/15543 Similarly could use angle test (which is basically dot prod anyhow) https://blender.stackexchange.com/questions/96066/how-to-get-all-faces-based-on-a-certain-angle-of-their-normals – batFINGER Oct 08 '21 at 14:45
  • @batFINGER Thanks for the heads-up you're right ! I didn't take that into account – Gorgious Oct 08 '21 at 18:10