116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
"""
|
|
Clear Cache Operator.
|
|
|
|
Provides operators to clear mask cache directories.
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import bpy
|
|
from bpy.types import Operator
|
|
from bpy.props import BoolProperty
|
|
|
|
from ..core.utils import get_cache_root, get_cache_dir_for_strip
|
|
|
|
|
|
class SEQUENCER_OT_clear_mask_cache(Operator):
|
|
"""Clear mask cache directories."""
|
|
|
|
bl_idname = "sequencer.clear_mask_cache"
|
|
bl_label = "Clear Mask Cache"
|
|
bl_description = "Delete cached mask images"
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
all_strips: BoolProperty(
|
|
name="All Strips",
|
|
description="Clear cache for all strips (otherwise only current strip)",
|
|
default=False,
|
|
)
|
|
|
|
def execute(self, context):
|
|
total_size = 0
|
|
cleared_count = 0
|
|
|
|
if self.all_strips:
|
|
# Clear all cache directories
|
|
cache_root = get_cache_root()
|
|
|
|
if os.path.exists(cache_root):
|
|
# Calculate size before deletion
|
|
for root, dirs, files in os.walk(cache_root):
|
|
for file in files:
|
|
file_path = os.path.join(root, file)
|
|
try:
|
|
total_size += os.path.getsize(file_path)
|
|
except OSError:
|
|
pass
|
|
|
|
# Delete cache directory
|
|
try:
|
|
shutil.rmtree(cache_root)
|
|
cleared_count = len(os.listdir(cache_root)) if os.path.exists(cache_root) else 0
|
|
self.report({'INFO'}, f"Cleared all cache ({self._format_size(total_size)})")
|
|
except Exception as e:
|
|
self.report({'ERROR'}, f"Failed to clear cache: {e}")
|
|
return {'CANCELLED'}
|
|
else:
|
|
self.report({'INFO'}, "No cache to clear")
|
|
return {'FINISHED'}
|
|
|
|
else:
|
|
# Clear cache for active strip only
|
|
seq_editor = context.scene.sequence_editor
|
|
if not seq_editor or not seq_editor.active_strip:
|
|
self.report({'WARNING'}, "No strip selected")
|
|
return {'CANCELLED'}
|
|
|
|
strip = seq_editor.active_strip
|
|
cache_dir = get_cache_dir_for_strip(strip.name)
|
|
|
|
if os.path.exists(cache_dir):
|
|
# Calculate size
|
|
for root, dirs, files in os.walk(cache_dir):
|
|
for file in files:
|
|
file_path = os.path.join(root, file)
|
|
try:
|
|
total_size += os.path.getsize(file_path)
|
|
except OSError:
|
|
pass
|
|
|
|
# Delete
|
|
try:
|
|
shutil.rmtree(cache_dir)
|
|
self.report({'INFO'}, f"Cleared cache for {strip.name} ({self._format_size(total_size)})")
|
|
except Exception as e:
|
|
self.report({'ERROR'}, f"Failed to clear cache: {e}")
|
|
return {'CANCELLED'}
|
|
else:
|
|
self.report({'INFO'}, f"No cache for {strip.name}")
|
|
return {'FINISHED'}
|
|
|
|
return {'FINISHED'}
|
|
|
|
def _format_size(self, size_bytes):
|
|
"""Format bytes to human-readable size."""
|
|
for unit in ['B', 'KB', 'MB', 'GB']:
|
|
if size_bytes < 1024.0:
|
|
return f"{size_bytes:.1f} {unit}"
|
|
size_bytes /= 1024.0
|
|
return f"{size_bytes:.1f} TB"
|
|
|
|
|
|
# Registration
|
|
classes = [
|
|
SEQUENCER_OT_clear_mask_cache,
|
|
]
|
|
|
|
|
|
def register():
|
|
for cls in classes:
|
|
bpy.utils.register_class(cls)
|
|
|
|
|
|
def unregister():
|
|
for cls in reversed(classes):
|
|
bpy.utils.unregister_class(cls)
|