Suppose I have the following camera perspective.
As you can see some of the Cube vertices are visible from the camera, others are not.
Is there a way from python to detect if a certain vertex is in the camera field of view or not?
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.
Sign up to join this communitySuppose I have the following camera perspective.
As you can see some of the Cube vertices are visible from the camera, others are not.
Is there a way from python to detect if a certain vertex is in the camera field of view or not?
import bpy
import bmesh
from mathutils import Vector
from bpy_extras.object_utils import world_to_camera_view
scene = bpy.context.scene
cam = bpy.data.objects['Camera']
obj = bpy.data.objects['Cube']
mesh = obj.data
mat_world = obj.matrix_world
cs, ce = cam.data.clip_start, cam.data.clip_end
assert obj.mode == "EDIT"
bm = bmesh.from_edit_mesh(mesh)
for v in bm.verts:
co_ndc = world_to_camera_view(scene, cam, mat_world @ v.co)
#check wether point is inside frustum
if (0.0 < co_ndc.x < 1.0 and
0.0 < co_ndc.y < 1.0 and
cs < co_ndc.z < ce):
v.select = True
else:
v.select = False
bmesh.update_edit_mesh(mesh, False, False)