Skip to content

Instantly share code, notes, and snippets.

@AGulev
Last active March 8, 2018 19:28
Show Gist options
  • Save AGulev/b02da861ecb2177ac53dcac4e23eed93 to your computer and use it in GitHub Desktop.
Save AGulev/b02da861ecb2177ac53dcac4e23eed93 to your computer and use it in GitHub Desktop.
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)
else:
if x.name == 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:
need_to_remove.append(image)
output.append({"atlas": atlas_name, "remove": image.value})
changes = changes + 1
else:
atlas_sprites.append(image.value)
for image in need_to_remove:
image.get_parent().get_parent().remove(image.get_parent())
if changes > 0:
tree.write(tree.get_document_path())
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)))
else:
print('Usage: python delete_duplicate_sprites_in_atlases.py project_folder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment