python-lottie  0.6.10+dev4166086
A framework to work with lottie files and telegram animated stickers (tgs)
composition.py
Go to the documentation of this file.
1 from .base import LottieObject, Index, LottieProp
2 from .layers import Layer
3 
4 
5 ## @ingroup Lottie
7  """!
8  Base class for layer holders
9  """
10  _props = [
11  LottieProp("layers", "layers", Layer, True),
12  ]
13 
14  def __init__(self):
15  ## List of Composition Layers
16  self.layers = [] # ShapeLayer, SolidLayer, CompLayer, ImageLayer, NullLayer, TextLayer
17 
18  self._index_gen = Index()
19 
20  def layer(self, index):
21  for layer in self.layers:
22  if layer.index == index:
23  return layer
24  raise IndexError("No layer %s" % index)
25 
26  def add_layer(self, layer: Layer):
27  """!
28  @brief Appends a layer to the composition
29  @see insert_layer
30  """
31  return self.insert_layer(len(self.layers), layer)
32 
33  @classmethod
34  def load(cls, lottiedict):
35  obj = super().load(lottiedict)
36  obj._fixup()
37  return obj
38 
39  def _fixup(self):
40  for layer in self.layers:
41  layer.composition = self
42 
43  def insert_layer(self, index, layer: Layer):
44  """!
45  @brief Inserts a layer to the composition
46  @note Layers added first will be rendered on top of later layers
47  """
48  self.layers.insert(index, layer)
49  self.prepare_layer(layer)
50  return layer
51 
52  def prepare_layer(self, layer: Layer):
53  layer.composition = self
54  if layer.index is None:
55  layer.index = next(self._index_gen)
56  self._on_prepare_layer(layer)
57 
58  def _on_prepare_layer(self, layer):
59  raise NotImplementedError
60 
61  def clone(self):
62  c = super().clone()
63  c._index_gen._i = self._index_gen._i
64  return c
65 
66  def remove_layer(self, layer: Layer):
67  """!
68  @brief Removes a layer (and all of its children) from this composition
69  @param layer Layer to be removed
70  """
71  if layer.composition is not self:
72  return
73 
74  children = list(layer.children)
75 
76  layer.composition = None
77  self.layers.remove(layer)
78 
79  for c in children:
80  self.remove_layer(c)
lottie.objects.composition.Composition.remove_layer
def remove_layer(self, Layer layer)
Removes a layer (and all of its children) from this composition.
Definition: composition.py:66
lottie.objects.composition.Composition.insert_layer
def insert_layer(self, index, Layer layer)
Inserts a layer to the composition.
Definition: composition.py:43
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:224
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
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:16
lottie.objects.composition.Composition._on_prepare_layer
def _on_prepare_layer(self, layer)
Definition: composition.py:58
lottie.objects.composition.Composition.add_layer
def add_layer(self, Layer layer)
Appends a layer to the composition.
Definition: composition.py:26
lottie.objects.composition.Composition.clone
def clone(self)
Returns a copy of the object.
Definition: composition.py:61
lottie.objects.composition.Composition.load
def load(cls, lottiedict)
Loads from a JSON object.
Definition: composition.py:34
lottie.objects.composition.Composition.prepare_layer
def prepare_layer(self, Layer layer)
Definition: composition.py:52
lottie.objects.base.Index
Simple iterator to generate increasing integers.
Definition: base.py:306
lottie.objects.composition.Composition._index_gen
_index_gen
Definition: composition.py:18
lottie.objects.composition.Composition.__init__
def __init__(self)
Definition: composition.py:14
lottie.objects.composition.Composition.layer
def layer(self, index)
Definition: composition.py:20