Skip to content

Instantly share code, notes, and snippets.

@Elteoremadebeethoven
Created December 27, 2024 20:12
Show Gist options
  • Save Elteoremadebeethoven/c3d584d5f4f10a2ce37680656fef4750 to your computer and use it in GitHub Desktop.
Save Elteoremadebeethoven/c3d584d5f4f10a2ce37680656fef4750 to your computer and use it in GitHub Desktop.
From my video "From ManimCE to ManimG" code
from manimlib import *
# manim_config["camera"]["background_color"] = YELLOW
# manim_config["camera"]["fps"] = 60
class Example0(Scene):
def construct(self):
# Tex (GL) = MathTex (CE)
# TexText (GL) = Tex (CE)
t = TexText("This is a test")
# self.frame (GL) = self.camera.frame (CE)
t.set_width(self.frame.get_width()-1)
self.play(Write(t), run_time=2)
self.wait()
class Example1(Scene):
def construct(self):
self.camera.background_rgba = list(color_to_rgba(GREEN, 1))
t = Square()
self.play(ShowCreation(t)) # Create (CE)
self.play(t.animate.to_edge(DOWN))
self.play(t.animate.to_edge(LEFT))
self.wait()
class Example2(Scene):
def construct(self):
t = Tex(r"x^2 + y^2 = z^2", template="french_cursive")
t.scale(3)
self.play(Write(t))
self.wait()
class MusicTex(TexText):
def __init__(self,
*args,
template="music_template",
**kwargs):
super().__init__(*args, template=template, **kwargs)
class Example3(Scene):
def construct(self):
t = MusicTex(r"""
\parindent10mm
\instrumentnumber{1} % a single instrument
\setname1{Piano} % whose name is Piano
\setstaffs1{2} % with two staffs
\generalmeter{\meterfrac44} % 4/4 meter chosen
\startextract % starting real score
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\hl j\en
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\ql l\sk\ql n\en
\bar
\Notes\ibu0f0\qb0{dgf}|\qlp i\en
\notes\tbu0\qb0g|\ibbl1j3\qb1j\tbl1\qb1k\en
\Notes\ibu0f0\qb0{cge}\tbu0\qb0g|\hl j\en
\zendextract
""")
t.scale(0.6)
self.add(t)
class Example4(Scene):
def construct(self):
t = TexText("Hello world", template="times_template")
# t.width = config.frame_width - 3
# equivalent to
t.set_width(FRAME_WIDTH-3)
# equivalent to
# t.stretch_to_fit_height(FRAME_HEIGHT)
self.play(Write(t), run_time=3)
# Code is not working now
class Example5(Scene):
def construct(self):
t = Tex(
r"""
\begin{aligned}
\frac{\mathrm{d} x}{\mathrm{d} t} & =\sigma(y-x z) \\
\frac{\mathrm{d} y}{\mathrm{d} t} & =x(\rho-z)-y \\
x^2 + y^2 + {x \over z y} &= x y \\
\sqrt{\int_{x}^{y \over \sqrt{\alpha}} z^{x y \over
\sqrt{z + y + \beta + \alpha {x \over d x\beta\alpha}}}} &= x
\end{aligned}
""",
t2c={
"x": RED,
"y": GREEN,
"z": BLUE,
"\\int": TEAL,
"\\beta": ORANGE,
"\\alpha": PURPLE,
"\\mathrm{d}": PINK
},
font_size=30
).scale(2)
for s in t["x"]: s.scale(0.5)
self.add(t)
class Example3D1(Scene):
def construct(self):
def param_gauss(u, v, sigma=0.4):
x = u
y = v
sigma, mu = sigma, [0.0, 0.0]
d = np.linalg.norm(np.array([x - mu[0], y - mu[1]]))
z = np.exp(-(d ** 2 / (2.0 * sigma ** 2)))
return np.array([x, y, z])
p1 = ParametricSurface(
param_gauss,
v_range=[-3, 3],
u_range=[-3, 3],
).set_color(RED)
p2 = ParametricSurface(
lambda u,v: param_gauss(u, v, 1),
v_range=[-3, 3],
u_range=[-3, 3],
).set_color(BLUE)
self.add(p1)
self.wait()
self.play(Transform(p1, p2), run_time=5)
self.wait()
class Example3D2(Scene):
def construct(self):
axes = ThreeDAxes(
x_range=[-2, 2, 1],
y_range=[-2, 2, 1],
z_range=[-2, 2, 1],
width=5, # x_length (CE)
height=5, # y_length (CE)
depth=5 # z_length (CE)
)
x_label = Tex("x").next_to(axes.x_axis.get_end(), UP)
y_label = Tex("y").next_to(axes.y_axis.get_end(), RIGHT)
z_label = Tex("z").next_to(axes.z_axis.get_end(), OUT)
z_label.rotate(PI/2, axis=RIGHT)
self.add(axes, x_label, y_label, z_label)
self.wait()
# Equivalent to move_camera
self.play(
self.frame.animate.set_euler_angles(
phi=90*DEGREES,
theta=90*DEGREES
),
run_time=2
)
class Example3D3(Scene):
def construct(self):
axes = ThreeDAxes(
x_range=[-2, 2, 1],
y_range=[-2, 2, 1],
z_range=[-2, 2, 1],
)
x_label = Tex("x").next_to(axes.x_axis.get_end(), UP)
y_label = Tex("y").next_to(axes.y_axis.get_end(), RIGHT)
z_label = Tex("z").next_to(axes.z_axis.get_end(), OUT)
z_label.rotate(PI/2, axis=RIGHT)
self.add(axes, x_label, y_label, z_label)
self.frame.set_phi(60*DEGREES)
# Equivalent to begin_ambient_camera_rotation
self.frame.add_updater(lambda m, dt: m.increment_theta(-0.1 * dt))
self.wait(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment