# cartoon transform def ct(names, *args): ''' names: a list of mobjects args represent lists, such as [0,1,[0,0,1,1,2,2]], the 1st and 2nd numbers represent the index of mobject in the names list ''' 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() ''' represent two groups of transforming ''' 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'ReplacementTransform({u}, {v}),' ''' bug here: can't use self-defined functions, eval() will throw an exception: NameError: xxx is not defined ''' 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
# cartoon transform 2 def ct2(a, b, c): ''' a and b represent mobjects c represents the corresponding transform parts ''' 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'ReplacementTransform({u}, {v}),' ag += item ag += ')' agg = eval(ag) return agg
由于这两个函数很不规范,使用eval()实现,这导致后期的优化很困难,做如下更改:
# cartoon transform def ct(names, *args): ''' names: a list of mobjects args represent lists, such as [0,1,[0,0,1,1,2,2]], the 1st and 2nd numbers represent the index of mobject in the names list ''' ''' get the transforming part ''' def pretrans0(name, i): if type(i) == int: back = name[i] elif type(i) == str: m = i.find('.') n1 = num(i[0:m]) n2 = num(i[m+1:]) back = name[n1:n2] return back def pretrans(name, i): if type(i) == int: back = name[i] elif type(i) == str: m = i.find('.') n1 = num(i[0:m]) n2 = num(i[m+1:]) back = name[n1:n2] elif type(i) == list: back = VGroup() for j in i: item = pretrans0(name, j) back.add(item) return back ag = [] # range(0, n-1)是错的 itemli = [] for l in names: itemli.append(list(range(len(l)))) ''' delete the tranformed part ''' 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() ''' two groups of transforming ''' for m in range(len(args)): n = 0 for i in args[m][2]: n += 1 if n % 2 != 0: u = pretrans(names[args[m][0]], i) countli(itemli[args[m][0]], i) set1.add(args[m][0]) else: v = pretrans(names[args[m][1]], i) item = at(u,v) ag.append(item) countli(itemli[args[m][1]], i) set2.add(args[m][1]) for i in set1: for ii in itemli[i]: # reverse GrowFromCenter不可用 ag.append(ast(names[i][ii])) for j in set2: for jj in itemli[j]: ag.append(agf(names[j][jj])) return AnimationGroup(*ag)
# cartoon transform 2 def ct2(a, b, c): ''' a and b represent mobjects c represents the corresponding transform parts ''' def pretrans0(name, i): if type(i) == int: back = name[i] elif type(i) == str: m = i.find('.') n1 = num(i[0:m]) n2 = num(i[m+1:]) back = name[n1:n2] return back def pretrans(name, i): if type(i) == int: back = name[i] elif type(i) == str: m = i.find('.') n1 = num(i[0:m]) n2 = num(i[m+1:]) back = name[n1:n2] elif type(i) == list: back = VGroup() for j in i: item = pretrans0(name, j) back.add(item) return back n = 0 ag = [] # range(0, n-1)是错的 for i in c: n += 1 if n % 2 != 0: u = pretrans(a, i) else: v = pretrans(b, i) item = at(u,v) ag.append(item) return AnimationGroup(ag)
并将ct2淘汰
标签:name,自定义,back,transform,重塑,str,n1,n2,type From: https://www.cnblogs.com/daxiangcai/p/16819118.html