File indexing completed on 2025-01-19 03:59:52

0001 import math
0002 from .base import LottieObject, LottieProp, LottieEnum
0003 from .properties import MultiDimensional, Value, NVector, ShapeProperty, PositionValue
0004 
0005 
0006 ## @ingroup Lottie
0007 class Transform(LottieObject):
0008     """!
0009     Layer transform
0010     """
0011     _props = [
0012         LottieProp("anchor_point", "a", MultiDimensional, False),
0013         LottieProp("position", "p", PositionValue, False),
0014         LottieProp("scale", "s", MultiDimensional, False),
0015         LottieProp("rotation", "r", Value, False),
0016         LottieProp("opacity", "o", Value, False),
0017         #LottieProp("position_x", "px", Value, False),
0018         #LottieProp("position_y", "py", Value, False),
0019         #LottieProp("position_z", "pz", Value, False),
0020         LottieProp("skew", "sk", Value, False),
0021         LottieProp("skew_axis", "sa", Value, False),
0022     ]
0023 
0024     def __init__(self):
0025         ## Transform Anchor Point
0026         self.anchor_point = MultiDimensional(NVector(0, 0))
0027         ## Transform Position
0028         self.position = PositionValue(NVector(0, 0))
0029         ## Transform Scale
0030         self.scale = MultiDimensional(NVector(100, 100))
0031         ## Transform Rotation
0032         self.rotation = Value(0)
0033         ## Transform Opacity
0034         self.opacity = Value(100)
0035 
0036         """
0037         # Transform Position X
0038         #self.position_x = Value()
0039         ## Transform Position Y
0040         #self.position_y = Value()
0041         ## Transform Position Z
0042         #self.position_z = Value()
0043         """
0044 
0045         ## Transform Skew
0046         self.skew = Value(0)
0047         ## Transform Skew Axis.
0048         ## An angle, if 0 skews on the X axis, if 90 skews on the Y axis
0049         self.skew_axis = Value(0)
0050 
0051     def to_matrix(self, time, auto_orient=False):
0052         from ..utils.transform import TransformMatrix
0053         mat = TransformMatrix()
0054 
0055         anchor = self.anchor_point.get_value(time) if self.anchor_point else NVector(0, 0)
0056         mat.translate(-anchor.x, -anchor.y)
0057 
0058         scale = self.scale.get_value(time) if self.scale else NVector(100, 100)
0059         mat.scale(scale.x / 100, scale.y / 100)
0060 
0061         skew = (self.skew.get_value(time) * math.pi / 180) if self.skew else 0
0062         if skew != 0:
0063             axis = (self.skew_axis.get_value(time) * math.pi / 180) if self.skew_axis else 0
0064             mat.skew_from_axis(-skew, axis)
0065 
0066         rot = (self.rotation.get_value(time) * math.pi / 180) if self.rotation else 0
0067         if rot:
0068             mat.rotate(-rot)
0069 
0070         if auto_orient:
0071             if self.position and self.position.animated:
0072                 ao_angle = self.position.get_tangent_angle(time)
0073                 mat.rotate(-ao_angle)
0074 
0075         pos = self.position.get_value(time) if self.position else NVector(0, 0)
0076         mat.translate(pos.x, pos.y)
0077 
0078         return mat
0079 
0080 
0081 ## @ingroup Lottie
0082 class MaskMode(LottieEnum):
0083     """!
0084     How masks interact with each other
0085     @see https://helpx.adobe.com/after-effects/using/alpha-channels-masks-mattes.html
0086     """
0087     No = "n"
0088     Add = "a"
0089     Subtract = "s"
0090     Intersect = "i"
0091     ## @note Not in lottie web
0092     Lightent = "l"
0093     ## @note Not in lottie web
0094     Darken = "d"
0095     ## @note Not in lottie web
0096     Difference = "f"
0097 
0098 
0099 ## @ingroup Lottie
0100 ## @todo Implement SVG/SIF I/O
0101 class Mask(LottieObject):
0102     _props = [
0103         LottieProp("inverted", "inv", bool, False),
0104         LottieProp("name", "nm", str, False),
0105         LottieProp("shape", "pt", ShapeProperty, False),
0106         LottieProp("opacity", "o", Value, False),
0107         LottieProp("mode", "mode", MaskMode, False),
0108         LottieProp("dilate", "x", Value, False),
0109     ]
0110 
0111     def __init__(self, bezier=None):
0112         ## Inverted Mask flag
0113         self.inverted = False
0114         ## Mask name. Used for expressions and effects.
0115         self.name = None
0116         ## Mask vertices
0117         self.shape = ShapeProperty(bezier)
0118         ## Mask opacity.
0119         self.opacity = Value(100)
0120         ## Mask mode. Not all mask types are supported.
0121         self.mode = MaskMode.Intersect
0122         self.dilate = Value(0)
0123 
0124     def __str__(self):
0125         return self.name or super().__str__()