Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Last active March 6, 2018 16:57
Show Gist options
  • Save Jerakin/d4feecaeb3ecb76d20b5e36a51ea699a to your computer and use it in GitHub Desktop.
Save Jerakin/d4feecaeb3ecb76d20b5e36a51ea699a to your computer and use it in GitHub Desktop.
Remove obsolete assets from a gui file
import deftree
import os
import sys
def run_on_file(project_path, gui_file):
tree = deftree.parse(gui_file)
output = []
output.extend(clean_textures(tree))
output.extend(clean_layers(tree))
output.extend(clean_fonts(tree))
output.extend(clean_spine_scenes(tree))
if output:
output.insert(0, gui_file.replace(project_path, ""))
else:
output.append("No obsolete resources found")
return output
def resource_used_in_scene(root, resource_name):
used_resource = list()
for x in root.iter_elements("nodes"):
if not x:
continue
resource = x.get_attribute(resource_name)
if resource:
atlas = resource.value.split("/")[0]
if atlas and atlas not in used_resource:
used_resource.append(atlas)
return used_resource
def clean_textures(tree):
out = []
root = tree.get_root()
gui_file = tree.get_document_path()
# CLEAN UP TEXTURES
for x in root.iter_elements("textures"):
if not x:
continue
name_attr = x.get_attribute("name")
if name_attr:
name = name_attr.value
if name not in resource_used_in_scene(root, "texture"):
root.remove(x)
out.append(" Atlas {}".format(name))
tree.write(gui_file)
return out
def clean_layers(tree):
out = []
root = tree.get_root()
gui_file = tree.get_document_path()
# CLEAN UP LAYERS
for x in root.iter_elements("layers"):
if not x:
continue
name_attr = x.get_attribute("name")
if name_attr:
name = name_attr.value
if name not in resource_used_in_scene(root, "layer"):
root.remove(x)
out.append(" Layer {}".format(name))
tree.write(gui_file)
return out
def clean_fonts(tree):
out = []
root = tree.get_root()
gui_file = tree.get_document_path()
# CLEAN UP FONTS
for x in root.iter_elements("fonts"):
if not x:
continue
name_attr = x.get_attribute("name")
if name_attr:
name = name_attr.value
if name not in resource_used_in_scene(root, "font"):
root.remove(x)
out.append(" Font {}".format(name))
tree.write(gui_file)
return out
def clean_spine_scenes(tree):
out = []
root = tree.get_root()
gui_file = tree.get_document_path()
# CLEAN UP SPINE
for x in root.iter_elements("spine_scenes"):
if not x:
continue
name_attr = x.get_attribute("name")
if name_attr:
name = name_attr.value
if name not in resource_used_in_scene(root, "spine_scene"):
root.remove(x)
out.append(" Spine Scene {}".format(name))
tree.write(gui_file)
return out
def main(project):
for gui in [os.path.join(root, f) for root, folders, files in os.walk(project) for f in files if f.endswith(".gui")]:
output = run_on_file(project, gui)
for line in output:
print(line)
if __name__ == '__main__':
if len(sys.argv) > 1:
project_root = sys.argv[1]
if os.path.exists(project_root):
main(project_root)
else:
print('Usage: python delete_obsolete_resources.py project_folder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment