A community-maintained Python framework for creating mathematical animations.
Using Camera class instead of displayer module
Grant Sanderson74517d798d76
diff --git a/brachistochrone/curves.py b/brachistochrone/curves.pyindex 539173cf0..85dfec8d6 100644--- a/brachistochrone/curves.py+++ b/brachistochrone/curves.py@@ -24,7 +24,7 @@ from topics.geometry import \from topics.characters import Randolph, Mathematicianfrom topics.functions import ParametricFunction, FunctionGraphfrom topics.number_line import NumberPlane-from region import Region, region_from_polygon_vertices+from mobject.region import Region, region_from_polygon_verticesfrom scene import SceneRANDY_SCALE_VAL = 0.3diff --git a/brachistochrone/drawing_images.py b/brachistochrone/drawing_images.pyindex 0afd3cf80..f37796d74 100644--- a/brachistochrone/drawing_images.py+++ b/brachistochrone/drawing_images.py@@ -28,7 +28,7 @@ from topics.geometry import \from topics.characters import Randolph, Mathematician, ThoughtBubblefrom topics.functions import ParametricFunctionfrom topics.number_line import NumberPlane-from region import Region, region_from_polygon_vertices+from mobject.region import Region, region_from_polygon_verticesfrom scene import Scenediff --git a/brachistochrone/light.py b/brachistochrone/light.pyindex 77b265e98..7ba181ed9 100644--- a/brachistochrone/light.py+++ b/brachistochrone/light.py@@ -24,7 +24,7 @@ from topics.geometry import \from topics.characters import Randolph, Mathematicianfrom topics.functions import ParametricFunction, FunctionGraphfrom topics.number_line import NumberPlane-from region import Region, region_from_polygon_vertices+from mobject.region import Region, region_from_polygon_verticesfrom scene import Scenefrom brachistochrone.curves import Cycloiddiff --git a/brachistochrone/metaphors.py b/brachistochrone/metaphors.pyindex bfd4d6149..51ce4ca1e 100644--- a/brachistochrone/metaphors.py+++ b/brachistochrone/metaphors.py@@ -24,7 +24,7 @@ from topics.geometry import \from topics.characters import Randolph, Mathematicianfrom topics.functions import ParametricFunction, FunctionGraphfrom topics.number_line import NumberPlane-from region import Region, region_from_polygon_vertices+from mobject.region import Region, region_from_polygon_verticesfrom scene import Scenediff --git a/brachistochrone/wordplay.py b/brachistochrone/wordplay.pyindex fb912bd63..aedc466eb 100644--- a/brachistochrone/wordplay.py+++ b/brachistochrone/wordplay.py@@ -25,7 +25,7 @@ from topics.geometry import \from topics.characters import Randolph, Mathematicianfrom topics.functions import ParametricFunction, FunctionGraphfrom topics.number_line import NumberPlane-from region import Region, region_from_polygon_vertices+from mobject.region import Region, region_from_polygon_verticesfrom scene import Scenefrom generate_logo import LogoGenerationfrom brachistochrone.drawing_images import sort_by_colordiff --git a/camera.py b/camera.pyindex 1c03c582b..e33c7c600 100644--- a/camera.py+++ b/camera.py@@ -54,17 +54,6 @@ class Camera(object):def reset(self):self.set_image(np.array(self.background))- # def paint_region(region, image_array = None, color = None):- # pixels = get_pixels(image_array)- # assert region.shape == pixels.shape[:2]- # if color is None:- # #Random dark color- # rgb = 0.5 * np.random.random(3)- # else:- # rgb = np.array(Color(color).get_rgb())- # pixels[region.bool_grid] = (255*rgb).astype('uint8')- # return pixels-def capture_mobject(self, mobject):return self.capture_mobjects([mobject])@@ -77,10 +66,27 @@ class Camera(object):mobjects = reduce(op.add, all_families, [])for mobject in mobjects:- self.display_points(- mobject.points, mobject.rgbs,- self.adjusted_thickness(mobject.point_thickness)- )+ if mobject.display_mode == "region":+ self.display_region(mobject)+ elif mobject.display_mode == "points":+ self.display_points(+ mobject.points, mobject.rgbs,+ self.adjusted_thickness(mobject.point_thickness)+ )+ else:+ raise Exception("Invalid display mode")++ def display_region(self, region):+ (h, w) = self.pixel_height, self.pixel_width+ scalar = 2*self.space_height / h+ xs = scalar*np.arange(-w/2, w/2)+ ys = -scalar*np.arange(-h/2, h/2)+ x_array = np.dot(np.ones((h, 1)), xs.reshape((1, w)))+ y_array = np.dot(ys.reshape(h, 1), np.ones((1, w)))+ covered = region.condition(x_array, y_array)+ rgb = np.array(Color(region.color).get_rgb())+ rgb = (255*rgb).astype('uint8')+ self.pixel_array[covered] = rgbdef display_points(self, points, rgbs, thickness):if len(points) == 0:@@ -138,31 +144,6 @@ class Camera(object):pixel_coords[:,1] < self.pixel_height,])- # def add_thickness(pixel_indices_and_rgbs, thickness, width, height):- # """- # Imagine dragging each pixel around like a paintbrush in- # a plus-sign-shaped pixel arrangement surrounding it.-- # Pass rgb = None to do nothing to them- # """- # thickness = adjusted_thickness(thickness, width, height)- # original = np.array(pixel_indices_and_rgbs)- # n_extra_columns = pixel_indices_and_rgbs.shape[1] - 2- # for nudge in range(-thickness/2+1, thickness/2+1):- # if nudge == 0:- # continue- # for x, y in [[nudge, 0], [0, nudge]]:- # pixel_indices_and_rgbs = np.append(- # pixel_indices_and_rgbs,- # original+([x, y] + [0]*n_extra_columns),- # axis = 0- # )- # admissibles = (pixel_indices_and_rgbs[:,0] >= 0) & \- # (pixel_indices_and_rgbs[:,0] < width) & \- # (pixel_indices_and_rgbs[:,1] >= 0) & \- # (pixel_indices_and_rgbs[:,1] < height)- # return pixel_indices_and_rgbs[admissibles]-def adjusted_thickness(self, thickness):big_width = PRODUCTION_QUALITY_DISPLAY_CONFIG["width"]big_height = PRODUCTION_QUALITY_DISPLAY_CONFIG["height"]@@ -185,7 +166,18 @@ class Camera(object):+class MovingCamera(Camera):+ """+ Stays in line with the height, width and position+ of a given mobject+ """+ def __init__(self, mobject, **kwargs):+ digest_locals(self)+ Camera.__init__(self, **kwargs)+ def capture_mobjects(self, *args, **kwargs):+ self.space_height = self.mobject.get_height()+ self.space_center = self.mobject.get_center()diff --git a/fluid_flow.py b/fluid_flow.pyindex c2e524990..a948fc6ef 100644--- a/fluid_flow.py+++ b/fluid_flow.py@@ -1,7 +1,7 @@from mobject import Mobjectfrom mobject.image_mobject import MobjectFromRegionfrom mobject.tex_mobject import TextMobject-from region import region_from_polygon_vertices+from mobject.region import region_from_polygon_verticesfrom topics.geometry import Arrow, Dot, Circle, Line, FilledRectanglefrom topics.number_line import NumberPlane, XYZAxesfrom topics.three_dimensions import Spherediff --git a/mobject/mobject.py b/mobject/mobject.pyindex 932a6d5a1..bb519af55 100644--- a/mobject/mobject.py+++ b/mobject/mobject.py@@ -7,7 +7,6 @@ from random import randomfrom copy import deepcopyfrom colour import Color-import displayer as dispfrom helpers import *@@ -20,6 +19,7 @@ class Mobject(object):"color" : WHITE,"point_thickness" : DEFAULT_POINT_THICKNESS,"name" : None,+ "display_mode" : "points"}DIM = 3def __init__(self, *sub_mobjects, **kwargs):@@ -100,7 +100,10 @@ class Mobject(object):return selfdef show(self):- Image.fromarray(disp.paint_mobject(self)).show()+ from camera import Camera+ camera = Camera()+ camera.capture_mobject(self)+ Image.fromarray(camera.get_image()).show()def save_image(self, name = None):Image.fromarray(disp.paint_mobject(self)).save(diff --git a/region.py b/mobject/region.pysimilarity index 72%rename from region.pyrename to mobject/region.pyindex def9b1300..89769fca5 100644--- a/region.py+++ b/mobject/region.py@@ -4,15 +4,16 @@ from PIL import Imageimport cv2from copy import deepcopy+from mobject import Mobject-import displayer as dispfrom helpers import *-class Region(object):- def __init__(self,- condition = None,- shape = None,- ):+#TODO, this whole class should be something vectorized.+class Region(Mobject):+ DEFAULT_CONFIG = {+ "display_mode" : "region"+ }+ def __init__(self, condition = (lambda x, y : True), **kwargs):"""Condition must be a function which takes in two realarrays (representing x and y values of space respectively)@@ -20,35 +21,14 @@ class Region(object):a function from R^2 to {True, False}, but & and | must beused in place of "and" and "or""""- if shape == None:- self.shape = (DEFAULT_HEIGHT, DEFAULT_WIDTH)- else:- self.shape = shape- # self.condition = condition- (h, w) = self.shape- scalar = 2*SPACE_HEIGHT / h- xs = scalar*np.arange(-w/2, w/2)- ys = -scalar*np.arange(-h/2, h/2)- #TODO, do xs and ys really need to be saved in self?- self.xs = np.dot(- np.ones((h, 1)),- xs.reshape((1, w))- )- self.ys = np.dot(- ys.reshape(h, 1),- np.ones((1, w))- )- if condition:- self.bool_grid = condition(self.xs, self.ys)- else:- self.bool_grid = np.ones(self.shape, dtype = 'bool')-- def show(self, color = None):- Image.fromarray(disp.paint_region(self, color = color)).show()+ Mobject.__init__(self, **kwargs)+ self.condition = conditiondef _combine(self, region, op):- assert region.shape == self.shape- self.bool_grid = op(self.bool_grid, region.bool_grid)+ self.condition = lambda x, y : op(+ self.condition(x, y),+ region.condition(x, y)+ )def union(self, region):self._combine(region, lambda bg1, bg2 : bg1 | bg2)diff --git a/old_projects/complex_multiplication_article.py b/old_projects/complex_multiplication_article.pyindex 4a893fcc0..9af406fa5 100644--- a/old_projects/complex_multiplication_article.py+++ b/old_projects/complex_multiplication_article.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scenefrom topics.complex_numbers import *diff --git a/old_projects/counting_in_binary.py b/old_projects/counting_in_binary.pyindex 3d77ccf53..459d30731 100644--- a/old_projects/counting_in_binary.py+++ b/old_projects/counting_in_binary.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scene, SceneFromVideofrom script_wrapper import command_line_create_scenediff --git a/old_projects/eulers_characteristic_formula.py b/old_projects/eulers_characteristic_formula.pyindex b74748b35..20aca735c 100644--- a/old_projects/eulers_characteristic_formula.py+++ b/old_projects/eulers_characteristic_formula.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *import displayer as dispfrom scene import Scene, GraphScenefrom scene.graphs import *diff --git a/old_projects/inventing_math.py b/old_projects/inventing_math.pyindex 8f1a92d8a..430db5eba 100644--- a/old_projects/inventing_math.py+++ b/old_projects/inventing_math.py@@ -11,7 +11,7 @@ from random import samplefrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scene, RearrangeEquationfrom script_wrapper import command_line_create_scenediff --git a/old_projects/inventing_math_images.py b/old_projects/inventing_math_images.pyindex 8d48b2903..81273f873 100644--- a/old_projects/inventing_math_images.py+++ b/old_projects/inventing_math_images.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scenefrom script_wrapper import command_line_create_scenefrom inventing_math import divergent_sum, draw_youdiff --git a/old_projects/moser_intro.py b/old_projects/moser_intro.pyindex 58febd6f5..32bcf4f5c 100644--- a/old_projects/moser_intro.py+++ b/old_projects/moser_intro.py@@ -9,7 +9,7 @@ from copy import deepcopyfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import SceneRADIUS = SPACE_HEIGHT - 0.1diff --git a/old_projects/moser_main.py b/old_projects/moser_main.pyindex 2cb10fdba..26800ea05 100644--- a/old_projects/moser_main.py+++ b/old_projects/moser_main.py@@ -12,7 +12,7 @@ import inspectfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scene, GraphScene, PascalsTriangleScenefrom script_wrapper import command_line_create_scenediff --git a/old_projects/music_and_measure.py b/old_projects/music_and_measure.pyindex 69a86386f..02ee8e60a 100644--- a/old_projects/music_and_measure.py+++ b/old_projects/music_and_measure.py@@ -9,7 +9,7 @@ from fractions import Fraction, gcdfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scenefrom inventing_math import Underbracediff --git a/old_projects/playground_counting_in_binary.py b/old_projects/playground_counting_in_binary.pyindex 5e0367308..6ebe7e572 100644--- a/old_projects/playground_counting_in_binary.py+++ b/old_projects/playground_counting_in_binary.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scene, SceneFromVideofrom script_wrapper import command_line_create_scenediff --git a/old_projects/pythagorean_proof.py b/old_projects/pythagorean_proof.pyindex 6d0b5d6af..ba909fc9c 100644--- a/old_projects/pythagorean_proof.py+++ b/old_projects/pythagorean_proof.py@@ -7,7 +7,7 @@ from helpers import *from scene import Scenefrom geometry import Polygon-from region import region_from_polygon_vertices, region_from_line_boundary+from mobject.region import region_from_polygon_vertices, region_from_line_boundaryA_COLOR = BLUEB_COLOR = MAROON_Ddiff --git a/old_projects/tau_poem.py b/old_projects/tau_poem.pyindex bb530b730..8fa136971 100644--- a/old_projects/tau_poem.py+++ b/old_projects/tau_poem.py@@ -9,7 +9,7 @@ import sysfrom animation import *from mobject import *from constants import *-from region import *+from mobject.region import *from scene import Scenefrom script_wrapper import command_line_create_scenefrom generate_logo import LogoGenerationdiff --git a/scene/scene.py b/scene/scene.pyindex 6081e33fa..ccd74c106 100644--- a/scene/scene.py+++ b/scene/scene.py@@ -103,32 +103,30 @@ class Scene(object):self.camera.capture_mobjects(self.mobjects)return self- def play(self, *animations, **kwargs):- self.num_animations += 1+ def align_run_times(self, *animations, **kwargs):if "run_time" in kwargs:run_time = kwargs["run_time"]else:run_time = animations[0].run_timefor animation in animations:animation.set_run_time(run_time)+ return animations++ def separate_static_and_moving_mobjects(self, *animations):moving_mobjects = [mobjectfor anim in animationsfor mobject in anim.mobject.submobject_family()]-bundle = Mobject(*self.mobjects)static_mobjects = filter(lambda m : m not in moving_mobjects,bundle.submobject_family())- self.camera.reset()- self.camera.capture_mobjects(- static_mobjects,- include_sub_mobjects = False- )- static_image = self.camera.get_image()+ return moving_mobjects, static_mobjects+ def get_time_progression(self, animations):+ run_time = animations[0].run_timetimes = np.arange(0, run_time, self.frame_duration)time_progression = ProgressDisplay(times)time_progression.set_description(@@ -136,7 +134,24 @@ class Scene(object):str(animations[0]) + \(", etc." if len(animations) > 1 else ""))- for t in time_progression:+ 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):for animation in animations:animation.update(t / animation.run_time)self.camera.capture_mobjects([