Created
July 1, 2012 12:30
-
-
Save zeffii/3028278 to your computer and use it in GitHub Desktop.
select front facing faces in blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment