feat(blenvy): fixes & improvements to the settings handling

This commit is contained in:
kaosat.dev 2024-05-26 20:09:15 +02:00
parent b957f0573b
commit 2470736f81
5 changed files with 71 additions and 50 deletions

View File

@ -93,7 +93,7 @@ General issues:
- [x] addon-prefs => settings - [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] 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 - [x] remove hard coded path for standard gltf settings
- [ ] load settings on file load - [x] load settings on file load
- [x] auto_export - [x] auto_export
- [ ] components - [x] components
- [ ] add handling of errors when trying to load settings - [ ] add handling of errors when trying to load settings

View File

@ -1,16 +1,21 @@
import bpy import bpy
from bpy_types import (PropertyGroup) from bpy_types import (PropertyGroup)
from bpy.props import (EnumProperty, PointerProperty, StringProperty, BoolProperty, CollectionProperty, IntProperty) 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): # list of settings we do NOT want to save
print("save settings", settings, context, dict(settings)) settings_black_list = ['settings_save_enabled', 'dry_run']
bla = generate_complete_preferences_dict(settings, AutoExportSettings, [])
print("bla", bla) def save_settings(settings, context):
upsert_settings(settings.settings_save_path, dict(settings)) 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): class AutoExportSettings(PropertyGroup):
settings_save_path = ".blenvy_export_settings" # where to store data in bpy.texts 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( auto_export: BoolProperty(
name='Auto export', name='Auto export',
@ -104,7 +109,13 @@ class AutoExportSettings(PropertyGroup):
def load_settings(self): def load_settings(self):
settings = load_settings(self.settings_save_path) settings = load_settings(self.settings_save_path)
if settings is not None: if settings is not None:
for setting in settings: self.settings_save_enabled = False # we disable auto_saving of our settings
print("setting", setting, settings[setting]) try:
setattr(self, setting, settings[setting]) 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

View File

@ -2,17 +2,18 @@ import os
import bpy import bpy
from bpy_types import (PropertyGroup) from bpy_types import (PropertyGroup)
from bpy.props import (EnumProperty, PointerProperty, StringProperty, BoolProperty, CollectionProperty, IntProperty) 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 .propGroups.prop_groups import generate_propertyGroups_for_components
from .components.metadata import ensure_metadata_for_all_items from .components.metadata import ensure_metadata_for_all_items
# list of settings we do NOT want to save # 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): def save_settings(settings, context):
print("save settings", settings, context, dict(settings)) if settings.settings_save_enabled:
settings_dict = dict(settings) settings_dict = generate_complete_preferences_dict(settings, ComponentsSettings, [])
upsert_settings(settings.settings_save_path, {key: settings_dict[key] for key in settings_dict.keys() if key not in settings_black_list}) 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 # helper function to deal with timer
def toggle_watcher(self, context): def toggle_watcher(self, context):
@ -46,8 +47,7 @@ def watch_schema():
#bpy.ops.object.reload_registry() #bpy.ops.object.reload_registry()
# we need to add an additional delay as the file might not have loaded yet # 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) 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: except Exception as error:
pass pass
return component_settings.watcher_poll_frequency if component_settings.watcher_enabled else None return component_settings.watcher_poll_frequency if component_settings.watcher_enabled else None
@ -56,6 +56,7 @@ def watch_schema():
class ComponentsSettings(PropertyGroup): class ComponentsSettings(PropertyGroup):
settings_save_path = ".blenvy_components_settings" # where to store data in bpy.texts 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( schema_path: StringProperty(
name="schema path", name="schema path",
@ -112,11 +113,19 @@ class ComponentsSettings(PropertyGroup):
def load_settings(self): def load_settings(self):
settings = load_settings(self.settings_save_path) settings = load_settings(self.settings_save_path)
print("component settings", settings)
if settings is not None: if settings is not None:
for setting in settings: self.settings_save_enabled = False # we disable auto_saving of our settings
print("setting", setting, settings[setting]) try:
setattr(self, setting, settings[setting]) for setting in settings:
registry = bpy.context.components_registry print("setting", setting, settings[setting])
registry.load_schema() setattr(self, setting, settings[setting])
generate_propertyGroups_for_components() except:pass
ensure_metadata_for_all_items() 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

View File

@ -1,12 +1,24 @@
import os import os
import bpy import bpy
from bpy_types import (PropertyGroup) 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 .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.auto_export.settings as auto_export_settings
import blenvy.add_ons.bevy_components.settings as component_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): def update_scene_lists(blenvy, context):
blenvy.main_scene_names = [scene.name for scene in blenvy.main_scenes] # FIXME: unsure 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 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): def update_mode(blenvy, context):
upsert_settings(blenvy.settings_save_path, {"mode": blenvy.mode }) upsert_settings(blenvy.settings_save_path, {"mode": blenvy.mode })
def is_scene_already_in_use(self, scene):
def is_scene_ok(self, scene):
try: try:
print("SELF", self)
return scene.name not in self.main_scenes and scene.name not in self.library_scenes return scene.name not in self.main_scenes and scene.name not in self.library_scenes
except: except:
print("FAILURE")
return True return True
class BlenvyManager(PropertyGroup): class BlenvyManager(PropertyGroup):
settings_save_path = ".blenvy_common_settings" # where to store data in bpy.texts 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( mode: EnumProperty(
items=( items=(
@ -110,21 +118,19 @@ class BlenvyManager(PropertyGroup):
main_scenes: CollectionProperty(name="main scenes", type=SceneSelector) # type: ignore 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_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: 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_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 # sub ones
auto_export: PointerProperty(type=auto_export_settings.AutoExportSettings) # type: ignore auto_export: PointerProperty(type=auto_export_settings.AutoExportSettings) # type: ignore
components: PointerProperty(type=component_settings.ComponentsSettings) # 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 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_ok)# 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 @classmethod
def register(cls): def register(cls):
bpy.types.WindowManager.blenvy = PointerProperty(type=BlenvyManager) bpy.types.WindowManager.blenvy = PointerProperty(type=BlenvyManager)
@ -157,4 +163,4 @@ class BlenvyManager(PropertyGroup):
self.auto_export.load_settings() self.auto_export.load_settings()
# now load component settings # now load component settings
self.auto_export.load_settings() self.components.load_settings()

View File

@ -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 # 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 = {} complete_preferences = {}
defaults = {} defaults = {}
@ -36,21 +36,16 @@ def generate_complete_preferences_dict(settings, presets, ignore_list=[]):
for k in presets.__annotations__: for k in presets.__annotations__:
item = presets.__annotations__[k] item = presets.__annotations__[k]
default = item.keywords.get('default', None) default = item.keywords.get('default', None)
defaults[k] = default if default is not None:
#complete_preferences[k] = default defaults[k] = default
if preset_defaults:
complete_preferences[k] = default
# print("defaults", defaults) # print("defaults", defaults)
for key in list(settings.keys()): for key in list(settings.keys()):
if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults
complete_preferences[key] = getattr(settings, key, None) 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())) complete_preferences = dict(filter(filter_out, dict(complete_preferences).items()))
return complete_preferences return complete_preferences