From 8b1c9a2df6b4362ae1c90ef8401e470fb3ee7775 Mon Sep 17 00:00:00 2001 From: Matthias Humt Date: Thu, 12 Dec 2024 13:20:44 +0100 Subject: [PATCH 1/2] Add camera utility functions for Blender - Implement `get_camera` to retrieve the active camera. - Refactor `get_camera_pose` to use `get_camera`. - Add `get_resolution` to return camera resolution. --- blenderproc/python/camera/CameraUtility.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/blenderproc/python/camera/CameraUtility.py b/blenderproc/python/camera/CameraUtility.py index 530377440..817de46c9 100644 --- a/blenderproc/python/camera/CameraUtility.py +++ b/blenderproc/python/camera/CameraUtility.py @@ -11,6 +11,17 @@ from blenderproc.python.utility.Utility import KeyFrame +def get_camera(frame: Optional[int] = None) -> Entity: + """ Retrieves the active camera in the current Blender scene and frame. + + :param frame: The frame number whose assigned camera should be returned. If None is give, the current frame + is used. + :return: The camera entity. + """ + with KeyFrame(frame): + return Entity(bpy.context.scene.camera) + + def add_camera_pose(cam2world_matrix: Union[np.ndarray, Matrix], frame: Optional[int] = None) -> int: """ Sets a new camera pose to a new or existing frame @@ -45,8 +56,7 @@ def get_camera_pose(frame: Optional[int] = None) -> np.ndarray: is used. :return: The 4x4 cam2world transformation matrix. """ - with KeyFrame(frame): - return np.array(Entity(bpy.context.scene.camera).get_local2world_mat()) + return np.array(get_camera(frame).get_local2world_mat()) def get_camera_frustum(clip_start: Optional[float] = None, clip_end: Optional[float] = None, @@ -135,6 +145,11 @@ def rotation_from_forward_vec(forward_vec: Union[np.ndarray, Vector], up_axis: s return np.array(rotation_matrix) +def get_resolution() -> Tuple[int, int]: + """ Returns the camera resolution.""" + return bpy.context.scene.render.resolution_x, bpy.context.scene.render.resolution_y + + def set_resolution(image_width: int = None, image_height: int = None): """ Sets the camera resolution. From 7a8a6f5188d84c4bdd67b634252cafe3a73cdec5 Mon Sep 17 00:00:00 2001 From: Matthias Humt Date: Thu, 12 Dec 2024 13:40:21 +0100 Subject: [PATCH 2/2] Add context management for camera pose retrieval - Wrap camera pose retrieval in a KeyFrame context manager. - Update docstring for get_resolution to clarify it returns render resolution. --- blenderproc/python/camera/CameraUtility.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/blenderproc/python/camera/CameraUtility.py b/blenderproc/python/camera/CameraUtility.py index 817de46c9..78a4a44d9 100644 --- a/blenderproc/python/camera/CameraUtility.py +++ b/blenderproc/python/camera/CameraUtility.py @@ -56,7 +56,8 @@ def get_camera_pose(frame: Optional[int] = None) -> np.ndarray: is used. :return: The 4x4 cam2world transformation matrix. """ - return np.array(get_camera(frame).get_local2world_mat()) + with KeyFrame(frame): + return np.array(get_camera(frame).get_local2world_mat()) def get_camera_frustum(clip_start: Optional[float] = None, clip_end: Optional[float] = None, @@ -146,7 +147,10 @@ def rotation_from_forward_vec(forward_vec: Union[np.ndarray, Vector], up_axis: s def get_resolution() -> Tuple[int, int]: - """ Returns the camera resolution.""" + """ Returns the render resolution. + + :return: The render width and height in pixels. + """ return bpy.context.scene.render.resolution_x, bpy.context.scene.render.resolution_y