forked from 3b1b/videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconfigurable_scene.py
66 lines (59 loc) · 2 KB
/
reconfigurable_scene.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
58
59
60
61
62
63
64
65
66
from manimlib.animation.transform import Transform
from manimlib.constants import *
from manimlib.mobject.mobject import Mobject
from manimlib.scene.scene import Scene
class ReconfigurableScene(Scene):
"""
Note, this seems to no longer work as intented.
"""
CONFIG = {
"allow_recursion": True,
}
def setup(self):
self.states = []
self.num_recursions = 0
def transition_to_alt_config(
self,
return_to_original_configuration=True,
transformation_kwargs=None,
**new_config
):
if transformation_kwargs is None:
transformation_kwargs = {}
original_state = self.get_state()
state_copy = original_state.copy()
self.states.append(state_copy)
if not self.allow_recursion:
return
alt_scene = self.__class__(
skip_animations=True,
allow_recursion=False,
**new_config
)
alt_state = alt_scene.states[len(self.states) - 1]
if return_to_original_configuration:
self.clear()
self.transition_between_states(
state_copy, alt_state,
**transformation_kwargs
)
self.transition_between_states(
state_copy, original_state,
**transformation_kwargs
)
self.clear()
self.add(*original_state)
else:
self.transition_between_states(
original_state, alt_state,
**transformation_kwargs
)
self.__dict__.update(new_config)
def get_state(self):
# Want to return a mobject that maintains the most
# structure. The way to do that is to extract only
# those that aren't inside another.
return Mobject(*self.get_top_level_mobjects())
def transition_between_states(self, start_state, target_state, **kwargs):
self.play(Transform(start_state, target_state, **kwargs))
self.wait()