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

Expose resetcam parameter #386

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion brainrender/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def render(
interactive=None,
camera=None,
zoom=None,
resetcam=False,
**kwargs,
):
"""
Expand All @@ -203,6 +204,7 @@ def render(
Pass a valid camera input to specify the camera position when
the scene is rendered.
:param zoom: float, if None atlas default is used
:param resetcam: bool, if True the camera is reset between renders
:param kwargs: additional arguments to pass to self.plotter.show
"""
logger.debug(
Expand Down Expand Up @@ -265,7 +267,7 @@ def render(
bg=settings.BACKGROUND_COLOR,
rate=40,
axes=self.plotter.axes,
resetcam=False,
resetcam=resetcam,
)
elif self.backend == "k3d": # pragma: no cover
# Remove silhouettes
Expand Down
31 changes: 22 additions & 9 deletions brainrender/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,47 @@ def __init__(

@staticmethod
def _make_frame(
scene, frame_number, tot_frames, azimuth=0, elevation=0, roll=0
scene,
frame_number,
tot_frames,
resetcam,
azimuth=0,
elevation=0,
roll=0,
):
"""
Default `make_frame_func`. Rotates the camera in 3 directions

:param scene: scene to be animated.
:param frame_number: int, not used
:param tot_frames: int, total number of frames
:param resetcam: bool, if True the camera is reset
:param azimuth: integer, specify the rotation in degrees
per frame on the relative axis. (Default value = 0)
:param elevation: integer, specify the rotation in degrees
per frame on the relative axis. (Default value = 0)
:param roll: integer, specify the rotation in degrees
per frame on the relative axis. (Default value = 0)
"""
scene.plotter.show(interactive=False)
scene.plotter.show(interactive=False, resetcam=resetcam)
scene.plotter.camera.Elevation(elevation)
scene.plotter.camera.Azimuth(azimuth)
scene.plotter.camera.Roll(roll)

def generate_frames(self, fps, duration, video, *args, **kwargs):
def generate_frames(self, fps, duration, video, resetcam, *args, **kwargs):
"""
Loop to generate frames

:param fps: int, frame rate
:param duration: float, video duration in seconds
:param video: vedo Video class used to create the video
:param resetcam: bool, if True the camera is reset
"""
nframes = int(fps * duration)
for i in track(range(nframes), description="Generating frames"):
self.make_frame_func(self.scene, i, nframes, *args, **kwargs)
self.make_frame_func(
self.scene, i, nframes, resetcam, *args, **kwargs
)
video.add_frame()

def compress(self, temp_name):
Expand All @@ -111,6 +121,7 @@ def make_video(
duration=10,
fps=30,
fix_camera=False,
resetcam=False,
render_kwargs={},
**kwargs,
):
Expand All @@ -121,6 +132,7 @@ def make_video(
:param duration: float, duration of the video in seconds
:param fps: int, frame rate
:param fix_camera: bool, if True the focal point of the camera is set based on the first frame
:param resetcam: bool, if True the camera is reset
:param render_kwargs: dict, any extra keyword argument to be passed to `scene.render`
:param **kwargs: any extra keyword argument to be passed to `make_frame_func`
"""
Expand Down Expand Up @@ -157,7 +169,7 @@ def make_video(
)

# Make frames
self.generate_frames(fps, duration, video, *args, **kwargs)
self.generate_frames(fps, duration, video, resetcam, *args, **kwargs)
self.scene.close()

# Stitch frames into uncompressed video
Expand Down Expand Up @@ -291,13 +303,14 @@ def get_keyframe_framenumber(self, fps):
}
self.keyframes_numbers = sorted(list(self.keyframes.keys()))

def generate_frames(self, fps, duration, video):
def generate_frames(self, fps, duration, video, resetcam):
"""
Loop to generate frames

:param fps: int, frame rate
:param duration: float, video duration in seconds
:param video: vedo Video class used to create the video
:param resetcam: bool, if True the camera is reset
"""
logger.debug(
f"Generating animation keyframes. Duration: {duration}, fps: {fps}"
Expand All @@ -315,7 +328,7 @@ def generate_frames(self, fps, duration, video):
for framen in track(
range(self.nframes), description="Generating frames..."
):
self._make_frame(framen)
self._make_frame(framen, resetcam)

if framen > 1:
video.add_frame()
Expand Down Expand Up @@ -359,7 +372,7 @@ def get_frame_params(self, frame_number):
params["camera"] = get_camera_params(self.scene)
return params

def _make_frame(self, frame_number):
def _make_frame(self, frame_number, resetcam):
"""
Creates a frame with the correct params
and calls the keyframe callback function if defined.
Expand Down Expand Up @@ -388,7 +401,7 @@ def _make_frame(self, frame_number):
camera=camera.copy(),
zoom=frame_params["zoom"],
interactive=False,
resetcam=False,
resetcam=resetcam,
)

def _interpolate_cameras(self, cam1, cam2):
Expand Down
2 changes: 1 addition & 1 deletion examples/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
anim.add_keyframe(4, camera="frontal", zoom=1.2)

# Make videos
anim.make_video(duration=5, fps=15)
anim.make_video(duration=5, fps=15, resetcam=True)
Loading