3b1b-manim/scene.py

129 lines
3.7 KiB
Python
Raw Normal View History

from PIL import Image
from colour import Color
import numpy as np
2015-03-26 22:49:22 -06:00
import itertools as it
import warnings
import time
import os
import copy
import progressbar
import inspect
from helpers import *
from mobject import *
2015-04-03 16:41:25 -07:00
from animation import *
import displayer as disp
class Scene(object):
def __init__(self,
2015-03-26 22:49:22 -06:00
name = None,
frame_duration = DEFAULT_FRAME_DURATION,
2015-03-26 22:49:22 -06:00
background = None,
height = DEFAULT_HEIGHT,
2015-04-03 16:41:25 -07:00
width = DEFAULT_WIDTH,
start_dither_time = DEFAULT_DITHER_TIME):
self.frame_duration = frame_duration
self.frames = []
2015-03-26 22:49:22 -06:00
self.mobjects = []
if background:
self.original_background = np.array(background)
2015-03-26 22:49:22 -06:00
#TODO, Error checking?
else:
2015-04-03 16:41:25 -07:00
self.original_background = np.zeros(
(height, width, 3),
dtype = 'uint8'
)
self.background = self.original_background
self.shape = self.background.shape[:2]
2015-04-14 17:55:25 -07:00
#TODO, space shape
self.name = name
def __str__(self):
return self.name or "Babadinook" #TODO
2015-04-03 16:41:25 -07:00
def set_name(self, name):
self.name = name
def add(self, *mobjects):
2015-03-26 22:49:22 -06:00
"""
Mobjects will be displayed, from background to foreground,
in the order with which they are entered.
"""
for mobject in mobjects:
#In case it's already in there, it should
#now be closer to the foreground.
self.remove(mobject)
self.mobjects.append(mobject)
def remove(self, *mobjects):
2015-03-26 22:49:22 -06:00
for mobject in mobjects:
while mobject in self.mobjects:
self.mobjects.remove(mobject)
def highlight_region(self, region, color = None):
self.background = disp.paint_region(
region,
image_array = self.background,
color = color,
)
def reset_background(self):
self.background = self.original_background
2015-04-03 16:41:25 -07:00
def animate(self, *animations, **kwargs):
if "run_time" in kwargs:
run_time = kwargs["run_time"]
else:
run_time = animations[0].run_time
for animation in animations:
animation.set_run_time(run_time)
moving_mobjects = [anim.mobject for anim in animations]
2015-03-26 22:49:22 -06:00
self.remove(*moving_mobjects)
background = self.get_frame()
2015-04-03 16:41:25 -07:00
print "Generating " + ", ".join(map(str, animations))
progress_bar = progressbar.ProgressBar(maxval=run_time)
progress_bar.start()
for t in np.arange(0, run_time, self.frame_duration):
progress_bar.update(t)
2015-03-26 22:49:22 -06:00
new_frame = background
2015-04-03 16:41:25 -07:00
for animation in animations:
animation.update(t / animation.run_time)
new_frame = disp.paint_mobject(animation.mobject, new_frame)
2015-03-26 22:49:22 -06:00
self.frames.append(new_frame)
2015-04-03 16:41:25 -07:00
for animation in animations:
animation.clean_up()
2015-03-26 22:49:22 -06:00
self.add(*moving_mobjects)
progress_bar.finish()
2015-03-26 22:49:22 -06:00
def get_frame(self):
frame = self.background
for mob in self.mobjects:
frame = disp.paint_mobject(mob, frame)
return frame
2015-03-26 22:49:22 -06:00
def dither(self, duration = DEFAULT_DITHER_TIME):
self.frames += [self.get_frame()]*int(duration / self.frame_duration)
def write_to_gif(self, name = None, end_dither_time = DEFAULT_DITHER_TIME):
2015-03-26 22:49:22 -06:00
self.dither(end_dither_time)
disp.write_to_gif(self, name or str(self))
def write_to_movie(self, name = None, end_dither_time = DEFAULT_DITHER_TIME):
2015-03-26 22:49:22 -06:00
self.dither(end_dither_time)
disp.write_to_movie(self, name or str(self))
2015-04-03 16:41:25 -07:00
def show_frame(self):
Image.fromarray(self.get_frame()).show()