Skip to content

Instantly share code, notes, and snippets.

@scurest
Created May 12, 2019 17:00
Show Gist options
  • Save scurest/0db0cc8eec1ac744f88afbe1d320f3da to your computer and use it in GitHub Desktop.
Save scurest/0db0cc8eec1ac744f88afbe1d320f3da to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Splits apicula's .daes into multiple .daes with one animation each.
# Original script by Inferry: <https://www.vg-resource.com/thread-30547-post-634261.html#pid634261>
import sys, os, copy
import xml.etree.ElementTree as ET
ET.register_namespace('', "http://www.collada.org/2005/11/COLLADASchema")
def main(path):
tree = ET.parse(path)
root = tree.getroot()
ns = {'d': 'http://www.collada.org/2005/11/COLLADASchema'}
lib_clips = root.find('d:library_animation_clips', ns)
if not lib_clips:
print('No animations. Nothing to do.')
return
clips = lib_clips.findall('d:animation_clip', ns)
print('Found', len(clips), 'animations.')
root.remove(lib_clips)
lib_anims = root.find('d:library_animations', ns)
all_anims = lib_anims.findall('d:animation', ns)
for clip in clips:
anim_ids = set(
inst_anim.attrib.get('url')[1:]
for inst_anim in clip.findall('d:instance_animation', ns)
)
# Keep only the <animations>s for this clip
for anim in lib_anims.findall('d:animation', ns):
lib_anims.remove(anim)
for anim in all_anims:
if anim.attrib.get('id') in anim_ids:
lib_anims.append(anim)
clip_name = clip.attrib.get('name')
save_path = pick_file_name(path, clip_name)
tree.write(save_path, encoding='utf-8', xml_declaration=True)
print('Saved animation', clip_name, 'to file', save_path)
def pick_file_name(path, anim_name):
dot_i = path.rfind('.')
ext = '' if dot_i == -1 else path[dot_i:]
base = path if dot_i == -1 else path[:dot_i]
save_path_base = base + '.' + anim_name
# Find available file name
cntr = 1
while os.path.exists(save_path_base + ext):
save_path_base = base + '.' + anim_name + '.' + ('%03d' % cntr)
cntr += 1
return save_path_base + ext
if len(sys.argv) != 2 or sys.argv[1] in ['-h', '--help']:
print('Usage: splitter.py file.dae')
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment