3
$\begingroup$

Blender 2.79b.

Simple script should return indices of camera-oriented faces.

Mesh has two rather similiar directed faces, but code returns only one:

import bpy
import bmesh
import numpy

scene = bpy.context.scene

cam_mat = scene.camera.matrix_world
cam_dir = [e[2] for e in cam_mat][:-1]

bm = bmesh.new()
bm.from_object(bpy.context.active_object, scene, deform=False)
id = [f.index for f in bm.faces if numpy.dot(f.normal, cam_dir)>0]

print(id)

Default backface culling works fine though.

$\endgroup$
3
  • 1
    $\begingroup$ Probably because the mesh/f.normal coordinates are in object space. Not in world space. $\endgroup$
    – lemon
    Mar 10, 2020 at 10:57
  • $\begingroup$ @lemon Seems, like you nailed it. Multiplied object's world matrix by normal and that's it. Thank you! $\endgroup$
    – Serge L
    Mar 10, 2020 at 11:16
  • $\begingroup$ looks like I need many camera ray directions (camera location - face center) rather than one for all. $\endgroup$
    – Serge L
    Mar 10, 2020 at 12:36

1 Answer 1

3
$\begingroup$

Ended with this. Transformed geometry to world coordinates (with bmesh.ops.transform as suggested by batFINGER) and replaced single camera direction to multiple directions between camera location and faces' centers:

import bpy
import bmesh
import numpy
from mathutils import Vector

scene = bpy.context.scene

obj_mat = bpy.context.active_object.matrix_world
cam_mat = scene.camera.matrix_world
cam_loc = Vector(([e[3] for e in cam_mat][:-1]))

bm = bmesh.new()
bm.from_object(bpy.context.active_object, scene, deform=False)
bmesh.ops.transform(bm, verts=bm.verts, matrix=obj_mat) #convert to world coordinates
id = [f.index for f in bm.faces if numpy.dot(f.normal, cam_loc-f.calc_center_median())>0]

print(id)
$\endgroup$
1
  • 1
    $\begingroup$ Since you are not going to rewrite the object bmesh, could bmesh.ops.transform(bm, verts=bm.verts, matrix=obj_mat) to put bm into global coords. And / or use space to put in camera obj coords. $\endgroup$
    – batFINGER
    Mar 11, 2020 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .