python-lottie  0.7.0+devbe239c9
A framework to work with lottie files and telegram animated stickers (tgs)
wagging.py
1 #!/usr/bin/env python3
2 import sys
3 import os
4 import math
5 sys.path.insert(0, os.path.join(
6  os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
7  "lib"
8 ))
9 from lottie.utils import script
10 from lottie import objects
11 from lottie.utils import animation as anutils
12 from lottie import Point, Color
13 
14 last_frame = 120
15 an = objects.Animation(last_frame)
16 layer = objects.ShapeLayer()
17 an.add_layer(layer)
18 
19 
20 def f(x):
21  return -64*x/400+64
22 
23 
24 def fp(x, offset):
25  return Point(x, offset+f(x))
26 
27 
28 def displace(f):
29  return Point(0, math.cos(f*math.pi*2)*80)
30 
31 
32 handle_length = 30
33 
34 
35 def setup(offset, exp):
36  g = layer.add_shape(objects.Group())
37  shape1 = g.add_shape(objects.Path())
38  bez1 = shape1.shape.value
39  g.transform.opacity.value = 60
40  g.add_shape(objects.Fill(Color(0, 1, 1)))
41 
42  g = layer.add_shape(objects.Group())
43  shape2 = g.add_shape(objects.Path())
44  bez2 = shape2.shape.value
45  g.add_shape(objects.Fill(Color(0, 0, 1)))
46 
47  for i in range(6):
48  p1 = Point(300/5 * i, offset-(1-(300/5 * i)/300)*16+16)
49  p2 = Point(400/5 * i, offset - f(400/5 * i))
50 
51  if i == 5:
52  t2 = Point(0, 0)
53  t1 = Point(0, 0)
54  else:
55  t2 = Point(400/5 * i - handle_length, offset - f(400/5 * i - handle_length)) - p2
56  t1 = Point(300/5 * i - handle_length, offset-(1-(300/5 * i - handle_length)/300)*16+16) - p1
57 
58  bez1.add_smooth_point(p1, t1)
59  bez2.add_smooth_point(p2, t2)
60 
61  for i in range(4, -1, -1):
62  p1 = fp(300/5 * i, offset)
63  t1 = fp(300/5 * i + handle_length, offset) - p1
64  bez1.add_smooth_point(p1, t1)
65 
66  p2 = fp(400/5 * i, offset)
67  t2 = fp(400/5 * i + handle_length, offset) - p2
68  bez2.add_smooth_point(p2, t2)
69 
70  displacer = anutils.FollowDisplacer(Point(400, offset), 200, displace, 0, last_frame, 10, exp)
71  displacer.animate_bezier(shape1.shape)
72  displacer.animate_bezier(shape2.shape)
73 
74 
75 setup(128, 1/2)
76 setup(256, 1)
77 setup(256+128, 2)
78 
79 
80 script.script_main(an)
def Point(x, y)
Definition: nvector.py:135