saving animation to file

main
Nekkowe! 2023-10-01 12:24:44 +02:00
parent 58023f2918
commit 470223d7de
2 changed files with 23 additions and 4 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__ __pycache__
output

24
main.py
View File

@ -1,15 +1,26 @@
from sim import Environment, CAMSReverseAndSidestepAgent, Direction from sim import Environment, CAMSReverseAndSidestepAgent, Direction
import time import datetime
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import os
from pathlib import Path
from matplotlib.animation import FuncAnimation from matplotlib.animation import FuncAnimation
ANIMATION_OUTPUT_DIRECTORY = "./output"
STEPS = 1000 STEPS = 1000
np.random.seed(12345) np.random.seed(12345)
fig, ax = plt.subplots() fig, ax = plt.subplots()
env = Environment((100, 100)) env = Environment((100, 100))
start_time = datetime.datetime.now()
start_time_timestamp = start_time.strftime("%Y-%m-%dT%H%M%S%z")
saved_animation_filename = "CAMS-replicator-simulation-{}.mp4".format(start_time_timestamp)
output_path = os.path.join(ANIMATION_OUTPUT_DIRECTORY, saved_animation_filename)
if not os.path.exists(ANIMATION_OUTPUT_DIRECTORY):
os.makedirs(ANIMATION_OUTPUT_DIRECTORY)
first_agent = CAMSReverseAndSidestepAgent( first_agent = CAMSReverseAndSidestepAgent(
environment=env, environment=env,
position=(50,50), position=(50,50),
@ -29,6 +40,13 @@ ani = FuncAnimation(
fig=fig, fig=fig,
func=update, func=update,
frames=STEPS, frames=STEPS,
blit=True) blit=True,
interval=1000/60,
repeat=False
)
plt.show() #plt.show()
print("Saving animation, this might take some time...")
ani.save(output_path, progress_callback=(lambda i, n: print(f'Saving frame {i}/{n}')))
print("Animation saved.")