From 5a346ac25cfd012464fabfdaf0b2855f03802ff2 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Mon, 4 Nov 2024 12:13:43 -0500 Subject: [PATCH 1/3] Add a time property to scene --- manim/scene/scene.py | 21 +++++++++++-------- tests/module/scene/test_scene.py | 8 +++---- tests/test_scene_rendering/test_play_logic.py | 6 +++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/manim/scene/scene.py b/manim/scene/scene.py index f171709b4f..17d1828383 100644 --- a/manim/scene/scene.py +++ b/manim/scene/scene.py @@ -158,6 +158,11 @@ def __init__( def camera(self): return self.renderer.camera + @property + def time(self): + """The time since the start of the scene.""" + return self.time + def __deepcopy__(self, clone_from_id): cls = self.__class__ result = cls.__new__(cls) @@ -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, @@ -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 ---------- @@ -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) @@ -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): diff --git a/tests/module/scene/test_scene.py b/tests/module/scene/test_scene.py index 6c3240980b..70ea9eaf2a 100644 --- a/tests/module/scene/test_scene.py +++ b/tests/module/scene/test_scene.py @@ -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): diff --git a/tests/test_scene_rendering/test_play_logic.py b/tests/test_scene_rendering/test_play_logic.py index d62a5cf051..4aaae3c8ad 100644 --- a/tests/test_scene_rendering/test_play_logic.py +++ b/tests/test_scene_rendering/test_play_logic.py @@ -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() From 3359575df8feabc5145cca64df5137c8d907ac9a Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Mon, 4 Nov 2024 12:25:00 -0500 Subject: [PATCH 2/3] fix mistaken search/replace --- manim/scene/scene.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/scene/scene.py b/manim/scene/scene.py index 17d1828383..c2033db21d 100644 --- a/manim/scene/scene.py +++ b/manim/scene/scene.py @@ -161,7 +161,7 @@ def camera(self): @property def time(self): """The time since the start of the scene.""" - return self.time + return self.renderer.time def __deepcopy__(self, clone_from_id): cls = self.__class__ From 30d2597115e081820567d3e1d1a7f8c4e7501880 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 7 Nov 2024 16:32:26 -0500 Subject: [PATCH 3/3] Add typehint --- manim/scene/scene.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/scene/scene.py b/manim/scene/scene.py index c2033db21d..a05413184c 100644 --- a/manim/scene/scene.py +++ b/manim/scene/scene.py @@ -159,7 +159,7 @@ def camera(self): return self.renderer.camera @property - def time(self): + def time(self) -> float: """The time since the start of the scene.""" return self.renderer.time