diff --git a/tools/blenvy/TODO.md b/tools/blenvy/TODO.md index a8b1591..c21db4c 100644 --- a/tools/blenvy/TODO.md +++ b/tools/blenvy/TODO.md @@ -93,7 +93,7 @@ General issues: - [x] addon-prefs => settings - [x] generate_gltf_export_preferences => should not use add-on prefs at all ? since we are not overriding gltf settings that way anymore ? - [x] remove hard coded path for standard gltf settings - - [ ] load settings on file load + - [x] load settings on file load - [x] auto_export - - [ ] components + - [x] components - [ ] add handling of errors when trying to load settings \ No newline at end of file diff --git a/tools/blenvy/add_ons/auto_export/settings.py b/tools/blenvy/add_ons/auto_export/settings.py index fd7a441..1927506 100644 --- a/tools/blenvy/add_ons/auto_export/settings.py +++ b/tools/blenvy/add_ons/auto_export/settings.py @@ -1,16 +1,21 @@ import bpy from bpy_types import (PropertyGroup) from bpy.props import (EnumProperty, PointerProperty, StringProperty, BoolProperty, CollectionProperty, IntProperty) -from blenvy.settings import upsert_settings, generate_complete_preferences_dict, load_settings +from blenvy.settings import load_settings, upsert_settings, generate_complete_preferences_dict -def save_settings(settings, context): - print("save settings", settings, context, dict(settings)) - bla = generate_complete_preferences_dict(settings, AutoExportSettings, []) - print("bla", bla) - upsert_settings(settings.settings_save_path, dict(settings)) +# list of settings we do NOT want to save +settings_black_list = ['settings_save_enabled', 'dry_run'] + +def save_settings(settings, context): + if settings.settings_save_enabled: + settings_dict = generate_complete_preferences_dict(settings, AutoExportSettings, []) + print("save settings", settings, context, settings_dict) + upsert_settings(settings.settings_save_path, {key: settings_dict[key] for key in settings_dict.keys() if key not in settings_black_list}) class AutoExportSettings(PropertyGroup): + settings_save_path = ".blenvy_export_settings" # where to store data in bpy.texts + settings_save_enabled = BoolProperty(name="settings save enabled", default=True) auto_export: BoolProperty( name='Auto export', @@ -104,7 +109,13 @@ class AutoExportSettings(PropertyGroup): def load_settings(self): settings = load_settings(self.settings_save_path) if settings is not None: - for setting in settings: - print("setting", setting, settings[setting]) - setattr(self, setting, settings[setting]) + self.settings_save_enabled = False # we disable auto_saving of our settings + try: + for setting in settings: + print("setting", setting, settings[setting]) + setattr(self, setting, settings[setting]) + except: pass + # TODO: remove setting if there was a failure + + self.settings_save_enabled = True diff --git a/tools/blenvy/add_ons/bevy_components/settings.py b/tools/blenvy/add_ons/bevy_components/settings.py index 280f4e9..4fed13b 100644 --- a/tools/blenvy/add_ons/bevy_components/settings.py +++ b/tools/blenvy/add_ons/bevy_components/settings.py @@ -2,17 +2,18 @@ import os import bpy from bpy_types import (PropertyGroup) from bpy.props import (EnumProperty, PointerProperty, StringProperty, BoolProperty, CollectionProperty, IntProperty) -from blenvy.settings import load_settings, upsert_settings +from blenvy.settings import load_settings, upsert_settings, generate_complete_preferences_dict from .propGroups.prop_groups import generate_propertyGroups_for_components from .components.metadata import ensure_metadata_for_all_items # list of settings we do NOT want to save -settings_black_list = ['watcher_active'] +settings_black_list = ['settings_save_enabled', 'watcher_active'] def save_settings(settings, context): - print("save settings", settings, context, dict(settings)) - settings_dict = dict(settings) - upsert_settings(settings.settings_save_path, {key: settings_dict[key] for key in settings_dict.keys() if key not in settings_black_list}) + if settings.settings_save_enabled: + settings_dict = generate_complete_preferences_dict(settings, ComponentsSettings, []) + print("save settings", settings, context,settings_dict) + upsert_settings(settings.settings_save_path, {key: settings_dict[key] for key in settings_dict.keys() if key not in settings_black_list}) # helper function to deal with timer def toggle_watcher(self, context): @@ -46,8 +47,7 @@ def watch_schema(): #bpy.ops.object.reload_registry() # we need to add an additional delay as the file might not have loaded yet bpy.app.timers.register(lambda: bpy.ops.object.reload_registry(), first_interval=1) - - component_settings.schemaTimeStamp = stamp + component_settings.schemaTimeStamp = stamp except Exception as error: pass return component_settings.watcher_poll_frequency if component_settings.watcher_enabled else None @@ -56,6 +56,7 @@ def watch_schema(): class ComponentsSettings(PropertyGroup): settings_save_path = ".blenvy_components_settings" # where to store data in bpy.texts + settings_save_enabled: BoolProperty(name="settings save enabled", default=True)# type: ignore schema_path: StringProperty( name="schema path", @@ -112,11 +113,19 @@ class ComponentsSettings(PropertyGroup): def load_settings(self): settings = load_settings(self.settings_save_path) + print("component settings", settings) if settings is not None: - for setting in settings: - print("setting", setting, settings[setting]) - setattr(self, setting, settings[setting]) - registry = bpy.context.components_registry - registry.load_schema() - generate_propertyGroups_for_components() - ensure_metadata_for_all_items() + self.settings_save_enabled = False # we disable auto_saving of our settings + try: + for setting in settings: + print("setting", setting, settings[setting]) + setattr(self, setting, settings[setting]) + except:pass + try: + registry = bpy.context.components_registry + registry.load_schema() + generate_propertyGroups_for_components() + ensure_metadata_for_all_items() + except:pass + + self.settings_save_enabled = True diff --git a/tools/blenvy/core/blenvy_manager.py b/tools/blenvy/core/blenvy_manager.py index e7ddb3e..9fab61a 100644 --- a/tools/blenvy/core/blenvy_manager.py +++ b/tools/blenvy/core/blenvy_manager.py @@ -1,12 +1,24 @@ import os import bpy from bpy_types import (PropertyGroup) -from bpy.props import (EnumProperty, PointerProperty, StringProperty, CollectionProperty, IntProperty) +from bpy.props import (BoolProperty, EnumProperty, PointerProperty, StringProperty, CollectionProperty, IntProperty) from .scene_helpers import SceneSelector -from ..settings import upsert_settings, load_settings +from ..settings import upsert_settings, load_settings, generate_complete_preferences_dict import blenvy.add_ons.auto_export.settings as auto_export_settings import blenvy.add_ons.bevy_components.settings as component_settings + +# list of settings we do NOT want to save +settings_black_list = ['settings_save_enabled', 'main_scene_selector', 'main_scenes', 'main_scenes_index', 'library_scene_selector', 'library_scenes', 'library_scenes_index', + #'project_root_path_full', 'assets_path_full', '' + ] + +def save_settings(settings, context): + if settings.settings_save_enabled: + settings_dict = generate_complete_preferences_dict(settings, BlenvyManager, []) + print("save settings", settings, context, settings_dict) + # upsert_settings(settings.settings_save_path, {key: settings_dict[key] for key in settings_dict.keys() if key not in settings_black_list}) + def update_scene_lists(blenvy, context): blenvy.main_scene_names = [scene.name for scene in blenvy.main_scenes] # FIXME: unsure blenvy.library_scene_names = [scene.name for scene in blenvy.library_scenes] # FIXME: unsure @@ -21,20 +33,16 @@ def update_asset_folders(blenvy, context): def update_mode(blenvy, context): upsert_settings(blenvy.settings_save_path, {"mode": blenvy.mode }) - -def is_scene_ok(self, scene): +def is_scene_already_in_use(self, scene): try: - - print("SELF", self) - return scene.name not in self.main_scenes and scene.name not in self.library_scenes except: - print("FAILURE") return True class BlenvyManager(PropertyGroup): settings_save_path = ".blenvy_common_settings" # where to store data in bpy.texts + settings_save_enabled = BoolProperty(name="settings save enabled", default=True) mode: EnumProperty( items=( @@ -110,21 +118,19 @@ class BlenvyManager(PropertyGroup): main_scenes: CollectionProperty(name="main scenes", type=SceneSelector) # type: ignore main_scenes_index: IntProperty(name = "Index for main scenes list", default = 0, update=update_scene_lists) # type: ignore - main_scene_names = [] # FIXME: unsure + #main_scene_names = [] # FIXME: unsure library_scenes: CollectionProperty(name="library scenes", type=SceneSelector) # type: ignore library_scenes_index: IntProperty(name = "Index for library scenes list", default = 0, update=update_scene_lists) # type: ignore - library_scene_names = [] # FIXME: unsure + #library_scene_names = [] # FIXME: unsure # sub ones auto_export: PointerProperty(type=auto_export_settings.AutoExportSettings) # type: ignore components: PointerProperty(type=component_settings.ComponentsSettings) # type: ignore - main_scene_selector: PointerProperty(type=bpy.types.Scene, name="main scene", description="main_scene_picker", poll=is_scene_ok)# type: ignore - library_scene_selector: PointerProperty(type=bpy.types.Scene, name="library scene", description="library_scene_picker", poll=is_scene_ok)# type: ignore + main_scene_selector: PointerProperty(type=bpy.types.Scene, name="main scene", description="main_scene_picker", poll=is_scene_already_in_use)# type: ignore + library_scene_selector: PointerProperty(type=bpy.types.Scene, name="library scene", description="library_scene_picker", poll=is_scene_already_in_use)# type: ignore - - @classmethod def register(cls): bpy.types.WindowManager.blenvy = PointerProperty(type=BlenvyManager) @@ -157,4 +163,4 @@ class BlenvyManager(PropertyGroup): self.auto_export.load_settings() # now load component settings - self.auto_export.load_settings() + self.components.load_settings() diff --git a/tools/blenvy/settings.py b/tools/blenvy/settings.py index f07301d..00f5c23 100644 --- a/tools/blenvy/settings.py +++ b/tools/blenvy/settings.py @@ -23,7 +23,7 @@ def load_settings(name): # given the input (actual) settings, filters out any invalid/useless params & params that are equal to defaults -def generate_complete_preferences_dict(settings, presets, ignore_list=[]): +def generate_complete_preferences_dict(settings, presets, ignore_list=[], preset_defaults=True): complete_preferences = {} defaults = {} @@ -36,21 +36,16 @@ def generate_complete_preferences_dict(settings, presets, ignore_list=[]): for k in presets.__annotations__: item = presets.__annotations__[k] default = item.keywords.get('default', None) - defaults[k] = default - #complete_preferences[k] = default + if default is not None: + defaults[k] = default + if preset_defaults: + complete_preferences[k] = default # print("defaults", defaults) for key in list(settings.keys()): if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults complete_preferences[key] = getattr(settings, key, None) - - print("complete_preferences", complete_preferences) - - """for key in list(settings.keys()): - if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults - complete_preferences[key] = settings[key]""" - complete_preferences = dict(filter(filter_out, dict(complete_preferences).items())) return complete_preferences