File indexing completed on 2025-01-19 03:59:52
0001 from .base import LottieObject, Index, LottieProp 0002 from .layers import Layer 0003 0004 0005 ## @ingroup Lottie 0006 class Composition(LottieObject): 0007 """! 0008 Base class for layer holders 0009 """ 0010 _props = [ 0011 LottieProp("layers", "layers", Layer, True), 0012 ] 0013 0014 def __init__(self): 0015 ## List of Composition Layers 0016 self.layers = [] # ShapeLayer, SolidLayer, CompLayer, ImageLayer, NullLayer, TextLayer 0017 0018 self._index_gen = Index() 0019 0020 def layer(self, index): 0021 for layer in self.layers: 0022 if layer.index == index: 0023 return layer 0024 raise IndexError("No layer %s" % index) 0025 0026 def add_layer(self, layer: Layer): 0027 """! 0028 @brief Appends a layer to the composition 0029 @see insert_layer 0030 """ 0031 return self.insert_layer(len(self.layers), layer) 0032 0033 @classmethod 0034 def load(cls, lottiedict): 0035 obj = super().load(lottiedict) 0036 obj._fixup() 0037 return obj 0038 0039 def _fixup(self): 0040 for layer in self.layers: 0041 layer.composition = self 0042 0043 def insert_layer(self, index, layer: Layer): 0044 """! 0045 @brief Inserts a layer to the composition 0046 @note Layers added first will be rendered on top of later layers 0047 """ 0048 self.layers.insert(index, layer) 0049 self.prepare_layer(layer) 0050 return layer 0051 0052 def prepare_layer(self, layer: Layer): 0053 layer.composition = self 0054 if layer.index is None: 0055 layer.index = next(self._index_gen) 0056 self._on_prepare_layer(layer) 0057 0058 def _on_prepare_layer(self, layer): 0059 raise NotImplementedError 0060 0061 def clone(self): 0062 c = super().clone() 0063 c._index_gen._i = self._index_gen._i 0064 return c 0065 0066 def remove_layer(self, layer: Layer): 0067 """! 0068 @brief Removes a layer (and all of its children) from this composition 0069 @param layer Layer to be removed 0070 """ 0071 if layer.composition is not self: 0072 return 0073 0074 children = list(layer.children) 0075 0076 layer.composition = None 0077 self.layers.remove(layer) 0078 0079 for c in children: 0080 self.remove_layer(c)