From a7af5e72c6004384aaaceb8cb8fcd8ad9dc848ee Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sat, 9 Jan 2021 10:48:45 -0800 Subject: [PATCH] Added Mobject.get_grid --- manimlib/mobject/mobject.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/manimlib/mobject/mobject.py b/manimlib/mobject/mobject.py index 3bd5afe5..ef5612eb 100644 --- a/manimlib/mobject/mobject.py +++ b/manimlib/mobject/mobject.py @@ -1001,8 +1001,11 @@ class Mobject(object): def arrange_in_grid(self, n_rows=None, n_cols=None, buff=None, - h_buff=MED_LARGE_BUFF, - v_buff=MED_LARGE_BUFF, + h_buff=None, + v_buff=None, + buff_ratio=None, + h_buff_ratio=0.5, + v_buff_ratio=0.5, aligned_edge=ORIGIN, fill_rows_first=True): submobs = self.submobjects @@ -1012,9 +1015,18 @@ class Mobject(object): n_rows = len(submobs) // n_cols if n_cols is None: n_cols = len(submobs) // n_rows + if buff is not None: h_buff = buff v_buff = buff + else: + if buff_ratio is not None: + v_buff_ratio = buff_ratio + h_buff_ratio = buff_ratio + if h_buff is None: + h_buff = h_buff_ratio * self[0].get_width() + if v_buff is None: + v_buff = v_buff_ratio * self[0].get_height() x_unit = h_buff + max([sm.get_width() for sm in submobs]) y_unit = v_buff + max([sm.get_height() for sm in submobs]) @@ -1029,6 +1041,19 @@ class Mobject(object): self.center() return self + def get_grid(self, n_rows, n_cols, height=None, **kwargs): + """ + Returns a new mobject containing multiple copies of this one + arranged in a grid + """ + grid = self.get_group_class()( + *(self.copy() for n in range(n_rows * n_cols)) + ) + grid.arrange_in_grid(n_rows, n_cols, **kwargs) + if height is not None: + grid.set_height(height) + return grid + def sort(self, point_to_num_func=lambda p: p[0], submob_func=None): if submob_func is not None: self.submobjects.sort(key=submob_func)