0

I wrote a Python script that parametrized the location and rotation of the camera using a random generator.

import bpy
import random

bpy.data.scenes["Scene"].frame_current = 0

for i in range(100): # Set the current frame bpy.context.scene.frame_set(i)

# move with camera
# location
location_x = random.uniform(-0.25, 0.25)
location_y = random.uniform(-0.15, 0.15)
location_z = random.uniform(2.8, 3.8)
bpy.data.objects["Camera"].location[0] = location_x
bpy.data.objects["Camera"].location[1] = location_y
bpy.data.objects["Camera"].location[2] = location_z
# rotation
random_x = random.uniform(-0.0523599, 0.0523599)
random_y = random.uniform(-0.10472, 0.10472)
random_z = random.uniform(-0.20944, 0.20944)
bpy.data.objects["Camera"].rotation_euler[0] = random_x
bpy.data.objects["Camera"].rotation_euler[1] = random_y
bpy.data.objects["Camera"].rotation_euler[2] = random_z

#-----------Setup animation------------------
bpy.data.objects["Camera"].keyframe_insert(data_path="location", frame=i)
bpy.data.objects["Camera"].keyframe_insert(data_path="rotation_euler", frame=i)

The camera render should capture the ID card (gray rectangle) as you can see in the image below. However, sometimes part of the ID card is missing. Typically one corner is missing.

Can I somehow check if the whole ID card will be in the rendered image?

I tried approaches such as the camera's view frustum in world space, and converting ID card coordinates to world space to check if the coordinates are inside the rectangle. I think I was close to solving the problem, but it didn't work.

Here is my blend file: https://en.webshare.cz/#/file/KJpPypF1p4/id-card-blend. You can run animation and see different frames. Please, do you have any suggestions?

enter image description here

Gordon Brinkmann
  • 28,589
  • 1
  • 19
  • 49
  • Hi Petra, i am sorry, but how shall we help you? you didn't provide any useful information what you have done or how we could reproduce it. So please provide your blend file so we can check it out ourselves. thx. – Chris Oct 24 '23 at 17:41
  • Thanks for you suggestions. You are right. I edited my question and added a blend file with a Python script. – Petra Svobodova Oct 24 '23 at 18:05
  • Hi. Thanks for the update, but I think the most interesting point of your question is still missing. How did you check if the object is in the camera frustum? There are similar questions with answers, like this one: https://blender.stackexchange.com/q/32975/107598. Have you tried? What didn't work? – Blunder Oct 24 '23 at 19:33
  • Please use the python tag when asking python questions... I'm filtering questions by tags and maybe others do that, too. – Gordon Brinkmann Oct 25 '23 at 10:28

1 Answers1

2

Thanks for advices. It helped!

It seems that this solution works.

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['ID_card'] mesh = obj.data mat_world = obj.matrix_world cs, ce = cam.data.clip_start, cam.data.clip_end

bpy.context.view_layer.objects.active = obj # Set the active object bpy.ops.object.mode_set(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 print("not inside frustum.")