这个版本已经几乎是无法优化的最佳版本了
函数部分
%%manim -v WARNING -ql s1 class s1(ThreeDScene): def construct(self): # 预定义 if True: r = RIGHT;l = LEFT;d = DOWN;u = UP;d = DEGREES;rr = RED;bb = BLUE;gg = GREEN;gg2 = GOLD;pp = PINK;tt = TEAL;h = SVGMobject("D:\manimSVG\constants\h.svg");h2o = SVGMobject("D:\manimSVG\constants\h2o.svg"); # 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.1) self.play(*a) self.wait(0.1) elif type(a) == set: self.clear() self.add(*a) else: self.wait(0.1) self.play(a) self.wait(0.1) # xxp molecule move def xmm(a,b,c): return a.animate.shift(xgd(b,c)) # 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: # shift def sh(*args): i = 1 for a in args: if i % 2 != 0: a.shift(args[i]) i += 1 # scale def sc(*args): i = 1 for a in args: if i % 2 != 0: a.scale(args[i]) i += 1 # coloring def c(*args): i = 1 for a in args: if i % 2 != 0: xcss(a, args[i]) i += 1 # Wait def w(t): return Wait(t) # 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) # execute narrator def ext(a): for i in range(len(a)): globals()[f't{i+1}']=xn(a[i+1]) # execute narrator 2 def exn(a): for i in range(len(a)): globals()[f'n{i+1}']=xn2(a[i+1]) # execute mobject def exm(name, num): for i in range(num): globals()[name + str(i+1)]=msm(name + str(i+1)) # ApplyMethod Shift def amsh(a, b): return ApplyMethod(a.shift,b) # ApplyMethod Scale def amsc(a, b): return ApplyMethod(a.scale,b)View Code
1、定义变量部分
对于同类型的变量(narrator、svg等)可以同时定义
ext({1:'love', 2:'is', 3:'boundless'})
2、变量预定义部分
对于同类型的变换(移动、旋转、上色等)可以同时定义
sh(t1,3*u,t2,2*r,t3,4*l)
3、动画部分
可以实现连续、同时开始的动画,并支持refresh(),但是注意animate方法在此处无法使用,用ApplyMethod方法替代
xxp(afi(t1),[lsh(t1,3*u),agf(t2)],aro(t3,PI),[lsh(t3,4*l),aro(t1,90*d),auw(t2)])
最后,可以优化的只有函数了,根据不同的动画,定义不同的函数才是优化的最终方向
标签:xxp,return,color,args,kwargs,2.0,def,manim From: https://www.cnblogs.com/daxiangcai/p/16630683.html