A community-maintained Python framework for creating mathematical animations.
Improve creation time for large Text Mobjects (#3181)
Kevin Lubickc62c5a3abafc
diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.pyindex 579bbebf2..8793275b6 100644--- a/manim/mobject/svg/svg_mobject.py+++ b/manim/mobject/svg/svg_mobject.py@@ -82,6 +82,12 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):A dictionary with keyword arguments passed to:class:`.VMobjectFromSVGPath` used for importing path elements.If ``None`` (the default), no additional arguments are passed.+ use_svg_cache+ If True (default), the svg inputs (e.g. file_name, settings)+ will be used as a key and a copy of the created mobject will+ be saved using that key to be quickly retrieved if the same+ inputs need be processed later. For large SVGs which are used+ only once, this can be omitted to improve performance.kwargsFurther arguments passed to the parent class."""@@ -101,6 +107,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):stroke_width: float | None = None,svg_default: dict | None = None,path_string_config: dict | None = None,+ use_svg_cache: bool = True,**kwargs,):super().__init__(color=None, stroke_color=None, fill_color=None, **kwargs)@@ -135,7 +142,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):path_string_config = {}self.path_string_config = path_string_config- self.init_svg_mobject()+ self.init_svg_mobject(use_svg_cache=use_svg_cache)self.set_style(fill_color=fill_color,@@ -146,7 +153,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):)self.move_into_position()- def init_svg_mobject(self) -> None:+ def init_svg_mobject(self, use_svg_cache: bool) -> None:"""Checks whether the SVG has already been imported andgenerates it if not.@@ -154,14 +161,16 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):--------:meth:`.SVGMobject.generate_mobject`"""- hash_val = hash_obj(self.hash_seed)- if hash_val in SVG_HASH_TO_MOB_MAP:- mob = SVG_HASH_TO_MOB_MAP[hash_val].copy()- self.add(*mob)- return+ if use_svg_cache:+ hash_val = hash_obj(self.hash_seed)+ if hash_val in SVG_HASH_TO_MOB_MAP:+ mob = SVG_HASH_TO_MOB_MAP[hash_val].copy()+ self.add(*mob)+ returnself.generate_mobject()- SVG_HASH_TO_MOB_MAP[hash_val] = self.copy()+ if use_svg_cache:+ SVG_HASH_TO_MOB_MAP[hash_val] = self.copy()@propertydef hash_seed(self) -> tuple:diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.pyindex 084b9ec7a..a39400f31 100644--- a/manim/mobject/text/text_mobject.py+++ b/manim/mobject/text/text_mobject.py@@ -492,6 +492,7 @@ class Text(SVGMobject):height=height,width=width,should_center=should_center,+ use_svg_cache=False,**kwargs,)self.text = text@@ -504,18 +505,58 @@ class Text(SVGMobject):if len(each.points) == 0:continuepoints = each.points- last = points[0]- each.clear_points()+ curve_start = points[0]+ assert len(curve_start) == self.dim, curve_start+ # Some of the glyphs in this text might not be closed,+ # so we close them by identifying when one curve ends+ # but it is not where the next curve starts.+ # It is more efficient to temporarily create a list+ # of points and add them one at a time, then turn them+ # into a numpy array at the end, rather than creating+ # new numpy arrays every time a point or fixing line+ # is added (which is O(n^2) for numpy arrays).+ closed_curve_points = []+ # OpenGL has points be part of quadratic Bezier curves;+ # Cairo uses cubic Bezier curves.+ if nppc == 3: # RendererType.OPENGL++ def add_line_to(end):+ nonlocal closed_curve_points+ start = closed_curve_points[-1]+ closed_curve_points += [+ start,+ (start + end) / 2,+ end,+ ]++ else: # RendererType.CAIRO++ def add_line_to(end):+ nonlocal closed_curve_points+ start = closed_curve_points[-1]+ closed_curve_points += [+ start,+ (start + start + end) / 3,+ (start + end + end) / 3,+ end,+ ]+for index, point in enumerate(points):- each.append_points([point])+ closed_curve_points.append(point)if (index != len(points) - 1and (index + 1) % nppc == 0and any(point != points[index + 1])):- each.add_line_to(last)- last = points[index + 1]- each.add_line_to(last)+ # Add straight line from last point on this curve to the+ # start point on the next curve. We represent the line+ # as a cubic bezier curve where the two control points+ # are half-way between the start and stop point.+ add_line_to(curve_start)+ curve_start = points[index + 1]+ # Make sure last curve is closed+ add_line_to(curve_start)+ each.points = np.array(closed_curve_points, ndmin=2)# anti-aliasingif height is None and width is None:self.scale(TEXT_MOB_SCALE_FACTOR)@@ -1170,18 +1211,56 @@ class MarkupText(SVGMobject):if len(each.points) == 0:continuepoints = each.points- last = points[0]- each.clear_points()+ curve_start = points[0]+ assert len(curve_start) == self.dim, curve_start+ # Some of the glyphs in this text might not be closed,+ # so we close them by identifying when one curve ends+ # but it is not where the next curve starts.+ # It is more efficient to temporarily create a list+ # of points and add them one at a time, then turn them+ # into a numpy array at the end, rather than creating+ # new numpy arrays every time a point or fixing line+ # is added (which is O(n^2) for numpy arrays).+ closed_curve_points = []+ # OpenGL has points be part of quadratic Bezier curves;+ # Cairo uses cubic Bezier curves.+ if nppc == 3: # RendererType.OPENGL++ def add_line_to(end):+ nonlocal closed_curve_points+ start = closed_curve_points[-1]+ closed_curve_points += [+ start,+ (start + end) / 2,+ end,+ ]++ else: # RendererType.CAIRO++ def add_line_to(end):+ nonlocal closed_curve_points+ start = closed_curve_points[-1]+ closed_curve_points += [+ start,+ (start + start + end) / 3,+ (start + end + end) / 3,+ end,+ ]+for index, point in enumerate(points):- each.append_points([point])+ closed_curve_points.append(point)if (index != len(points) - 1and (index + 1) % nppc == 0and any(point != points[index + 1])):- each.add_line_to(last)- last = points[index + 1]- each.add_line_to(last)+ # Add straight line from last point on this curve to the+ # start point on the next curve.+ add_line_to(curve_start)+ curve_start = points[index + 1]+ # Make sure last curve is closed+ add_line_to(curve_start)+ each.points = np.array(closed_curve_points, ndmin=2)if self.gradient:self.set_color_by_gradient(*self.gradient)