python-lottie  0.7.0+dev66cafb9
A framework to work with lottie files and telegram animated stickers (tgs)
envelope_deformation.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 
15 an = objects.Animation(60)
16 
17 layer = objects.ShapeLayer()
18 an.add_layer(layer)
19 
20 heart = objects.Bezier()
21 heart.add_point(Point(50, 20), Point(50, -20), Point(-50, -20))
22 heart.add_smooth_point(Point(0, 50), Point(-5, -10))
23 heart.add_smooth_point(Point(50, 100), Point(-10, 0))
24 heart.add_smooth_point(Point(100, 50), Point(-5, 10))
25 heart.closed = True
26 
27 g1 = layer.add_shape(objects.Group())
28 shape = g1.add_shape(objects.Path())
29 shape.shape.value = heart
30 fill = layer.add_shape(objects.Fill(Color(1, 0, 0)))
31 stroke = layer.add_shape(objects.Stroke(Color(0, 0, 0), 5))
32 
33 
34 
35 g2 = layer.add_shape(objects.Group())
36 bb = shape.bounding_box()
37 shapeb = g2.add_shape(objects.Path())
38 shapeb.shape.value.add_point(Point(bb.x1, bb.y1))
39 shapeb.shape.value.add_point(Point(bb.x2, bb.y1))
40 shapeb.shape.value.add_point(Point(bb.x2, bb.y2))
41 shapeb.shape.value.add_point(Point(bb.x1, bb.y2))
42 fill = layer.add_shape(objects.Fill([1, 1, 0]))
43 
44 
45 env = anutils.EnvelopeDeformation(Point(bb.x1, bb.y1), Point(bb.x2, bb.y2))
46 
47 env.add_keyframe(
48  0,
49  Point(256-128, 256-128),
50  Point(256+128, 256-128),
51  Point(256+128, 256+128),
52  Point(256-128, 256+128),
53 )
54 
55 env.add_keyframe(
56  30,
57  Point(256, 256-64),
58  Point(256, 256-128-64),
59  Point(256, 256+128+64),
60  Point(256, 256+64),
61 )
62 env.add_keyframe(
63  60,
64  Point(256+128, 256-128),
65  Point(256-128, 256-128),
66  Point(256-128, 256+128),
67  Point(256+128, 256+128),
68 )
69 
70 env.animate_bezier(shape.shape)
71 env.animate_bezier(shapeb.shape)
72 
73 
74 script.script_main(an)
75 
def Point(x, y)
Definition: nvector.py:135