tommy24 commited on
Commit
7cb600e
·
verified ·
1 Parent(s): cf4677b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI
2
  from fastapi.responses import FileResponse
3
  import os
4
  import subprocess
 
5
 
6
  app = FastAPI()
7
 
@@ -11,23 +12,38 @@ def read_root():
11
 
12
  @app.get("/generate")
13
  def generate_animation():
14
- # Define file paths
15
- output_dir = "media"
16
- output_file = os.path.join(output_dir, "ExampleScene.mp4")
17
-
18
- # Ensure output directory exists
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  os.makedirs(output_dir, exist_ok=True)
20
-
21
- # Run the Manim command
22
- subprocess.run([
23
- "manim",
24
- "-pql", "example_scene.py", # 'l' for low quality (quick render)
25
- "ExampleScene",
26
- "--media_dir", output_dir
27
- ])
28
-
29
- # Check if the file exists and return
30
- if os.path.exists(output_file):
31
- return FileResponse(output_file, media_type="video/mp4", filename="example_scene.mp4")
32
  else:
33
- return {"error": "Animation generation failed!"}
 
2
  from fastapi.responses import FileResponse
3
  import os
4
  import subprocess
5
+ from manim import *
6
 
7
  app = FastAPI()
8
 
 
12
 
13
  @app.get("/generate")
14
  def generate_animation():
15
+ class PointMovingOnShapes(Scene):
16
+ def construct(self):
17
+ # Define the circle and dot
18
+ circle = Circle(radius=1, color=BLUE)
19
+ dot = Dot()
20
+ dot2 = dot.copy().shift(RIGHT)
21
+ self.add(dot)
22
+
23
+ # Create a line
24
+ line = Line([3, 0, 0], [5, 0, 0])
25
+ self.add(line)
26
+
27
+ # Animate the circle, dot, and line
28
+ self.play(GrowFromCenter(circle))
29
+ self.play(Transform(dot, dot2))
30
+ self.play(MoveAlongPath(dot, circle), run_time=2, rate_func=linear)
31
+ self.play(Rotating(dot, about_point=[2, 0, 0]), run_time=1.5)
32
+ self.wait()
33
+
34
+ # Directory to save rendered video
35
+ output_dir = "/tmp/manim_output"
36
  os.makedirs(output_dir, exist_ok=True)
37
+
38
+ # Rendering the scene to a video file
39
+ scene = PointMovingOnShapes()
40
+ scene.render(directory=output_dir)
41
+
42
+ # Path to the rendered video file (manim by default saves as .mp4)
43
+ video_path = os.path.join(output_dir, "media", "videos", "PointMovingOnShapes", "1080p60", "PointMovingOnShapes.mp4")
44
+
45
+ # Return the video file as a response
46
+ if os.path.exists(video_path):
47
+ return FileResponse(video_path)
 
48
  else:
49
+ return {"message": "Error: Animation rendering failed."}