# 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_scene_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 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) def load_tracker_prefs(): # FIXME Ça écrase les prefs à chaque quoi prefs = get_addon_prefs() tracker_prefs_file = os.getenv('TRACKER_PREFS') if not tracker_prefs_file: return tracker_datas = read_file(os.path.expandvars(tracker_prefs_file)) for tracker_data in tracker_datas: 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)) 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_scene_settings().tracker_name)) def draw(self, context): prefs = get_addon_prefs() settings = get_scene_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) row = box.row() row.operator("vse_toolbox.tracker_connect", text='Connect') classes=[ Trackers, VSETB_Prefs, ] def register(): for cls in classes: bpy.utils.register_class(cls) load_trackers() prefs = get_addon_prefs() tracker_name = os.getenv('TRACKER_NAME') if tracker_name is not None: prefs['tracker_name'] = os.path.expandvars(norm_str(tracker_name, format=str.upper)) load_tracker_prefs() def unregister(): for cls in reversed(classes + TRACKERS): bpy.utils.unregister_class(cls) TRACKERS.clear()