# 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, ) from vse_toolbox.bl_utils import get_addon_prefs, get_settings 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 get_tracker_items(self, context): return [(norm_str(a.name, format=str.upper), a.name, "", i) for i, a in enumerate(TRACKERS)] 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 if obj is Tracker or obj.name in (a.name for a in TRACKERS): continue try: print(f'Register Plugin {name}') bpy.utils.register_class(obj) setattr(Trackers, norm_str(obj.name), PointerProperty(type=obj)) TRACKERS.append(obj) except Exception as e: print(f'Could not register library_type {name}') print(e) 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) @property def tracker(self): return getattr(self.trackers, norm_str(get_settings().tracker_name)) def draw(self, context): prefs = get_addon_prefs() settings = get_settings() 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: box.prop(settings, 'tracker_name', text='Tracker') self.tracker.draw_prefs(box) classes=[ Trackers, VSETB_Prefs, ] def register(): for cls in classes: bpy.utils.register_class(cls) load_trackers() prefs = get_addon_prefs() settings = get_settings() tracker_name = os.getenv('TRACKER_NAME') if tracker_name: prefs['tracker_name'] = os.path.expandvars(norm_str(tracker_name, format=str.upper)) tracker_prefs_file = os.getenv('TRACKER_PREFS') if tracker_prefs_file: tracker_prefs = read_file(os.path.expandvars(tracker_prefs_file)) tracker_data = tracker_prefs.get(norm_str(tracker_name), {}) for k, v in tracker_data.items(): setattr(prefs.tracker, k, os.path.expandvars(v)) def unregister(): for cls in reversed(classes + TRACKERS): bpy.utils.unregister_class(cls) TRACKERS.clear()