5 from .base
import exporter
6 from ..parsers.baseporter
import ExtraOption
7 from ..parsers.tgs
import parse_tgs
8 from lottie
import __version__
9 from ..objects
import assets
12 @exporter("dotLottie Archive", ["lottie"], [
ExtraOption("id", help="ID of the animation", default=None),
13 ExtraOption(
"append", help=
"Append animation to existing archive", action=
"store_true"),
14 ExtraOption(
"revision", help=
"File revision", type=int, default=
None),
15 ExtraOption(
"author", help=
"File author", default=
None),
16 ExtraOption(
"speed", help=
"Playback speed", type=float, default=1),
17 ExtraOption(
"theme_color", help=
"Theme color", type=str, default=
"#ffffff"),
18 ExtraOption(
"no_loop", help=
"Disable Looping", action=
"store_false", dest=
"loop"),
19 ExtraOption(
"no_pack", help=
"Don't auto-pack images", action=
"store_false", dest=
"pack_images"),
21 def export_dotlottie(animation, file, id=None, append=False, revision=None, author=None,
22 speed=1.0, theme_color="#ffffff", loop=True, pack_images=True):
27 with zipfile.ZipFile(file,
"r")
as zf:
28 with zf.open(
"manifest.json")
as manifest:
29 meta = json.load(manifest)
31 for name
in zf.namelist():
32 if name !=
"manifest.json":
33 files[name] = zf.read(name)
36 "generator":
"Python Lottie " + __version__,
44 if revision
is not None:
45 meta[
"revision"] = revision
47 if author
is not None:
48 meta[
"author"] = author
52 idok = string.ascii_letters + string.digits +
"_-"
53 id =
"".join(filter(
lambda x: x
in idok, animation.name.replace(
" ",
"_")))
55 id =
"animation_%s" % len(meta[
"animations"])
57 meta[
"animations"].append({
60 "themeColor": theme_color,
64 if pack_images
and animation.assets:
65 animation = animation.clone()
67 for asset
in animation.assets:
68 if isinstance(asset, assets.FileAsset):
69 ext, data = asset.data()
74 basename =
"image_%s.%s" % (image_no, ext)
76 if pathname+basename
not in files:
78 files[pathname+basename] = data
80 asset.file_name = basename
81 asset.is_embedded =
False
83 files[
"manifest.json"] = json.dumps(meta)
84 files[
"animations/%s.json" % id] = json.dumps(animation.to_dict())
86 with zipfile.ZipFile(file,
"w")
as zf:
87 for name, data
in files.items():
88 zf.writestr(name, data)
89
def export_dotlottie(animation, file, id=None, append=False, revision=None, author=None, speed=1.0, theme_color="#ffffff", loop=True, pack_images=True)