Exporting Krita layers for use in Spriter

Warning - Old Content

This post is quite old, and it might not apply anymore, or maybe there's a better way to do the same thing nowadays. Take with a big grain of salt.

I’m currently working on a game where the characters are hand-painted in Krita (an amazing digital painting tool!). I’m then using Spriter (also awesome!) to put together 2d animations. My original workflow for getting the images from Krita to Spriter is very manual and error-prone, so I’ve put together an automated script to do it for me.

While Krita itself has the ability to save its layers as PNGs, it only works if the layers are all at the top-level, i.e. no nested layer groups. Since I’m working with 50+ layers, that would be painful! I tried just removing the layer groups every time I wanted to export, but that was almost as bad.

UPDATE: Originally, I was exporting to PSD, but the Krita developers got in touch and pointed out that .ORA files are really just zips. My updated workflow is to export as a .ORA file, then use a script to unzip them then manipulate with ImageMagick for the final resizing.

The script has to read the data from the stack.xml file, but it’s not as complicated as reading the PSD data using imagemagick.

updated export script as a gist

#!/bin/bash

# painting2sprites - A simple script to read an ORA, resize and trim output PNGs.

INPUT_FILE=$1
OUTPUT_DIR=$2

RESIZE_SCALE="25%"

if [ "$2" == "" ]; then
    echo "Usage: $0 <input_ora> <output_dir>"
    exit 1
fi

if [ ! -r $INPUT_FILE ]; then
    echo "unable to open input file '$INPUT_FILE'"
    exit 2
fi

if [ ! -d $OUTPUT_DIR ]; then
    echo "output dir doesn't exist or is not a directory"
    exit 2
fi

# create a temporary folder
TMP_DIR=`mktemp -d`

echo "Extracting layer data..."

# extra the contents of the ORA file
# ORA is just a ZIP, woo!
unzip -q $INPUT_FILE -d $TMP_DIR

# read layer information from the stack.xml file.
LAYER_NODES=`grep "layer" $TMP_DIR/stack.xml | grep -v hidden`

# go through the visible layer nodes
IFS=$'\n'
for LAYER_NODE in $LAYER_NODES; do
    # get the name and filename
    LAYER_NAME=`echo $LAYER_NODE | sed "s/^.*name=\"//" | sed "s/\".*$//"`
    LAYER_FILE=`echo $LAYER_NODE | sed "s/^.*src=\"//" | sed "s/\".*$//"`

    # skip the background layer.
    if [ "$LAYER_NAME" == "background" ]; then
        continue
    fi

    # munge the layer name into the filename.
    OUT_FILE=`echo "$LAYER_NAME" | sed "s/ /_/g" | tr "A-Z" "a-z"`

    echo "exporting layer '$LAYER_NAME' to file '${OUT_FILE}.png'"

    # auto-crop: use 'trim' to remove excess transparent border (Krita already mostly does this)
    # resize: scale down 4x to get reasonable game-sized assets.
    convert "$TMP_DIR/$LAYER_FILE" -trim -resize "$RESIZE_SCALE" "$OUTPUT_DIR/${OUT_FILE}.png"
done
unset IFS

# clean up
rm -rf $TMP_DIR

exit 0