vse_toolbox/operators/addon.py

110 lines
3.1 KiB
Python

# SPDX-License-Identifier: GPL-2.0-or-later
import importlib
import os
import bpy
from bpy.types import Operator
import vse_toolbox
from vse_toolbox.bl_utils import (get_addon_prefs, get_scene_settings)
from vse_toolbox.file_utils import (read_file, )
class VSETB_OT_reload_addon(Operator):
bl_idname = "vse_toolbox.reload_addon"
bl_options = {"UNDO"}
bl_label = 'Reload VSE ToolBox Addon'
bl_description = 'Reload The VSE ToolBox Addon'
def execute(self, context):
print('Execute reload')
vse_toolbox.unregister()
importlib.reload(vse_toolbox)
vse_toolbox.register()
return {'FINISHED'}
class VSETB_OT_load_settings(Operator):
bl_idname = "vse_toolbox.load_settings"
bl_options = {"UNDO"}
bl_label = 'Load Settings'
bl_description = 'Load VSE ToolBox settings from config file'
def execute(self, context):
prefs = get_addon_prefs()
settings = get_scene_settings()
project = settings.active_project
if not prefs.config_path:
return
addon_config = read_file(os.path.expandvars(prefs.config_path))
addon_config['trackers'] = addon_config.get('trackers')
trackers = addon_config.pop('trackers')
addon_config['spreadsheet_export'] = addon_config.get('spreadsheet_export')
spreadsheet_export_config = addon_config.pop('spreadsheet_export')
project_name = addon_config.get('project_name')
if project_name:
settings.project_name = project_name
# Project Properties
for k, v in addon_config.items():
try:
setattr(project, k, v)
except Exception:
print(f'Could not set property {k} with value {v} to project {settings.project_name}')
export_cells = project.spreadsheet_export.cells
for k, v in spreadsheet_export_config.items():
if k == 'cells':
#project.spreadsheet.clear()
for i, cell_data in enumerate(v):
if not 'name' in cell_data:
print(f'cell_data {cell_data} need to have a attribute name')
continue
cell = export_cells.get(cell_data['name'])
if not cell:
print(f"cell {cell_data['name']} not in spreadsheet")
continue
export_cells.move(list(export_cells).index(cell), i)
for prop_name in ('export_name', 'enabled'):
if prop_name in cell_data:
setattr(cell, prop_name, cell_data[prop_name])
else:
try:
setattr(project.spreadsheet_export, k, v)
except Exception:
print(f'Could not set option {k} with value {v} to spreadsheet')
return {'FINISHED'}
classes = (
VSETB_OT_reload_addon,
VSETB_OT_load_settings
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)