Skip to content

Instantly share code, notes, and snippets.

@dearsip

dearsip/movie.py Secret

Created May 30, 2022 01:26
Show Gist options
  • Save dearsip/15279450881d0ec0a4e5b5b2718c3415 to your computer and use it in GitHub Desktop.
Save dearsip/15279450881d0ec0a4e5b5b2718c3415 to your computer and use it in GitHub Desktop.
# https://www.nicovideo.jp/watch/sm40542900 で使用した動画の生成コード
# Manim https://www.manim.community/
# 動作環境 Jupyter Notebook https://try.manim.community/ 実行の際はクラス名に注意
from manim import *
# ハローワールド
class HelloWorld(Scene):
def construct(self):
self.play(Write(Text("Hello, World!")))
# MarkUp
class MarkUp(Scene):
def construct(self):
text = MarkupText(
"<span underline='double'>ソフトウェアトーク</span><b>理工サイド</b>交流祭",
font="meiryo", stroke_width=4)
text.set_color_by_gradient(RED, YELLOW, BLUE)
text.set_stroke_color(GREEN)
for i in [2,6,15]:
text[i].next_to(text[i-1], RIGHT, buff = .17) # 「ト」が右に寄りがちなのでカーニング
self.play(Write(text))
self.wait()
self.play(Unwrite(text), reverse=False)
self.wait()
# Jupyter Notebook用の英語版
class MarkUpEn(Scene):
def construct(self):
text = MarkupText(
"<span underline='double'>SoftwareTalk</span>\n<b>Science &amp; Engineering</b>\nExchange Festival",
font="meiryo", stroke_width=4)
text.set_color_by_gradient(RED, YELLOW, BLUE)
text.set_stroke_color(GREEN)
self.play(Write(text))
self.wait()
self.play(Unwrite(text), reverse=False)
self.wait()
# 式変形
class Matching(Scene):
def construct(self):
eq1 = MathTex(r"{{ax^2}}+{{bx}}+ {{c}}={{0}}")
eq2 = MathTex(r"{{ax^2}}+{{bx}}{{=}}-{{c}}")
eq3 = MathTex(r"x^2+\frac bax{{=-\frac ca}}")
eq4 = MathTex(r"x^2+\frac bax{{+\left(\frac b{2a}\right)^2}}=-\frac ca{{+\left(\frac b{2a}\right)^2}}")
eq5 = MathTex(r"{{\left(x+\frac b{2a}\right)^2}}=-\frac ca{{+\left(\frac b{2a}\right)^2}}")
self.add(eq1)
self.wait(0.5)
self.play(TransformMatchingTex(eq1, eq2, path_arc=PI/2))
self.wait(0.5)
self.play(TransformMatchingShapes(eq2, eq3))
self.wait(0.5)
self.play(TransformMatchingTex(eq3, eq4))
self.wait(0.5)
self.play(TransformMatchingShapes(eq4[2:], eq5[1:]),
TransformMatchingShapes(eq4[1::-1],eq5[0]))
self.wait(0.5)
self.play(eq5.animate.shift(DOWN))
# TransformMatchingShapes と ...Tex の比較 とてもきたない HackGen > https://github.com/yuru7/HackGen
class ShapesAndTex(Scene):
def construct(self):
t1 = MathTex(R"\sin^2\theta{{+}}\cos^2\theta{{=1}}").scale(2)
t2 = MathTex(R"\cos^2\theta{{+}}\sin^2\theta{{=1}}").scale(2)
s1 = Text("TransformMatchingShapes", font="HackGen").shift(UP*2)
s2 = Text("TransformMatchingTex", font="HackGen").shift(UP*2)
code1 = Text(R"\sin^2\theta{{+}}\cos^2\theta{{=1}}",t2c={"{":RED, "}":RED},font="HackGen").scale(.8).shift(DOWN*2)
code2 = Text(R"\cos^2\theta{{+}}\sin^2\theta{{=1}}",t2c={"{":RED, "}":RED},font="HackGen").scale(.8).shift(DOWN*2)
n = 0
cl = [RED, GREEN, BLUE, YELLOW]
t1c1 = t1.copy()
t1c2 = t1.copy()
t1b = t1.copy()
for t in t1c1:
for l in t:
l.set_color(cl[n%3])
n += 1
n = 0
for t in t1c2:
t.set_color(cl[n%4])
n += 1
t2c1 = t2.copy()
t2c2 = t2.copy()
t2b = t2.copy()
n = 0
for t in t2c1:
t.set_color(cl[2-n])
n += 1
n = 0
for t in t2c2:
for l in t:
l.set_color(cl[n%3])
n += 1
t2c2[0][2].set_color(cl[0])
t2c2[2][0].set_color(cl[2])
self.add(t1,s1,code1)
self.wait(0.5)
self.play(Transform(t1,t1c1,lag_ratio=0.1))
self.wait(.5)
self.play(FadeOut(code1,run_time=0.3))
self.play(TransformMatchingShapes(t1, t2c2, path_arc=PI/2))
self.play(FadeIn(code2,run_time=0.3))
self.play(Transform(t2c2,t2b,lag_ratio=0.1))
self.play(Transform(s1[:17],s2[:17]),Transform(s1[17:],s2[17:]))
self.wait(0.3)
self.play(Transform(t2c2,t2c1,lag_ratio=0.1))
self.wait(.2)
self.play(FadeOut(code2,run_time=0.3))
self.play(TransformMatchingTex(t2c2, t1c2, path_arc=PI/2))
self.play(FadeIn(code1,run_time=0.3))
self.play(Transform(t1c2,t1b,lag_ratio=0.1))
self.wait(0.5)
# 透過のサンプル ただしJupyter Notebook上ではオプションtを入れると見られなくなるので注意
from numpy.random import rand
class Transparent(Scene):
def construct(self):
def set_speed(mobject, d):
mobject.add_updater(lambda mobject, dt: mobject.shift(DOWN*d*dt))
g = VGroup()
for _ in range(200):
c = Circle(radius=rand()*.2+.2, color=random_color(),
fill_opacity=1).move_to([(rand()-.5)*20,rand()*5+5,0.])
set_speed(c, rand()*3)
g.add(c)
self.add(g)
self.wait(15)
# おまけ1
class D3Cayrey(Scene):
def construct(self):
p = Group()
p.add(Point(UP*3))
p.add(Point(np.array([-np.sqrt(3)/2,-.5,0])*3))
p.add(Point(np.array([ np.sqrt(3)/2,-.5,0])*3))
p.add(Point(UP*1.2))
p.add(Point(np.array([-np.sqrt(3)/2,-.5,0])*1.2))
p.add(Point(np.array([ np.sqrt(3)/2,-.5,0])*1.2))
t = Group()
t.add(Text("a b c").scale(.5).move_to(p[0]))
t.add(Text("c a b").scale(.5).move_to(p[1]))
t.add(Text("b c a").scale(.5).move_to(p[2]))
t.add(Text("a c b").scale(.5).move_to(p[3]))
t.add(Text("c b a").scale(.5).move_to(p[4]))
t.add(Text("b a c").scale(.5).move_to(p[5]))
se = [[0,1],[1,2],[2,0],[3,5],[5,4],[4,3],[0,3],[1,4],[2,5],[3,0],[4,1],[5,2]]
a = Group()
for i in range(len(se)):
a.add(Arrow(p[se[i][0]],p[se[i][1]],stroke_width=4,buff=.5))
for i in range(6,len(se)):
a[i].shift(a[i].copy().rotate(-PI/2).get_vector()*.15)
a[:6].set_color(YELLOW)
self.add(t,a)
# おまけ2 なんかかっこいいのつくろうとおもったつくりかけ
class SinRoll(Scene):
t = 0
v = 1
def construct(self):
circle = Circle()
line = Line(ORIGIN, UP)
dot = Dot(UP)
c = VGroup(circle, line, dot)
c.save_state()
def update_roll(mobj, dt):
self.t += dt * self.v
s = smooth(self.t)
mobj.restore()
mobj.shift(2*PI*s*RIGHT)
mobj.rotate(-2*PI*s)
tip = Dot(UP)
def update_tip(mobj, dt):
s = smooth(self.t)
mobj.move_to(2*PI*s*RIGHT + np.cos(2*PI*s)*UP)
path = VMobject()
path.set_points_as_corners([tip.get_center(), tip.get_center()])
def update_path(path):
previous_path = path.copy()
previous_path.add_points_as_corners([tip.get_center()])
path.become(previous_path)
arrows = [Arrow(1.5*DOWN, 1.5*UP, stroke_width=2, max_tip_length_to_length_ratio=0), Arrow(1.5*LEFT, 9*RIGHT, stroke_width=2, max_tip_length_to_length_ratio=0)]
self.add(c, tip, path)
self.play(GrowArrow(arrows[0], run_time=.5), GrowArrow(arrows[1], run_time=.7))
c.add_updater(update_roll)
path.add_updater(update_path)
tip.add_updater(update_tip)
self.wait(1./self.v + 0.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment