Skip to content

Commit c71b73b

Browse files
authored
Merge pull request #1200 from eulertour/opengl-tex
Add text and SVG mobjects to OpenGL
2 parents 9699727 + f11c9a6 commit c71b73b

13 files changed

+2087
-31
lines changed

manim/animation/transform.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import numpy as np
3232

33+
from .. import config
3334
from ..animation.animation import Animation
3435
from ..constants import DEFAULT_POINTWISE_FUNCTION_RUN_TIME, DEGREES, OUT
3536
from ..mobject.mobject import Group, Mobject
@@ -80,7 +81,10 @@ def begin(self) -> None:
8081
self.target_copy = self.target_mobject.copy()
8182
# Note, this potentially changes the structure
8283
# of both mobject and target_mobject
83-
self.mobject.align_data(self.target_copy)
84+
if config["renderer"] == "opengl":
85+
self.mobject.align_data_and_family(self.target_copy)
86+
else:
87+
self.mobject.align_data(self.target_copy)
8488
super().begin()
8589

8690
def create_target(self) -> typing.Union[Mobject, None]:

manim/mobject/mobject.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ def __deepcopy__(self, clone_from_id):
223223
return result
224224

225225
def __repr__(self):
226-
return str(self.name)
226+
if config["renderer"] == "opengl":
227+
return super().__repr__()
228+
else:
229+
return str(self.name)
227230

228231
def reset_points(self):
229232
"""Sets :attr:`points` to be an empty array."""

manim/mobject/opengl_mobject.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,101 @@ def animate(self):
135135
# Borrowed from https://github.com/ManimCommunity/manim/
136136
return _AnimationBuilder(self)
137137

138+
@property
139+
def width(self):
140+
"""The width of the mobject.
141+
142+
Returns
143+
-------
144+
:class:`float`
145+
146+
Examples
147+
--------
148+
.. manim:: WidthExample
149+
150+
class WidthExample(Scene):
151+
def construct(self):
152+
decimal = DecimalNumber().to_edge(UP)
153+
rect = Rectangle(color=BLUE)
154+
rect_copy = rect.copy().set_stroke(GRAY, opacity=0.5)
155+
156+
decimal.add_updater(lambda d: d.set_value(rect.width))
157+
158+
self.add(rect_copy, rect, decimal)
159+
self.play(rect.animate.set(width=7))
160+
self.wait()
161+
162+
See also
163+
--------
164+
:meth:`length_over_dim`
165+
166+
"""
167+
168+
# Get the length across the X dimension
169+
return self.length_over_dim(0)
170+
138171
# Only these methods should directly affect points
172+
@width.setter
173+
def width(self, value):
174+
self.rescale_to_fit(value, 0, stretch=False)
175+
176+
@property
177+
def height(self):
178+
"""The height of the mobject.
179+
180+
Returns
181+
-------
182+
:class:`float`
183+
184+
Examples
185+
--------
186+
.. manim:: HeightExample
187+
188+
class HeightExample(Scene):
189+
def construct(self):
190+
decimal = DecimalNumber().to_edge(UP)
191+
rect = Rectangle(color=BLUE)
192+
rect_copy = rect.copy().set_stroke(GRAY, opacity=0.5)
193+
194+
decimal.add_updater(lambda d: d.set_value(rect.height))
195+
196+
self.add(rect_copy, rect, decimal)
197+
self.play(rect.animate.set(height=5))
198+
self.wait()
199+
200+
See also
201+
--------
202+
:meth:`length_over_dim`
203+
204+
"""
205+
206+
# Get the length across the Y dimension
207+
return self.length_over_dim(1)
208+
209+
@height.setter
210+
def height(self, value):
211+
self.rescale_to_fit(value, 1, stretch=False)
212+
213+
@property
214+
def depth(self):
215+
"""The depth of the mobject.
216+
217+
Returns
218+
-------
219+
:class:`float`
220+
221+
See also
222+
--------
223+
:meth:`length_over_dim`
224+
225+
"""
226+
227+
# Get the length across the Z dimension
228+
return self.length_over_dim(2)
229+
230+
@depth.setter
231+
def depth(self, value):
232+
self.rescale_to_fit(value, 2, stretch=False)
139233

140234
def resize_points(self, new_length, resize_func=resize_array):
141235
if new_length != len(self.data["points"]):
@@ -619,7 +713,6 @@ def rotate(
619713
self,
620714
angle,
621715
axis=OUT,
622-
about_point: Union[np.ndarray, List, None] = None,
623716
**kwargs,
624717
):
625718
rot_matrix_T = rotation_matrix_transpose(angle, axis)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from ...constants import *
2+
from ..svg.svg_mobject import SVGMobject
3+
from ..types.opengl_vectorized_mobject import OpenGLVMobject
4+
from .opengl_svg_path import OpenGLSVGPathMobject
5+
from .style_utils import cascade_element_style, parse_style
6+
7+
8+
class OpenGLSVGMobject(OpenGLVMobject, SVGMobject):
9+
def __init__(
10+
self,
11+
file_name=None,
12+
should_center=True,
13+
height=2,
14+
width=None,
15+
unpack_groups=True, # if False, creates a hierarchy of VGroups
16+
stroke_width=DEFAULT_STROKE_WIDTH,
17+
fill_opacity=1.0,
18+
should_subdivide_sharp_curves=False,
19+
should_remove_null_curves=False,
20+
**kwargs,
21+
):
22+
self.def_map = {}
23+
self.file_name = file_name or self.file_name
24+
self.ensure_valid_file()
25+
self.should_center = should_center
26+
self.unpack_groups = unpack_groups
27+
self.path_string_config = {
28+
"should_subdivide_sharp_curves": should_subdivide_sharp_curves,
29+
"should_remove_null_curves": should_remove_null_curves,
30+
}
31+
OpenGLVMobject.__init__(
32+
self, stroke_width=stroke_width, fill_opacity=fill_opacity, **kwargs
33+
)
34+
self.move_into_position(width, height)
35+
36+
def init_points(self):
37+
self.generate_points()
38+
39+
def path_string_to_mobject(self, path_string: str, style: dict):
40+
return OpenGLSVGPathMobject(
41+
path_string, **self.path_string_config, **parse_style(style)
42+
)

manim/mobject/svg/opengl_svg_path.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
3+
from ..types.opengl_vectorized_mobject import OpenGLVMobject
4+
from .svg_path import SVGPathMobject
5+
6+
7+
class OpenGLSVGPathMobject(OpenGLVMobject, SVGPathMobject):
8+
def __init__(
9+
self,
10+
path_string,
11+
should_subdivide_sharp_curves=False,
12+
should_remove_null_curves=False,
13+
**kwargs
14+
):
15+
self.path_string = path_string
16+
OpenGLVMobject.__init__(
17+
self,
18+
long_lines=True,
19+
should_subdivide_sharp_curves=should_subdivide_sharp_curves,
20+
should_remove_null_curves=should_remove_null_curves,
21+
**kwargs
22+
)
23+
self.current_path_start = np.zeros((1, self.dim))
24+
25+
def init_points(self):
26+
self.generate_points()
27+
28+
def start_new_path(self, point):
29+
SVGPathMobject.start_new_path(self, point)

0 commit comments

Comments
 (0)