Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

OpenGL compatibility via metaclass: svg_mobject, text_mobject, tex_mobject (#1511)

Benjamin Hackldd62bc51940c
diff --git a/manim/mobject/svg/opengl_svg_mobject.py b/manim/mobject/svg/opengl_svg_mobject.py
index 7e7d0c944..411a35465 100644
--- a/manim/mobject/svg/opengl_svg_mobject.py
+++ b/manim/mobject/svg/opengl_svg_mobject.py
@@ -5,7 +5,7 @@ from .opengl_svg_path import OpenGLSVGPathMobject
from .style_utils import cascade_element_style, parse_style
-class OpenGLSVGMobject(OpenGLVMobject, SVGMobject):
+class OpenGLSVGMobject(SVGMobject):
def __init__(
self,
file_name=None,
diff --git a/manim/mobject/svg/opengl_svg_path.py b/manim/mobject/svg/opengl_svg_path.py
index cd6cf530b..638395ee6 100644
--- a/manim/mobject/svg/opengl_svg_path.py
+++ b/manim/mobject/svg/opengl_svg_path.py
@@ -4,7 +4,7 @@ from ..types.opengl_vectorized_mobject import OpenGLVMobject
from .svg_path import SVGPathMobject
-class OpenGLSVGPathMobject(OpenGLVMobject, SVGPathMobject):
+class OpenGLSVGPathMobject(SVGPathMobject):
def __init__(
self,
path_string,
diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.py
index 9973a079a..ec5f0a467 100644
--- a/manim/mobject/svg/svg_mobject.py
+++ b/manim/mobject/svg/svg_mobject.py
@@ -20,12 +20,12 @@ from ...constants import *
from ...mobject.geometry import Circle, Line, Rectangle, RoundedRectangle
from ...mobject.opengl_geometry import OpenGLRectangle, OpenGLRoundedRectangle
from ...mobject.types.opengl_vectorized_mobject import OpenGLVGroup
-from ...mobject.types.vectorized_mobject import VGroup, VMobject
+from ...mobject.types.vectorized_mobject import MetaVMobject, VGroup, VMobject
from .style_utils import cascade_element_style, parse_style
from .svg_path import SVGPathMobject, string_to_numbers
-class SVGMobject(VMobject):
+class SVGMobject(metaclass=MetaVMobject):
"""A SVGMobject is a Vector Mobject constructed from an SVG (or XDV) file.
SVGMobjects are constructed from the XML data within the SVG file
@@ -73,6 +73,8 @@ class SVGMobject(VMobject):
unpack_groups=True, # if False, creates a hierarchy of VGroups
stroke_width=DEFAULT_STROKE_WIDTH,
fill_opacity=1.0,
+ should_subdivide_sharp_curves=False,
+ should_remove_null_curves=False,
**kwargs,
):
self.def_map = {}
@@ -80,9 +82,11 @@ class SVGMobject(VMobject):
self.ensure_valid_file()
self.should_center = should_center
self.unpack_groups = unpack_groups
- VMobject.__init__(
- self, fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs
- )
+ self.path_string_config = {
+ "should_subdivide_sharp_curves": should_subdivide_sharp_curves,
+ "should_remove_null_curves": should_remove_null_curves,
+ }
+ super().__init__(fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs)
self.move_into_position(width, height)
def ensure_valid_file(self):
@@ -130,6 +134,8 @@ class SVGMobject(VMobject):
self.add(*mobjects[0].submobjects)
doc.unlink()
+ init_points = generate_points
+
def get_mobjects_from(
self,
element: MinidomElement,
@@ -233,7 +239,9 @@ class SVGMobject(VMobject):
VMobjectFromSVGPathstring
A VMobject from the given path string, or d attribute.
"""
- return SVGPathMobject(path_string, **parse_style(style))
+ return SVGPathMobject(
+ path_string, **self.path_string_config, **parse_style(style)
+ )
def attribute_to_float(self, attr):
"""A helper method which converts the attribute to float.
@@ -561,4 +569,14 @@ class SVGMobject(VMobject):
self.width = width
def init_colors(self, propagate_colors=False):
- VMobject.init_colors(self, propagate_colors=propagate_colors)
+ if config.renderer == "opengl":
+ self.set_style(
+ fill_color=self.fill_color or self.color,
+ fill_opacity=self.fill_opacity,
+ stroke_color=self.stroke_color or self.color,
+ stroke_width=self.stroke_width,
+ stroke_opacity=self.stroke_opacity,
+ recurse=propagate_colors,
+ )
+ else:
+ VMobject.init_colors(self, propagate_colors=propagate_colors)
diff --git a/manim/mobject/svg/svg_path.py b/manim/mobject/svg/svg_path.py
index 8d41d33fa..116213ee5 100644
--- a/manim/mobject/svg/svg_path.py
+++ b/manim/mobject/svg/svg_path.py
@@ -12,7 +12,7 @@ import numpy as np
from ... import config
from ...constants import *
-from ...mobject.types.vectorized_mobject import VMobject
+from ...mobject.types.vectorized_mobject import MetaVMobject
from ...utils.deprecation import deprecated
@@ -210,10 +210,12 @@ def string_to_numbers(num_string: str) -> List[float]:
return float_results
-class SVGPathMobject(VMobject):
+class SVGPathMobject(metaclass=MetaVMobject):
def __init__(self, path_string, **kwargs):
self.path_string = path_string
- VMobject.__init__(self, **kwargs)
+ if config.renderer == "opengl":
+ kwargs["long_lines"] = True
+ super().__init__(**kwargs)
self.current_path_start = np.zeros((1, self.dim))
def get_path_commands(self):
@@ -267,6 +269,8 @@ class SVGPathMobject(VMobject):
# people treat y-coordinate differently
self.rotate(np.pi, RIGHT, about_point=ORIGIN)
+ init_points = generate_points
+
def handle_command(self, command, coord_string, prev_command):
"""Core logic for handling each of the various path commands."""
# Relative SVG commands are specified as lowercase letters
diff --git a/manim/mobject/svg/tex_mobject.py b/manim/mobject/svg/tex_mobject.py
index 4426a77fb..c737b18c0 100644
--- a/manim/mobject/svg/tex_mobject.py
+++ b/manim/mobject/svg/tex_mobject.py
@@ -270,6 +270,8 @@ class SingleStringMathTex(SVGMobject):
fill_opacity=fill_opacity,
background_stroke_width=background_stroke_width,
background_stroke_color=background_stroke_color,
+ should_subdivide_sharp_curves=True,
+ should_remove_null_curves=True,
**kwargs,
)
if height is None:
@@ -364,14 +366,14 @@ class SingleStringMathTex(SVGMobject):
def path_string_to_mobject(self, path_string, style):
# Overwrite superclass default to use
# specialized path_string mobject
- return TexSymbol(path_string, z_index=self.z_index, **parse_style(style))
+ return TexSymbol(path_string, **self.path_string_config, **parse_style(style))
def organize_submobjects_left_to_right(self):
self.sort(lambda p: p[0])
return self
def init_colors(self, propagate_colors=True):
- SVGMobject.init_colors(self, propagate_colors=propagate_colors)
+ super().init_colors(propagate_colors=propagate_colors)
class MathTex(SingleStringMathTex):
@@ -496,7 +498,6 @@ class MathTex(SingleStringMathTex):
tex_string,
tex_environment=self.tex_environment,
tex_template=self.tex_template,
- z_index=self.z_index,
)
num_submobs = len(sub_tex_mob.submobjects)
new_index = curr_index + num_submobs
@@ -505,13 +506,19 @@ class MathTex(SingleStringMathTex):
# part of the whole MathTex to be a VectorizedPoint
# positioned in the right part of the MathTex
sub_tex_mob.submobjects = [VectorizedPoint()]
+ if config.renderer == "opengl":
+ sub_tex_mob.assemble_family()
last_submob_index = min(curr_index, len(self.submobjects) - 1)
sub_tex_mob.move_to(self.submobjects[last_submob_index], RIGHT)
else:
sub_tex_mob.submobjects = self.submobjects[curr_index:new_index]
+ if config.renderer == "opengl":
+ sub_tex_mob.assemble_family()
new_submobjects.append(sub_tex_mob)
curr_index = new_index
self.submobjects = new_submobjects
+ if config.renderer == "opengl":
+ self.assemble_family()
return self
def get_parts_by_tex(self, tex, substring=True, case_sensitive=True):
diff --git a/manim/mobject/svg/text_mobject.py b/manim/mobject/svg/text_mobject.py
index b7dda9ccb..731d8fca6 100644
--- a/manim/mobject/svg/text_mobject.py
+++ b/manim/mobject/svg/text_mobject.py
@@ -143,7 +143,7 @@ class Paragraph(VGroup):
lines_str_list = lines_str.split("\n")
self.chars = self.gen_chars(lines_str_list)
- chars_lines_text_list = VGroup()
+ chars_lines_text_list = self.get_group_class()()
char_index_counter = 0
for line_index in range(lines_str_list.__len__()):
chars_lines_text_list.add(
@@ -165,7 +165,7 @@ class Paragraph(VGroup):
self.lines[1].extend(
[self.alignment for _ in range(chars_lines_text_list.__len__())]
)
- VGroup.__init__(
+ self.get_group_class().__init__(
self, *[self.lines[0][i] for i in range(self.lines[0].__len__())], **config
)
self.move_to(np.array([0, 0, 0]))
@@ -186,9 +186,9 @@ class Paragraph(VGroup):
The generated 2d-VGroup of chars.
"""
char_index_counter = 0
- chars = VGroup()
+ chars = self.get_group_class()()
for line_no in range(lines_str_list.__len__()):
- chars.add(VGroup())
+ chars.add(self.get_group_class()())
chars[line_no].add(
*self.lines_text.chars[
char_index_counter : char_index_counter
@@ -474,13 +474,16 @@ class Text(SVGMobject):
self.text = text
if self.disable_ligatures:
self.submobjects = [*self.gen_chars()]
- self.chars = VGroup(*self.submobjects)
+ self.chars = self.get_group_class()(*self.submobjects)
self.text = text_without_tabs.replace(" ", "").replace("\n", "")
- nppc = self.n_points_per_cubic_curve
+ if config.renderer == "opengl":
+ nppc = self.n_points_per_curve
+ else:
+ nppc = self.n_points_per_cubic_curve
for each in self:
- if len(each.points) == 0:
+ if len(each.get_points()) == 0:
continue
- points = each.points
+ points = each.get_points()
last = points[0]
each.clear_points()
for index, point in enumerate(points):
@@ -507,7 +510,7 @@ class Text(SVGMobject):
return f"Text({repr(self.original_text)})"
def gen_chars(self):
- chars = VGroup()
+ chars = self.get_group_class()()
submobjects_char_index = 0
for char_index in range(self.text.__len__()):
if self.text[char_index] in (" ", "\t", "\n"):
@@ -666,7 +669,7 @@ class Text(SVGMobject):
)
def init_colors(self, propagate_colors=True):
- SVGMobject.init_colors(self, propagate_colors=propagate_colors)
+ super().init_colors(propagate_colors=propagate_colors)
class MarkupText(SVGMobject):
@@ -943,8 +946,7 @@ class MarkupText(SVGMobject):
file_name = self.text2svg()
PangoUtils.remove_last_M(file_name)
- SVGMobject.__init__(
- self,
+ super().__init__(
file_name,
fill_opacity=fill_opacity,
stroke_width=stroke_width,
@@ -954,14 +956,17 @@ class MarkupText(SVGMobject):
unpack_groups=unpack_groups,
**kwargs,
)
- self.chars = VGroup(*self.submobjects)
+ self.chars = self.get_group_class()(*self.submobjects)
self.text = text_without_tabs.replace(" ", "").replace("\n", "")
- nppc = self.n_points_per_cubic_curve
+ if config.renderer == "opengl":
+ nppc = self.n_points_per_curve
+ else:
+ nppc = self.n_points_per_cubic_curve
for each in self:
- if len(each.points) == 0:
+ if len(each.get_points()) == 0:
continue
- points = each.points
+ points = each.get_points()
last = points[0]
each.clear_points()
for index, point in enumerate(points):
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 9c88ba694..1263b71e4 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -1868,7 +1868,7 @@ class VDict(VMobject):
super().add(value)
-class VectorizedPoint(VMobject):
+class VectorizedPoint(metaclass=MetaVMobject):
def __init__(
self,
location=ORIGIN,
@@ -1881,8 +1881,7 @@ class VectorizedPoint(VMobject):
):
self.artificial_width = artificial_width
self.artificial_height = artificial_height
- VMobject.__init__(
- self,
+ super().__init__(
color=color,
fill_opacity=fill_opacity,
stroke_width=stroke_width,
@@ -1890,11 +1889,13 @@ class VectorizedPoint(VMobject):
)
self.set_points(np.array([location]))
- @VMobject.width.getter
+ basecls = OpenGLVMobject if config.renderer == "opengl" else VMobject
+
+ @basecls.width.getter
def width(self):
return self.artificial_width
- @VMobject.height.getter
+ @basecls.height.getter
def height(self):
return self.artificial_height