vse_toolbox/preferences.py

158 lines
4.1 KiB
Python
Raw Normal View History

2023-03-14 13:38:04 +01:00
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
import inspect
import os
from bpy.props import (
BoolProperty,
EnumProperty,
FloatProperty,
IntProperty,
PointerProperty,
StringProperty,
)
from bpy.types import (
AddonPreferences,
PropertyGroup,
)
2023-03-21 18:33:29 +01:00
from vse_toolbox.bl_utils import get_addon_prefs, get_scene_settings
2023-03-14 13:38:04 +01:00
from vse_toolbox.constants import (
TRACKERS,
TRACKERS_DIR,
)
from vse_toolbox.file_utils import (
import_module_from_path,
norm_str,
read_file,
)
from vse_toolbox.resources.trackers.kitsu import Kitsu
def load_trackers():
from vse_toolbox.resources.trackers.tracker import Tracker
TRACKERS.clear()
tracker_files = list(TRACKERS_DIR.glob('*.py'))
for tracker_file in tracker_files:
if tracker_file.stem.startswith('_'):
continue
mod = import_module_from_path(tracker_file)
for name, obj in inspect.getmembers(mod):
if not inspect.isclass(obj):
continue
if not Tracker in obj.__mro__:
continue
2023-04-20 00:12:39 +02:00
if obj is Tracker or name in (a.__name__ for a in TRACKERS):
2023-03-14 13:38:04 +01:00
continue
try:
2023-04-20 00:12:39 +02:00
print(f'Register Tracker {name}')
2023-03-14 13:38:04 +01:00
bpy.utils.register_class(obj)
2023-04-20 00:12:39 +02:00
#obj.register()
setattr(Trackers, norm_str(name), PointerProperty(type=obj))
2023-03-14 13:38:04 +01:00
TRACKERS.append(obj)
except Exception as e:
2023-04-20 00:12:39 +02:00
print(f'Could not register Tracker {name}')
2023-03-14 13:38:04 +01:00
print(e)
2023-04-24 18:51:26 +02:00
def load_prefs():
2023-03-21 18:33:29 +01:00
prefs = get_addon_prefs()
2023-04-24 18:51:26 +02:00
prefs_config_file = prefs.config_path
2023-03-21 18:33:29 +01:00
2023-04-24 18:51:26 +02:00
if not prefs_config_file:
2023-03-21 18:33:29 +01:00
return
2023-04-24 18:51:26 +02:00
prefs_datas = read_file(os.path.expandvars(prefs_config_file))
2023-03-21 18:33:29 +01:00
2023-04-24 18:51:26 +02:00
for tracker_data in prefs_datas['trackers']:
2023-03-21 18:33:29 +01:00
tracker_name = norm_str(tracker_data['name'])
if not hasattr(prefs.trackers, tracker_name):
continue
tracker_pref = getattr(prefs.trackers, tracker_name)
if not tracker_pref:
continue
for k, v in tracker_data.items():
if k in ('name',):
continue
setattr(tracker_pref, k, os.path.expandvars(v))
2023-04-24 18:51:26 +02:00
prefs['tracker_name'] = prefs_datas['tracker_name']
2023-03-14 13:38:04 +01:00
class Trackers(PropertyGroup):
def __iter__(self):
return (getattr(self, p) for p in self.bl_rna.properties.keys() if p not in ('rna_type', 'name'))
class VSETB_Prefs(AddonPreferences):
bl_idname = __package__
trackers : PointerProperty(type=Trackers)
expand_settings: BoolProperty(default=False)
2023-04-24 18:51:26 +02:00
config_path : StringProperty(subtype='FILE_PATH')
2023-03-14 13:38:04 +01:00
@property
def tracker(self):
2023-03-21 18:33:29 +01:00
return getattr(self.trackers, norm_str(get_scene_settings().tracker_name))
2023-03-14 13:38:04 +01:00
def draw(self, context):
prefs = get_addon_prefs()
2023-03-21 18:33:29 +01:00
settings = get_scene_settings()
2023-03-14 13:38:04 +01:00
layout = self.layout
col = layout.column(align=True)
box = col.box()
row = box.row(align=True)
icon = "DISCLOSURE_TRI_DOWN" if self.expand_settings else "DISCLOSURE_TRI_RIGHT"
row.prop(self, 'expand_settings', icon=icon, emboss=False, text='')
row.label(icon='PREFERENCES')
row.label(text='Settings')
subrow = row.row()
subrow.alignment = 'RIGHT'
subrow.operator("vse_toolbox.reload_addon", text='Reload Addon')
if self.expand_settings:
2023-04-24 18:51:26 +02:00
box.prop(self, 'config_path', text='Config Path')
2023-03-14 13:38:04 +01:00
box.prop(settings, 'tracker_name', text='Tracker')
self.tracker.draw_prefs(box)
2023-04-24 18:51:26 +02:00
#row = box.row()
box.operator("vse_toolbox.tracker_connect", text='Connect')
2023-03-14 13:38:04 +01:00
2023-04-24 18:51:26 +02:00
classes = [
2023-03-14 13:38:04 +01:00
Trackers,
VSETB_Prefs,
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
load_trackers()
prefs = get_addon_prefs()
2023-03-21 18:33:29 +01:00
2023-04-24 18:51:26 +02:00
config_path = os.getenv('VSE_TOOLBOX_CONFIG')
if config_path:
prefs['config_path'] = os.path.expandvars(config_path)
load_prefs()
2023-03-14 13:38:04 +01:00
def unregister():
for cls in reversed(classes + TRACKERS):
bpy.utils.unregister_class(cls)
TRACKERS.clear()