Blame synfig-studio/plugins/lottie-exporter/canvas.py

AnishGulati 1c4546
"""
AnishGulati 1c4546
This module converts the canvas to lottie format
AnishGulati 1c4546
"""
AnishGulati 1c4546
import settings
AnishGulati 1f6b9a
from common.misc import calculate_pixels_per_unit
AnishGulati 1c4546
AnishGulati decf6d
AnishGulati 9cc644
def calc_time(root, lottie, which):
AnishGulati 12ca25
    """
AnishGulati 12ca25
    Converts the starting time and ending time to lottie format
AnishGulati be7f5d
AnishGulati be7f5d
    Args:
AnishGulati 0f1f11
        root   (lxml.etree._Element) : Synfig format animation file
AnishGulati be7f5d
        lottie (dict)                : Lottie format animation file
AnishGulati be7f5d
        which  (str)                 : Differentiates between in time and out time
AnishGulati be7f5d
AnishGulati be7f5d
    Returns:
AnishGulati be7f5d
        (None)
AnishGulati 12ca25
    """
AnishGulati 9cc644
    if which == "ip":
AnishGulati 12ca25
        phase = "begin-time"
AnishGulati 9cc644
    elif which == "op":
AnishGulati 12ca25
        phase = "end-time"
AnishGulati 12ca25
    time = root.attrib[phase].split(" ")
AnishGulati 9cc644
    lottie[which] = 0
AnishGulati 12ca25
    for frame in time:
AnishGulati c447db
        # Adding time in hours
AnishGulati c447db
        if frame[-1] == "h":
AnishGulati c447db
            lottie[which] += float(frame[:-1]) * 60 * 60 * lottie["fr"]
AnishGulati c447db
        # Adding time in minutes
AnishGulati c447db
        elif frame[-1] == "m":
AnishGulati c447db
            lottie[which] += float(frame[:-1]) * 60 * lottie["fr"]
AnishGulati 9cc644
        # Adding time in seconds
AnishGulati c447db
        elif frame[-1] == "s":
AnishGulati 12ca25
            lottie[which] += float(frame[:-1]) * lottie["fr"]
AnishGulati 9cc644
        # Adding time in frames
AnishGulati 12ca25
        elif frame[-1] == "f":
AnishGulati 12ca25
            lottie[which] += float(frame[:-1])
AnishGulati 9cc644
AnishGulati 2e8b43
    # To support canvas with single frames
AnishGulati 2e8b43
    if which == "op":
AnishGulati 2e8b43
        lottie[which] += 1
AnishGulati 2e8b43
AnishGulati 9cc644
AnishGulati 1c4546
def gen_canvas(lottie, root):
AnishGulati 12ca25
    """
AnishGulati 1c4546
    Generates the canvas for the lottie format
AnishGulati 1c4546
    It is the outer most dictionary in the lottie json format
AnishGulati be7f5d
AnishGulati be7f5d
    Args:
AnishGulati be7f5d
        lottie (dict)               : Lottie format animation file
AnishGulati be7f5d
        root   (lxml.etree._Element): Synfig format animation file
AnishGulati be7f5d
AnishGulati be7f5d
    Returns:
AnishGulati be7f5d
        (None)
AnishGulati 1c4546
    """
AnishGulati 12ca25
    settings.view_box_canvas["val"] = [float(itr) for itr in root.attrib["view-box"].split()]
AnishGulati 1c4546
    if "width" in root.attrib.keys():
AnishGulati 1c4546
        lottie["w"] = int(root.attrib["width"])
AnishGulati 1c4546
    else:
AnishGulati 1c4546
        lottie["w"] = settings.DEFAULT_WIDTH
AnishGulati 1c4546
AnishGulati 1c4546
    if "height" in root.attrib.keys():
AnishGulati 1c4546
        lottie["h"] = int(root.attrib["height"])
AnishGulati 1c4546
    else:
AnishGulati 1c4546
        lottie["h"] = settings.DEFAULT_HEIGHT
AnishGulati 1c4546
AnishGulati 00b801
    settings.ADDITIONAL_PRECOMP_WIDTH = 4*lottie["w"]
AnishGulati 00b801
    settings.ADDITIONAL_PRECOMP_HEIGHT = 4*lottie["h"]
AnishGulati 5e53c0
AnishGulati 1c4546
    name = settings.DEFAULT_NAME
AnishGulati 1c4546
    for child in root:
AnishGulati 1c4546
        if child.tag == "name":
AnishGulati 1c4546
            name = child.text
AnishGulati 1c4546
            break
AnishGulati 1c4546
    lottie["nm"] = name
AnishGulati 1c4546
    lottie["ddd"] = settings.DEFAULT_3D
AnishGulati 1c4546
    lottie["v"] = settings.LOTTIE_VERSION
AnishGulati 1c4546
    lottie["fr"] = float(root.attrib["fps"])
AnishGulati bf4ed0
    lottie["assets"] = []       # Creating array for storing assets
AnishGulati 5f578b
    lottie["markers"] = []      # Creating array for storing markers
AnishGulati 9cc644
    calc_time(root, lottie, "ip")
AnishGulati 9cc644
    calc_time(root, lottie, "op")
AnishGulati 1c4546
    calculate_pixels_per_unit()