python-lottie  0.6.10+dev42a5126
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.layers = [] # ShapeLayer, SolidLayer, CompLayer, ImageLayer, NullLayer, TextLayer
18 
19  self._index_gen = Index()
20 
21  def layer(self, index):
22  for layer in self.layers:
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_layer(len(self.layers), 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.layers:
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.layers.insert(index, layer)
50  self.prepare_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)
57  self._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._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.layers.remove(layer)
79 
80  for c in children:
81  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:67
lottie.objects.composition.Composition.insert_layer
def insert_layer(self, index, Layer layer)
Inserts a layer to the composition.
Definition: composition.py:44
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:17
lottie.objects.composition.Composition._on_prepare_layer
def _on_prepare_layer(self, layer)
Definition: composition.py:59
lottie.objects.composition.Composition.add_layer
def add_layer(self, Layer layer)
Appends a layer to the composition.
Definition: composition.py:27
lottie.objects.composition.Composition.clone
def clone(self)
Returns a copy of the object.
Definition: composition.py:62
lottie.objects.composition.Composition.load
def load(cls, lottiedict)
Loads from a JSON object.
Definition: composition.py:35
lottie.objects.composition.Composition.prepare_layer
def prepare_layer(self, Layer layer)
Definition: composition.py:53
lottie.objects.base.Index
Simple iterator to generate increasing integers.
Definition: base.py:314
lottie.objects.composition.Composition._index_gen
_index_gen
Definition: composition.py:19
lottie.objects.composition.Composition.__init__
def __init__(self)
Definition: composition.py:14
lottie.objects.composition.Composition.layer
def layer(self, index)
Definition: composition.py:21