• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Zeus Game Assets

Making Game Assets For Awesome Developers All Around The Globe

  • Home
  • Blog
  • FAQ
  • Licensing
  • About

Blog

Krita Scripting: Export Top Level Layers into PNGs

February 6, 2021 by Zeus

Exporting layers into individual image files is something you may have to do quite often when working in Krita.

Manually exporting a couple of layers is rather easy. Things get complicated, and time consuming, when you need to make frequent changes, or when you have a large number of layers.

In this tutorial, we will write a Python script that exports your top level layers into PNG files.

Our Goal

Let’s say we have a .kra file with three lovely car sprites, each in its own layer.

Our file has 3 layers: car 1, car 2 and car 3, plus the Background layer

We want Krita to create PNG files each named after the corresponding layer.

How we want the exported files to look like

We also want our script to:

  • Organize the files in a folder next to our .kra file
  • Have the size of each PNG file match the size of the layer
  • Ignore specific layers like the Background layer

Let’s Code!

Open Scripter by going to Tools > Scripts > Scripter.

The Scripter interface

Let’s start by importing the krita and os modules.

Let’s also get a reference to the active document.

from krita import *
import os

doc = Krita.instance().activeDocument()

The Export Directory

We will store the PNG files in a subfolder named export_png located next to our .kra file.

The path to our export directory is obtained by:

  1. Using os.path.dirname with the document’s file path as argument to get the path to our working directory.
  2. Using os.path.join to add export_png to our working directory’s path.

If the export directory doesn’t exist in the filesystem (which is to be expected when you run the script for the first time), we’ll create it using os.makedirs.

wdir = os.path.dirname(doc.fileName())
exportDir = os.path.join(wdir, "export_png")

if not os.path.isdir(exportDir):
  os.makedirs(exportDir)

Looping Through Top Level Layers

Before doing anything, we need Krita to go into Batch Mode, because we don’t want annoying dialogs to popup every time we run our script.

Application.setBatchmode(True)

(Application is just a shortcut for Krita.instance().)

In Python, layers are called nodes.

To get the list of top level nodes, we use Document.topLevelNodes(), and we loop through them with a for loop.

for node in doc.topLevelNodes():

Let’s avoid exporting any layer named background or references. We’ll use continue to skip the current iteration.

  if node.name().lower() in ["background", "references"]:
    continue

If you’re wondering why I’ve used lowercase names, it’s because I want the comparison to be case-insensitive.

Exporting Nodes to PNG files

First, let’s get the path to our PNG file. This is easy since we already have the path to our export directory.

  path = os.path.join(exportDir, node.name() + ".png")

Saving a node to PNG (or any format for that matter) is easy. The Node class has a convenient save method that we can use.

The save method requires an InfoObject as argument, which needs specific properties for each file format. These properties are what Krita asks you about in a popup dialog when you manually export through the graphical user interface.

If you’re not in Batch Mode, you’re gonna see a ton of these, yay for Batch Mode!
  info = InfoObject()
  info.setProperty("alpha", True)
  info.setProperty("compression", 9)
  info.setProperty("forceSRGB", False)
  info.setProperty("indexed", False)
  info.setProperty("interlaced", False)
  info.setProperty("saveSRGBProfile", False)
  info.setProperty("transparencyFillcolor", [0,0,0])

  node.save(path, doc.resolution(), doc.resolution(), info)

You probably don’t need to set every property of the InfoObject. Some of them have default values. But I like to be thorough and have complete control over my script.

That’s it for our loop!

Let’s wrap things up by going out of Batch mode and displaying a nice little message.

Application.setBatchmode(False)
QMessageBox.information(Application.activeWindow().qwindow(), "Done and Done", "All done!")

Okay, we’re done, phew!

Press the play button in the Scripter interface to run your script.

Full Script

from krita import *
import os

doc = Krita.instance().activeDocument()
wdir = os.path.dirname(doc.fileName())

exportDir = os.path.join(wdir, "export_png")

if not os.path.isdir(exportDir):
  os.makedirs(exportDir)

Application.setBatchmode(True)

for node in doc.topLevelNodes():

  if node.name().lower() in ["background", "references"]:
    continue

  info = InfoObject()
  info.setProperty("alpha", True)
  info.setProperty("compression", 9)
  info.setProperty("forceSRGB", False)
  info.setProperty("indexed", False)
  info.setProperty("interlaced", False)
  info.setProperty("saveSRGBProfile", False)
  info.setProperty("transparencyFillcolor", [0,0,0])

  path = os.path.join(exportDir, node.name() + ".png")

  node.save(path, doc.resolution(), doc.resolution(), info)

Application.setBatchmode(False)
QMessageBox.information(Application.activeWindow().qwindow(), "Done and Done", "All done!")

Challenges

You can take your script to the next level, here are a few challenges for you :

  • Turn your script into a Krita extension that adds a menu item to Krita’s graphical user interface.
  • Have PNG files match the size of the document rather than the node’s.
  • Export all layers in all open documents, rather than just in the active document like we did in this tutorial.

Krita’s class reference and scripting school are your friends.

Happy coding!

Filed Under: Tutorials Tagged With: Krita, Programming

How To Defeat The Law of Diminishing Returns and Keep Yourself Motivated

April 22, 2020 by Zeus

One of the best ways to increase the chances of your game project succeeding is to identify and mitigate risks early on.

Today we’re going to talk about something that can hurt your motivation over time.

Something that has the power to make you quit.

Something you don’t even think about, but it is there, watching your every move.

It eats starry-eyed game developers like you for breakfast every day.

And most of them aren’t even aware of its presence!

I’m going to show you what it looks like, and teach you how to defeat it!

Are you afraid yet?

You better be.

Because we’re going to talk about the law of diminishing returns!

[Read more…] about How To Defeat The Law of Diminishing Returns and Keep Yourself Motivated

Filed Under: General

Modeling and Texturing the JU87 Stuka – My First Timelapse Video

March 1, 2020 by Zeus

This week, I worked on the WW2 German dive-bomber JU87 Stuka for our PolyWW2 game asset collection, and I decided to record it and create a timelapse video.

It took me 12 hours to model, uv unwrap, and texture. There is still some work needed to make the model game-ready, but that would be a bit boring to watch, so I won’t be recording it.

Enjoy!

Filed Under: General

12 Tips To Rekindle Your Love For Game Making

February 24, 2020 by Zeus

Making games is a demanding job, to say the least.

No matter how good you are at what you do, you are always at risk of being creatively drained and burnt out.

Or just plain bored.

Over the years, I’ve applied some good advice that kept me afloat, I’ve also crashed and burned several times.

So I believe I have some valuable tips that I’m going to share with you in this post.

[Read more…] about 12 Tips To Rekindle Your Love For Game Making

Filed Under: General

Misconceptions About 3D That Hurt Your Game

February 15, 2020 by Zeus

If you’re anything like me, you’ve started your game development journey by making simple 2D games.

A lot of beginner tutorials and books introduce people to game making through 2D.

Have you ever asked yourself why?

Is 3D a magnitude harder to work with when compared to 2D?

Do you really need AAA grade resources to produce a fun 3D game?

Is 2D game development actually harder?

For a long time, I was held back by some limiting beliefs about 3D, I never dared to make the jump because of them.

Now that I have, I’d like to share some common misconceptions about 3D to spare you the pain that I’ve endured and to help you reach your full potential as a game developer.

[Read more…] about Misconceptions About 3D That Hurt Your Game

Filed Under: General

Becoming a 3D Game Artist

February 8, 2020 by Zeus

I’m a decent 2D artist, but deep down, I’ve always wanted to make 3D models for games. I guess I just lacked the determination and patience to go through the learning process.

Like a child sitting at a piano, feeling the music inside but unable to hit the right notes at the right time, I’ve always had the 3D music in me, but I needed to get real and start learning the basics.

I decided to begin by learning 2D art a few years ago. My reasoning was that it would be easier to jump from 2D to 3D. In hindsight, I was right.

I followed tutorials, learned the ropes, and kept doodling every day. Soon enough, I reached a level decent enough to work as a professional 2D game artist.

I produced more than a hundred 2D game assets in the past 3 years to sell on various asset stores, I also did some freelancing on the side.

The experience changed my life for the better.

I interacted with customers for the first time in my life. It was humbling to see people willingly part with their hard-earned cash in return for the fruits of my labor.

I earned enough income to upgrade my tools. I purchased a better graphical tablet, better software, a better desk, and a more comfortable chair.

I also published my first indie game on Steam in April of 2019. Which took a few months of hard work.

So with all that experience, I felt ready to jump into that sweet third dimension.

In August of 2019, I took the leap.

I knew the drill: follow tutorials, practice every day, don’t give in to frustration, etc. I was a professional, after all.

I learned to love the process. The small failures. The tiny successes.

I produced terrible results, but I knew it was part of the fun. So I kept at it, often remodeling the same thing 4 to 6 times just to get it right.

3D wasn’t completely new to me. I had some experience making simple models in the past. What I lacked was discipline and motivation to get to the next level.

As with 2D art, I gathered a collection of images and videos from the web that I would look at every day. Being inspired is important for an artist, as I’ve learned throughout my short career.

I got pretty good at modeling. So I started focusing more on UV mapping and texturing. It took me quite a while to wrap my mind around these subjects. Fortunately, the web is rife with tutorials and courses to learn just about anything.

Now that my skills are on par with many professional artists out there, I can finally start adding my personal touch to the world of 3D game art.

For me, 3D is all about more ways to express myself. In practice, it will allow my studio to produce more useful game assets for you and other developers all around the globe.

I’m happy and excited thinking of the beautiful assets I will be sharing in the months to come. I’ll try to write posts like these on a weekly basis to keep you informed of what is going on in the studio.

Filed Under: General

Primary Sidebar

Recent Posts

  • Krita Scripting: Export Top Level Layers into PNGs
  • How To Defeat The Law of Diminishing Returns and Keep Yourself Motivated
  • Modeling and Texturing the JU87 Stuka – My First Timelapse Video
  • 12 Tips To Rekindle Your Love For Game Making
  • Misconceptions About 3D That Hurt Your Game
Tweets by zeusgameassets

Copyright © 2023