python-lottie  0.7.0+dev66cafb9
A framework to work with lottie files and telegram animated stickers (tgs)
layers.py
Go to the documentation of this file.
1 import warnings
2 from .base import LottieObject, LottieProp, PseudoBool, LottieEnum, LottieValueConverter
3 from .effects import Effect
4 from .helpers import Transform, Mask, VisualObject, BlendMode
5 from .shapes import ShapeElement
6 from .text import TextAnimatorData
7 from .properties import Value, MultiDimensional
8 from ..utils.color import Color, color_from_hex, color_to_hex
9 from .styles import LayerStyle
10 
11 
12 ## @ingroup Lottie
13 ## @todo SVG masks
15  Normal = 0
16  Alpha = 1
17  InvertedAlpha = 2
18  Luma = 3
19  InvertedLuma = 4
20 
21 
22 #ingroup Lottie
24  """!
25  Base class for all layers
26  """
27  _props = [
28  LottieProp("threedimensional", "ddd", PseudoBool, False),
29  LottieProp("hidden", "hd", bool, False),
30  LottieProp("is_guide", "guide", bool, False),
31  LottieProp("type", "ty", int, False),
32  LottieProp("index", "ind", int, False),
33  LottieProp("parent_index", "parent", int, False),
34  LottieProp("time_stretch", "sr", float, False),
35  LottieProp("in_point", "ip", float, False),
36  LottieProp("out_point", "op", float, False),
37  LottieProp("start_time", "st", float, False),
38  ]
39  ## %Layer type.
40  ## @see https://github.com/bodymovin/bodymovin-extension/blob/master/bundle/jsx/enums/layerTypes.jsx
41  type = None
42  _classses = {}
43 
44  def __init__(self):
45  super().__init__()
46 
47  ## Whether the layer is threedimensional
48  self.threedimensionalthreedimensional = 0
49  ## Whether the layer is hidden
50  self.hiddenhidden = None
51  ## Index that can be used for parenting and referenced in expressions
52  self.indexindex = None
53  ## Must be the `ind` property of another layer
54  self.parent_indexparent_index = None
55  self.time_stretchtime_stretch = 1
56  ## Frame when the layer becomes visible
57  self.in_pointin_point = None
58  ## Frame when the layer becomes invisible
59  self.out_pointout_point = None
60  ## Start Time of layer. Sets the start time of the layer.
61  self.start_timestart_time = 0
62  ## When true, the layer should not be rendered
63  self.is_guideis_guide = None
64 
65  @classmethod
66  def _load_get_class(cls, lottiedict):
67  if not Layer._classses:
68  Layer._classses = {
69  sc.type: sc
70  for sc in Layer.__subclasses__() + VisualLayer.__subclasses__()
71  if sc.type is not None
72  }
73  type_id = lottiedict["ty"]
74  if type_id not in Layer._classses:
75  warnings.warn("Unknown layer type: %s" % type_id)
76  return Layer
77  return Layer._classses[type_id]
78 
79 
80 ## @ingroup Lottie
82  """!
83  Base class for layers that have a visual component
84  """
85  _props = [
86  LottieProp("collapse_transform", "cp", bool, False),
87  LottieProp("transform", "ks", Transform, False),
88  LottieProp("auto_orient", "ao", PseudoBool, False),
89 
90  LottieProp("blend_mode", "bm", BlendMode, False),
91 
92  LottieProp("matte_mode", "tt", MatteMode, False),
93  LottieProp("matte_target", "td", int, False),
94  LottieProp("matte_parent", "tp", int, False),
95  LottieProp("css_class", "cl", str, False),
96  LottieProp("layer_xml_id", "ln", str, False),
97  LottieProp("layer_xml_tag_name", "tg", str, False),
98 
99  LottieProp("motion_blur", "mb", bool, False),
100  LottieProp("layer_style", "sy", LayerStyle, True),
101 
102  LottieProp("has_masks", "hasMask", bool, False),
103  LottieProp("masks", "masksProperties", Mask, True),
104  LottieProp("effects", "ef", Effect, True),
105  ]
106 
107  @property
108  def has_masks(self):
109  """!
110  Whether the layer has some masks applied
111  """
112  return bool(self.masksmasks) if getattr(self, "masks") is not None else None
113 
114  def __init__(self):
115  super().__init__()
116 
117  self.collapse_transformcollapse_transform = None
118  ## Transform properties
119  self.transformtransform = Transform()
120  ## Auto-Orient along path AE property.
121  self.auto_orientauto_orient = False
122 
123  ## CSS class used by the SVG renderer
124  self.css_classcss_class = None
125  ## `id` attribute used by the SVG renderer
126  self.layer_xml_idlayer_xml_id = None
127  ## tag name used by the SVG renderer
128  self.layer_xml_tag_namelayer_xml_tag_name = None
129 
130  ## Whether motion blur is enabled for the layer
131  self.motion_blurmotion_blur = None
132  ## Styling effects for this layer
133  self.layer_stylelayer_style = None
134 
135  ## List of Effects
136  self.effectseffects = None
137  ## Layer Time Stretching
138  self.stretchstretch = 1
139  ## List of Masks
140  self.masksmasks = None
141  ## Blend Mode
142  self.blend_modeblend_mode = BlendMode.Normal
143  ## Matte mode, the layer will inherit the transparency from the layer above
144  self.matte_modematte_mode = None
145  self.matte_targetmatte_target = None
146  self.matte_parentmatte_parent = None
147  ## Composition owning the layer, set by add_layer
148  self.compositioncomposition = None
149 
150  def add_child(self, layer):
151  if not self.compositioncomposition or self.indexindex is None:
152  raise Exception("Must set composition / index first")
153  self._child_inout_auto_child_inout_auto(layer)
154  self.compositioncomposition.add_layer(layer)
155  layer.parent_index = self.indexindex
156  return layer
157 
158  def _child_inout_auto(self, layer):
159  if layer.in_point is None:
160  layer.in_point = self.in_pointin_point
161  if layer.out_point is None:
162  layer.out_point = self.out_pointout_point
163 
164  @property
165  def parent(self):
166  if self.parent_indexparent_indexparent_index is None:
167  return None
168  return self.compositioncomposition.layer(self.parent_indexparent_indexparent_index)
169 
170  @parent.setter
171  def parent(self, layer):
172  if layer is None:
173  self.parent_indexparent_indexparent_index = None
174  else:
175  self.parent_indexparent_indexparent_index = layer.index
176  layer._child_inout_auto(self)
177 
178  @property
179  def children(self):
180  for layer in self.compositioncomposition.layers:
181  if layer.parent_index == self.indexindex:
182  yield layer
183 
184  def __repr__(self):
185  return "<%s %s %s>" % (type(self).__name__, self.indexindex, self.namename)
186 
187  def __str__(self):
188  return "%s %s" % (
189  self.namename or super().__str__(),
190  self.indexindex if self.indexindex is not None else ""
191  )
192 
193  def remove(self):
194  """!
195  @brief Removes this layer from the componsitin
196  """
197  self.compositioncomposition.remove_layer(self)
198 
199 
200 ## @ingroup Lottie
202  """!
203  Layer with no data, useful to group layers together
204  """
205  ## %Layer type.
206  type = 3
207 
208  def __init__(self):
209  super().__init__()
210 
211 
212 ## @ingroup Lottie
214  _props = [
215  LottieProp("data", "t", TextAnimatorData, False),
216  ]
217  ## %Layer type.
218  type = 5
219 
220  def __init__(self):
221  super().__init__()
222  ## Text Data
223  self.datadata = TextAnimatorData()
224 
225 
226 ## @ingroup Lottie
228  """!
229  Layer containing ShapeElement objects
230  """
231  _props = [
232  LottieProp("shapes", "shapes", ShapeElement, True),
233  ]
234  ## %Layer type.
235  type = 4
236 
237  def __init__(self):
238  super().__init__()
239  ## Shape list of items
240  self.shapesshapes = [] # ShapeElement
241 
242  def add_shape(self, shape):
243  self.shapesshapes.append(shape)
244  return shape
245 
246  def insert_shape(self, index, shape):
247  self.shapesshapes.insert(index, shape)
248  return shape
249 
250 
251 ## @ingroup Lottie
252 ## @todo SIF I/O
254  _props = [
255  LottieProp("image_id", "refId", str, False),
256  ]
257  ## %Layer type.
258  type = 2
259 
260  def __init__(self, image_id=""):
261  super().__init__()
262  ## id pointing to the source image defined on 'assets' object
263  self.image_idimage_id = image_id
264 
265 
266 ## @ingroup Lottie
268  _props = [
269  LottieProp("reference_id", "refId", str, False),
270  LottieProp("time_remapping", "tm", Value, False),
271  LottieProp("width", "w", int, False),
272  LottieProp("height", "h", int, False),
273  ]
274  ## %Layer type.
275  type = 0
276 
277  def __init__(self, reference_id=""):
278  super().__init__()
279  ## id pointing to the source composition defined on 'assets' object
280  self.reference_idreference_id = reference_id
281  ## Comp's Time remapping
282  self.time_remappingtime_remapping = None
283  ## Width
284  self.widthwidth = 512
285  ## Height
286  self.heightheight = 512
287 
288 
289 ColorString = LottieValueConverter(color_from_hex, color_to_hex, "Color string")
290 
291 
292 ## @ingroup Lottie
294  """!
295  Layer with a solid color rectangle
296  """
297  _props = [
298  LottieProp("color", "sc", ColorString, False),
299  LottieProp("height", "sh", float, False),
300  LottieProp("width", "sw", float, False),
301  ]
302  ## %Layer type.
303  type = 1
304 
305  def __init__(self, color=Color(), width=512, height=512):
306  super().__init__()
307  ## Color of the layer as a @c \#rrggbb hex
308  self.colorcolor = color
309  ## Height of the layer.
310  self.heightheight = height
311  ## Width of the layer.
312  self.widthwidth = width
313 
314 
315 #ingroup Lottie
317  """!
318  3D Camera
319  """
320  type = 13
321  _props = [
322  LottieProp("type", "ty", int, False),
323  LottieProp("transform", "ks", Transform, False),
324  LottieProp("perspective", "pe", Value, False),
325  ]
326 
327  def __init__(self):
328  super().__init__()
329 
330  ## Layer transform
331  self.transformtransform = None
332  ## Distance from the Z=0 plane.
333  ## Small values yield a higher perspective effect.
334  self.perspectiveperspective = None
335 
336 
337 #ingroup Lottie
339  _props = [
340  LottieProp("type", "ty", int, False),
341  LottieProp("data_source_id", "refId", str, False),
342  ]
343  type = 15
344 
345  def __init__(self):
346  super().__init__()
347 
348  ## ID of the data source in assets
349  self.data_source_iddata_source_id = None
350 
351 
352 #ingroup Lottie
354  _props = [
355  LottieProp("level", "lv", MultiDimensional, False),
356  ]
357 
358  def __init__(self):
359  super().__init__()
360 
361  self.levellevel = MultiDimensional([0, 0])
362 
363 
364 #ingroup Lottie
366  """!
367  A layer playing sounds
368  """
369  _props = [
370  LottieProp("type", "ty", int, False),
371  LottieProp("audio_settings", "au", AudioSettings, False),
372  LottieProp("sound_id", "refId", str, False),
373  ]
374  type = 6
375 
376  def __init__(self):
377  super().__init__()
378 
379  self.audio_settingsaudio_settings = None
380  ## ID of the sound as specified in the assets
381  self.sound_idsound_id = None
Base class for enum-like types in the Lottie JSON structure.
Definition: base.py:42
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:230
Lottie <-> Python property mapper.
Definition: base.py:88
Factory for property types that require special conversions.
Definition: base.py:64
A layer playing sounds.
Definition: layers.py:365
sound_id
ID of the sound as specified in the assets.
Definition: layers.py:381
perspective
Distance from the Z=0 plane.
Definition: layers.py:334
data_source_id
ID of the data source in assets.
Definition: layers.py:349
image_id
id pointing to the source image defined on 'assets' object
Definition: layers.py:263
def __init__(self, image_id="")
Definition: layers.py:260
Base class for all layers.
Definition: layers.py:23
in_point
Frame when the layer becomes visible.
Definition: layers.py:57
out_point
Frame when the layer becomes invisible.
Definition: layers.py:59
index
Index that can be used for parenting and referenced in expressions.
Definition: layers.py:52
hidden
Whether the layer is hidden.
Definition: layers.py:50
start_time
Start Time of layer.
Definition: layers.py:61
parent_index
Must be the ind property of another layer.
Definition: layers.py:54
threedimensional
Whether the layer is threedimensional.
Definition: layers.py:48
is_guide
When true, the layer should not be rendered.
Definition: layers.py:63
Layer with no data, useful to group layers together.
Definition: layers.py:201
reference_id
id pointing to the source composition defined on 'assets' object
Definition: layers.py:280
time_remapping
Comp's Time remapping.
Definition: layers.py:282
def __init__(self, reference_id="")
Definition: layers.py:277
Layer containing ShapeElement objects.
Definition: layers.py:227
def insert_shape(self, index, shape)
Definition: layers.py:246
shapes
Shape list of items.
Definition: layers.py:240
def add_shape(self, shape)
Definition: layers.py:242
Layer with a solid color rectangle.
Definition: layers.py:293
def __init__(self, color=Color(), width=512, height=512)
Definition: layers.py:305
height
Height of the layer.
Definition: layers.py:310
color
Color of the layer as a #rrggbb hex.
Definition: layers.py:308
Base class for layers that have a visual component.
Definition: layers.py:81
auto_orient
Auto-Orient along path AE property.
Definition: layers.py:121
layer_style
Styling effects for this layer.
Definition: layers.py:133
composition
Composition owning the layer, set by add_layer.
Definition: layers.py:148
transform
Transform properties.
Definition: layers.py:119
layer_xml_id
id attribute used by the SVG renderer
Definition: layers.py:126
stretch
Layer Time Stretching.
Definition: layers.py:138
matte_mode
Matte mode, the layer will inherit the transparency from the layer above.
Definition: layers.py:144
def has_masks(self)
Whether the layer has some masks applied.
Definition: layers.py:108
css_class
CSS class used by the SVG renderer.
Definition: layers.py:124
motion_blur
Whether motion blur is enabled for the layer.
Definition: layers.py:131
def remove(self)
Removes this layer from the componsitin.
Definition: layers.py:193
def add_child(self, layer)
Definition: layers.py:150
layer_xml_tag_name
tag name used by the SVG renderer
Definition: layers.py:128
def _child_inout_auto(self, layer)
Definition: layers.py:158
An animatable property that holds a NVector.
Definition: properties.py:482