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