python-lottie  0.6.11+devc144cca
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, PreCompLayer
3 from .assets import Asset, Precomp
4 from .text import FontList, Chars
5 from .composition import Composition
6 from .helpers import VisualObject, Marker
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  Motion blur settings
42  """
43  _props = [
44  LottieProp("shutter_angle", "sa", float, False),
45  LottieProp("shutter_phase", "sp", float, False),
46  LottieProp("samples_per_frame", "spf", float, False),
47  LottieProp("adaptive_sample_limit", "asl", float, False),
48  ]
49 
50  def __init__(self):
51  super().__init__()
52 
53  ## Angle in degrees
54  self.shutter_angle = None
55  ## Angle in degrees
56  self.shutter_phase = None
57  self.samples_per_frame = None
59 
60 
61 ## @ingroup Lottie
63  """!
64  Top level object, describing the animation
65 
66  @see http://docs.aenhancers.com/items/compitem/
67  """
68  _props = [
69  LottieProp("version", "v", str, False),
70  LottieProp("frame_rate", "fr", float, False),
71  LottieProp("in_point", "ip", float, False),
72  LottieProp("out_point", "op", float, False),
73  LottieProp("width", "w", int, False),
74  LottieProp("height", "h", int, False),
75  LottieProp("threedimensional", "ddd", PseudoBool, False),
76  LottieProp("assets", "assets", Asset, True),
77  #LottieProp("comps", "comps", Animation, True),
78  LottieProp("fonts", "fonts", FontList),
79  LottieProp("chars", "chars", Chars, True),
80  LottieProp("markers", "markers", Marker, True),
81  LottieProp("motion_blur", "mb", MotionBlur, False),
82  LottieProp("metadata", "meta", Metadata, False)
83  ]
84  _version = "5.5.2"
85 
86  def __init__(self, n_frames=60, framerate=60):
87  super().__init__()
88  ## The time when the composition work area begins, in frames.
89  self.in_point = 0
90  ## The time when the composition work area ends.
91  ## Sets the final Frame of the animation
92  self.out_point = n_frames
93  ## Frames per second
94  self.frame_rate = framerate
95  ## Composition Width
96  self.width = 512
97  ## Composition has 3-D layers
98  self.threedimensional = False
99  ## Composition Height
100  self.height = 512
101  ## Bodymovin Version
102  self.version = self._version
103  ## source items that can be used in multiple places. Comps and Images for now.
104  self.assets = [] # Image, Precomp
105  ## source chars for text layers
106  self.chars = None
107  ## Available fonts
108  self.fonts = None
109  self.metadata = None
110  self.motion_blur = None
111  self.markers = None
112 
113  def precomp(self, name):
114  for ass in self.assets:
115  if isinstance(ass, Precomp) and ass.id == name:
116  return ass
117  return None
118 
119  def _on_prepare_layer(self, layer):
120  if layer.in_point is None:
121  layer.in_point = self.in_point
122  if layer.out_point is None:
123  layer.out_point = self.out_point
124 
125  def to_precomp(self):
126  """!
127  Turns the main comp into a precomp
128  """
129  precomp = Precomp()
130  #precomp.frame_rate = self.frame_rate
131  precomp.layers = self.layers
132  precomp.name = self.name
133  name_id = 0
134  base_name = self.name or "Animation"
135  name = base_name
136  index = 0
137  while True:
138  if index >= len(self.assets):
139  break
140 
141  while self.assets[index].id == name:
142  name_id += 1
143  name = "%s %s" % (base_name, name_id)
144  index = -1
145 
146  index += 1
147  precomp.id = name
148  self.assets.append(precomp)
149 
150  precomp_layer = PreCompLayer()
151  precomp_layer.width = self.width
152  precomp_layer.height = self.height
153  precomp_layer.in_point = self.in_point
154  precomp_layer.out_point = self.out_point
155  precomp_layer.reference_id = name
156  self.layers = [precomp_layer]
157  return precomp
158 
159  def scale(self, width, height):
160  """!
161  Scales the animation so it fits in width/height
162  """
163  if self.width != width or self.height != height:
164  self.to_precomp()
165 
166  scale = min(width/self.width, height/self.height)
167  self.width = width
168  self.height = height
169 
170  self.layers[0].transform.scale.value *= scale
171 
172  def tgs_sanitize(self):
173  """!
174  Cleans up some things to ensure it works as a telegram sticker
175  """
176  self.scale(512, 512)
177 
178  if self.frame_rate < 45:
179  self.frame_rate = 30
180  else:
181  self.frame_rate = 60
182 
183  def _fixup(self):
184  super()._fixup()
185  if self.assets:
186  for ass in self.assets:
187  if isinstance(ass, Precomp):
188  ass.animation = self
189  ass._fixup()
190 
191  def __str__(self):
192  return self.name or super().__str__()
lottie.objects.animation.Animation.__init__
def __init__(self, n_frames=60, framerate=60)
Definition: animation.py:86
lottie.objects.animation.Animation.threedimensional
threedimensional
Composition has 3-D layers.
Definition: animation.py:98
lottie.objects.assets.Precomp
Definition: assets.py:143
lottie.objects.animation.MotionBlur.shutter_angle
shutter_angle
Angle in degrees.
Definition: animation.py:54
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:92
lottie.objects.helpers.VisualObject
Definition: helpers.py:100
lottie.objects.animation.Animation.metadata
metadata
Definition: animation.py:109
lottie.objects.animation.Metadata.author
author
Definition: animation.py:32
lottie.objects.animation.Animation.height
height
Composition Height.
Definition: animation.py:100
lottie.objects.animation.Metadata.keywords
keywords
Definition: animation.py:33
lottie.objects.animation.Animation._version
string _version
Definition: animation.py:84
lottie.objects.animation.Animation.scale
def scale(self, width, height)
Scales the animation so it fits in width/height.
Definition: animation.py:159
lottie.objects.animation.Animation
Top level object, describing the animation.
Definition: animation.py:62
lottie.objects.animation.MotionBlur
Motion blur settings.
Definition: animation.py:39
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:225
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:172
lottie.objects.animation.Animation.__str__
def __str__(self)
Definition: animation.py:191
lottie.objects.animation.MotionBlur.adaptive_sample_limit
adaptive_sample_limit
Definition: animation.py:58
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
lottie.objects.layers.PreCompLayer
Definition: layers.py:233
lottie.objects.animation.Animation.assets
assets
source items that can be used in multiple places.
Definition: animation.py:104
lottie.objects.composition.Composition
Base class for layer holders.
Definition: composition.py:6
lottie.objects.animation.MotionBlur.shutter_phase
shutter_phase
Angle in degrees.
Definition: animation.py:56
lottie.objects.animation.Animation.fonts
fonts
Available fonts.
Definition: animation.py:108
lottie.objects.animation.Animation.width
width
Composition Width.
Definition: animation.py:96
lottie.objects.animation.Metadata.generator
generator
Definition: animation.py:31
lottie.objects.animation.Animation.chars
chars
source chars for text layers
Definition: animation.py:106
lottie.objects.animation.Animation.in_point
in_point
The time when the composition work area begins, in frames.
Definition: animation.py:89
lottie.objects.animation.Animation.layers
layers
Definition: animation.py:156
lottie.objects.animation.MotionBlur.samples_per_frame
samples_per_frame
Definition: animation.py:57
lottie.objects.animation.Animation.frame_rate
frame_rate
Frames per second.
Definition: animation.py:94
lottie.objects.animation.Metadata.theme_color
theme_color
Definition: animation.py:35
lottie.objects.animation.Animation.precomp
def precomp(self, name)
Definition: animation.py:113
lottie.objects.animation.Metadata.__init__
def __init__(self)
Definition: animation.py:30
lottie.objects.animation.Animation.version
version
Bodymovin Version.
Definition: animation.py:102
lottie.objects.animation.Animation.to_precomp
def to_precomp(self)
Turns the main comp into a precomp.
Definition: animation.py:125
lottie.objects.animation.MotionBlur.__init__
def __init__(self)
Definition: animation.py:50
lottie.objects.animation.Animation.motion_blur
motion_blur
Definition: animation.py:110
lottie.objects.animation.Animation.markers
markers
Definition: animation.py:111