2015-03-22 16:15:29 -06:00
|
|
|
from PIL import Image
|
|
|
|
from colour import Color
|
|
|
|
import numpy as np
|
2015-03-26 22:49:22 -06:00
|
|
|
import itertools as it
|
2015-03-22 16:15:29 -06:00
|
|
|
import warnings
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import copy
|
2015-12-31 09:25:36 -08:00
|
|
|
from tqdm import tqdm as ProgressDisplay
|
2015-10-09 19:53:38 -07:00
|
|
|
import inspect
|
2016-02-23 22:29:32 -08:00
|
|
|
import subprocess as sp
|
2015-03-22 16:15:29 -06:00
|
|
|
|
|
|
|
from helpers import *
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2016-02-23 22:29:32 -08:00
|
|
|
from camera import Camera
|
2015-06-09 11:26:12 -07:00
|
|
|
from tk_scene import TkSceneRoot
|
2015-10-28 16:03:33 -07:00
|
|
|
from mobject import Mobject
|
2015-03-22 16:15:29 -06:00
|
|
|
|
|
|
|
class Scene(object):
|
2015-09-28 16:25:18 -07:00
|
|
|
DEFAULT_CONFIG = {
|
2016-02-27 13:33:46 -08:00
|
|
|
"shape" : (DEFAULT_HEIGHT, DEFAULT_WIDTH),
|
|
|
|
"frame_duration" : DEFAULT_FRAME_DURATION,
|
|
|
|
"construct_args" : [],
|
|
|
|
"background" : None,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
|
|
|
def __init__(self, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_config(self, kwargs)
|
2016-02-23 22:29:32 -08:00
|
|
|
self.camera = Camera(
|
2016-02-27 13:33:46 -08:00
|
|
|
pixel_shape = self.shape,
|
2016-02-23 22:29:32 -08:00
|
|
|
background = self.background
|
|
|
|
)
|
2015-12-15 11:31:19 -08:00
|
|
|
self.frames = []
|
2015-10-29 13:45:28 -07:00
|
|
|
self.mobjects = []
|
2015-12-31 09:25:36 -08:00
|
|
|
self.num_animations = 0
|
2015-10-29 13:45:28 -07:00
|
|
|
|
2015-09-28 16:25:18 -07:00
|
|
|
self.construct(*self.construct_args)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
def construct(self):
|
|
|
|
pass #To be implemented in subclasses
|
2015-03-22 16:15:29 -06:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-10-08 11:14:55 -07:00
|
|
|
if hasattr(self, "name"):
|
|
|
|
return self.name
|
2015-10-10 18:48:54 -07:00
|
|
|
return self.__class__.__name__ + \
|
|
|
|
self.args_to_string(*self.construct_args)
|
2015-10-12 19:39:46 -07:00
|
|
|
|
2015-04-03 16:41:25 -07:00
|
|
|
def set_name(self, name):
|
|
|
|
self.name = name
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
2015-04-03 16:41:25 -07:00
|
|
|
|
2015-10-29 13:45:28 -07:00
|
|
|
def get_frame(self):
|
2016-02-23 22:29:32 -08:00
|
|
|
return self.camera.get_image()
|
2015-10-29 13:45:28 -07:00
|
|
|
|
2015-03-22 16:15:29 -06:00
|
|
|
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.
|
|
|
|
"""
|
2015-10-29 13:45:28 -07:00
|
|
|
if not all_elements_are_instances(mobjects, Mobject):
|
|
|
|
raise Exception("Adding something which is not a mobject")
|
|
|
|
old_mobjects = filter(lambda m : m not in mobjects, self.mobjects)
|
|
|
|
self.mobjects = old_mobjects + list(mobjects)
|
2016-02-23 22:29:32 -08:00
|
|
|
self.camera.capture_mobjects(mobjects)
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
2015-03-22 16:15:29 -06:00
|
|
|
|
2015-11-02 19:09:55 -08:00
|
|
|
def add_mobjects_among(self, values):
|
2015-10-09 19:53:38 -07:00
|
|
|
"""
|
|
|
|
So a scene can just add all mobjects it's defined up to that point
|
2015-11-02 19:09:55 -08:00
|
|
|
by calling add_mobjects_among(locals().values())
|
2015-10-09 19:53:38 -07:00
|
|
|
"""
|
2015-11-02 19:09:55 -08:00
|
|
|
mobjects = filter(lambda x : isinstance(x, Mobject), values)
|
|
|
|
self.add(*mobjects)
|
2015-11-02 13:03:01 -08:00
|
|
|
return self
|
2015-10-09 19:53:38 -07:00
|
|
|
|
2015-03-22 16:15:29 -06:00
|
|
|
def remove(self, *mobjects):
|
2015-10-29 13:45:28 -07:00
|
|
|
if not all_elements_are_instances(mobjects, Mobject):
|
|
|
|
raise Exception("Removing something which is not a mobject")
|
|
|
|
mobjects = filter(lambda m : m in self.mobjects, mobjects)
|
2015-11-02 19:09:55 -08:00
|
|
|
if len(mobjects) == 0:
|
2015-10-29 13:45:28 -07:00
|
|
|
return
|
|
|
|
self.mobjects = filter(lambda m : m not in mobjects, self.mobjects)
|
|
|
|
self.repaint_mojects()
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
|
|
|
|
2015-10-12 19:39:46 -07:00
|
|
|
def bring_to_front(self, mobject):
|
|
|
|
self.add(mobject)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def bring_to_back(self, mobject):
|
|
|
|
self.remove(mobject)
|
|
|
|
self.mobjects = [mobject] + self.mobjects
|
|
|
|
return self
|
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
def clear(self):
|
2015-10-29 13:45:28 -07:00
|
|
|
self.mobjects = []
|
2015-06-10 22:00:35 -07:00
|
|
|
return self
|
2015-03-30 17:51:26 -07:00
|
|
|
|
2015-10-29 13:45:28 -07:00
|
|
|
def repaint_mojects(self):
|
2016-02-23 22:29:32 -08:00
|
|
|
self.camera.reset()
|
|
|
|
self.camera.capture_mobjects(self.mobjects)
|
2015-10-06 15:28:21 -07:00
|
|
|
return self
|
|
|
|
|
2016-02-27 12:44:52 -08:00
|
|
|
def align_run_times(self, *animations, **kwargs):
|
2015-04-03 16:41:25 -07:00
|
|
|
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)
|
2016-02-27 12:44:52 -08:00
|
|
|
return animations
|
|
|
|
|
|
|
|
def separate_static_and_moving_mobjects(self, *animations):
|
2015-11-02 19:09:55 -08:00
|
|
|
moving_mobjects = [
|
|
|
|
mobject
|
|
|
|
for anim in animations
|
2015-12-22 11:02:58 -08:00
|
|
|
for mobject in anim.mobject.submobject_family()
|
2015-11-02 19:09:55 -08:00
|
|
|
]
|
2015-11-02 13:03:01 -08:00
|
|
|
bundle = Mobject(*self.mobjects)
|
|
|
|
static_mobjects = filter(
|
|
|
|
lambda m : m not in moving_mobjects,
|
2015-12-22 11:02:58 -08:00
|
|
|
bundle.submobject_family()
|
2015-11-02 13:03:01 -08:00
|
|
|
)
|
2016-02-27 12:44:52 -08:00
|
|
|
return moving_mobjects, static_mobjects
|
2015-03-22 16:15:29 -06:00
|
|
|
|
2016-02-27 12:44:52 -08:00
|
|
|
def get_time_progression(self, animations):
|
|
|
|
run_time = animations[0].run_time
|
2015-12-31 09:25:36 -08:00
|
|
|
times = np.arange(0, run_time, self.frame_duration)
|
|
|
|
time_progression = ProgressDisplay(times)
|
|
|
|
time_progression.set_description(
|
|
|
|
"Animation %d: "%self.num_animations + \
|
2016-01-09 20:09:11 -08:00
|
|
|
str(animations[0]) + \
|
|
|
|
(", etc." if len(animations) > 1 else "")
|
2015-12-31 09:25:36 -08:00
|
|
|
)
|
2016-02-27 12:44:52 -08:00
|
|
|
return time_progression
|
|
|
|
|
|
|
|
def play(self, *animations, **kwargs):
|
|
|
|
if len(animations) == 0:
|
|
|
|
raise Warning("Called Scene.play with no animations")
|
|
|
|
return
|
|
|
|
self.num_animations += 1
|
|
|
|
animations = self.align_run_times(*animations, **kwargs)
|
|
|
|
moving_mobjects, static_mobjects = \
|
|
|
|
self.separate_static_and_moving_mobjects(*animations)
|
|
|
|
self.camera.reset()
|
|
|
|
self.camera.capture_mobjects(
|
|
|
|
static_mobjects,
|
|
|
|
include_sub_mobjects = False
|
|
|
|
)
|
|
|
|
static_image = self.camera.get_image()
|
|
|
|
|
|
|
|
for t in self.get_time_progression(animations):
|
2015-04-03 16:41:25 -07:00
|
|
|
for animation in animations:
|
|
|
|
animation.update(t / animation.run_time)
|
2016-02-27 13:33:46 -08:00
|
|
|
self.camera.capture_mobjects(moving_mobjects)
|
|
|
|
frame = self.camera.get_image()
|
|
|
|
|
|
|
|
self.frames.append(frame)
|
2016-02-23 22:29:32 -08:00
|
|
|
self.camera.set_image(static_image)
|
2015-04-03 16:41:25 -07:00
|
|
|
for animation in animations:
|
|
|
|
animation.clean_up()
|
2016-01-04 10:15:04 -08:00
|
|
|
self.add(*[anim.mobject for anim in animations])
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
2015-03-22 16:15:29 -06:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
def play_over_time_range(self, t0, t1, *animations):
|
2015-06-27 04:49:10 -07:00
|
|
|
needed_scene_time = max(abs(t0), abs(t1))
|
|
|
|
existing_scene_time = len(self.frames)*self.frame_duration
|
|
|
|
if existing_scene_time < needed_scene_time:
|
|
|
|
self.dither(needed_scene_time - existing_scene_time)
|
|
|
|
existing_scene_time = needed_scene_time
|
|
|
|
#So negative values may be used
|
|
|
|
if t0 < 0:
|
|
|
|
t0 = float(t0)%existing_scene_time
|
|
|
|
if t1 < 0:
|
|
|
|
t1 = float(t1)%existing_scene_time
|
|
|
|
t0, t1 = min(t0, t1), max(t0, t1)
|
|
|
|
|
2015-08-17 13:19:34 -07:00
|
|
|
moving_mobjects = [anim.mobject for anim in animations]
|
2015-06-27 04:49:10 -07:00
|
|
|
for t in np.arange(t0, t1, self.frame_duration):
|
2015-08-17 13:19:34 -07:00
|
|
|
for animation in animations:
|
|
|
|
animation.update((t-t0)/(t1 - t0))
|
2015-06-27 04:49:10 -07:00
|
|
|
index = int(t/self.frame_duration)
|
2016-02-23 22:29:32 -08:00
|
|
|
self.camera.set_image(self.frames[index])
|
|
|
|
self.camera.capture_mobjects(moving_mobjects)
|
|
|
|
self.frames[index] = self.camera.get_image()
|
2015-08-17 13:19:34 -07:00
|
|
|
for animation in animations:
|
|
|
|
animation.clean_up()
|
2016-02-23 22:29:32 -08:00
|
|
|
self.repaint_mojects()
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
2015-04-16 21:09:12 -07:00
|
|
|
|
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)
|
2015-06-09 11:26:12 -07:00
|
|
|
return self
|
2015-03-22 16:15:29 -06:00
|
|
|
|
2016-01-30 14:44:45 -08:00
|
|
|
def repeat_frames(self, num):
|
2015-08-07 18:10:00 -07:00
|
|
|
self.frames = self.frames*num
|
|
|
|
return self
|
|
|
|
|
2016-01-30 14:44:45 -08:00
|
|
|
def reverse_frames(self):
|
|
|
|
self.frames.reverse()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def invert_colors(self):
|
|
|
|
white_frame = 255*np.ones(self.get_frame().shape, dtype = 'uint8')
|
|
|
|
self.frames = [
|
|
|
|
white_frame-frame
|
|
|
|
for frame in self.frames
|
|
|
|
]
|
|
|
|
return self
|
|
|
|
|
2015-06-09 11:26:12 -07:00
|
|
|
def show_frame(self):
|
2015-04-03 16:41:25 -07:00
|
|
|
Image.fromarray(self.get_frame()).show()
|
|
|
|
|
2015-06-09 11:26:12 -07:00
|
|
|
def preview(self):
|
|
|
|
TkSceneRoot(self)
|
|
|
|
|
2015-06-19 08:31:02 -07:00
|
|
|
def save_image(self, directory = MOVIE_DIR, name = None):
|
2015-12-23 14:51:19 -08:00
|
|
|
path = os.path.join(directory, "images")
|
|
|
|
file_name = (name or str(self)) + ".png"
|
|
|
|
full_path = os.path.join(path, file_name)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
Image.fromarray(self.get_frame()).save(full_path)
|
2015-05-17 15:08:51 -07:00
|
|
|
|
2016-02-23 22:29:32 -08:00
|
|
|
def get_movie_file_path(self, name, extension):
|
|
|
|
file_path = os.path.join(MOVIE_DIR, name)
|
|
|
|
if not file_path.endswith(extension):
|
|
|
|
file_path += extension
|
|
|
|
directory = os.path.split(file_path)[0]
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
return file_path
|
|
|
|
|
|
|
|
def write_to_movie(self, name = None):
|
|
|
|
if len(self.frames) == 0:
|
|
|
|
print "No frames, I'll just save an image instead"
|
|
|
|
self.show_frame()
|
|
|
|
self.save_image(name = name)
|
|
|
|
return
|
|
|
|
if name is None:
|
|
|
|
name = str(self)
|
|
|
|
|
|
|
|
file_path = self.get_movie_file_path(name, ".mp4")
|
|
|
|
print "Writing to %s"%file_path
|
|
|
|
|
|
|
|
fps = int(1/self.frame_duration)
|
2016-02-27 13:33:46 -08:00
|
|
|
dim = tuple(reversed(self.shape))
|
2016-02-23 22:29:32 -08:00
|
|
|
|
|
|
|
command = [
|
|
|
|
FFMPEG_BIN,
|
|
|
|
'-y', # overwrite output file if it exists
|
|
|
|
'-f', 'rawvideo',
|
|
|
|
'-vcodec','rawvideo',
|
|
|
|
'-s', '%dx%d'%dim, # size of one frame
|
|
|
|
'-pix_fmt', 'rgb24',
|
|
|
|
'-r', str(fps), # frames per second
|
|
|
|
'-i', '-', # The imput comes from a pipe
|
|
|
|
'-an', # Tells FFMPEG not to expect any audio
|
|
|
|
'-vcodec', 'mpeg',
|
|
|
|
'-c:v', 'libx264',
|
|
|
|
'-pix_fmt', 'yuv420p',
|
|
|
|
'-loglevel', 'error',
|
|
|
|
file_path,
|
|
|
|
]
|
|
|
|
process = sp.Popen(command, stdin=sp.PIPE)
|
|
|
|
for frame in self.frames:
|
|
|
|
process.stdin.write(frame.tostring())
|
|
|
|
process.stdin.close()
|
|
|
|
process.wait()
|
|
|
|
|
2015-05-07 21:28:02 -07:00
|
|
|
# To list possible args that subclasses have
|
|
|
|
# Elements should always be a tuple
|
|
|
|
args_list = []
|
|
|
|
|
|
|
|
# For subclasses to turn args in the above
|
|
|
|
# list into stings which can be appended to the name
|
|
|
|
@staticmethod
|
|
|
|
def args_to_string(*args):
|
|
|
|
return ""
|
2015-10-29 17:00:46 -07:00
|
|
|
|
2015-10-10 18:48:54 -07:00
|
|
|
@staticmethod
|
|
|
|
def string_to_args(string):
|
|
|
|
raise Exception("string_to_args Not Implemented!")
|
2015-08-17 13:19:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-22 16:15:29 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|