Skip to content

Commit d9eab15

Browse files
committed
WIP: ThreeDSlide
1 parent b3210ec commit d9eab15

File tree

6 files changed

+102
-65
lines changed

6 files changed

+102
-65
lines changed

example.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@ def construct(self):
2828
# Each slide MUST end with an animation (a self.wait is considered an animation)
2929
self.play(dot.animate.move_to(ORIGIN))
3030

31+
3132
class ThreeDExample(ThreeDSlide):
3233
def construct(self):
3334
axes = ThreeDAxes()
34-
circle=Circle()
35+
circle = Circle()
3536

36-
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
37-
self.add(circle,axes)
37+
# self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
38+
self.add(circle, axes)
3839

39-
self.begin_ambient_camera_rotation(rate=0.1)
40+
# self.begin_ambient_camera_rotation(rate=0.1)
4041
self.pause()
4142

42-
self.stop_ambient_camera_rotation()
43-
self.move_camera(phi=75 * DEGREES, theta=30 * DEGREES)
43+
# self.stop_ambient_camera_rotation()
44+
# self.move_camera(phi=75 * DEGREES, theta=30 * DEGREES)
4445
self.wait()
4546

4647
self.start_loop()

manim_slides/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
def cli():
1313
pass
1414

15+
1516
cli.add_command(present)
1617
cli.add_command(wizard)
1718
cli.add_command(init)

manim_slides/present.py

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,33 @@ class State(Enum):
2121
END = 3
2222

2323
def __str__(self):
24-
if self.value == 0: return "Playing"
25-
if self.value == 1: return "Paused"
26-
if self.value == 2: return "Wait"
27-
if self.value == 3: return "End"
24+
if self.value == 0:
25+
return "Playing"
26+
if self.value == 1:
27+
return "Paused"
28+
if self.value == 2:
29+
return "Wait"
30+
if self.value == 3:
31+
return "End"
2832
return "..."
2933

34+
3035
def now():
3136
return round(time.time() * 1000)
3237

38+
3339
def fix_time(x):
3440
return x if x > 0 else 1
3541

42+
3643
class Presentation:
3744
def __init__(self, config, last_frame_next: bool = False):
3845
self.last_frame_next = last_frame_next
3946
self.slides = config["slides"]
4047
self.files = config["files"]
4148

49+
print(self.slides)
50+
4251
self.lastframe = []
4352

4453
self.caps = [None for _ in self.files]
@@ -48,14 +57,15 @@ def __init__(self, config, last_frame_next: bool = False):
4857
def add_last_slide(self):
4958
last_slide_end = self.slides[-1]["end_animation"]
5059
last_animation = len(self.files)
51-
self.slides.append(dict(
52-
start_animation = last_slide_end,
53-
end_animation = last_animation,
54-
type = "last",
55-
number = len(self.slides) + 1,
56-
terminated = False
57-
))
58-
60+
self.slides.append(
61+
dict(
62+
start_animation=last_slide_end,
63+
end_animation=last_animation,
64+
type="last",
65+
number=len(self.slides) + 1,
66+
terminated=False,
67+
)
68+
)
5969

6070
def reset(self):
6171
self.current_animation = 0
@@ -78,7 +88,7 @@ def rewind_slide(self):
7888
self.current_animation = self.current_slide["start_animation"]
7989
self.current_cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
8090

81-
def load_this_cap(self,cap_number):
91+
def load_this_cap(self, cap_number):
8292
if self.caps[cap_number] == None:
8393
# unload other caps
8494
for i in range(len(self.caps)):
@@ -135,7 +145,10 @@ def update_state(self, state):
135145
self.rewind_slide()
136146
elif self.current_slide["type"] == "last":
137147
self.current_slide["terminated"] = True
138-
elif self.current_slide["type"] == "last" and self.current_slide["end_animation"] == self.current_animation:
148+
elif (
149+
self.current_slide["type"] == "last"
150+
and self.current_slide["end_animation"] == self.current_animation
151+
):
139152
state = State.WAIT
140153
else:
141154
# Play next video!
@@ -162,15 +175,19 @@ def __init__(self, presentations, config, start_paused=False, fullscreen=False):
162175

163176
if fullscreen:
164177
cv2.namedWindow("Video", cv2.WND_PROP_FULLSCREEN)
165-
cv2.setWindowProperty("Video", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
178+
cv2.setWindowProperty(
179+
"Video", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN
180+
)
166181

167182
@property
168183
def current_presentation(self):
169184
return self.presentations[self.current_presentation_i]
170185

171186
def run(self):
172187
while True:
173-
self.lastframe, self.state = self.current_presentation.update_state(self.state)
188+
self.lastframe, self.state = self.current_presentation.update_state(
189+
self.state
190+
)
174191
if self.state == State.PLAYING or self.state == State.PAUSED:
175192
if self.start_paused:
176193
self.state = State.PAUSED
@@ -200,39 +217,34 @@ def show_info(self):
200217
info,
201218
f"Animation: {self.current_presentation.current_animation}",
202219
(grid_x[0], grid_y[0]),
203-
*font_args
204-
)
205-
cv2.putText(
206-
info,
207-
f"State: {self.state}",
208-
(grid_x[1], grid_y[0]),
209-
*font_args
220+
*font_args,
210221
)
222+
cv2.putText(info, f"State: {self.state}", (grid_x[1], grid_y[0]), *font_args)
211223

212224
cv2.putText(
213225
info,
214226
f"Slide {self.current_presentation.current_slide['number']}/{len(self.current_presentation.slides)}",
215227
(grid_x[0], grid_y[1]),
216-
*font_args
228+
*font_args,
217229
)
218230
cv2.putText(
219231
info,
220232
f"Slide Type: {self.current_presentation.current_slide['type']}",
221233
(grid_x[1], grid_y[1]),
222-
*font_args
234+
*font_args,
223235
)
224236

225237
cv2.putText(
226238
info,
227239
f"Scene {self.current_presentation_i + 1}/{len(self.presentations)}",
228-
((grid_x[0]+grid_x[1])//2, grid_y[2]),
229-
*font_args
240+
((grid_x[0] + grid_x[1]) // 2, grid_y[2]),
241+
*font_args,
230242
)
231243

232244
cv2.imshow("Info", info)
233245

234246
def handle_key(self):
235-
sleep_time = math.ceil(1000/self.current_presentation.fps)
247+
sleep_time = math.ceil(1000 / self.current_presentation.fps)
236248
key = cv2.waitKeyEx(fix_time(sleep_time - self.lag))
237249

238250
if self.config.QUIT.match(key):
@@ -241,7 +253,9 @@ def handle_key(self):
241253
self.state = State.PAUSED
242254
elif self.state == State.PAUSED and self.config.PLAY_PAUSE.match(key):
243255
self.state = State.PLAYING
244-
elif self.state == State.WAIT and (self.config.CONTINUE.match(key) or self.config.PLAY_PAUSE.match(key)):
256+
elif self.state == State.WAIT and (
257+
self.config.CONTINUE.match(key) or self.config.PLAY_PAUSE.match(key)
258+
):
245259
self.current_presentation.next()
246260
self.state = State.PLAYING
247261
elif self.state == State.PLAYING and self.config.CONTINUE.match(key):
@@ -258,7 +272,6 @@ def handle_key(self):
258272
self.current_presentation.rewind_slide()
259273
self.state = State.PLAYING
260274

261-
262275
def quit(self):
263276
cv2.destroyAllWindows()
264277
sys.exit()
@@ -267,10 +280,19 @@ def quit(self):
267280
@click.command()
268281
@click.argument("scenes", nargs=-1)
269282
@config_path_option
270-
@click.option("--folder", default=FOLDER_PATH, type=click.Path(exists=True, file_okay=False), help="Set slides folder.")
283+
@click.option(
284+
"--folder",
285+
default=FOLDER_PATH,
286+
type=click.Path(exists=True, file_okay=False),
287+
help="Set slides folder.",
288+
)
271289
@click.option("--start-paused", is_flag=True, help="Start paused.")
272290
@click.option("--fullscreen", is_flag=True, help="Fullscreen mode.")
273-
@click.option("--last-frame-next", is_flag=True, help="Show the next animation first frame as last frame (hack).")
291+
@click.option(
292+
"--last-frame-next",
293+
is_flag=True,
294+
help="Show the next animation first frame as last frame (hack).",
295+
)
274296
@click.help_option("-h", "--help")
275297
def present(scenes, config_path, folder, start_paused, fullscreen, last_frame_next):
276298
"""Present the different scenes"""
@@ -279,7 +301,9 @@ def present(scenes, config_path, folder, start_paused, fullscreen, last_frame_ne
279301
for scene in scenes:
280302
config_file = os.path.join(folder, f"{scene}.json")
281303
if not os.path.exists(config_file):
282-
raise Exception(f"File {config_file} does not exist, check the scene name and make sure to use Slide as your scene base class")
304+
raise Exception(
305+
f"File {config_file} does not exist, check the scene name and make sure to use Slide as your scene base class"
306+
)
283307
config = json.load(open(config_file))
284308
presentations.append(Presentation(config, last_frame_next=last_frame_next))
285309

@@ -288,5 +312,7 @@ def present(scenes, config_path, folder, start_paused, fullscreen, last_frame_ne
288312
else:
289313
config = Config()
290314

291-
display = Display(presentations, config=config, start_paused=start_paused, fullscreen=fullscreen)
315+
display = Display(
316+
presentations, config=config, start_paused=start_paused, fullscreen=fullscreen
317+
)
292318
display.run()

manim_slides/slide.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class Slide(Scene):
1111
def __init__(self, *args, output_folder=FOLDER_PATH, **kwargs):
12-
super(Slide, self).__init__(*args, **kwargs)
12+
super().__init__(*args, **kwargs)
1313
self.output_folder = output_folder
1414
self.slides = list()
1515
self.current_slide = 1
@@ -18,16 +18,19 @@ def __init__(self, *args, output_folder=FOLDER_PATH, **kwargs):
1818
self.pause_start_animation = 0
1919

2020
def play(self, *args, **kwargs):
21-
super(Slide, self).play(*args, **kwargs)
21+
print("PLAY", *args, kwargs.items())
22+
super().play(*args, **kwargs)
2223
self.current_animation += 1
2324

2425
def pause(self):
25-
self.slides.append(dict(
26-
type="slide",
27-
start_animation=self.pause_start_animation,
28-
end_animation=self.current_animation,
29-
number=self.current_slide
30-
))
26+
self.slides.append(
27+
dict(
28+
type="slide",
29+
start_animation=self.pause_start_animation,
30+
end_animation=self.current_animation,
31+
number=self.current_slide,
32+
)
33+
)
3134
self.current_slide += 1
3235
self.pause_start_animation = self.current_animation
3336

@@ -36,13 +39,17 @@ def start_loop(self):
3639
self.loop_start_animation = self.current_animation
3740

3841
def end_loop(self):
39-
assert self.loop_start_animation is not None, "You have to start a loop before ending it"
40-
self.slides.append(dict(
41-
type="loop",
42-
start_animation=self.loop_start_animation,
43-
end_animation=self.current_animation,
44-
number=self.current_slide
45-
))
42+
assert (
43+
self.loop_start_animation is not None
44+
), "You have to start a loop before ending it"
45+
self.slides.append(
46+
dict(
47+
type="loop",
48+
start_animation=self.loop_start_animation,
49+
end_animation=self.current_animation,
50+
number=self.current_slide,
51+
)
52+
)
4653
self.current_slide += 1
4754
self.loop_start_animation = None
4855
self.pause_start_animation = self.current_animation
@@ -52,7 +59,7 @@ def render(self, *args, **kwargs):
5259
max_files_cached = config["max_files_cached"]
5360
config["max_files_cached"] = float("inf")
5461

55-
super(Slide, self).render(*args, **kwargs)
62+
super().render(*args, **kwargs)
5663

5764
config["max_files_cached"] = max_files_cached
5865

@@ -78,12 +85,10 @@ def render(self, *args, **kwargs):
7885
shutil.copyfile(src_file, dst_file)
7986
files.append(dst_file)
8087

81-
f = open(os.path.join(self.output_folder, "%s.json" % (scene_name, )), "w")
82-
json.dump(dict(
83-
slides=self.slides,
84-
files=files
85-
), f)
88+
f = open(os.path.join(self.output_folder, "%s.json" % (scene_name,)), "w")
89+
json.dump(dict(slides=self.slides, files=files), f)
8690
f.close()
8791

88-
class ThreeDSlide(ThreeDScene, Slide):
92+
93+
class ThreeDSlide(Slide, ThreeDScene):
8994
pass

manim_slides/wizard.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ def wizard(config_path, force, merge):
2727
"""Launch configuration wizard."""
2828
return _init(config_path, force, merge, skip_interactive=False)
2929

30+
3031
@click.command()
3132
@config_options
3233
def init(config_path, force, merge, skip_interactive=False):
3334
"""Initialize a new default configuration file."""
3435
return _init(config_path, force, merge, skip_interactive=True)
3536

37+
3638
def _init(config_path, force, merge, skip_interactive=False):
3739

3840
if os.path.exists(config_path):
3941
click.secho(f"The `{CONFIG_PATH}` configuration file exists")
4042

4143
if not force and not merge:
42-
choice = click.prompt("Do you want to continue and (o)verwrite / (m)erge it, or (q)uit?", type=click.Choice(["o", "m", "q"], case_sensitive=False))
44+
choice = click.prompt(
45+
"Do you want to continue and (o)verwrite / (m)erge it, or (q)uit?",
46+
type=click.Choice(["o", "m", "q"], case_sensitive=False),
47+
)
4348

4449
force = choice == "o"
4550
merge = choice == "m"
@@ -52,7 +57,6 @@ def _init(config_path, force, merge, skip_interactive=False):
5257
click.secho("Exiting.")
5358
sys.exit(0)
5459

55-
5660
config = Config()
5761

5862
if not skip_interactive:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
python_requires=">=3.7",
3131
install_requires=[
3232
"click>=8.0",
33-
"click-default-group>=1.2"
33+
"click-default-group>=1.2",
3434
"numpy>=1.19.3",
3535
"pydantic>=1.9.1",
3636
"opencv-python>=4.6",

0 commit comments

Comments
 (0)