# Migration functions for preset storage system import bpy import json import os import shutil from datetime import datetime def migrate_presets_if_needed(): """ Migrate presets from older versions if needed. This function checks if migration is required and performs it automatically. Returns: dict with 'migrated_count' key indicating number of presets migrated. """ print("[TTG MIGRATION DEBUG] Starting migrate_presets_if_needed()") preferences = bpy.context.preferences.addons.get("Text_Texture_Generator_Pro") if not preferences: print("[TTG MIGRATION DEBUG] No preferences found - returning proper dict format") return {'migrated_count': 0} addon_version = preferences.preferences.get("addon_version", "0.0.0") current_version = "1.0.0" # This should match the addon's current version if addon_version != current_version: print(f"Text Texture Generator: Migrating presets from version {addon_version} to {current_version}") # Backup existing presets before migration backup_success = backup_presets() if not backup_success: print("Text Texture Generator: Warning - Failed to backup presets before migration") # Perform migration steps based on version differences migration_success = True migrated_count = 0 try: # Version-specific migration logic would go here # For now, we'll just update the version number preferences.preferences["addon_version"] = current_version # In a real migration, this would be the actual count of migrated presets # For now, we assume 1 migration operation (version update) migrated_count = 1 if migration_success: print(f"Text Texture Generator: Successfully migrated presets to version {current_version}") else: print("Text Texture Generator: Migration completed with warnings") except Exception as e: print(f"Text Texture Generator: Error during preset migration: {str(e)}") migration_success = False migrated_count = 0 print(f"[TTG MIGRATION DEBUG] Migration completed, returning proper dict format: migrated_count={migrated_count}") return {'migrated_count': migrated_count} print("[TTG MIGRATION DEBUG] No migration needed, returning proper dict format: migrated_count=0") return {'migrated_count': 0} # No migration needed def backup_presets(): """ Create a backup of current presets before migration. Returns True if backup was successful, False otherwise. """ try: # Get the addon directory addon_dir = os.path.dirname(__file__) parent_dir = os.path.dirname(addon_dir) # Create backup directory with timestamp timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") backup_dir = os.path.join(parent_dir, f"preset_backup_{timestamp}") # Find preset files to backup preset_files = [] # Check for blend file presets if bpy.data.filepath: blend_file_dir = os.path.dirname(bpy.data.filepath) preset_file = os.path.join(blend_file_dir, ".text_texture_presets.json") if os.path.exists(preset_file): preset_files.append(preset_file) # Check for persistent presets preferences = bpy.context.preferences.addons.get("Text_Texture_Generator_Pro") if preferences and hasattr(preferences.preferences, 'persistent_presets_path'): persistent_path = preferences.preferences.persistent_presets_path if persistent_path and os.path.exists(persistent_path): preset_files.append(persistent_path) # Check for legacy presets in addon directory legacy_preset_file = os.path.join(parent_dir, "text_texture_presets.json") if os.path.exists(legacy_preset_file): preset_files.append(legacy_preset_file) # Create backup if there are files to backup if preset_files: os.makedirs(backup_dir, exist_ok=True) for preset_file in preset_files: filename = os.path.basename(preset_file) backup_path = os.path.join(backup_dir, filename) shutil.copy2(preset_file, backup_path) print(f"Text Texture Generator: Backed up {filename} to {backup_dir}") # Create a backup info file backup_info = { "backup_timestamp": timestamp, "addon_version_before_migration": bpy.context.preferences.addons.get("Text_Texture_Generator_Pro", {}).get("addon_version", "unknown"), "files_backed_up": [os.path.basename(f) for f in preset_files] } info_file = os.path.join(backup_dir, "backup_info.json") with open(info_file, 'w') as f: json.dump(backup_info, f, indent=2) print(f"Text Texture Generator: Created preset backup in {backup_dir}") return True else: print("Text Texture Generator: No preset files found to backup") return True # Not an error if there are no presets to backup except Exception as e: print(f"Text Texture Generator: Error creating preset backup: {str(e)}") return False