lazy loading profiles

This commit is contained in:
Nicolas Predella 2026-05-16 16:28:53 +03:00
parent e1a3e0c746
commit 340f7a0612
4 changed files with 33 additions and 21 deletions

View file

@ -82,19 +82,28 @@ class SLICENODE_PrusaslicerConf(SliceNode):
cls._cache = LocalCache() cls._cache = LocalCache()
return cls._cache return cls._cache
def ensure_profiles_loaded(self):
cache = self.get_cache()
if not cache.profiles:
# Use a path relative to the addon directory
import os
addon_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
profiles_path = os.path.join(addon_dir, "profiles", "prusaslicer")
if os.path.isdir(profiles_path):
cache.load([profiles_path])
if self.printer_id != 'NONE':
cache.evaluate_compatibility({self.printer_id}, set(cache.vendors))
def update_profiles(self, context): def update_profiles(self, context):
cache = self.get_cache() cache = self.get_cache()
if not cache.profiles and self.config_path:
abs_path = bpy.path.abspath(self.config_path)
if os.path.isdir(abs_path):
cache.load([abs_path])
if self.printer_id != 'NONE': if self.printer_id != 'NONE':
cache.evaluate_compatibility({self.printer_id}, set(cache.vendors)) cache.evaluate_compatibility({self.printer_id}, set(cache.vendors))
notify_tree_changed(self, context) notify_tree_changed(self, context)
def get_printers(self, context): def get_printers(self, context):
self.ensure_profiles_loaded()
cache = self.get_cache() cache = self.get_cache()
items = [('NONE', "None", "No printer selected", 'NONE', 0)] items = [('NONE', "None", "No printer selected", 'NONE', 0)]
for i, (k, p) in enumerate(cache.printer_profiles.items(), 1): for i, (k, p) in enumerate(cache.printer_profiles.items(), 1):
@ -102,6 +111,7 @@ class SLICENODE_PrusaslicerConf(SliceNode):
return items return items
def get_filaments(self, context): def get_filaments(self, context):
self.ensure_profiles_loaded()
cache = self.get_cache() cache = self.get_cache()
items = [('NONE', "None", "No filament selected", 'NONE', 0)] items = [('NONE', "None", "No filament selected", 'NONE', 0)]
@ -122,6 +132,7 @@ class SLICENODE_PrusaslicerConf(SliceNode):
return items return items
def get_prints(self, context): def get_prints(self, context):
self.ensure_profiles_loaded()
cache = self.get_cache() cache = self.get_cache()
items = [('NONE', "None", "No print selected", 'NONE', 0)] items = [('NONE', "None", "No print selected", 'NONE', 0)]
@ -166,12 +177,6 @@ class SLICENODE_PrusaslicerConf(SliceNode):
def get_print_enum(self): return self._get_enum_idx(self.get_prints(None), self.print_id) def get_print_enum(self): return self._get_enum_idx(self.get_prints(None), self.print_id)
def set_print_enum(self, value): self._set_enum_idx(self.get_prints(None), value, 'print_id') def set_print_enum(self, value): self._set_enum_idx(self.get_prints(None), value, 'print_id')
config_path: NfStringProperty(
name="Profiles",
subtype='DIR_PATH',
update=update_profiles
) # pyright: ignore[reportInvalidTypeForm]
printer_enum: NfEnumProperty( printer_enum: NfEnumProperty(
name="Printer", name="Printer",
items=get_printers, items=get_printers,
@ -198,13 +203,14 @@ class SLICENODE_PrusaslicerConf(SliceNode):
def draw_buttons(self, context, layout): def draw_buttons(self, context, layout):
assert layout assert layout
layout.prop(self, "config_path") self.ensure_profiles_loaded()
layout.prop(self, "printer_enum") layout.prop(self, "printer_enum")
if self.printer_id != 'NONE': if self.printer_id != 'NONE':
layout.prop(self, "filament_enum") layout.prop(self, "filament_enum")
layout.prop(self, "print_enum") layout.prop(self, "print_enum")
def evaluate_output(self, socket_name: str, **_kwargs) -> Any: def evaluate_output(self, socket_name: str, **_kwargs) -> Any:
self.ensure_profiles_loaded()
if self.printer_id == 'NONE' or self.filament_id == 'NONE' or self.print_id == 'NONE': if self.printer_id == 'NONE' or self.filament_id == 'NONE' or self.print_id == 'NONE':
return None return None
@ -910,10 +916,10 @@ class SLICENODE_PrinterList(SliceNode):
def _refresh(self): def _refresh(self):
import json import json
if not self.config_path: if not self.printers_json_path:
return return
abs_path = bpy.path.abspath(self.config_path) abs_path = bpy.path.abspath(self.printers_json_path)
if not os.path.exists(abs_path): if not os.path.exists(abs_path):
return return
@ -950,13 +956,13 @@ class SLICENODE_PrinterList(SliceNode):
def update_config(self, context): def update_config(self, context):
self._refresh() self._refresh()
config_path: NfStringProperty(name="Config File", default="", subtype='FILE_PATH', update=update_config) # pyright: ignore[reportInvalidTypeForm] printers_json_path: NfStringProperty(name="Config File", default="", subtype='FILE_PATH', update=update_config) # pyright: ignore[reportInvalidTypeForm]
def init(self, context): def init(self, context):
pass pass
def draw_buttons(self, context, layout): def draw_buttons(self, context, layout):
layout.prop(self, "config_path") # pyright: ignore[reportOptionalMemberAccess] layout.prop(self, "printers_json_path") # pyright: ignore[reportOptionalMemberAccess]
def evaluate_output(self, socket_name: str, **_kwargs) -> Any: def evaluate_output(self, socket_name: str, **_kwargs) -> Any:
self._refresh() self._refresh()

View file

@ -41,11 +41,8 @@ def get_referenced_objects(node, visited=None):
_is_updating = False _is_updating = False
_tree_changed = False _tree_changed = False
@persistent def tree_update_handler(scene, depsgraph):
def depsgraph_update_handler(scene, depsgraph): global _tree_changed
global _is_updating, _tree_changed
if _is_updating:
return
# Names of objects that have changed # Names of objects that have changed
updated_object_names = {update.id.name for update in depsgraph.updates if isinstance(update.id, bpy.types.Object)} updated_object_names = {update.id.name for update in depsgraph.updates if isinstance(update.id, bpy.types.Object)}
@ -92,6 +89,14 @@ def depsgraph_update_handler(scene, depsgraph):
finally: finally:
_is_updating = False _is_updating = False
@persistent
def depsgraph_update_handler(scene, depsgraph):
global _is_updating, _tree_changed
if _is_updating: return
tree_update_handler(scene, depsgraph)
@register_class @register_class
class SLICETREE_NodeTree(NodeTree): class SLICETREE_NodeTree(NodeTree):
bl_idname = "SLICETREE_NodeTree" bl_idname = "SLICETREE_NodeTree"

View file

@ -0,0 +1 @@
/home/nicolas/Workspace/Design Projects/3d Print Library/Configs