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