Created
June 20, 2023 07:32
-
-
Save abul4fia/1419b181e8e3410ef78e6acc25c3df94 to your computer and use it in GitHub Desktop.
How to fix objects and transforms in a manim 3D scene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Context: | |
# | |
# Although ThreeDScene has the method `self.add_fixed_in_frame_mobjects()` | |
# it doesnt work as expected if you want to have transforms that happen | |
# in the frame coordinates, instead of in 3D coordinates | |
# | |
# See https://discord.com/channels/581738731934056449/1120217688728424549/1120217688728424549 | |
# | |
# This fix fixes the fixed objects in a different way that works under transforms | |
# The trick is to add a `.fixed` attribute to all mobjects that should not be | |
# projected by the camera | |
class MyCamera(ThreeDCamera): | |
def transform_points_pre_display(self, mobject, points): | |
if getattr(mobject, "fixed", False): | |
return points | |
else: | |
return super().transform_points_pre_display(mobject, points) | |
class MyThreeDScene(ThreeDScene): | |
def __init__(self, camera_class=MyCamera, ambient_camera_rotation=None, | |
default_angled_camera_orientation_kwargs=None, **kwargs): | |
super().__init__(camera_class=camera_class, **kwargs) | |
def make_fixed(*mobs): | |
for mob in mobs: | |
mob.fixed = True | |
for submob in mob.family_members_with_points(): | |
submob.fixed = True | |
class Test(MyThreeDScene): | |
def construct(self): | |
r = Rectangle() | |
tex = MathTex("{{f(x,y)}} = {{ e^{-(x^2 + y^2)} }}").to_edge(UP * 1.1) | |
tex[2].set_color(YELLOW) | |
texN = MathTex( "{{f(x,y)}} = " | |
"{{\\int_{-2}^2 \\int_{-2}^2 f(x,y) dx dy \\approx \\sum_{i=0}^{n-1} \\sum_{j=0}^{m-1} f(i^*,j^*) \\Delta a}}") | |
make_fixed(tex, texN) | |
texN.to_edge(UP * 1.1) | |
self.set_camera_orientation(phi=75 * DEGREES, theta=-45 * DEGREES) | |
self.begin_ambient_camera_rotation(rate=0.5) | |
self.add(r) | |
self.add(tex) | |
self.wait() | |
self.play(TransformMatchingTex(tex, texN)) | |
self.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, this is the solution I needed.