"""
SURYA - GLTF Export Script
Exports all scenes from surya_all.blend as individual GLTF files.

Usage:
    /Applications/Blender.app/Contents/MacOS/Blender surya_all.blend --background --python export_gltf.py
"""

import bpy
import os

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
EXPORTS_DIR = os.path.join(SCRIPT_DIR, "exports")

def export_all_gltf():
    """Export each scene as a GLTF file."""
    os.makedirs(EXPORTS_DIR, exist_ok=True)
    
    print("=" * 60)
    print("SURYA - GLTF Export")
    print("=" * 60)
    
    exported = []
    failed = []
    
    for scene in bpy.data.scenes:
        scene_name = scene.name
        print(f"\nExporting: {scene_name}...")
        
        # Set as active scene
        bpy.context.window.scene = scene
        
        # Deselect all, then select all visible
        bpy.ops.object.select_all(action='DESELECT')
        for obj in scene.objects:
            if obj.visible_get():
                obj.select_set(True)
        
        gltf_path = os.path.join(EXPORTS_DIR, f"{scene_name}.gltf")
        
        try:
            bpy.ops.export_scene.gltf(
                filepath=gltf_path,
                export_format='GLTF_SEPARATE',
                export_animations=True,
                export_apply=False,
                export_texcoords=True,
                export_normals=True,
                export_materials='EXPORT',
                use_selection=False,
                export_extras=False,
                export_yup=True,
                export_cameras=True,
            )
            print(f"  ✓ Exported: {gltf_path}")
            exported.append(scene_name)
        except Exception as e:
            print(f"  ✗ Failed: {e}")
            failed.append((scene_name, str(e)))
    
    # Summary
    print("\n" + "=" * 60)
    print("GLTF EXPORT SUMMARY")
    print("=" * 60)
    print(f"\nSuccessful: {len(exported)}")
    for name in exported:
        print(f"  ✓ {name}.gltf")
    
    if failed:
        print(f"\nFailed: {len(failed)}")
        for name, error in failed:
            print(f"  ✗ {name}: {error}")
    
    print(f"\nOutput directory: {EXPORTS_DIR}")


if __name__ == "__main__":
    export_all_gltf()
