Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script for removing duplicates in flipbook animation in Defold project
import os, sys, hashlib
import deftree
def all_files(ending):
# Generator to get files
for root, folders, files in os.walk(project_root):
for f in files:
if f.endswith(ending):
yield os.path.join(root, f)
def hashfile(path, blocksize = 65536):
afile = open(path, 'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
def remove_duplicates_in_flipbook_animations(project_root):
img_counter = 0
atlas_counter = 0
for x in all_files(".atlas"):
tree = deftree.parse(x)
root = tree.get_root()
dup = {}
changes = 0
for element in root.iter_elements("animations"):
for image in element.iter_elements("images"):
img = image.get_attribute("image")
path = project_root[:-1] + img.value
file_hash = hashfile(path)
if (file_hash in dup) and (path != dup[file_hash]):
print("Replace {} with {} in atlas {}".format(path, dup[file_hash], x))
set_path = dup[file_hash].replace(project_root, "/")
image.set_attribute("image", set_path)
changes = changes + 1
img_counter = img_counter + 1
else:
dup[file_hash] = path
if changes >= 1:
tree.write(tree.get_document_path())
if changes >= 1:
atlas_counter = atlas_counter + 1
print("{} images in {} atlases was replaced".format(img_counter, atlas_counter))
if len(sys.argv) > 1:
project_root = sys.argv[1]
if os.path.exists(project_root):
#find and replace animation duplicates in atlases
remove_duplicates_in_flipbook_animations(project_root)
else:
print('Usage: python remove_duplicates_in_flipbook.py project_folder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.