Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script for removing multi-adding same sprites to the non animated atlas in Defold project
import os, sys, hashlib
import deftree
def all_files(ending, project_root):
# 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 get_attribute(root, attribute_name):
# To simplify getting all images in an atlas
for x in root.iter():
if deftree.is_element(x):
get_attribute(x, attribute_name)
if == attribute_name:
yield x
def remove_duplicate_sprites_in_atlases(project_root):
#Replace duplicates in atlases
output = []
for atlas in all_files(".atlas", project_root):
changes = 0
atlas_name = os.path.basename(atlas) #atlas name
tree = deftree.parse(atlas)
root = tree.get_root()
atlas_sprites = []
need_to_remove = []
for image in get_attribute(root, "image"):
if image.get_parent().get_parent().name != "animations":
if image.value in atlas_sprites:
output.append({"atlas": atlas_name, "remove": image.value})
changes = changes + 1
for image in need_to_remove:
if changes > 0:
return output
if len(sys.argv) > 1:
project_root = sys.argv[1]
if os.path.exists(project_root):
#Remove duplicate spritest in atlases
output = remove_duplicate_sprites_in_atlases(project_root)
#print output
for value in output:
print("Atlas {} : Remove {} ".format(value["atlas"], value["remove"]))
print("Remove {} duplicates".format(len(output)))
print('Usage: python project_folder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment