batch_apply_palette
Apply a color palette to multiple Aseprite files simultaneously to maintain consistent visual style across pixel art projects.
Instructions
Apply a palette to multiple Aseprite files.
Args: input_dir: Directory containing input files palette_file: Path to palette file or preset name file_pattern: File pattern to match (default: "*.aseprite") create_backup: Create backup of original files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_dir | Yes | ||
| palette_file | Yes | ||
| file_pattern | No | *.aseprite | |
| create_backup | No |
Implementation Reference
- aseprite_mcp/tools/batch.py:329-424 (handler)Implementation of the batch_apply_palette tool which iterates over files in a directory and applies a specified palette.
async def batch_apply_palette( input_dir: str, palette_file: str, file_pattern: str = "*.aseprite", create_backup: bool = True ) -> str: """Apply a palette to multiple Aseprite files. Args: input_dir: Directory containing input files palette_file: Path to palette file or preset name file_pattern: File pattern to match (default: "*.aseprite") create_backup: Create backup of original files """ try: # Validate inputs input_path = validate_file_path(input_dir, must_exist=True) if not input_path.is_dir(): raise ValidationError("input_dir", str(input_path), "Must be a directory") # Check if palette_file is a preset or file from .palette import PRESET_PALETTES is_preset = palette_file.lower() in PRESET_PALETTES if not is_preset: palette_path = validate_file_path(palette_file, must_exist=True) # Find all matching files files = list(input_path.glob(file_pattern)) if not files: return f"No files matching '{file_pattern}' found in {input_path}" # Process each file results = [] errors = [] for file in files: try: # Create backup if requested if create_backup: backup_file = file.with_suffix(file.suffix + ".bak") import shutil shutil.copy2(file, backup_file) # Apply palette if is_preset: from .palette import apply_preset_palette result = await apply_preset_palette(str(file), palette_file) else: # Extract palette from file and apply builder = LuaBuilder() builder.open_sprite(str(palette_path)) builder.add_line('local paletteSpr = app.activeSprite') builder.add_line('local sourcePalette = paletteSpr.palettes[1]') builder.open_sprite(str(file)) builder.add_line('local targetSpr = app.activeSprite') builder.add_line('targetSpr:setPalette(sourcePalette)') builder.save_sprite() cmd = get_command() success, output = cmd.execute_lua_script(builder.build()) result = f"Applied palette to {file.name}" results.append(result) except Exception as e: errors.append(f"Failed {file.name}: {e}") if not get_config().batch.continue_on_error: break # Summary summary = f"Batch apply palette completed:\n" summary += f"- Processed: {len(results)} files\n" summary += f"- Failed: {len(errors)} files\n" if create_backup: summary += "- Backups created with .bak extension\n" if results: summary += "\nSuccessful:\n" + "\n".join(results[:10]) if len(results) > 10: summary += f"\n... and {len(results) - 10} more" if errors: summary += "\n\nErrors:\n" + "\n".join(errors[:5]) if len(errors) > 5: summary += f"\n... and {len(errors) - 5} more" return summary except (ValidationError, AsepriteError) as e: return f"Failed to batch apply palette: {e}" except Exception as e: return f"Unexpected error: {e}"