Skip to content

Commit

Permalink
Add a time property to Scene (#3997)
Browse files Browse the repository at this point in the history
* Add a time property to scene

* fix mistaken search/replace

* Add typehint
  • Loading branch information
JasonGrace2282 authored Nov 7, 2024
1 parent ee0501c commit 97efef4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
21 changes: 12 additions & 9 deletions manim/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ def __init__(
def camera(self):
return self.renderer.camera

@property
def time(self) -> float:
"""The time since the start of the scene."""
return self.renderer.time

def __deepcopy__(self, clone_from_id):
cls = self.__class__
result = cls.__new__(cls)
Expand Down Expand Up @@ -1083,15 +1088,15 @@ def play(
)
return

start_time = self.renderer.time
start_time = self.time
self.renderer.play(self, *args, **kwargs)
run_time = self.renderer.time - start_time
run_time = self.time - start_time
if subcaption:
if subcaption_duration is None:
subcaption_duration = run_time
# The start of the subcaption needs to be offset by the
# run_time of the animation because it is added after
# the animation has already been played (and Scene.renderer.time
# the animation has already been played (and Scene.time
# has already been updated).
self.add_subcaption(
content=subcaption,
Expand Down Expand Up @@ -1504,7 +1509,7 @@ def add_subcaption(
r"""Adds an entry in the corresponding subcaption file
at the current time stamp.
The current time stamp is obtained from ``Scene.renderer.time``.
The current time stamp is obtained from ``Scene.time``.
Parameters
----------
Expand Down Expand Up @@ -1541,10 +1546,8 @@ def construct(self):
subtitle = srt.Subtitle(
index=len(self.renderer.file_writer.subcaptions),
content=content,
start=datetime.timedelta(seconds=float(self.renderer.time + offset)),
end=datetime.timedelta(
seconds=float(self.renderer.time + offset + duration)
),
start=datetime.timedelta(seconds=float(self.time + offset)),
end=datetime.timedelta(seconds=float(self.time + offset + duration)),
)
self.renderer.file_writer.subcaptions.append(subtitle)

Expand Down Expand Up @@ -1592,7 +1595,7 @@ def construct(self):
"""
if self.renderer.skip_animations:
return
time = self.renderer.time + time_offset
time = self.time + time_offset
self.renderer.file_writer.add_sound(sound_file, time, gain, **kwargs)

def on_mouse_motion(self, point, d_point):
Expand Down
8 changes: 4 additions & 4 deletions tests/module/scene/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def test_scene_add_remove(dry_run):

def test_scene_time(dry_run):
scene = Scene()
assert scene.renderer.time == 0
assert scene.time == 0
scene.wait(2)
assert scene.renderer.time == 2
assert scene.time == 2
scene.play(FadeIn(Circle()), run_time=0.5)
assert pytest.approx(scene.renderer.time) == 2.5
assert pytest.approx(scene.time) == 2.5
scene.renderer._original_skipping_status = True
scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
assert pytest.approx(scene.renderer.time) == 7.5
assert pytest.approx(scene.time) == 7.5


def test_subcaption(dry_run):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_scene_rendering/test_play_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def test_non_static_wait_detection(using_temp_config, disabling_caching):
def test_wait_with_stop_condition(using_temp_config, disabling_caching):
class TestScene(Scene):
def construct(self):
self.wait_until(lambda: self.renderer.time >= 1)
assert self.renderer.time >= 1
self.wait_until(lambda: self.time >= 1)
assert self.time >= 1
d = Dot()
d.add_updater(lambda mobj, dt: self.add(Mobject()))
self.add(d)
self.play(Wait(run_time=5, stop_condition=lambda: len(self.mobjects) > 5))
assert len(self.mobjects) > 5
assert self.renderer.time < 2
assert self.time < 2

scene = TestScene()
scene.render()
Expand Down

0 comments on commit 97efef4

Please sign in to comment.