manim 1.0
作者:互联网
代码结构:
%%manim -v WARNING -ql s1 class s1(Scene): def construct(self): if type('xxp') == str: if type('manim CE') == str: if type('other function') == str: def playscene1(): def playscene2(): def playscene3(): …View Code
其中,前面部分是自定义的函数以及重定义manim方法(使用缩写代替),后面部分是动画
函数:
# xxp if type('xxp') == str: # xxp transform def xt(names, *args): def pretrans0(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' return back def pretrans(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' elif type(i) == list: back = 'VGroup(' for j in i: item = pretrans0(name, j) + ',' back += item back += ')' return back ag = 'AnimationGroup(' # range(0, n-1)是错的 itemli = [] for l in names: itemli.append(list(range(len(l)))) def countli0(name, i): if type(i) == int: name.remove(i) elif type(i) == str: m = i.find('.') n1 = int(i[0:m]) n2 = int(i[m+1:])-1 p1 = name.index(n1) p2 = name.index(n2) del name[p1:p2+1] def countli(name, i): if type(i) == int: name.remove(i) elif type(i) == str: m = i.find('.') n1 = int(i[0:m]) n2 = int(i[m+1:])-1 p1 = name.index(n1) p2 = name.index(n2) del name[p1:p2+1] elif type(i) == list: for j in i: item = countli0(name, j) set1 = set() set2 = set() for m in range(len(args)): n = 0 for i in args[m][2]: n += 1 if n % 2 != 0: u = pretrans(f'names[args[{str(m)}][0]]', i) countli(itemli[args[m][0]], i) set1.add(args[m][0]) else: v = pretrans(f'names[args[{str(m)}][1]]', i) item = f'Transform({u}, {v}),' ag += item countli(itemli[args[m][1]], i) set2.add(args[m][1]) for i in set1: for ii in itemli[i]: # reverse GrowFromCenter不可用 ag += f'ShrinkToCenter(names[{str(i)}][{str(ii)}]), ' for j in set2: for jj in itemli[j]: ag += f'GrowFromCenter(names[{str(j)}][{str(jj)}]), ' ag += ')' agg = eval(ag) return agg # xxp transform 2 def xt2(a, b, c): def pretrans0(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' return back def pretrans(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' elif type(i) == list: back = 'VGroup(' for j in i: item = pretrans0(name, j) + ',' back += item back += ')' return back n = 0 ag = 'AnimationGroup(' # range(0, n-1)是错的 for i in c: n += 1 if n % 2 != 0: u = pretrans('a', i) else: v = pretrans('b', i) item = f'Transform({u}, {v}),' ag += item ag += ')' agg = eval(ag) return agg # xxp transform 3 def xt3(a,b): x=b.get_center()-a.get_center() y=b.width/a.width return AnimationGroup(a.animate.shift(x).scale(y)) # xxp color to hex def xc2h(c): return rgb_to_hex(color_to_rgb(c)) # xxp opacity scale def xos(inter, num, *args): # 最好让透明度也一样 def premovescale(a, b): # x = VGroup() # y = VGroup() for i in a: # 设置get_fill_opacity() == 0.5会失效 if i.get_fill_opacity() < 1: x = i # x.add(i) for j in b: if j.get_fill_opacity() < 1: y = j # y.add(j) scale = y.height/x.height a.scale(scale) # 先缩放再移动 vector = y.get_center() - x.get_center() a.shift(vector) if inter == 1: for n in range(len(args)): if n == 0: premovescale(args[n], Text('H', font='Times New Roman', fill_opacity=0.5).scale(num)) args[n].move_to(ORIGIN) else: premovescale(args[n], args[n-1]) if inter == 2: for n in range(len(args) - 1): premovescale(args[n+1], args[n]) # xxp color and opacity init def xcoi(*args): for a in args: # 先缩放再设置透明度(否则会消失不见) a.set_opacity(1) for i in a: if xc2h(i.color) == '#ff0000': i.set_color(RED).set_sheen(0.1) elif xc2h(i.color) == '#ffff00': i.set_color(GOLD).set_sheen(0.1) elif xc2h(i.color) == '#00ff00': i.set_color(GREEN).set_sheen(0.1) elif xc2h(i.color) == '#00ffff': i.set_color(TEAL).set_sheen(0.1) elif xc2h(i.color) == '#0000ff': i.set_color(BLUE).set_sheen(0.1) elif xc2h(i.color) == '#ff00ff': i.set_color(PINK).set_sheen(0.1) # xxp check def xc(*args): self.camera.background_color = WHITE for svg in args: if type(svg) == list: self.clear() for sss in svg: self.add(sss.set(color = RED)) for sss in svg: n = 0 for i in sss: num = Integer(number=n, stroke_width=2).set_color(BLACK).move_to(i) n += 1 self.add(num) self.wait() else: self.clear() self.add(svg.set(color = RED)) n = 0 for i in svg: num = Integer(number=n, stroke_width=2).set_color(BLACK).move_to(i) n += 1 self.add(num) self.wait() self.clear() # xxp align def xa(b, a): l = len(a) for i in range(0, l): b[i].set_x(a[i].get_x()) if i > 0: b[i].set_y(b[0].get_y()) # xxp find def xf(a, b): n1 = str.find(a.text, b) n2 = n1 + len(b) return a[n1:n2] # xxp narrator def xn(a): text = Text(a, font='STZhongsong').to_edge(DOWN).scale(0.6) return text # xxp narrator fangsong def xnf(a): text = Text(a, font='STFangsong').scale(0.7).to_edge(DOWN) return text # xxp upnarrator def xun(a): text = Text(a, font='STZhongsong').to_edge(UP).scale(0.8) return text # xxp para def xp(x, y): a = Text(y, font="STZhongsong").to_edge(DOWN).scale(0.6) b = Text(x, font="STZhongsong").to_edge(1.8*DOWN).scale(0.6) return VGroup(a, b) # xxp scale def xs(a, n1, b, n2): scale = b[n2].width/a[n1].width a.scale(scale) # xxp move def xm(a, n1, b, n2): vect = b[n2].get_center() - a[n1].get_center() a.shift(vect) # xxp move and scale def xms(a, n1, b, n2): scale = b[n2].width/a[n1].width a.scale(scale) vect = b[n2].get_center() - a[n1].get_center() a.shift(vect) # xxp choose color def xcc(a, b): for i in a: if rgb_to_hex(color_to_rgb(i.color)) == b: return i # xxp narrator wait def xnw(a): self.wait(0.1*len(a)) # xxp text flower def xtf(a): at = Text(a, font = 'STZhongsong',stroke_width=2).set_color(color = ['#f3e9e0', '#fea8a9']) # at[0].set_color(color = ['#f3e9e0', '#fea8a9']) # at[1].set_color(color = ['#f3e9e0', '#fea8a9']) fl = msm('icon')[8].rotate(PI/2).set_color(color = ['#fea8a9', '#f3e9e0']).scale(1.5) at.next_to(fl, RIGHT) vg = VGroup(fl, at) vg.set(height = 0.6).to_corner(UL) return vg # xxp text flower2 def xtf2(judge, a): if judge == 1: at = Text(a, font = 'STFangsong',stroke_width=2).set_color(color = ['#f3e9e0', '#b1d85c']) elif judge == 2: # 一般不是中文 at = MarkupText(a, stroke_width=2).set_color(color = ['#f3e9e0', '#b1d85c']) # at[0].set_color(color = ['#f3e9e0', '#fea8a9']) # at[1].set_color(color = ['#f3e9e0', '#fea8a9']) fl = msm('icon')[8].rotate(PI/2).set_color(color = ['#b1d85c', '#f3e9e0']).scale(1.5) at.next_to(fl, RIGHT) vg = VGroup(fl, at) vg.set(height = 0.6).to_corner(UL).shift(2*RIGHT) return vg # xxp text flower3 def xtf3(a): at = Text(a, font = 'STZhongsong',stroke_width=2).set_color(color = ['#ccffff', '#afccff']) # at[0].set_color(color = ['#f3e9e0', '#fea8a9']) # at[1].set_color(color = ['#f3e9e0', '#fea8a9']) fl = msm('icon')[8].rotate(PI/2).set_color(color = ['#afccff', '#ccffff']).scale(1.5) at.next_to(fl, RIGHT) vg = VGroup(fl, at) vg.set(height = 0.6).to_corner(UL).shift(2*DOWN) return vg # xxp color move and scale def xcms(a, b): x = VGroup() y = VGroup() for i in a: if rgb_to_hex(color_to_rgb(i.color)) == '#000000': x.add(i) for j in b: if rgb_to_hex(color_to_rgb(j.color)) == '#000000': y.add(j) scale = y.width/x.width vect = y.get_center() - x.get_center() a.shift(vect) a.scale(scale) # xxp single scale def xss(a, n): x = Text('C', font="Times New Roman") y = a[n] scale = x.width/y.width a.scale(scale) # xxp pre postion def xpp(a, pos): d1 = Dot().to_corner(UL) d2 = Dot().to_edge(UP) d3 = Dot().to_corner(UR) d4 = Dot().to_edge(LEFT) d5 = Dot() d6 = Dot().to_edge(RIGHT) d7 = Dot().to_corner(DL) d8 = Dot().to_edge(DOWN) d9 = Dot().to_corner(DR) if pos == 'ul': a.move_to((d1.get_center() + d5.get_center())/2) elif pos == 'u': a.move_to((d2.get_center() + d5.get_center())/2) elif pos == 'ur': a.move_to((d3.get_center() + d5.get_center())/2) elif pos == 'l': a.move_to((d4.get_center() + d5.get_center())/2) elif pos == 'o': a.move_to(d5.get_center()) elif pos == 'r': a.move_to((d6.get_center() + d5.get_center())/2) elif pos == 'dl': a.move_to((d7.get_center() + d5.get_center())/2) elif pos == 'd': a.move_to((d8.get_center() + d5.get_center())/2) elif pos == 'dr': a.move_to((d9.get_center() + d5.get_center())/2) # xxp arrange and align def xaa(mobs, b, d): vg = VGroup() for m in mobs: vg.add(m) vg.arrange(buff=b) if d == 'u': dire = UP elif d == 'd': dire = DOWN for i in mobs: if i != mobs[0]: i.align_to(mobs[0], dire) # xxp add other def xao(*args): if len(args) == 2: sa(args[0].to_corner(UR), args[1].to_corner(DL)) if len(args) == 3: sa(args[0].to_corner(UR), args[1].to_corner(DL), args[2].to_corner(DR)) # xxp grow from center def xgf(a, li): avg = 'AnimationGroup(' def pregrow(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' return back for j in li: partani = pregrow('a', j) avg += f'GrowFromCenter({partani}), ' avg += ')' return eval(avg) # xxp reverse grow from center def xrgf(a, li): avg = 'AnimationGroup(' def pregrow(name, i): if type(i) == int: back = name + '[' + str(i) + ']' elif type(i) == str: m = i.find('.') n1 = i[0:m] n2 = i[m+1:] back = name + '[' + n1 + ':' + n2 + ']' return back for j in li: partani = pregrow('a', j) avg += f'GrowFromCenter({partani}, reverse_rate_function = True), ' avg += ')' return eval(avg) # xxp color and sheen static def xcss(a, b): a.set_color(b).set_sheen(0.1) # xxp place in line def xpl(*args): vg = VGroup() frame = FullScreenRectangle() wid = frame.width for i in args: vg.add(i.copy()) wid -= i.width mybuff = wid/(len(args)+1) vg.arrange(buff=mybuff) for j in range(len(args)): args[j].match_x(vg[j]) # xxp place in grid def xpg(*args): n = 0 full = FullScreenRectangle() li = ['ul','ur','dl','dr'] for a in args: if a.width > a.height: a.set(width = full.width/3) xpp(a,li[n]) else: a.set(height = full.height/3) xpp(a,li[n]) n += 1 # xxp color 1 def xc1(*args): for a in args: a.save_state() a.set_color(WHITE).set_sheen(0) # xxp color 2 def xc2(*args): li = [] for a in args: li.append(Restore(a)) return self.play(*li) # xxp color 3 def xc3(*args): li = [] for a in args: li.append(a.animate.set_color(WHITE)) return self.play(*li) # xxp animations list def xal(t,*argss): args = [] for i in argss: args.append(i) # 不是args = atgs.reverse() args.reverse() n = 0 for a in args: if n == 1: ag = AnimationGroup(args[1],args[0],lag_ratio=t) elif n > 1: ag = AnimationGroup(a,ag,lag_ratio=t) n += 1 return ag # xxp sub number def xsn(*args): vg = VGroup() for i in range(len(args)): n = Integer(i+1).next_to(args[i],0.7*UP) xcss(n, BLUE) vg.add(n) return vg # xxp get distance def xgd(a,b): return b.get_center() - a.get_center() # xxp get distance by codrdinate def xgdc(a,b): return b - a.get_center() # xxp text flower list def xtfl(a,*args): # for a in args: vg = VGroup() li0 = xtf(a) vg.add(li0) n = 1 for a in args: li = xtf2(1,a) xm(li,0,li0,0);li.shift(n*0.9*DOWN+RIGHT) vg.add(li) n += 1 return vg # xxp text flower list abbr def xtfla(a,b): # for a in args: vg = VGroup() li0 = xtf(a) n = 1 li1 = xtf2(1,'');xm(li1,0,li0,0);li1.shift(DOWN+RIGHT) vg.add(li0,li1) for i in range(b-1): li = xtf2(1,'') xm(li,0,li1,0);li.shift(n*0.2*DOWN) vg.add(li) n += 1 return vg # xxp corner check def xcc(a,b): a.to_corner(UL);b.to_corner(DR) xc([a,b]) # xxp edge check def xec(a,b): a.to_edge(UP);b.to_edge(DOWN) xc([a,b]) # xxp grid check def xgc(*args): xpg(*args) xc([*args]) # xxp orbit color def xoc(a): a.set_color([BLUE_C, BLUE_E]).set_sheen(0.3).set_opacity(0.8) # manim CE if type('manim CE') == str: # self.play() def sp(*args, **kwargs): return self.play(*args, **kwargs) # self.wait() def sw(*args, **kwargs): return self.wait(*args, **kwargs) # self.add() def sa(*args, **kwargs): return self.add(*args, **kwargs) # self.clear() def sc(*args, **kwargs): return self.clear(*args, **kwargs) # self.remove() def sr(*args, **kwargs): return self.remove(*args, **kwargs) # Animation Transform() def at(a, b, *args, **kwargs): return Transform(a, b, *args, **kwargs) # Animation ReplacementTransform() def art(a, b, *args, **kwargs): return ReplacementTransform(a, b, *args, **kwargs) # Animation TransformMatchingShapes() def atm(a, b, *args, **kwargs): return TransformMatchingShapes(a, b, *args, **kwargs) # Animation ClockwiseTransform() def act(a, b, *args, **kwargs): return ClockwiseTransform(a, b, *args, **kwargs) # Animation CounterclockwiseTransform() def acct(a, b, *args, **kwargs): return CounterclockwiseTransform(a, b, *args, **kwargs) # Animation Write() def aw(a, *args, **kwargs): return Write(a, *args, **kwargs) # Animation LaggedStart() def als(a, *args, **kwargs): return LaggedStart(a, *args, **kwargs) # Animation Unwrite() def auw(a, *args, **kwargs): return Unwrite(a, *args, **kwargs) # Animation FadeOut() def afo(a, *args, **kwargs): return FadeOut(a, *args, **kwargs) # Animation FadeIn() def afi(a, *args, **kwargs): return FadeIn(a, *args, **kwargs) # Animation Restore() def ar(a, *args, **kwargs): return Restore(a, *args, **kwargs) # Animation DrawBorderThenFill() def adb(a, *args, **kwargs): return DrawBorderThenFill(a, *args, **kwargs) # Animation DrawBorderThenFill() reverse def adbr(a, *args, **kwargs): return DrawBorderThenFill(a, *args, **kwargs, reverse_rate_function = True) # Animation GrowFromCenter() def agf(a, *args, **kwargs): return GrowFromCenter(a, *args, **kwargs) # Animation MoveAlongPath() def ama(a, b, *args, **kwargs): return MoveAlongPath(a, b, *args, **kwargs) # Animation SpinInFromNothing def asi(a, *args, **kwargs): return SpinInFromNothing(a, angle=2 * PI, *args, **kwargs) # Animation SpinInFromNothing reverse def asir(a, *args, **kwargs): return SpinInFromNothing(a, angle=2 * PI, *args, **kwargs, reverse_rate_function = True) # Animation ShrinkToCenter def ast(a, *args, **kwargs): return ShrinkToCenter(a, *args, **kwargs) # Animation Circumscribe def ac(a, *args, **kwargs): return Circumscribe(a, *args, **kwargs) # Animation Indicate def ai(a, *args, **kwargs): return Indicate(a, *args, **kwargs) # Group sheen def gs(*args): li = [] for i in args: li.append(i.animate.set_sheen(0.1)) return AnimationGroup(*li, run_time=0.5) # Group color def gc(*args): li = [] for i in range(len(args)): if i % 2 == 0: li.append(args[i].animate.set_color(args[i+1])) return AnimationGroup(*li) # mobject Text def mt(text, *args, **kwargs): return Text(text, *args, **kwargs) # mobject SVGMobject def msm(name, *args, **kwargs): sm = SVGMobject(f'D:\\manimSVG\\{name}.svg') return sm # mobject MathTex def mmt(text, *args, **kwargs): return MathTex(text, *args, **kwargs) # mobject VGroup def mvg(*args, **kwargs): return VGroup(*args, **kwargs) # mobject ArcBetweenPoints def mab(a, b, *args, **kwargs): return ArcBetweenPoints(a.get_center(), b.get_center(), *args, **kwargs) # other function if type('other function') == str: # refresh def refresh(*args, **kwargs): sc() sa(*args, **kwargs) # color refresh def crefresh(*args, **kwargs): sc() for i in args: xcoi(i) sa(*args, **kwargs)View Code
动画主要包括两部分:
1、Mobject的定义和初始化(包括缩放、上色、移动位置等)
2、Mobject的动画
标签:set,1.0,color,args,kwargs,return,def,manim 来源: https://www.cnblogs.com/daxiangcai/p/16607637.html