Python scripts for exporting and importing transform masks to and from an excel table

These are two python scripts, made to convert animated transform masks’ keyframes into excel tables and back.

Example of the table(“time” is keyframe number):

Why would you need that?
Relatively quick and accurate mass edits of coordinates and making your animated objects follow certain paths that can be calculated with formulas.
(Won’t lie, the solution i made is very rough and there’s still a lot of slow manual work compared to making these animations in programs actually designed for this. I’m hoping that some day i’ll achieve a more convenient solution that will work as a plugin and would only require inputting starting coordinates and settings like radius, but it would take me so much time, i’m not sure if i can afford that. I’ve already been in a hole of developing new things for months, it ended up badly for my health.)


I compiled the scripts into 2 executables that should work without python, but i have no way to test them on another system, so if these won’t work for you, you might need to use python to make the scripts work in their open form instead.
To make them work with python you will need to have installed python and modules “pandas” and “openpyxl”.
It might require some other modules, in which case it will most likely tell you what’s missing after you run the scripts. If they just close without doing anything and sending any messages, you might have to open and run them in IDLE to get error messages.


Due to the fact that i used help of chatgpt and also tried to save a lot of time, all of the keyframe channels ended up being squished together in one table. It’s a bit inconvenient. To add new keyframes and coordinates you will have to move the rest of the data further down.
Also, for the reasons mentioned above, there might be some redundant parts in the code, if i missed any.
If you know python, you can check the script yourself and tell me if there’s anything you don’t like.
I’m not a professional, mistakes are not uncommon for me.


HOW TO USE:
1.Get your .kra project file containing animated transform masks, change its format to .zip and open it with an archive manager.
2.Find a folder named Layers, then find your keyframes file of .xml format.
If you use multiple masks and can’t figure out which file you need, maindoc.xml shows real names of the masks.
3.Take the file out and place it in the folder with the executables.
4.Run the “Convert to excel.exe”
5.Make necessary edits/additions.
6.Run the “Convert back to xml.exe”
7.Rename the new file and replace it with the old one in the archive.
8.Change the archive back to .kra format.
9.Launch the edited project and see if the changes worked.
10.When making another conversion, to avoid conflicts, delete or move the old used files from the folder with executables.


Example of a use case:
ezgif.com-gif-maker (1)
Animation of an object moving in a circular pattern(This can also be achieved by grouping the object with an invisible layer and rotating transform mask, or combining rotating and moving transform masks. Or using a more suitable program for this. Each method has its pros and cons):

One of the ways to make rough calculations for this pattern is:

  • current x = 1300 (example)
  • current y = 960 (example)
  • [fr] = total amount of frames, for example 48
  • r = radius of the circle. Presumably in pixels. The one used in the example had a value of 100.
  • [angle increment] = 2*PI()/[fr]
  1. Set up an Excel table with two columns: Frame Number and Angle.

  2. In the Frame Number column, list the frame numbers from 1 to the total number of keyframes.

  3. Then calculate the angle column using:
    Angle = (Frame Number - 1) * Angle increment

  4. In another two columns, calculate the x and y coordinates for each angle using these equations:
    x = 1300 + radius * cos(Angle)
    y = 960 + radius * sin(Angle)

  5. The resulting coordinates can be added as new keyframes in the table.

6 Likes

Congratulations on getting this done.

2 Likes

Oh yes ! :slight_smile: This could be very useful for me - thank you :+1:

You did it very quickly too.
Now I need to find time to study it and learn how to use it.

1 Like

Would it not be easier to write formulas for animation ?

Can you elaborate what you mean by “writing formulas for animation”?

Exporting the transform mask to a table and using excel for calculations technically is using formulas for animation, there’s just a lot of manual work involved.
If you meant making it more automated, that’s the next step, planned for some point in the future.

If there’s an easier way, i’d love to hear more about it.

Well if your animation is like regular over time you just need to place formula and feed the frame to calculate it with a for loop or something. And add a offset to each to make the disparity. The excel is just already solved.

@BrokenIterator Just a thought - does the Excel spreadsheet work with Libre Office’s Calc spreadsheet application? Libre Office is free, so …

Also, is it Windows only for the other facilities?

@EyeOdin
I assume you’re talking about the way i did the calculations in my example of the circle pattern, in which case i totally agree that there are better ways to create keyframes for these types of movement patterns. This one is merely a rough example.
I’d even say that one won’t even need to use a spreadsheet if they are just using a formula, i just did it that way to also make it useful for some other various purposes(Like doing simple manual edits for keyframe coordinates. It’s a pain to do that without a spreadsheet in Krita, it’s so slow to select each of them one by one).

@AhabGreybeard
From wikipedia:
“Calc is capable of opening and saving most spreadsheets in [Microsoft Excel file format]”
Is that the information you are looking for?

No clue about it being Windows only, i don’t have any other os to test that, sorry.

When I find time, if I can figure them out, I’ll see what I can do with them :slight_smile:

1 Like

I’m curious if it would be possible to make a plugin that would convert vector layers into transform mask paths. That would be way more convenient, plus it’s also possible to make vector shapes like the circle pattern.
However that’s probably going to be a lot more difficult compared to just a couple of the spreadsheet conversion scripts. It seems that different shapes are written in slightly different ways. Also some of them are loops, while others are normal paths.
If i find a way to convert the vector data to transform mask coordinates and tangents, i’ll try to look deeper into that.

You can convert a ‘shape’ such as an ellipse or a rectangle into a path representation.
Select it then in the Tool Options docker do Convert to Path.

That would be an extra stage of learning for you and probably have complications.
You can Export a vector layer as an .svg file and then try to create an application to parse and do stuff with the .svg file to generate a list of coordinates.

1 Like

Fighting through the complications could be worth it, especially if it will make it possible to do work right in the program, without extracting anything.
I assume that most artists would be a lot happier to only need to spend a few seconds to make a path using vectors, compared to having to dive into a whole table of data and needing to make formulas for their animations.

“You can convert a ‘shape’ such as an ellipse or a rectangle into a path representation.”
Wasn’t aware of that, thanks for the information.

I’ll see what i can do.

2 Likes

After many attempts to make vectors easily usable for transform masks, I’ve run into a dead end, as it turns out that:

  • Krita’s library doesn’t seem to actually have a method for inputting and outputting transform mask coordinates through scripts. There is some .xml data that can be output, but it’s just a config file, not the coordinates. It’s also weird that their tangents aren’t exposed in the animation curves docker. That would make it much easier to make circular paths manually.
    I assume that to make the scripts actually output the information i want, i’d have to edit the source code and compile my own version of krita. I’m not doing that. I don’t even know if it’s possible, as not all programs can be directly compiled from github, sometimes devs don’t give all of the files.
  • If a plugin needs an external python module, it’s a problem, because apparently adding these modules to krita is not as simple as “pip install”. Way too complicated for an average user. Perhaps it would be possible to solve this by making an installer with the plugin and modules, but at this point that’s just too many different steps for me.
  • Vectors have their own canvas that’s placed within the main canvas. It has its own coordinate system. The coordinates within the vector objects aren’t directly affected by transform actions such as movement, resizing and rotation, meaning that i’d have to make special formulas that would account for these actions and canvas size when converting them to coordinates of the main canvas. That’s a lot of work.
  • Tangents used in vector curves affect both x and y coordinates at once. Transforms use 2 separate curves for x and y. I have no clue what could possibly be the formula for separating one curve into two.

I found a few ways that could make vectors useful for conversion and work with transforms despite everything mentioned above, but the way it’s done is so specific that i would be the only person who would actually want to use that method. To do something useful with the vectors, one would basically have to:

  • Go through the process of extracting files and using external scripts. (Because i can’t make a convenient plugin that extracts the data on the fly.)
  • Move the vector’s transform to 0.0, never do any rotation or resizing. Only move the vector’s points, not the whole object.(So i wouldn’t have to make my code account for any vector’s transform actions)
  • Convert the whole vector to linear points instead of curves. Which would make any circular objects look like a bunch of straight lines, unless you add a lot of points. And it turns out that you can’t just ask krita to split all of the segments in half to add points, it only places new points between 2 selected points. So you’d have to manually go through each segment to divide them all. And the more round the shape is, the more segments you’d have to divide.

The final conclusion is that the difficulty of implementing and using this solution makes it not worth spending any more time and effort on it, as nobody besides me will actually want to use the currently feasible final product, especially considering that there are already existing programs designed specifically for that kind of work. A real shame, but at least i learned a few new things, so the time spent on it wasn’t wasted for me.

1 Like

All animated transform ‘curves’ are a bunch of straight lines because it’s the value of the ‘curve’ at each discrete frame that is applied to the visible frame.
There are no smooth changes in the final animation. So, the number of points you’d need would be determined by the total number of animation frames that the vector path was applied to.

This link seems to explain a method for doing it and shows the stages along the way, with a final list of x,y coordinates, I assume:
java - How to load and parse SVG documents - Stack Overflow

1 Like