vse_toolbox/operators/addon.py

149 lines
4.8 KiB
Python
Raw Normal View History

2023-05-02 18:38:16 +02:00
# 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)
2023-05-04 18:45:17 +02:00
from vse_toolbox.file_utils import (read_file, norm_str)
2023-05-02 18:38:16 +02:00
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:
2023-05-03 14:40:07 +02:00
return {'FINISHED'}
2023-05-02 18:38:16 +02:00
addon_config = read_file(os.path.expandvars(prefs.config_path))
addon_config['trackers'] = addon_config.get('trackers')
trackers = addon_config.pop('trackers')
2023-05-04 18:45:17 +02:00
addon_config['spreadsheet_export'] = addon_config.get('spreadsheet_export', {})
2023-05-02 20:46:45 +02:00
spreadsheet_export_config = addon_config.pop('spreadsheet_export')
2023-05-02 18:38:16 +02:00
2023-05-04 18:45:17 +02:00
addon_config['spreadsheet_import'] = addon_config.get('spreadsheet_import', {})
spreadsheet_import_config = addon_config.pop('spreadsheet_import')
2023-05-02 18:38:16 +02:00
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}')
2023-05-02 20:46:45 +02:00
export_cells = project.spreadsheet_export.cells
for k, v in spreadsheet_export_config.items():
if k == 'cells':
#project.spreadsheet.clear()
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
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
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
cell = export_cells.get(cell_data['name'])
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
if not cell:
print(f"cell {cell_data['name']} not in spreadsheet")
continue
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
export_cells.move(list(export_cells).index(cell), i)
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
for prop_name in ('export_name', 'enabled'):
if prop_name in cell_data:
setattr(cell, prop_name, cell_data[prop_name])
2023-05-02 18:38:16 +02:00
2023-05-02 20:46:45 +02:00
else:
2023-05-02 18:38:16 +02:00
try:
2023-05-02 20:46:45 +02:00
setattr(project.spreadsheet_export, k, v)
2023-05-02 18:38:16 +02:00
except Exception:
print(f'Could not set option {k} with value {v} to spreadsheet')
2023-05-04 18:45:17 +02:00
import_cells = project.spreadsheet_import.cells
if spreadsheet_import_config:
import_cells.clear()
2023-05-05 09:46:50 +02:00
cell_types = project.get_cell_types()
2023-05-04 18:45:17 +02:00
for k, v in spreadsheet_import_config.items():
if k == 'cells':
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 = import_cells.add()
cell.name = cell_data['name']
norm_import_name = norm_str(cell_data.get('import_name', cell_data['name']))
2023-05-05 09:46:50 +02:00
import_name = next((n for n, t in cell_types.items() if norm_str(n) == norm_import_name), None)
2023-05-04 18:45:17 +02:00
if import_name is None:
2023-05-05 09:46:50 +02:00
print(f'import_name {norm_import_name} not in {list(cell_types.values())}')
2023-05-04 18:45:17 +02:00
else:
cell.import_name = import_name
2023-05-05 09:46:50 +02:00
cell.type = cell_types[import_name]
2023-05-04 18:45:17 +02:00
if 'enabled' in cell_data:
try:
cell.enabled = cell_data['enabled']
except TypeError as e:
print(e)
else:
try:
setattr(project.spreadsheet_import, k, v)
except Exception:
print(f'Could not set option {k} with value {v} to spreadsheet')
2023-05-02 18:38:16 +02:00
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)