Skip to content

Commit 57a2ead

Browse files
committed
Move caching functions
1 parent a74313f commit 57a2ead

File tree

3 files changed

+99
-95
lines changed

3 files changed

+99
-95
lines changed

manim/renderer/cairo_renderer.py

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,7 @@
55
from ..utils.hashing import get_hash_from_play_call, get_hash_from_wait_call
66
from ..constants import DEFAULT_WAIT_TIME
77
from ..scene.scene_file_writer import SceneFileWriter
8-
9-
10-
def handle_caching_play(func):
11-
"""
12-
Decorator that returns a wrapped version of func that will compute the hash of the play invocation.
13-
14-
The returned function will act according to the computed hash: either skip the animation because it's already cached, or let the invoked function play normally.
15-
16-
Parameters
17-
----------
18-
func : Callable[[...], None]
19-
The play like function that has to be written to the video file stream. Take the same parameters as `scene.play`.
20-
"""
21-
22-
def wrapper(self, *args, **kwargs):
23-
self.revert_to_original_skipping_status()
24-
self.update_skipping_status()
25-
animations = self.scene.compile_play_args_to_animation_list(*args, **kwargs)
26-
self.scene.add_mobjects_from_animations(animations)
27-
if file_writer_config["skip_animations"]:
28-
logger.debug(f"Skipping animation {self.num_plays}")
29-
func(self, *args, **kwargs)
30-
# If the animation is skipped, we mark its hash as None.
31-
# When sceneFileWriter will start combining partial movie files, it won't take into account None hashes.
32-
self.animations_hashes.append(None)
33-
self.file_writer.add_partial_movie_file(None)
34-
return
35-
if not file_writer_config["disable_caching"]:
36-
mobjects_on_scene = self.scene.get_mobjects()
37-
hash_play = get_hash_from_play_call(
38-
self, self.camera, animations, mobjects_on_scene
39-
)
40-
if self.file_writer.is_already_cached(hash_play):
41-
logger.info(
42-
f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",
43-
{"hash_play": hash_play},
44-
)
45-
file_writer_config["skip_animations"] = True
46-
else:
47-
hash_play = "uncached_{:05}".format(self.num_plays)
48-
self.animations_hashes.append(hash_play)
49-
self.file_writer.add_partial_movie_file(hash_play)
50-
logger.debug(
51-
"List of the first few animation hashes of the scene: %(h)s",
52-
{"h": str(self.animations_hashes[:5])},
53-
)
54-
func(self, *args, **kwargs)
55-
56-
return wrapper
57-
58-
59-
def handle_caching_wait(func):
60-
"""
61-
Decorator that returns a wrapped version of func that will compute the hash of the wait invocation.
62-
63-
The returned function will act according to the computed hash: either skip the animation because it's already cached, or let the invoked function play normally.
64-
65-
Parameters
66-
----------
67-
func : Callable[[...], None]
68-
The wait like function that has to be written to the video file stream. Take the same parameters as `scene.wait`.
69-
"""
70-
71-
def wrapper(self, duration=DEFAULT_WAIT_TIME, stop_condition=None):
72-
self.revert_to_original_skipping_status()
73-
self.update_skipping_status()
74-
if file_writer_config["skip_animations"]:
75-
logger.debug(f"Skipping wait {self.num_plays}")
76-
func(self, duration, stop_condition)
77-
# If the animation is skipped, we mark its hash as None.
78-
# When sceneFileWriter will start combining partial movie files, it won't take into account None hashes.
79-
self.animations_hashes.append(None)
80-
self.file_writer.add_partial_movie_file(None)
81-
return
82-
if not file_writer_config["disable_caching"]:
83-
hash_wait = get_hash_from_wait_call(
84-
self, self.camera, duration, stop_condition, self.scene.get_mobjects()
85-
)
86-
if self.file_writer.is_already_cached(hash_wait):
87-
logger.info(
88-
f"Wait {self.num_plays} : Using cached data (hash : {hash_wait})"
89-
)
90-
file_writer_config["skip_animations"] = True
91-
else:
92-
hash_wait = "uncached_{:05}".format(self.num_plays)
93-
self.animations_hashes.append(hash_wait)
94-
self.file_writer.add_partial_movie_file(hash_wait)
95-
logger.debug(
96-
"Animations hashes list of the scene : (concatened to 5) %(h)s",
97-
{"h": str(self.animations_hashes[:5])},
98-
)
99-
func(self, duration, stop_condition)
100-
101-
return wrapper
8+
from ..utils.caching import handle_caching_play, handle_caching_wait
1029

10310

10411
def handle_play_like_call(func):

manim/utils/caching.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from .. import file_writer_config, logger
2+
from ..utils.hashing import get_hash_from_play_call, get_hash_from_wait_call
3+
from ..constants import DEFAULT_WAIT_TIME
4+
5+
6+
def handle_caching_play(func):
7+
"""
8+
Decorator that returns a wrapped version of func that will compute the hash of the play invocation.
9+
10+
The returned function will act according to the computed hash: either skip the animation because it's already cached, or let the invoked function play normally.
11+
12+
Parameters
13+
----------
14+
func : Callable[[...], None]
15+
The play like function that has to be written to the video file stream. Take the same parameters as `scene.play`.
16+
"""
17+
18+
def wrapper(self, *args, **kwargs):
19+
self.revert_to_original_skipping_status()
20+
self.update_skipping_status()
21+
animations = self.scene.compile_play_args_to_animation_list(*args, **kwargs)
22+
self.scene.add_mobjects_from_animations(animations)
23+
if file_writer_config["skip_animations"]:
24+
logger.debug(f"Skipping animation {self.num_plays}")
25+
func(self, *args, **kwargs)
26+
# If the animation is skipped, we mark its hash as None.
27+
# When sceneFileWriter will start combining partial movie files, it won't take into account None hashes.
28+
self.animations_hashes.append(None)
29+
self.file_writer.add_partial_movie_file(None)
30+
return
31+
if not file_writer_config["disable_caching"]:
32+
mobjects_on_scene = self.scene.get_mobjects()
33+
hash_play = get_hash_from_play_call(
34+
self, self.camera, animations, mobjects_on_scene
35+
)
36+
if self.file_writer.is_already_cached(hash_play):
37+
logger.info(
38+
f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",
39+
{"hash_play": hash_play},
40+
)
41+
file_writer_config["skip_animations"] = True
42+
else:
43+
hash_play = "uncached_{:05}".format(self.num_plays)
44+
self.animations_hashes.append(hash_play)
45+
self.file_writer.add_partial_movie_file(hash_play)
46+
logger.debug(
47+
"List of the first few animation hashes of the scene: %(h)s",
48+
{"h": str(self.animations_hashes[:5])},
49+
)
50+
func(self, *args, **kwargs)
51+
52+
return wrapper
53+
54+
55+
def handle_caching_wait(func):
56+
"""
57+
Decorator that returns a wrapped version of func that will compute the hash of the wait invocation.
58+
59+
The returned function will act according to the computed hash: either skip the animation because it's already cached, or let the invoked function play normally.
60+
61+
Parameters
62+
----------
63+
func : Callable[[...], None]
64+
The wait like function that has to be written to the video file stream. Take the same parameters as `scene.wait`.
65+
"""
66+
67+
def wrapper(self, duration=DEFAULT_WAIT_TIME, stop_condition=None):
68+
self.revert_to_original_skipping_status()
69+
self.update_skipping_status()
70+
if file_writer_config["skip_animations"]:
71+
logger.debug(f"Skipping wait {self.num_plays}")
72+
func(self, duration, stop_condition)
73+
# If the animation is skipped, we mark its hash as None.
74+
# When sceneFileWriter will start combining partial movie files, it won't take into account None hashes.
75+
self.animations_hashes.append(None)
76+
self.file_writer.add_partial_movie_file(None)
77+
return
78+
if not file_writer_config["disable_caching"]:
79+
hash_wait = get_hash_from_wait_call(
80+
self, self.camera, duration, stop_condition, self.scene.get_mobjects()
81+
)
82+
if self.file_writer.is_already_cached(hash_wait):
83+
logger.info(
84+
f"Wait {self.num_plays} : Using cached data (hash : {hash_wait})"
85+
)
86+
file_writer_config["skip_animations"] = True
87+
else:
88+
hash_wait = "uncached_{:05}".format(self.num_plays)
89+
self.animations_hashes.append(hash_wait)
90+
self.file_writer.add_partial_movie_file(hash_wait)
91+
logger.debug(
92+
"Animations hashes list of the scene : (concatened to 5) %(h)s",
93+
{"h": str(self.animations_hashes[:5])},
94+
)
95+
func(self, duration, stop_condition)
96+
97+
return wrapper

tests/control_data/logs_data/BasicSceneLoggingTest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{"levelname": "DEBUG", "module": "hashing", "message": "Hashing ..."}
33
{"levelname": "DEBUG", "module": "hashing", "message": "Hashing done in <> s."}
44
{"levelname": "DEBUG", "module": "hashing", "message": "Hash generated : <>"}
5-
{"levelname": "DEBUG", "module": "cairo_renderer", "message": "List of the first few animation hashes of the scene: <>"}
5+
{"levelname": "DEBUG", "module": "caching", "message": "List of the first few animation hashes of the scene: <>"}
66
{"levelname": "INFO", "module": "scene_file_writer", "message": "Animation 0 : Partial movie file written in <>"}
77
{"levelname": "DEBUG", "module": "scene_file_writer", "message": "Partial movie files to combine (1 files): <>"}
88
{"levelname": "INFO", "module": "scene_file_writer", "message": "\nFile ready at <>\n"}

0 commit comments

Comments
 (0)