Skip to content

Commit 3ad385a

Browse files
authored
Enhancement: Migrate width/height/depth to properties (#1024)
* Migrate width/height/depth to properties * Fix examples * Fix typos * Import manim for doctests * Expect Square for doctests * Improve docs for width/height/depth properties
1 parent 05ca294 commit 3ad385a

28 files changed

+333
-145
lines changed

docs/source/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Plotting with Manim
330330
y_max=6,
331331
x_labeled_nums=[0,2,3],
332332
**kwargs)
333-
333+
334334
def construct(self):
335335
self.setup_axes()
336336
curve1 = self.get_graph(lambda x: 4 * x - x ** 2, x_min=0, x_max=4)
@@ -435,7 +435,7 @@ Special Camera Settings
435435
dot = Dot().shift(UL * 2)
436436
image = ImageMobject(np.uint8([[0, 100, 30, 200],
437437
[255, 0, 5, 33]]))
438-
image.set_height(7)
438+
image.height = 7
439439
frame_text = Text("Frame", color=PURPLE).scale(1.4)
440440
zoomed_camera_text = Text("Zoomed camera", color=RED).scale(1.4)
441441

example_scenes/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def construct(self):
101101
)
102102
group = VGroup(example_text, example_tex)
103103
group.arrange(DOWN)
104-
group.set_width(config["frame_width"] - 2 * LARGE_BUFF)
104+
group.width = config["frame_width"] - 2 * LARGE_BUFF
105105

106106
self.play(Write(example_text))
107107
self.play(Write(example_tex))

manim/camera/moving_camera.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def frame_height(self):
6969
float
7070
The height of the frame.
7171
"""
72-
return self.frame.get_height()
72+
return self.frame.height
7373

7474
@property
7575
def frame_width(self):
@@ -80,7 +80,7 @@ def frame_width(self):
8080
float
8181
The width of the frame.
8282
"""
83-
return self.frame.get_width()
83+
return self.frame.width
8484

8585
@property
8686
def frame_center(self):
@@ -158,9 +158,9 @@ def cache_cairo_context(self, pixel_array, ctx):
158158
# def realign_frame_shape(self):
159159
# height, width = self.frame_shape
160160
# if self.fixed_dimension == 0:
161-
# self.frame_shape = (height, self.frame.get_width())
161+
# self.frame_shape = (height, self.frame.width
162162
# else:
163-
# self.frame_shape = (self.frame.get_height(), width)
163+
# self.frame_shape = (self.frame.height, width)
164164
# self.resize_frame_shape(fixed_dimension=self.fixed_dimension)
165165

166166
def get_mobjects_indicating_movement(self):

manim/camera/multi_camera.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def update_sub_cameras(self):
5454
for imfc in self.image_mobjects_from_cameras:
5555
pixel_height, pixel_width = self.pixel_array.shape[:2]
5656
imfc.camera.frame_shape = (
57-
imfc.camera.frame.get_height(),
58-
imfc.camera.frame.get_width(),
57+
imfc.camera.frame.height,
58+
imfc.camera.frame.width,
5959
)
6060
imfc.camera.reset_pixel_shape(
61-
int(pixel_height * imfc.get_height() / self.frame_height),
62-
int(pixel_width * imfc.get_width() / self.frame_width),
61+
int(pixel_height * imfc.height / self.frame_height),
62+
int(pixel_width * imfc.width / self.frame_width),
6363
)
6464

6565
def reset(self):

manim/grpc/impl/frame_server_impl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ def serialize_mobject(mobject):
404404
logger.info(
405405
f"Expected path {mobject.path} to be under the assets dir ({assets_dir_path})"
406406
)
407-
mob_proto.image_mobject_data.height = mobject.get_height()
408-
mob_proto.image_mobject_data.width = mobject.get_width()
407+
mob_proto.image_mobject_data.height = mobject.height
408+
mob_proto.image_mobject_data.width = mobject.width
409409
mob_center = mobject.get_center()
410410
mob_proto.image_mobject_data.center.x = mob_center[0]
411411
mob_proto.image_mobject_data.center.y = mob_center[1]

manim/mobject/frame.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,27 @@
1616

1717
class ScreenRectangle(Rectangle):
1818
def __init__(self, aspect_ratio=16.0 / 9.0, height=4, **kwargs):
19-
self.aspect_ratio = aspect_ratio
20-
self.height = height
21-
Rectangle.__init__(self, **kwargs)
22-
self.set_width(self.aspect_ratio * self.get_height(), stretch=True)
19+
Rectangle.__init__(self, width=aspect_ratio * height, height=height, **kwargs)
20+
21+
@property
22+
def aspect_ratio(self):
23+
"""The aspect ratio.
24+
25+
When set, the width is stretched to accommodate
26+
the new aspect ratio.
27+
"""
28+
29+
return self.width / self.height
30+
31+
@aspect_ratio.setter
32+
def aspect_ratio(self, value):
33+
self.stretch_to_fit_width(value * self.height)
2334

2435

2536
class FullScreenRectangle(ScreenRectangle):
2637
def __init__(self, **kwargs):
2738
ScreenRectangle.__init__(self, **kwargs)
28-
self.set_height(config["frame_height"])
39+
self.height = config["frame_height"]
2940

3041

3142
class FullScreenFadeRectangle(FullScreenRectangle):
@@ -41,8 +52,18 @@ def __init__(self, stroke_width=0, fill_color=BLACK, fill_opacity=0.7, **kwargs)
4152

4253
class PictureInPictureFrame(Rectangle):
4354
def __init__(self, height=3, aspect_ratio=16.0 / 9.0, **kwargs):
44-
self.height = height
45-
self.aspect_ratio = aspect_ratio
46-
Rectangle.__init__(
47-
self, width=self.aspect_ratio * self.height, height=self.height, **kwargs
48-
)
55+
Rectangle.__init__(self, width=aspect_ratio * height, height=height, **kwargs)
56+
57+
@property
58+
def aspect_ratio(self):
59+
"""The aspect ratio.
60+
61+
When set, the width is stretched to accommodate
62+
the new aspect ratio.
63+
"""
64+
65+
return self.width / self.height
66+
67+
@aspect_ratio.setter
68+
def aspect_ratio(self, value):
69+
self.stretch_to_fit_width(value * self.height)

manim/mobject/geometry.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def surround(self, mobject, dim_to_match=0, stretch=False, buffer_factor=1.2):
413413
# TODO: Figure out and fix
414414
self.replace(mobject, dim_to_match, stretch)
415415

416-
self.set_width(np.sqrt(mobject.get_width() ** 2 + mobject.get_height() ** 2))
416+
self.width = np.sqrt(mobject.width ** 2 + mobject.height ** 2)
417417
return self.scale(buffer_factor)
418418

419419
def point_at_angle(self, angle):
@@ -519,9 +519,7 @@ def __init__(self, label, radius=None, **kwargs) -> None:
519519
rendered_label = label
520520

521521
if radius is None:
522-
radius = (
523-
0.1 + max(rendered_label.get_width(), rendered_label.get_height()) / 2
524-
)
522+
radius = 0.1 + max(rendered_label.width, rendered_label.height) / 2
525523
Dot.__init__(self, radius=radius, **kwargs)
526524
rendered_label.move_to(self.get_center())
527525
self.add(rendered_label)
@@ -530,10 +528,8 @@ def __init__(self, label, radius=None, **kwargs) -> None:
530528
class Ellipse(Circle):
531529
def __init__(self, width=2, height=1, **kwargs):
532530
Circle.__init__(self, **kwargs)
533-
self.width = width
534-
self.height = height
535-
self.set_width(self.width, stretch=True)
536-
self.set_height(self.height, stretch=True)
531+
self.stretch_to_fit_width(width)
532+
self.stretch_to_fit_height(height)
537533

538534

539535
class AnnularSector(Arc):
@@ -788,11 +784,10 @@ def __init__(self, vmob, alpha, length=1, d_alpha=1e-6, **kwargs):
788784

789785
class Elbow(VMobject):
790786
def __init__(self, width=0.2, angle=0, **kwargs):
791-
self.width = width
792787
self.angle = angle
793788
VMobject.__init__(self, **kwargs)
794789
self.set_points_as_corners([UP, UP + RIGHT, RIGHT])
795-
self.set_width(self.width, about_point=ORIGIN)
790+
self.scale_to_fit_width(width, about_point=ORIGIN)
796791
self.rotate(self.angle, about_point=ORIGIN)
797792

798793

@@ -1227,20 +1222,27 @@ def __init__(
12271222
close_new_points=True,
12281223
**kwargs
12291224
):
1230-
self.height = height
1231-
self.width = width
12321225
self.mark_paths_closed = mark_paths_closed
12331226
self.close_new_points = close_new_points
12341227
Polygon.__init__(self, UL, UR, DR, DL, color=color, **kwargs)
1235-
self.set_width(self.width, stretch=True)
1236-
self.set_height(self.height, stretch=True)
1228+
self.stretch_to_fit_width(width)
1229+
self.stretch_to_fit_height(height)
12371230

12381231

12391232
class Square(Rectangle):
12401233
def __init__(self, side_length=2.0, **kwargs):
1241-
self.side_length = side_length
12421234
Rectangle.__init__(self, height=side_length, width=side_length, **kwargs)
12431235

1236+
@property
1237+
def side_length(self):
1238+
"""The square's side length."""
1239+
1240+
return self.width
1241+
1242+
@side_length.setter
1243+
def side_length(self, value):
1244+
self.width = value
1245+
12441246

12451247
class RoundedRectangle(Rectangle):
12461248
def __init__(self, corner_radius=0.5, **kwargs):
@@ -1279,8 +1281,8 @@ class ArrowTip(VMobject):
12791281
... def __init__(self, **kwargs):
12801282
... RegularPolygon.__init__(self, n=5, **kwargs)
12811283
... length = 0.35
1282-
... self.set_width(length)
1283-
... self.set_height(length, stretch=True)
1284+
... self.width = length
1285+
... self.stretch_to_fit_height(length)
12841286
>>> arr = Arrow(np.array([-2, -2, 0]), np.array([2, 2, 0]),
12851287
... tip_shape=MyCustomArrowTip)
12861288
>>> isinstance(arr.tip, RegularPolygon)
@@ -1419,8 +1421,8 @@ def __init__(
14191421
start_angle=start_angle,
14201422
**kwargs
14211423
)
1422-
self.set_width(length)
1423-
self.set_height(length, stretch=True)
1424+
self.width = length
1425+
self.stretch_to_fit_height(length)
14241426

14251427

14261428
class ArrowTriangleFilledTip(ArrowTriangleTip):
@@ -1450,8 +1452,8 @@ def __init__(
14501452
Circle.__init__(
14511453
self, fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs
14521454
)
1453-
self.set_width(length)
1454-
self.set_height(length, stretch=True)
1455+
self.width = length
1456+
self.stretch_to_fit_height(length)
14551457

14561458

14571459
class ArrowCircleFilledTip(ArrowCircleTip):
@@ -1482,8 +1484,8 @@ def __init__(
14821484
side_length=length,
14831485
**kwargs
14841486
)
1485-
self.set_width(length)
1486-
self.set_height(length, stretch=True)
1487+
self.width = length
1488+
self.stretch_to_fit_height(length)
14871489

14881490

14891491
class ArrowSquareFilledTip(ArrowSquareTip):

manim/mobject/logo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ def __init__(self, dark_theme: bool = True):
8484
tex.next_to(anim, buff=0.01)
8585
tex.align_to(self.M, DOWN)
8686
anim.add(tex)
87-
anim.set_color(self.font_color).set_height(
88-
m_height_over_anim_height * self.M.get_height()
89-
)
87+
anim.set_color(self.font_color)
88+
anim.height = m_height_over_anim_height * self.M.height
9089

9190
# Note: "anim" is only shown in the expanded state
9291
# and thus not yet added to the submobjects of self.

manim/mobject/matrix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def organize_mob_matrix(self, matrix):
132132
def add_brackets(self, left="\\big[", right="\\big]"):
133133
bracket_pair = MathTex(left, right)
134134
bracket_pair.scale(2)
135-
bracket_pair.stretch_to_fit_height(self.get_height() + 2 * self.bracket_v_buff)
135+
bracket_pair.stretch_to_fit_height(self.height + 2 * self.bracket_v_buff)
136136
l_bracket, r_bracket = bracket_pair.split()
137137
l_bracket.next_to(self, LEFT, self.bracket_h_buff)
138138
r_bracket.next_to(self, RIGHT, self.bracket_h_buff)
@@ -228,7 +228,7 @@ def get_det_text(
228228
):
229229
parens = MathTex("(", ")")
230230
parens.scale(initial_scale_factor)
231-
parens.stretch_to_fit_height(matrix.get_height())
231+
parens.stretch_to_fit_height(matrix.height)
232232
l_paren, r_paren = parens.split()
233233
l_paren.next_to(matrix, LEFT, buff=0.1)
234234
r_paren.next_to(matrix, RIGHT, buff=0.1)

0 commit comments

Comments
 (0)