manim CE
# 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 ReplacementTransform(a, b, *args, **kwargs) # Animation Transform() def at0(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 FadeOut() def afoo(a, *args, **kwargs): return FadeOut(a, *args, shift=DOWN, **kwargs) # Animation FadeIn() def afii(a, *args, **kwargs): return FadeIn(a, *args, shift=DOWN, **kwargs) # Animation Restore() def are(a, *args, **kwargs): return Restore(a, *args, **kwargs) # Animation Rotate() def aro(a, b, *args, **kwargs): return Rotate(a, about_point = b.get_center(), *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 GrowFromEdge() def agfe(a, b, *args, **kwargs): return GrowFromEdge(a, b, *args, **kwargs) # Animation GrowFromPoint() def agfp(a, b, *args, **kwargs): return GrowFromPoint(a, b, *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) # Animation Restore def ar(a, *args, **kwargs): return Restore(a, *args, **kwargs) # Animation Shift def ash(a, b, *args, **kwargs): return ApplyMethod(a.shift, b, *args, **kwargs) # Animation Succession def asu(*args): return Succession(*args) # Animation moveto def amt(a, b, *args, **kwargs): return ApplyMethod(a.move_to, b, *args, **kwargs) # Animation scale def asc(a, b, *args, **kwargs): return ApplyMethod(a.scale, b, *args, **kwargs) # Animation coloring def ac(a, b, *args, **kwargs): return ApplyMethod(a.set_color, b, *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 SVGMobject constant def msmc(name, *args, **kwargs): sm = SVGMobject(f'D:\\manimSVG\\constants\\{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(), angle = 120*DEGREES, *args, **kwargs) # mobject DashedLine def mdl(a, b, *args, **kwargs): return DashedLine(a, b, *args, **kwargs)View Code
xxp
# 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 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 opacity scale def xos(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) args[0].scale(num) for n in range(len(args) - 1): premovescale(args[n+1], args[n]) xcoi(*args) # 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 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 2 def xn2(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 li # xxp color 3 def xc3(*args): li = [] for a in args: # 不可使用animate li.append(ApplyMethod(a.set_color, WHITE)) return 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 coodrdinate 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) # xxp play def xxp(*args): for a in args: if type(a) == list: self.wait(0.5) self.play(*a) self.wait(0.5) elif type(a) == set: self.clear() self.add(*a) elif type(a) == tuple: for i in a: self.play(i) else: self.wait(0.5) self.play(a) self.wait(0.5) # xxp play no time gap def xxpn(*args): for a in args: if type(a) == list: self.play(*a) elif type(a) == set: self.clear() self.add(*a) elif type(a) == tuple: for i in a: self.play(i) else: self.play(a) # xxp molecule move def xmm(a,b,c): return a.animate.shift(xgd(b,c)) # xxp markup text def xmt(a): # 最多不能超过三个 for i in range(5): a = a.replace('_','<sub>',1) a = a.replace('_','</sub>',1) a = a.replace('^','<sup>',1) a = a.replace('^','</sup>',1) return MarkupText(a) # xxp move along arc def xmaa(a,b,c,s): arc = ArcBetweenPoints(b.get_center(), c.get_center(), angle = s*120*DEGREES) return MoveAlongPath(a, arc) # xxp dot group def xdg(num,r1,r2): vg = VGroup() for i in range(num): r = np.random.rand()*(r2-r1) + r1 t = np.random.rand()*2*PI vg.add(Circle(radius=0.05, color=BLUE_B, fill_opacity=1).shift(r*np.cos(t)*RIGHT+r*np.sin(t)*UP)) return vgView Code
标签:functions,xxp,return,color,args,kwargs,def,manim From: https://www.cnblogs.com/daxiangcai/p/16796087.html