import bpy
import os
import random

# Get the directory path for the blend file
blend_directory = os.path.dirname(bpy.data.filepath)

# Set the directory path for the VRM files (same as blend file directory)
vrm_directory = blend_directory

# Set the directory path for the output GLB files (same as blend file directory)
output_directory = blend_directory

# Set the array of Mixamo animation names to randomly select from
animation_names = ['Breathing Idle', 'Dance', 'Jump', 'Punching', 'Running', 'Walking']

# Loop through all VRM files in the directory
for filename in os.listdir(vrm_directory):
    if filename.endswith('.vrm'):
        # Import the VRM file into the scene
        bpy.ops.import_scene.vrm(filepath=os.path.join(vrm_directory, filename))
        
        # Randomly select a Mixamo animation
        animation_name = random.choice(animation_names)
        
        # Set the selected animation as the active action
        action = bpy.data.actions.get(animation_name)
        if action:
            bpy.context.object.animation_data.action = action
        
            # Export the posed VRM as a GLB file
            output_path = os.path.join(output_directory, filename.replace('.vrm', '.glb'))
            bpy.ops.export_scene.gltf(filepath=output_path, export_format='GLB', export_selected=True)
        
        # Remove the VRM object from the scene
        bpy.ops.object.select_all(action='SELECT')
        bpy.ops.object.delete(use_global=False)