Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

July 01, 2012

Select Front Facing Polygons

This snippet deals with selecting front facing polygons, thanks to paleajed for the hint on the eye code, and vrav for the suggestion to multiply obj_matrix with the face normal (turns out my demo object suzanne gets rotated around the x axis after insertion into the scene)
def select_front_facing(context):
"""
from: http://freespace.virgin.net/hugo.elias/routines/r_dot.htm
When deciding if a polygon is facing the camera, you need
only calculate the dot product of the normal vector of
that polygon, with a vector from the camera to one of the
polygon's vertices.
- If the dot product is less than zero, the polygon is facing the camera.
- If the value is greater than zero, it is facing away from the camera.
"""
region = context.region
rv3d = context.space_data.region_3d
obj = context.active_object
vertlist = obj.data.vertices
# [ ] be in object mode
# neat eye location code with the help of paleajed
eye = Vector(rv3d.view_matrix[2][:3])
eye.length = rv3d.view_distance
eye_location = rv3d.view_location + eye
face_list = []
for idx, polygon in enumerate(obj.data.polygons):
vert_index = polygon.vertices[0]
pnormal = obj.matrix_world * polygon.normal
world_coordinate = obj.matrix_world * vertlist[vert_index].co
result_vector = eye_location-world_coordinate
dot_value = pnormal.dot(result_vector.normalized())
if dot_value < 0.0:
polygon.select = False
else:
polygon.select = True
face_list.append(idx)
return face_list
Paper on Visibility in Computer Graphics by Jirˇ´ı Bittner and Peter Wonka . This paper looks promising and might pave the way to get a decent faux3d svg render of 3d geometry.