Jump to content
MoonStar

tiny script to sort spriter batch animations exports

Recommended Posts

First I'm not much of a programmer. So any feedback is welcome. Special corner cases not tested. 

 

Download AnimationSorter

 

Prerequisites

  • Animations must be batch export or all animations must have a prefix
  • Animation names must be in following format

prefix + animation + number + file extension 

 

So for example you have 3 animations: Attack, Idle, Run. 

When you export in spriter you are asked for a base file name. When you type "character" as base file name spriter spits out 3 animations like this

  • character_attack_000.png
  • character_attack_001.png
  • character_attack_002.png
  • character_idle_000.png
  • character_idle_001.png
  • character_idle_002.png
  • character_run_000.png
  • character_run_001.png
  • character_run_002.png

(Number of files is not important)

How it works

  1. Export animations in spriter (e.g batch export)
  2. when exporting enter a prefix e.g character
  3. put the executable script file into the same folder as your animation exports
  4. run exe
  5. enter the prefix which every animation files have in common e.g character from step 2
  6. enjoy that you don't need to sort animations anymore :)

 

What the script does:

Jj6zSYd.gif

The script searches for all png images in the same folder the script is. Then it extracts the given prefix from each image. character_attack_000.png becomes attack_000.png

Then it extracts the extension and number as well so only animation name is left wich is attack. Next it creates a folder with that animation name and afterwards puts each matching image file in that folder. And this for each image. 

 

Conclusion

I found the script to be a huge time saver. Especially with hundreds of image files (in the gif example 500+ images were sorted under 1s). So I hope it helps you guys out as well.

Here the full script:

import glob
import os
import shutil
import time


class AnimationSorter:
    """A simple sorter class"""

    def __init__(self):
        self.prefix = ''
        self.folders_count = 0
        self.files_count = 0

    def set_prefix(self):
        self.prefix = raw_input('Type in the prefix of the animations.\nPrefix: ')
        print self.prefix

    def get_animations(self):
        path = os.getcwd() + '\\'
        anims = glob.glob(path + '*.png')
        return anims

    def sortanim(self):

        anims = self.get_animations()

        if not anims:
            print('No files found.')
            return False

        self.files_count = len(anims)

        for i, x in enumerate(anims):

            basedir, filename = os.path.split(x)

            result = x.replace(str(self.prefix), "", 1)
            n = result.split("_", 1)
            result = n[0]
            result = result.translate(None, '_')

            dir_name, ext = os.path.splitext(result)

            if not os.path.exists(dir_name):
                os.makedirs(dir_name)
                print 'new directory: ' + dir_name
                self.folders_count += 1

            try:
                shutil.move(x, dir_name + '\\' + filename)
            except IOError as (errno, strerror):
                print "I/O error({0}): {1}".format(errno, strerror)
            except ValueError:
                print "Could not convert data to an integer."
            except:
                print "Unexpected error:"
                raise

            self.files_count += 1
            # print 'moving file success'

        return True


if __name__ == "__main__":

    print '___________ SORT ANIMATION EXPORTS ___________\n\n'
    sorter = AnimationSorter()

    sorter.set_prefix()

    start = time.time()
    # call your code here

    if sorter.sortanim():
        end = time.time()
        print 'Done.' \
              '\nTotal files: ' + str(sorter.files_count - 1) + \
              '\nTotal Folders: ' + str(sorter.folders_count) + \
              '\nTime needed: ' + str(end - start) + 's'

    k = raw_input('\n\nPress any key to close.\n')

 

 

 

Download AnimationSorter

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...