Skip to content

Commit 9360129

Browse files
authored
Fix color initialization in OpenGLVMobject.__init__() to prevent crashes in subclasses (#4056)
* Change order of statements in `OpenGLVMobject.__init__()`
1 parent 9fcf94f commit 9360129

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

manim/mobject/opengl/opengl_mobject.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from dataclasses import dataclass
1313
from functools import partialmethod, wraps
1414
from math import ceil
15-
from typing import TYPE_CHECKING, Generic
15+
from typing import TYPE_CHECKING, Any, Generic
1616

1717
import numpy as np
1818
from typing_extensions import TypedDict, TypeVar
@@ -103,14 +103,15 @@ class MobjectStatus:
103103

104104
# TODO: add this to the **kwargs of all mobjects that use OpenGLMobject
105105
class MobjectKwargs(TypedDict, total=False):
106+
color: ParsableManimColor | Sequence[ParsableManimColor] | None
106107
opacity: float
107108
reflectiveness: float
108109
shadow: float
109110
gloss: float
110111
is_fixed_in_frame: bool
111112
is_fixed_orientation: bool
112113
depth_test: bool
113-
name: str
114+
name: str | None
114115

115116

116117
class OpenGLMobject:
@@ -135,7 +136,7 @@ class OpenGLMobject:
135136
# TypedDict above so that autocomplete works for users
136137
def __init__(
137138
self,
138-
color=WHITE,
139+
color: ParsableManimColor | Sequence[ParsableManimColor] | None = WHITE,
139140
opacity: float = 1.0,
140141
reflectiveness: float = 0.0,
141142
shadow: float = 0.0,
@@ -144,7 +145,7 @@ def __init__(
144145
is_fixed_orientation: bool = False,
145146
depth_test: bool = True,
146147
name: str | None = None,
147-
**kwargs, # just dump
148+
**kwargs: Any, # just dump
148149
):
149150
self.color = color
150151
self.opacity = opacity

manim/mobject/opengl/opengl_vectorized_mobject.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@
6060

6161
# TODO: add this to the **kwargs of all mobjects that use OpenGLVMobject
6262
class VMobjectKwargs(MobjectKwargs, total=False):
63-
color: ParsableManimColor | list[ParsableManimColor]
64-
fill_color: ParsableManimColor | list[ParsableManimColor]
65-
fill_opacity: float
66-
stroke_color: ParsableManimColor | list[ParsableManimColor]
67-
stroke_opacity: float
63+
color: ParsableManimColor | Sequence[ParsableManimColor] | None
64+
fill_color: ParsableManimColor | Sequence[ParsableManimColor] | None
65+
fill_opacity: float | None
66+
stroke_color: ParsableManimColor | Sequence[ParsableManimColor] | None
67+
stroke_opacity: float | None
6868
stroke_width: float
6969
draw_stroke_behind_fill: bool
70-
background_image_file: str
70+
background_image_file: str | None
7171
long_lines: bool
7272
joint_type: LineJointType
7373
flat_stroke: bool
74+
shade_in_3d: bool
75+
checkerboard_colors: bool # TODO: remove
7476

7577

7678
class OpenGLVMobject(OpenGLMobject):
@@ -95,17 +97,10 @@ def __init__(
9597
long_lines: bool = False,
9698
joint_type: LineJointType = LineJointType.AUTO,
9799
flat_stroke: bool = False,
98-
shade_in_3d=False, # TODO: Can be ignored for now but we should think about using some sort of shader to introduce lighting after deferred rendering has completed
99-
checkerboard_colors=False, # ignore,
100+
shade_in_3d: bool = False, # TODO: Can be ignored for now but we should think about using some sort of shader to introduce lighting after deferred rendering has completed
101+
checkerboard_colors: bool = False, # ignore,
100102
**kwargs: Unpack[MobjectKwargs],
101103
):
102-
super().__init__(**kwargs)
103-
if fill_color is None:
104-
fill_color = self.color
105-
if stroke_color is None:
106-
stroke_color = self.color
107-
self.set_fill(color=fill_color, opacity=fill_opacity)
108-
self.set_stroke(color=stroke_color, opacity=stroke_opacity)
109104
self.stroke_width = listify(stroke_width)
110105
self.draw_stroke_behind_fill = draw_stroke_behind_fill
111106
self.background_image_file = background_image_file
@@ -116,6 +111,14 @@ def __init__(
116111
self.needs_new_triangulation = True
117112
self.triangulation = np.zeros(0, dtype="i4")
118113

114+
super().__init__(**kwargs)
115+
if fill_color is None:
116+
fill_color = self.color
117+
if stroke_color is None:
118+
stroke_color = self.color
119+
self.set_fill(color=fill_color, opacity=fill_opacity)
120+
self.set_stroke(color=stroke_color, width=stroke_width, opacity=stroke_opacity)
121+
119122
# self.refresh_unit_normal()
120123

121124
def _assert_valid_submobjects(self, submobjects: Iterable[OpenGLVMobject]) -> Self:

0 commit comments

Comments
 (0)