python-lottie  0.6.10+dev2509936
A framework to work with lottie files and telegram animated stickers (tgs)
animation.py
Go to the documentation of this file.
1 from .base import LottieObject, LottieProp, PseudoBool, Index
2 from .layers import Layer
3 from .assets import Asset, Precomp
4 from .text import FontList, Chars
5 from .composition import Composition
6 from .helpers import VisualObject
7 
8 ##\defgroup Lottie Lottie
9 #
10 # Objects of the lottie file structure.
11 
12 ## \defgroup LottieCheck Lottie (to check)
13 #
14 # Lottie objects that have not been tested
15 
16 
17 ## @ingroup Lottie
19  """!
20  Document metadata
21  """
22  _props = [
23  LottieProp("author", "a", str, False),
24  LottieProp("generator", "g", str, False),
25  LottieProp("keywords", "k", str, True),
26  LottieProp("description", "d", str, False),
27  LottieProp("theme_color", "tc", str, False),
28  ]
29 
30  def __init__(self):
31  self.generator = None
32  self.author = None
33  self.keywords = None
34  self.description = None
35  self.theme_color = None
36 
37 
38 ## @ingroup Lottie
40  """!
41  Top level object, describing the animation
42 
43  @see http://docs.aenhancers.com/items/compitem/
44  """
45  _props = [
46  LottieProp("version", "v", str, False),
47  LottieProp("frame_rate", "fr", float, False),
48  LottieProp("in_point", "ip", float, False),
49  LottieProp("out_point", "op", float, False),
50  LottieProp("width", "w", int, False),
51  LottieProp("height", "h", int, False),
52  LottieProp("threedimensional", "ddd", PseudoBool, False),
53  LottieProp("assets", "assets", Asset, True),
54  #LottieProp("comps", "comps", Animation, True),
55  LottieProp("fonts", "fonts", FontList),
56  LottieProp("chars", "chars", Chars, True),
57  #LottieProp("markers", "markers", Marker, True),
58  #LottieProp("motion_blur", "mb", MotionBlur, False),
59  LottieProp("metadata", "meta", Metadata, False)
60  ]
61  _version = "5.5.2"
62 
63  def __init__(self, n_frames=60, framerate=60):
64  super().__init__()
65  ## The time when the composition work area begins, in frames.
66  self.in_point = 0
67  ## The time when the composition work area ends.
68  ## Sets the final Frame of the animation
69  self.out_point = n_frames
70  ## Frames per second
71  self.frame_rate = framerate
72  ## Composition Width
73  self.width = 512
74  ## Composition has 3-D layers
75  self.threedimensional = False
76  ## Composition Height
77  self.height = 512
78  ## Bodymovin Version
79  self.version = self._version
80  ## source items that can be used in multiple places. Comps and Images for now.
81  self.assets = [] # Image, Precomp
82  ## source chars for text layers
83  self.chars = None
84  ## Available fonts
85  self.fonts = None
86  self.metadata = None
87 
88  def precomp(self, name):
89  for ass in self.assets:
90  if isinstance(ass, Precomp) and ass.id == name:
91  return ass
92  return None
93 
94  def _on_prepare_layer(self, layer):
95  if layer.in_point is None:
96  layer.in_point = self.in_point
97  if layer.out_point is None:
98  layer.out_point = self.out_point
99 
100  def tgs_sanitize(self):
101  """!
102  Cleans up some things to ensure it works as a telegram sticker
103  """
104  if self.width != 512 or self.height != 512:
105  scale = min(512/self.width, 512/self.height)
106  self.width = self.height = 512
107 
108  for layer in self.layers:
109  if layer.parent_index:
110  continue
111 
112  if layer.transform.scale.animated:
113  for kf in layer.transform.scale.keyframes:
114  if kf.start is not None:
115  kf.start *= scale
116  if kf.end is not None:
117  kf.end *= scale
118  else:
119  layer.transform.scale.value *= scale
120 
121  if layer.transform.position.animated:
122  for kf in layer.transform.position.keyframes:
123  if kf.start is not None:
124  kf.start *= scale
125  if kf.end is not None:
126  kf.end *= scale
127  else:
128  layer.transform.position.value *= scale
129 
130  if self.frame_rate < 45:
131  self.frame_rate = 30
132  else:
133  self.frame_rate = 60
134 
135  def _fixup(self):
136  super()._fixup()
137  if self.assets:
138  for ass in self.assets:
139  if isinstance(ass, Precomp):
140  ass.animation = self
141  ass._fixup()
142 
143  def __str__(self):
144  return self.name or super().__str__()
lottie.objects.animation.Animation.__init__
def __init__(self, n_frames=60, framerate=60)
Definition: animation.py:63
lottie.objects.animation.Animation.threedimensional
threedimensional
Composition has 3-D layers.
Definition: animation.py:75
lottie.objects.animation.Metadata.description
description
Definition: animation.py:34
lottie.objects.animation.Metadata
Document metadata.
Definition: animation.py:18
lottie.objects.helpers.VisualObject.name
name
Definition: helpers.py:109
lottie.objects.animation.Animation.out_point
out_point
The time when the composition work area ends.
Definition: animation.py:69
lottie.objects.helpers.VisualObject
Definition: helpers.py:100
lottie.objects.animation.Animation.metadata
metadata
Definition: animation.py:86
lottie.objects.animation.Metadata.author
author
Definition: animation.py:32
lottie.objects.animation.Animation.height
height
Composition Height.
Definition: animation.py:77
lottie.objects.animation.Metadata.keywords
keywords
Definition: animation.py:33
lottie.objects.animation.Animation._version
string _version
Definition: animation.py:61
lottie.objects.animation.Animation
Top level object, describing the animation.
Definition: animation.py:39
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:224
lottie.objects.animation.Animation.tgs_sanitize
def tgs_sanitize(self)
Cleans up some things to ensure it works as a telegram sticker.
Definition: animation.py:100
lottie.objects.animation.Animation.__str__
def __str__(self)
Definition: animation.py:143
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
lottie.objects.animation.Animation.assets
assets
source items that can be used in multiple places.
Definition: animation.py:81
lottie.objects.composition.Composition
Base class for layer holders.
Definition: composition.py:6
lottie.objects.composition.Composition.layers
layers
List of Composition Layers.
Definition: composition.py:17
lottie.objects.animation.Animation.fonts
fonts
Available fonts.
Definition: animation.py:85
lottie.objects.animation.Animation.width
width
Composition Width.
Definition: animation.py:73
lottie.objects.animation.Metadata.generator
generator
Definition: animation.py:31
lottie.objects.animation.Animation.chars
chars
source chars for text layers
Definition: animation.py:83
lottie.objects.animation.Animation.in_point
in_point
The time when the composition work area begins, in frames.
Definition: animation.py:66
lottie.objects.animation.Animation.frame_rate
frame_rate
Frames per second.
Definition: animation.py:71
lottie.objects.animation.Metadata.theme_color
theme_color
Definition: animation.py:35
lottie.objects.animation.Animation.precomp
def precomp(self, name)
Definition: animation.py:88
lottie.objects.animation.Metadata.__init__
def __init__(self)
Definition: animation.py:30
lottie.objects.animation.Animation.version
version
Bodymovin Version.
Definition: animation.py:79