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

0001 import zipfile
0002 import warnings
0003 from xml.etree import ElementTree
0004 
0005 from .base import importer
0006 from ..parsers.svg.importer import SvgParser
0007 from .. import objects
0008 
0009 ns = "{%s}" % "http://www.calligra.org/DTD/krita"
0010 
0011 
0012 def _ns(string):
0013     return string.format(ns=ns)
0014 
0015 
0016 def _import_layers(zf, animation, xml_parent, svg_parser, parent):
0017     for xml_layer in xml_parent.findall(_ns("./{ns}layers/{ns}layer")):
0018         nodetype = xml_layer.attrib["nodetype"]
0019 
0020         if nodetype == "grouplayer":
0021             layer = animation.add_layer(objects.NullLayer())
0022             _import_layers(zf, animation, xml_layer, svg_parser, layer)
0023         elif nodetype == "shapelayer":
0024             filename = "%s/layers/%s.shapelayer/content.svg" % (animation.name, xml_layer.attrib["filename"])
0025             with zf.open(filename) as svg_tree:
0026                 layer = svg_parser.etree_to_layer(animation, ElementTree.parse(svg_tree))
0027         else:
0028             warnings.warn("Unsupported krita layer %s" % nodetype)
0029             continue
0030 
0031         layer.name = xml_layer.attrib["name"]
0032         if xml_layer.attrib["visible"] == 0:
0033             layer.transform.opacity.value = 0
0034         layer.parent = parent
0035 
0036 
0037 @importer("Krita", ["kra"])
0038 def import_krita(file):
0039     with zipfile.ZipFile(file) as zf:
0040         with zf.open("maindoc.xml") as main:
0041             main_xml = ElementTree.parse(main)
0042 
0043         image = main_xml.find(_ns("./{ns}IMAGE"))
0044         fps = float(main_xml.find(_ns("./{ns}IMAGE/{ns}animation/{ns}framerate")).attrib["value"])
0045         framerange = main_xml.find(_ns("./{ns}IMAGE/{ns}animation/{ns}range")).attrib
0046 
0047         animation = objects.Animation(int(framerange["to"]), fps)
0048         animation.in_point = int(framerange["from"])
0049         animation.width = int(image.attrib["width"])
0050         animation.height = int(image.attrib["height"])
0051         animation.name = image.attrib["name"]
0052 
0053         parser = SvgParser()
0054         parser.dpi = int(image.attrib["x-res"])
0055 
0056         _import_layers(zf, animation, image, parser, None)
0057 
0058     return animation