python-lottie  0.6.11+devcecd248
A framework to work with lottie files and telegram animated stickers (tgs)
krita.py
Go to the documentation of this file.
1 import zipfile
2 import warnings
3 from xml.etree import ElementTree
4 
5 from .base import importer
6 from ..parsers.svg.importer import SvgParser
7 from .. import objects
8 
9 ns = "{%s}" % "http://www.calligra.org/DTD/krita"
10 
11 
12 def _ns(string):
13  return string.format(ns=ns)
14 
15 
16 def _import_layers(zf, animation, xml_parent, svg_parser, parent):
17  for xml_layer in xml_parent.findall(_ns("./{ns}layers/{ns}layer")):
18  nodetype = xml_layer.attrib["nodetype"]
19 
20  if nodetype == "grouplayer":
21  layer = animation.add_layer(objects.NullLayer())
22  _import_layers(zf, animation, xml_layer, svg_parser, layer)
23  elif nodetype == "shapelayer":
24  filename = "%s/layers/%s.shapelayer/content.svg" % (animation.name, xml_layer.attrib["filename"])
25  with zf.open(filename) as svg_tree:
26  layer = svg_parser.etree_to_layer(animation, ElementTree.parse(svg_tree))
27  else:
28  warnings.warn("Unsupported krita layer %s" % nodetype)
29  continue
30 
31  layer.name = xml_layer.attrib["name"]
32  if xml_layer.attrib["visible"] == 0:
33  layer.transform.opacity.value = 0
34  layer.parent = parent
35 
36 
37 @importer("Krita", ["kra"])
38 def import_krita(file):
39  with zipfile.ZipFile(file) as zf:
40  with zf.open("maindoc.xml") as main:
41  main_xml = ElementTree.parse(main)
42 
43  image = main_xml.find(_ns("./{ns}IMAGE"))
44  fps = float(main_xml.find(_ns("./{ns}IMAGE/{ns}animation/{ns}framerate")).attrib["value"])
45  framerange = main_xml.find(_ns("./{ns}IMAGE/{ns}animation/{ns}range")).attrib
46 
47  animation = objects.Animation(int(framerange["to"]), fps)
48  animation.in_point = int(framerange["from"])
49  animation.width = int(image.attrib["width"])
50  animation.height = int(image.attrib["height"])
51  animation.name = image.attrib["name"]
52 
53  parser = SvgParser()
54  parser.dpi = int(image.attrib["x-res"])
55 
56  _import_layers(zf, animation, image, parser, None)
57 
58  return animation
lottie.objects.layers.NullLayer
Layer with no data, useful to group layers together.
Definition: layers.py:166
lottie.objects.animation.Animation
Top level object, describing the animation.
Definition: animation.py:62
lottie.importers.krita.import_krita
def import_krita(file)
Definition: krita.py:38
lottie.importers.base.importer
importer
Definition: base.py:21
lottie.parsers.svg.importer.SvgParser
Definition: importer.py:201