165 lines
4.7 KiB
Python
165 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script para convertir archivos .ani a formato YAML con nomenclatura camelCase
|
|
"""
|
|
|
|
import os
|
|
import glob
|
|
import re
|
|
|
|
|
|
def parse_ani_file(file_path):
|
|
"""
|
|
Parsea un archivo .ani y retorna un diccionario con los datos
|
|
"""
|
|
data = {
|
|
'tileSetFile': None,
|
|
'frameWidth': None,
|
|
'frameHeight': None,
|
|
'animations': []
|
|
}
|
|
|
|
current_animation = None
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
|
|
# Ignorar líneas vacías
|
|
if not line:
|
|
continue
|
|
|
|
# Detectar inicio de animación
|
|
if line == '[animation]':
|
|
current_animation = {}
|
|
continue
|
|
|
|
# Detectar fin de animación
|
|
if line == '[/animation]':
|
|
if current_animation:
|
|
data['animations'].append(current_animation)
|
|
current_animation = None
|
|
continue
|
|
|
|
# Parsear parámetros
|
|
if '=' in line:
|
|
key, value = line.split('=', 1)
|
|
key = key.strip()
|
|
value = value.strip()
|
|
|
|
# Parámetros globales
|
|
if key == 'tileSetFile':
|
|
data['tileSetFile'] = value
|
|
elif key == 'frame_width':
|
|
data['frameWidth'] = int(value)
|
|
elif key == 'frame_height':
|
|
data['frameHeight'] = int(value)
|
|
# Parámetros de animación
|
|
elif current_animation is not None:
|
|
if key == 'name':
|
|
current_animation['name'] = value
|
|
elif key == 'speed':
|
|
current_animation['speed'] = float(value)
|
|
elif key == 'loop':
|
|
current_animation['loop'] = int(value)
|
|
elif key == 'frames':
|
|
# Convertir "0,1,2,3" a [0, 1, 2, 3]
|
|
current_animation['frames'] = [int(x.strip()) for x in value.split(',')]
|
|
|
|
return data
|
|
|
|
|
|
def generate_yaml(data, comment_header=""):
|
|
"""
|
|
Genera el contenido YAML a partir de los datos parseados
|
|
"""
|
|
lines = []
|
|
|
|
# Añadir comentario de header si existe
|
|
if comment_header:
|
|
lines.append(f"# {comment_header}")
|
|
|
|
# Parámetros globales
|
|
if data['tileSetFile']:
|
|
lines.append(f"tileSetFile: {data['tileSetFile']}")
|
|
if data['frameWidth'] is not None:
|
|
lines.append(f"frameWidth: {data['frameWidth']}")
|
|
if data['frameHeight'] is not None:
|
|
lines.append(f"frameHeight: {data['frameHeight']}")
|
|
|
|
# Animaciones
|
|
if data['animations']:
|
|
lines.append("")
|
|
lines.append("animations:")
|
|
|
|
for anim in data['animations']:
|
|
lines.append(f" - name: {anim.get('name', 'default')}")
|
|
lines.append(f" speed: {anim.get('speed', 0.0)}")
|
|
lines.append(f" loop: {anim.get('loop', 0)}")
|
|
|
|
# Frames en formato compacto [0, 1, 2, 3]
|
|
frames = anim.get('frames', [])
|
|
frames_str = '[' + ', '.join(str(f) for f in frames) + ']'
|
|
lines.append(f" frames: {frames_str}")
|
|
|
|
# Línea vacía entre animaciones (excepto la última)
|
|
if anim != data['animations'][-1]:
|
|
lines.append("")
|
|
|
|
return '\n'.join(lines) + '\n'
|
|
|
|
|
|
def convert_ani_to_yaml(ani_path):
|
|
"""
|
|
Convierte un archivo .ani a .yaml
|
|
"""
|
|
# Generar nombre del archivo de salida
|
|
yaml_path = ani_path.replace('.ani', '.yaml')
|
|
|
|
# Parsear archivo .ani
|
|
data = parse_ani_file(ani_path)
|
|
|
|
# Generar comentario de header basado en el nombre del archivo
|
|
filename = os.path.basename(ani_path)
|
|
comment = f"{filename.replace('.ani', '')} animation"
|
|
|
|
# Generar contenido YAML
|
|
yaml_content = generate_yaml(data, comment)
|
|
|
|
# Escribir archivo .yaml
|
|
with open(yaml_path, 'w', encoding='utf-8') as f:
|
|
f.write(yaml_content)
|
|
|
|
print(f"✓ {ani_path} → {yaml_path}")
|
|
|
|
return yaml_path
|
|
|
|
|
|
def main():
|
|
"""
|
|
Función principal que convierte todos los archivos .ani a .yaml
|
|
"""
|
|
# Buscar todos los archivos .ani
|
|
ani_files = glob.glob('data/**/*.ani', recursive=True)
|
|
|
|
if not ani_files:
|
|
print("No se encontraron archivos .ani")
|
|
return
|
|
|
|
print(f"Encontrados {len(ani_files)} archivos .ani\n")
|
|
|
|
# Convertir cada archivo
|
|
converted = 0
|
|
for ani_file in sorted(ani_files):
|
|
try:
|
|
convert_ani_to_yaml(ani_file)
|
|
converted += 1
|
|
except Exception as e:
|
|
print(f"✗ Error al convertir {ani_file}: {e}")
|
|
|
|
print(f"\n{converted}/{len(ani_files)} archivos convertidos exitosamente")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|