python-lottie  0.6.11+devc144cca
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
8 from ..utils.color import Color, color_from_hex, color_to_hex
9 
10 
11 ## @ingroup Lottie
12 ## @todo SVG masks
14  Normal = 0
15  Alpha = 1
16  InvertedAlpha = 2
17  Luma = 3
18  InvertedLuma = 4
19 
20 
21 ## @ingroup Lottie
23  _props = [
24  LottieProp("threedimensional", "ddd", PseudoBool, False),
25  LottieProp("hidden", "hd", bool, False),
26  LottieProp("type", "ty", int, False),
27  LottieProp("parent_index", "parent", int, False),
28 
29  LottieProp("stretch", "sr", float, False),
30  LottieProp("transform", "ks", Transform, False),
31  LottieProp("auto_orient", "ao", PseudoBool, False),
32 
33  LottieProp("in_point", "ip", float, False),
34  LottieProp("out_point", "op", float, False),
35  LottieProp("start_time", "st", float, False),
36  LottieProp("blend_mode", "bm", BlendMode, False),
37 
38  LottieProp("matte_mode", "tt", MatteMode, False),
39  LottieProp("index", "ind", int, False),
40  LottieProp("css_class", "cl", str, False),
41  LottieProp("layer_xml_id", "ln", str, False),
42  LottieProp("motion_blur", "mb", bool, False),
43 
44  LottieProp("has_masks", "hasMask", bool, False),
45  LottieProp("masks", "masksProperties", Mask, True),
46  LottieProp("effects", "ef", Effect, True),
47  LottieProp("matte_target", "td", int, False),
48  ]
49  ## %Layer type.
50  ## @see https://github.com/bodymovin/bodymovin-extension/blob/master/bundle/jsx/enums/layerTypes.jsx
51  type = None
52  _classses = {}
53 
54  @property
55  def has_masks(self):
56  """!
57  Whether the layer has some masks applied
58  """
59  return bool(self.masks) if getattr(self, "masks") is not None else None
60 
61  def __init__(self):
62  super().__init__()
63  ## Transform properties
65  ## Auto-Orient along path AE property.
66  self.auto_orient = False
67  ## 3d layer flag
68  self.threedimensional = False
69  ## Hidden layer
70  self.hidden = None
71  ## Layer index in AE. Used for parenting and expressions.
72  self.index = None
73 
74  ## CSS class used by the SVG renderer
75  self.css_class = None
76  ## `id` attribute used by the SVG renderer
77  self.layer_xml_id = None
78  ## Whether motion blur is enabled for the layer
79  self.motion_blur = None
80 
81  ## In Point of layer. Sets the initial frame of the layer.
82  self.in_point = None
83  ## Out Point of layer. Sets the final frame of the layer.
84  self.out_point = None
85  ## Start Time of layer. Sets the start time of the layer.
86  self.start_time = 0
87  ## List of Effects
88  self.effects = None
89  ## Layer Time Stretching
90  self.stretch = 1
91  ## Layer Parent. Uses ind of parent.
92  self.parent_index = None
93  ## List of Masks
94  self.masks = None
95  ## Blend Mode
96  self.blend_mode = BlendMode.Normal
97  ## Matte mode, the layer will inherit the transparency from the layer above
98  self.matte_mode = None
99  self.matte_target = None
100  ## Composition owning the layer, set by add_layer
101  self.composition = None
102 
103  def add_child(self, layer):
104  if not self.composition or self.index is None:
105  raise Exception("Must set composition / index first")
106  self._child_inout_auto(layer)
107  self.composition.add_layer(layer)
108  layer.parent_index = self.index
109  return layer
110 
111  def _child_inout_auto(self, layer):
112  if layer.in_point is None:
113  layer.in_point = self.in_point
114  if layer.out_point is None:
115  layer.out_point = self.out_point
116 
117  @property
118  def parent(self):
119  if self.parent_index is None:
120  return None
121  return self.composition.layer(self.parent_index)
122 
123  @parent.setter
124  def parent(self, layer):
125  if layer is None:
126  self.parent_index = None
127  else:
128  self.parent_index = layer.index
129  layer._child_inout_auto(self)
130 
131  @property
132  def children(self):
133  for layer in self.composition.layers:
134  if layer.parent_index == self.index:
135  yield layer
136 
137  @classmethod
138  def _load_get_class(cls, lottiedict):
139  if not Layer._classses:
140  Layer._classses = {
141  sc.type: sc
142  for sc in Layer.__subclasses__()
143  }
144  type_id = lottiedict["ty"]
145  if type_id not in Layer._classses:
146  warnings.warn("Unknown layer type: %s" % type_id)
147  return Layer
148  return Layer._classses[type_id]
149 
150  def __repr__(self):
151  return "<%s %s %s>" % (type(self).__name__, self.index, self.name)
152 
153  def __str__(self):
154  return "%s %s" % (
155  self.name or super().__str__(),
156  self.index if self.index is not None else ""
157  )
158 
159  def remove(self):
160  """!
161  @brief Removes this layer from the componsitin
162  """
163  self.composition.remove_layer(self)
164 
165 
166 ## @ingroup Lottie
168  """!
169  Layer with no data, useful to group layers together
170  """
171  ## %Layer type.
172  type = 3
173 
174  def __init__(self):
175  Layer.__init__(self)
176 
177 
178 ## @ingroup Lottie
180  _props = [
181  LottieProp("data", "t", TextAnimatorData, False),
182  ]
183  ## %Layer type.
184  type = 5
185 
186  def __init__(self):
187  Layer.__init__(self)
188  ## Text Data
190 
191 
192 ## @ingroup Lottie
194  """!
195  Layer containing ShapeElement objects
196  """
197  _props = [
198  LottieProp("shapes", "shapes", ShapeElement, True),
199  ]
200  ## %Layer type.
201  type = 4
202 
203  def __init__(self):
204  Layer.__init__(self)
205  ## Shape list of items
206  self.shapes = [] # ShapeElement
207 
208  def add_shape(self, shape):
209  self.shapes.append(shape)
210  return shape
211 
212  def insert_shape(self, index, shape):
213  self.shapes.insert(index, shape)
214  return shape
215 
216 
217 ## @ingroup Lottie
218 ## @todo SIF I/O
220  _props = [
221  LottieProp("image_id", "refId", str, False),
222  ]
223  ## %Layer type.
224  type = 2
225 
226  def __init__(self, image_id=""):
227  Layer.__init__(self)
228  ## id pointing to the source image defined on 'assets' object
229  self.image_id = image_id
230 
231 
232 ## @ingroup Lottie
234  _props = [
235  LottieProp("reference_id", "refId", str, False),
236  LottieProp("time_remapping", "tm", Value, False),
237  LottieProp("width", "w", int, False),
238  LottieProp("height", "h", int, False),
239  ]
240  ## %Layer type.
241  type = 0
242 
243  def __init__(self, reference_id=""):
244  Layer.__init__(self)
245  ## id pointing to the source composition defined on 'assets' object
246  self.reference_id = reference_id
247  ## Comp's Time remapping
248  self.time_remapping = None
249  ## Width
250  self.width = 512
251  ## Height
252  self.height = 512
253 
254 
255 ColorString = LottieValueConverter(color_from_hex, color_to_hex, "Color string")
256 
257 
258 ## @ingroup Lottie
260  """!
261  Layer with a solid color rectangle
262  """
263  _props = [
264  LottieProp("color", "sc", ColorString, False),
265  LottieProp("height", "sh", float, False),
266  LottieProp("width", "sw", float, False),
267  ]
268  ## %Layer type.
269  type = 1
270 
271  def __init__(self, color=Color(), width=512, height=512):
272  Layer.__init__(self)
273  ## Color of the layer as a @c \#rrggbb hex
274  self.color = color
275  ## Height of the layer.
276  self.height = height
277  ## Width of the layer.
278  self.width = width
lottie.objects.layers.Layer.effects
effects
List of Effects.
Definition: layers.py:88
lottie.objects.layers.PreCompLayer.height
height
Height.
Definition: layers.py:252
lottie.objects.layers.PreCompLayer.width
width
Width.
Definition: layers.py:250
lottie.objects.layers.Layer.transform
transform
Transform properties.
Definition: layers.py:64
lottie.objects.layers.Layer.auto_orient
auto_orient
Auto-Orient along path AE property.
Definition: layers.py:66
lottie.objects.layers.PreCompLayer.reference_id
reference_id
id pointing to the source composition defined on 'assets' object
Definition: layers.py:246
lottie.objects.layers.Layer.parent
def parent(self)
Definition: layers.py:118
lottie.objects.layers.Layer.__repr__
def __repr__(self)
Definition: layers.py:150
lottie.objects.layers.Layer.index
index
Layer index in AE.
Definition: layers.py:72
lottie.objects.helpers.VisualObject.name
name
Definition: helpers.py:109
lottie.objects.helpers.VisualObject
Definition: helpers.py:100
lottie.objects.layers.Layer.motion_blur
motion_blur
Whether motion blur is enabled for the layer.
Definition: layers.py:79
lottie.objects.layers.NullLayer
Layer with no data, useful to group layers together.
Definition: layers.py:167
lottie.objects.layers.ImageLayer
Definition: layers.py:219
lottie.objects.layers.SolidColorLayer.height
height
Height of the layer.
Definition: layers.py:276
lottie.objects.layers.ImageLayer.__init__
def __init__(self, image_id="")
Definition: layers.py:226
lottie.objects.layers.Layer
Definition: layers.py:22
lottie.objects.layers.PreCompLayer.__init__
def __init__(self, reference_id="")
Definition: layers.py:243
lottie.objects.layers.PreCompLayer.time_remapping
time_remapping
Comp's Time remapping.
Definition: layers.py:248
lottie.objects.layers.SolidColorLayer.__init__
def __init__(self, color=Color(), width=512, height=512)
Definition: layers.py:271
lottie.objects.layers.Layer.blend_mode
blend_mode
Blend Mode.
Definition: layers.py:96
lottie.objects.layers.Layer.type
type
Layer type.
Definition: layers.py:51
lottie.objects.layers.TextLayer.__init__
def __init__(self)
Definition: layers.py:186
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
lottie.objects.layers.PreCompLayer
Definition: layers.py:233
lottie.objects.layers.Layer.parent_index
parent_index
Layer Parent.
Definition: layers.py:92
lottie.objects.layers.Layer.__init__
def __init__(self)
Definition: layers.py:61
lottie.objects.layers.TextLayer.data
data
Text Data.
Definition: layers.py:189
lottie.objects.layers.TextLayer
Definition: layers.py:179
lottie.objects.layers.Layer._child_inout_auto
def _child_inout_auto(self, layer)
Definition: layers.py:111
lottie.objects.layers.NullLayer.__init__
def __init__(self)
Definition: layers.py:174
lottie.objects.layers.Layer.hidden
hidden
Hidden layer.
Definition: layers.py:70
lottie.objects.base.LottieEnum
Base class for enum-like types in the Lottie JSON structure.
Definition: base.py:42
lottie.objects.layers.Layer.masks
masks
List of Masks.
Definition: layers.py:94
lottie.objects.layers.Layer.in_point
in_point
In Point of layer.
Definition: layers.py:82
lottie.objects.layers.ImageLayer.image_id
image_id
id pointing to the source image defined on 'assets' object
Definition: layers.py:229
lottie.objects.layers.Layer.add_child
def add_child(self, layer)
Definition: layers.py:103
lottie.objects.layers.Layer.css_class
css_class
CSS class used by the SVG renderer.
Definition: layers.py:75
lottie.objects.layers.Layer.stretch
stretch
Layer Time Stretching.
Definition: layers.py:90
lottie.objects.layers.Layer.matte_target
matte_target
Definition: layers.py:99
lottie.objects.layers.Layer.start_time
start_time
Start Time of layer.
Definition: layers.py:86
lottie.objects.helpers.Transform
Layer transform.
Definition: helpers.py:7
lottie.objects.layers.Layer.out_point
out_point
Out Point of layer.
Definition: layers.py:84
lottie.objects.layers.ShapeLayer.shapes
shapes
Shape list of items.
Definition: layers.py:206
lottie.objects.layers.MatteMode
Definition: layers.py:13
lottie.objects.base.LottieValueConverter
Factory for property types that require special conversions.
Definition: base.py:64
lottie.objects.layers.ShapeLayer.add_shape
def add_shape(self, shape)
Definition: layers.py:208
lottie.objects.layers.SolidColorLayer.color
color
Color of the layer as a #rrggbb hex.
Definition: layers.py:274
lottie.objects.layers.Layer.matte_mode
matte_mode
Matte mode, the layer will inherit the transparency from the layer above.
Definition: layers.py:98
lottie.objects.layers.Layer.remove
def remove(self)
Removes this layer from the componsitin.
Definition: layers.py:159
lottie.objects.layers.Layer.__str__
def __str__(self)
Definition: layers.py:153
lottie.objects.layers.SolidColorLayer.width
width
Width of the layer.
Definition: layers.py:278
lottie.objects.layers.ShapeLayer.__init__
def __init__(self)
Definition: layers.py:203
lottie.objects.layers.Layer.layer_xml_id
layer_xml_id
id attribute used by the SVG renderer
Definition: layers.py:77
lottie.objects.layers.Layer.threedimensional
threedimensional
3d layer flag
Definition: layers.py:68
lottie.objects.layers.Layer.has_masks
def has_masks(self)
Whether the layer has some masks applied.
Definition: layers.py:55
lottie.objects.layers.ShapeLayer.insert_shape
def insert_shape(self, index, shape)
Definition: layers.py:212
lottie.objects.layers.SolidColorLayer
Layer with a solid color rectangle.
Definition: layers.py:259
lottie.objects.layers.Layer.composition
composition
Composition owning the layer, set by add_layer.
Definition: layers.py:101
lottie.objects.text.TextAnimatorData
Definition: text.py:180
lottie.objects.layers.ShapeLayer
Layer containing ShapeElement objects.
Definition: layers.py:193
lottie.objects.layers.Layer.children
def children(self)
Definition: layers.py:132