Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(camera): add camera utility functions #1172

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion blenderproc/python/camera/CameraUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,7 +57,7 @@ def get_camera_pose(frame: Optional[int] = None) -> np.ndarray:
: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,
Expand Down Expand Up @@ -135,6 +146,14 @@ 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 render resolution.

:return: The render width and height in pixels.
"""
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.

Expand Down