Skip to content

Commit 1a516db

Browse files
huguesdevimeuxHugues DevimeuxAathish04
authored
Fix n flag (#423)
* fixes n flag * added test for n flag * Apply suggestions from code review Co-authored-by: Aathish Sivasubrahmanian <[email protected]> Co-authored-by: Hugues Devimeux <[email protected]> Co-authored-by: Aathish Sivasubrahmanian <[email protected]>
1 parent 5becd64 commit 1a516db

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

manim/config/config_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ def _parse_file_writer_config(config_parser, args):
153153
}
154154

155155
# For internal use (no CLI flag)
156-
fw_config["skip_animations"] = any(
157-
[fw_config["save_last_frame"], fw_config["from_animation_number"]]
158-
)
156+
fw_config["skip_animations"] = fw_config["save_last_frame"]
159157
fw_config["max_files_cached"] = default.getint("max_files_cached")
160158
if fw_config["max_files_cached"] == -1:
161159
fw_config["max_files_cached"] = float("inf")

manim/scene/scene.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def __init__(self, **kwargs):
7171
)
7272
self.play_hashes_list = []
7373
self.mobjects = []
74+
self.original_skipping_status = file_writer_config["skip_animations"]
7475
# TODO, remove need for foreground mobjects
7576
self.foreground_mobjects = []
7677
self.num_plays = 0
@@ -88,7 +89,7 @@ def __init__(self, **kwargs):
8889
self.tear_down()
8990
# We have to reset these settings in case of multiple renders.
9091
file_writer_config["skip_animations"] = False
91-
self.original_skipping_status = file_writer_config["skip_animations"]
92+
9293
self.file_writer.finish()
9394
self.print_end_message()
9495

@@ -785,10 +786,10 @@ def update_skipping_status(self):
785786
"""
786787

787788
if file_writer_config["from_animation_number"]:
788-
if self.num_plays == file_writer_config["from_animation_number"]:
789-
file_writer_config["skip_animations"] = False
789+
if self.num_plays < file_writer_config["from_animation_number"]:
790+
file_writer_config["skip_animations"] = True
790791
if file_writer_config["upto_animation_number"]:
791-
if self.num_plays >= file_writer_config["upto_animation_number"]:
792+
if self.num_plays > file_writer_config["upto_animation_number"]:
792793
file_writer_config["skip_animations"] = True
793794
raise EndSceneEarlyException()
794795

@@ -806,8 +807,13 @@ def handle_caching_play(func):
806807

807808
def wrapper(self, *args, **kwargs):
808809
self.revert_to_original_skipping_status()
810+
self.update_skipping_status()
809811
animations = self.compile_play_args_to_animation_list(*args, **kwargs)
810812
self.add_mobjects_from_animations(animations)
813+
if file_writer_config["skip_animations"]:
814+
logger.debug(f"Skipping animation {self.num_plays}")
815+
func(self, *args, **kwargs)
816+
return
811817
if not file_writer_config["disable_caching"]:
812818
mobjects_on_scene = self.get_mobjects()
813819
hash_play = get_hash_from_play_call(
@@ -841,6 +847,7 @@ def handle_caching_wait(func):
841847

842848
def wrapper(self, duration=DEFAULT_WAIT_TIME, stop_condition=None):
843849
self.revert_to_original_skipping_status()
850+
self.update_skipping_status()
844851
if not file_writer_config["disable_caching"]:
845852
hash_wait = get_hash_from_wait_call(
846853
self, self.camera, duration, stop_condition, self.get_mobjects()
@@ -880,7 +887,6 @@ def handle_play_like_call(func):
880887
"""
881888

882889
def wrapper(self, *args, **kwargs):
883-
self.update_skipping_status()
884890
allow_write = not file_writer_config["skip_animations"]
885891
self.file_writer.begin_animation(allow_write)
886892
func(self, *args, **kwargs)

manim/scene/scene_file_writer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(self, scene, **kwargs):
4444
self.init_output_directories()
4545
self.init_audio()
4646
self.frame_count = 0
47+
self.index_partial_movie_file = 0
4748

4849
# Output directories and files
4950
def init_output_directories(self):
@@ -196,10 +197,11 @@ def get_next_partial_movie_path(self):
196197
result = os.path.join(
197198
self.partial_movie_directory,
198199
"{}{}".format(
199-
self.scene.play_hashes_list[self.scene.num_plays],
200+
self.scene.play_hashes_list[self.index_partial_movie_file],
200201
file_writer_config["movie_file_extension"],
201202
),
202203
)
204+
self.index_partial_movie_file += 1
203205
return result
204206

205207
def get_movie_file_path(self):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "SceneWithMultipleCallsWithNFlag",
3+
"config": {
4+
"codec_name": "h264",
5+
"width": 1920,
6+
"height": 1080,
7+
"avg_frame_rate": "60/1",
8+
"duration": "4.000000",
9+
"nb_frames": "240"
10+
}
11+
}

tests/helpers/video_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55
import json
66

7-
from manim.logger import logger
7+
from manim.config.logger import logger
88

99

1010
def capture(command):

tests/test_scene_rendering/simple_scenes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ def construct(self):
66
square = Square()
77
circle = Circle()
88
self.play(Transform(square, circle))
9+
10+
11+
class SceneWithMultipleCalls(Scene):
12+
def construct(self):
13+
number = Integer(0)
14+
self.add(number)
15+
for i in range(10):
16+
number.become(Integer(i))
17+
self.play(Animation(number))

tests/test_scene_rendering/test_cli_flags.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,24 @@ def test_basic_scene_l_flag(tmp_path, manim_cfg_file, simple_scenes_path):
4444
]
4545
out, err, exit_code = capture(command)
4646
assert exit_code == 0, err
47+
48+
49+
@pytest.mark.slow
50+
@video_comparison(
51+
"SceneWithMultipleCallsWithNFlag.json",
52+
"videos/simple_scenes/1080p60/SceneWithMultipleCalls.mp4",
53+
)
54+
def test_n_flag(tmp_path, simple_scenes_path):
55+
scene_name = "SceneWithMultipleCalls"
56+
command = [
57+
"python",
58+
"-m",
59+
"manim",
60+
simple_scenes_path,
61+
scene_name,
62+
"-n 3,6",
63+
"--media_dir",
64+
str(tmp_path),
65+
]
66+
_, err, exit_code = capture(command)
67+
assert exit_code == 0, err

0 commit comments

Comments
 (0)