python-lottie  0.6.10+dev2509936
A framework to work with lottie files and telegram animated stickers (tgs)
helpers.py
Go to the documentation of this file.
1 import math
2 from .base import LottieObject, LottieProp, LottieEnum
3 from .properties import MultiDimensional, Value, NVector, ShapeProperty, PositionValue
4 
5 
6 ## @ingroup Lottie
8  """!
9  Layer transform
10  """
11  _props = [
12  LottieProp("anchor_point", "a", MultiDimensional, False),
13  LottieProp("position", "p", PositionValue, False),
14  LottieProp("scale", "s", MultiDimensional, False),
15  LottieProp("rotation", "r", Value, False),
16  LottieProp("opacity", "o", Value, False),
17  #LottieProp("position_x", "px", Value, False),
18  #LottieProp("position_y", "py", Value, False),
19  #LottieProp("position_z", "pz", Value, False),
20  LottieProp("skew", "sk", Value, False),
21  LottieProp("skew_axis", "sa", Value, False),
22  ]
23 
24  def __init__(self):
25  ## Transform Anchor Point
26  self.anchor_point = MultiDimensional(NVector(0, 0))
27  ## Transform Position
28  self.position = PositionValue(NVector(0, 0))
29  ## Transform Scale
30  self.scale = MultiDimensional(NVector(100, 100))
31  ## Transform Rotation
32  self.rotation = Value(0)
33  ## Transform Opacity
34  self.opacity = Value(100)
35 
36  """
37  # Transform Position X
38  #self.position_x = Value()
39  ## Transform Position Y
40  #self.position_y = Value()
41  ## Transform Position Z
42  #self.position_z = Value()
43  """
44 
45  ## Transform Skew
46  self.skew = Value(0)
47  ## Transform Skew Axis.
48  ## An angle, if 0 skews on the X axis, if 90 skews on the Y axis
49  self.skew_axis = Value(0)
50 
51  def to_matrix(self, time, auto_orient=False):
52  from ..utils.transform import TransformMatrix
53  mat = TransformMatrix()
54 
55  anchor = self.anchor_point.get_value(time) if self.anchor_point else NVector(0, 0)
56  mat.translate(-anchor.x, -anchor.y)
57 
58  scale = self.scale.get_value(time) if self.scale else NVector(100, 100)
59  mat.scale(scale.x / 100, scale.y / 100)
60 
61  skew = (self.skew.get_value(time) * math.pi / 180) if self.skew else 0
62  if skew != 0:
63  axis = (self.skew_axis.get_value(time) * math.pi / 180) if self.skew_axis else 0
64  mat.skew_from_axis(-skew, axis)
65 
66  rot = (self.rotation.get_value(time) * math.pi / 180) if self.rotation else 0
67  if rot:
68  mat.rotate(-rot)
69 
70  if auto_orient:
71  if self.position and self.position.animated:
72  ao_angle = self.position.get_tangent_angle(time)
73  mat.rotate(-ao_angle)
74 
75  pos = self.position.get_value(time) if self.position else NVector(0, 0)
76  mat.translate(pos.x, pos.y)
77 
78  return mat
79 
80 
81 ## @ingroup Lottie
83  """!
84  How masks interact with each other
85  @see https://helpx.adobe.com/after-effects/using/alpha-channels-masks-mattes.html
86  """
87  No = "n"
88  Add = "a"
89  Subtract = "s"
90  Intersect = "i"
91  ## @note Not in lottie web
92  Lighten = "l"
93  ## @note Not in lottie web
94  Darken = "d"
95  ## @note Not in lottie web
96  Difference = "f"
97 
98 
99 ## @ingroup Lottie
101  _props = [
102  LottieProp("name", "nm", str, False),
103  LottieProp("match_name", "mn", str, False),
104  ]
105 
106  def __init__(self):
107  super().__init__()
108  # Object name
109  self.name = None
110  # Used for expression
111  self.match_name = None
112 
113 
114 ## @ingroup Lottie
115 ## @todo Implement SVG/SIF I/O
117  _props = [
118  LottieProp("inverted", "inv", bool, False),
119  LottieProp("shape", "pt", ShapeProperty, False),
120  LottieProp("opacity", "o", Value, False),
121  LottieProp("mode", "mode", MaskMode, False),
122  LottieProp("dilate", "x", Value, False),
123  ]
124 
125  def __init__(self, bezier=None):
126  super().__init__()
127  ## Inverted Mask flag
128  self.inverted = False
129  ## Mask vertices
130  self.shape = ShapeProperty(bezier)
131  ## Mask opacity.
132  self.opacity = Value(100)
133  ## Mask mode. Not all mask types are supported.
134  self.mode = MaskMode.Intersect
135  self.dilate = Value(0)
136 
137  def __str__(self):
138  return self.name or super().__str__()
lottie.objects.properties.Value
An animatable property that holds a float.
Definition: properties.py:608
lottie.objects.helpers.Transform.opacity
opacity
Transform Opacity.
Definition: helpers.py:34
lottie.objects.helpers.Mask.opacity
opacity
Mask opacity.
Definition: helpers.py:132
lottie.objects.helpers.VisualObject.name
name
Definition: helpers.py:109
lottie.objects.helpers.VisualObject
Definition: helpers.py:100
lottie.objects.helpers.Mask.__str__
def __str__(self)
Definition: helpers.py:137
lottie.objects.helpers.VisualObject.match_name
match_name
Definition: helpers.py:111
lottie.objects.helpers.Transform.anchor_point
anchor_point
Transform Anchor Point.
Definition: helpers.py:26
lottie.objects.helpers.Transform.scale
scale
Transform Scale.
Definition: helpers.py:30
lottie.objects.properties.ShapeProperty
An animatable property that holds a Bezier.
Definition: properties.py:672
lottie.objects.helpers.MaskMode
How masks interact with each other.
Definition: helpers.py:82
lottie.objects.base.LottieObject
Base class for mapping Python classes into Lottie JSON objects.
Definition: base.py:224
lottie.objects.helpers.Transform.to_matrix
def to_matrix(self, time, auto_orient=False)
Definition: helpers.py:51
lottie.objects.base.LottieProp
Lottie <-> Python property mapper.
Definition: base.py:88
lottie.objects.helpers.Transform.skew
skew
Transform Skew.
Definition: helpers.py:46
lottie.objects.helpers.Transform.skew_axis
skew_axis
Transform Skew Axis.
Definition: helpers.py:49
lottie.objects.base.LottieEnum
Base class for enum-like types in the Lottie JSON structure.
Definition: base.py:42
lottie.objects.helpers.Mask.__init__
def __init__(self, bezier=None)
Definition: helpers.py:125
lottie.objects.helpers.Mask.shape
shape
Mask vertices.
Definition: helpers.py:130
lottie.objects.helpers.Transform.__init__
def __init__(self)
Definition: helpers.py:24
lottie.objects.helpers.Transform.rotation
rotation
Transform Rotation.
Definition: helpers.py:32
lottie.objects.helpers.VisualObject.__init__
def __init__(self)
Definition: helpers.py:106
lottie.objects.helpers.Transform
Layer transform.
Definition: helpers.py:7
lottie.objects.helpers.Mask.dilate
dilate
Definition: helpers.py:135
lottie.objects.properties.MultiDimensional
An animatable property that holds a NVector.
Definition: properties.py:385
lottie.objects.properties.PositionValue
Definition: properties.py:415
lottie.objects.helpers.Transform.position
position
Transform Position.
Definition: helpers.py:28
lottie.objects.helpers.Mask.mode
mode
Mask mode.
Definition: helpers.py:134
lottie.objects.helpers.Mask
Definition: helpers.py:116
lottie.objects.helpers.Mask.inverted
inverted
Inverted Mask flag.
Definition: helpers.py:128