forked from ManimCommunity/manim
/
caching.py
57 lines (50 loc) · 2.29 KB
/
caching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from .. import config, logger
from ..utils.hashing import get_hash_from_play_call, get_hash_from_wait_call
from ..constants import DEFAULT_WAIT_TIME
def handle_caching_play(func):
"""Decorator that returns a wrapped version of func that will compute
the hash of the play invocation.
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.
Parameters
----------
func : Callable[[...], None]
The play like function that has to be written to the video file stream.
Take the same parameters as `scene.play`.
"""
def wrapper(self, scene, *args, **kwargs):
self.skip_animations = self.original_skipping_status
self.update_skipping_status()
animations = scene.compile_play_args_to_animation_list(*args, **kwargs)
scene.add_mobjects_from_animations(animations)
if self.skip_animations:
logger.debug(f"Skipping animation {self.num_plays}")
func(self, scene, *args, **kwargs)
# If the animation is skipped, we mark its hash as None. When
# Scenefilewriter starts combining partial movie files, it won't take into
# account None hashes.
self.animations_hashes.append(None)
self.file_writer.add_partial_movie_file(None)
return
if not config["disable_caching"]:
mobjects_on_scene = scene.get_mobjects()
hash_play = get_hash_from_play_call(
self, self.camera, animations, mobjects_on_scene
)
if self.file_writer.is_already_cached(hash_play):
logger.info(
f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",
{"hash_play": hash_play},
)
self.skip_animations = True
else:
hash_play = "uncached_{:05}".format(self.num_plays)
self.animations_hashes.append(hash_play)
self.file_writer.add_partial_movie_file(hash_play)
logger.debug(
"List of the first few animation hashes of the scene: %(h)s",
{"h": str(self.animations_hashes[:5])},
)
func(self, scene, *args, **kwargs)
return wrapper