diff --git a/__init__.py b/__init__.py index 7f673af030..9eafa7a675 100755 --- a/__init__.py +++ b/__init__.py @@ -118,9 +118,9 @@ def import_sverchok(): @profiling_startup("reg_stats") def register(): - from sverchok.core import sv_registration_utils + from sverchok.core import sv_register_modules import sverchok - sv_registration_utils.register_all(imported_modules + node_list) + sv_register_modules(imported_modules + node_list) sverchok.core.init_bookkeeping(__name__) if reload_event: @@ -130,10 +130,10 @@ def register(): def unregister(): import sverchok - from sverchok.core import sv_registration_utils + from sverchok.core import sv_unregister_modules sverchok.utils.clear_node_classes() - sv_registration_utils.unregister_all(imported_modules) - sv_registration_utils.unregister_all(node_list) + sv_unregister_modules(imported_modules) + sv_unregister_modules(node_list) # EOF diff --git a/core/__init__.py b/core/__init__.py index 1196755349..515174dc4e 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1,6 +1,5 @@ import importlib -import sverchok -from sverchok.core.socket_data import clear_all_socket_cache + root_modules = [ "node_tree", "data_structure", "core", @@ -26,7 +25,6 @@ def sv_register_modules(modules): def sv_unregister_modules(modules): - clear_all_socket_cache() for m in reversed(modules): if hasattr(m, "unregister"): try: @@ -36,23 +34,6 @@ def sv_unregister_modules(modules): print(str(e)) -def sv_registration_utils(): - """ this is a faux module for syntactic sugar on the imports in __init__ """ - pass - - -sv_registration_utils.register_all = sv_register_modules -sv_registration_utils.unregister_all = sv_unregister_modules - - -def reload_all(imported_modules, node_list): - # reload base modules - _ = [importlib.reload(im) for im in imported_modules] - - # reload nodes - _ = [importlib.reload(node) for node in node_list] - - def make_node_list(nodes): node_list = [] base_name = "sverchok.nodes" @@ -70,13 +51,21 @@ def import_modules(modules, base, im_list): def handle_reload_event(nodes, imported_modules): node_list = make_node_list(nodes) - reload_all(imported_modules, node_list) + + # reload base modules + for module in imported_modules: + importlib.reload(module) + + # reload nodes + for node in node_list: + importlib.reload(node) return node_list -def import_settings(imported_modules, sv_dir_name): - # "settings" treated separately in case the sverchok dir isn't named "sverchok" - settings = importlib.import_module(".settings", sv_dir_name) +def import_settings(imported_modules): + """Useful have the function to be sure that we do not import half of + Sverchok modules wia settings""" + settings = importlib.import_module(".settings", "sverchok") imported_modules.append(settings) @@ -95,7 +84,7 @@ def init_architecture(sv_name, utils_modules, ui_modules): (ui_modules, "sverchok.ui") ] print('sv: import settings') - import_settings(imported_modules, sv_name) + import_settings(imported_modules) print('sv: import all modules') import_all_modules(imported_modules, mods_bases) return imported_modules @@ -104,17 +93,18 @@ def init_architecture(sv_name, utils_modules, ui_modules): def init_bookkeeping(sv_name): from sverchok.utils import ascii_print, auto_gather_node_classes + from sverchok import data_structure, nodes - sverchok.data_structure.SVERCHOK_NAME = sv_name + data_structure.SVERCHOK_NAME = sv_name ascii_print.show_welcome() - auto_gather_node_classes() + auto_gather_node_classes(nodes) -activation_message = """\n -** Sverchok needs a couple of seconds to become activated when you enable it for the first time. ** -** Please restart Blender and enable it by pressing the tick box only once -- be patient! ** -""" def interupted_activation_detected(): + activation_message = """\n + ** Sverchok needs a couple of seconds to become activated when you enable it for the first time. ** + ** Please restart Blender and enable it by pressing the tick box only once -- be patient! ** + """ return NameError(activation_message) diff --git a/core/handlers.py b/core/handlers.py index 6871968795..c89495ff40 100644 --- a/core/handlers.py +++ b/core/handlers.py @@ -12,6 +12,7 @@ from sverchok.utils import app_handler_ops from sverchok.utils.handle_blender_data import BlTrees from sverchok.utils.logging import catch_log_error, debug +import sverchok.settings as settings _state = {'frame': None} @@ -216,9 +217,11 @@ def call_user_functions_on_post_load_event(scene): function() +settings.set_frame_change = set_frame_change + + def register(): app_handler_ops(append=handler_dict) - data_structure.setup_init() update_frame_change_mode() bpy.app.handlers.load_post.append(call_user_functions_on_post_load_event) diff --git a/core/socket_data.py b/core/socket_data.py index 7271d3844c..e0ed72a7a6 100644 --- a/core/socket_data.py +++ b/core/socket_data.py @@ -215,3 +215,7 @@ def clear_all_socket_cache(): Reset socket cache for all node-trees. """ socket_data_cache.clear() + + +def unregister(): + clear_all_socket_cache() diff --git a/data_structure.py b/data_structure.py index 4ea974c6ff..2167a2de31 100755 --- a/data_structure.py +++ b/data_structure.py @@ -40,7 +40,6 @@ import numpy as np -DEBUG_MODE = False RELOAD_EVENT = False # this is set correctly later. @@ -1203,35 +1202,6 @@ def extend_blender_class(cls): return cls -##################################################### -############### debug settings magic ################ -##################################################### - - -def sverchok_debug(mode): - """ - set debug mode to mode - """ - global DEBUG_MODE - DEBUG_MODE = mode - return DEBUG_MODE - - -def setup_init(): - """ - setup variables needed for sverchok to function - """ - global DEBUG_MODE - global SVERCHOK_NAME - import sverchok - SVERCHOK_NAME = sverchok.__name__ - addon = bpy.context.preferences.addons.get(SVERCHOK_NAME) - if addon: - DEBUG_MODE = addon.preferences.show_debug - else: - print("Setup of preferences failed") - - ##################################################### ############### update system magic! ################ ##################################################### diff --git a/dependencies.py b/dependencies.py index 9cc9e3b5fd..4bb38c5843 100644 --- a/dependencies.py +++ b/dependencies.py @@ -25,10 +25,12 @@ """ import logging +import sverchok.settings as settings # Logging setup # we have to set up logging here separately, because dependencies.py is loaded before settings.py, # so we can't use common settings. +# todo it's not True any more - can be refactored logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -189,3 +191,9 @@ def draw_message(box, package, dependencies=None): info("sv: Dependencies available: %s.", ", ".join(good_names)) else: info("sv: No dependencies are available.") + +settings.pip = pip +settings.sv_dependencies = sv_dependencies +settings.ensurepip = ensurepip +settings.draw_message = draw_message +settings.get_icon = get_icon diff --git a/settings.py b/settings.py index 6d407a9e2e..6ecc98fca5 100644 --- a/settings.py +++ b/settings.py @@ -5,21 +5,25 @@ import bpy from bpy.types import AddonPreferences from bpy.props import BoolProperty, FloatVectorProperty, EnumProperty, IntProperty, FloatProperty, StringProperty -from sverchok.dependencies import sv_dependencies, pip, ensurepip, draw_message, get_icon -from sverchok import data_structure -from sverchok.core import tasks # don't remove this should fix #4229 (temp solution) -from sverchok.core import handlers -from sverchok.utils import logging -from sverchok.utils.sv_gist_tools import TOKEN_HELP_URL -from sverchok.utils.sv_extra_addons import draw_extra_addons -from sverchok.ui import color_def + from sverchok.ui.utils import message_on_layout +"""Don't import other Sverchok modules here""" + if bpy.app.version >= (2, 91, 0): PYPATH = sys.executable else: PYPATH = bpy.app.binary_path_python + +# names from other modules +sv_dependencies, pip, ensurepip, draw_message, get_icon = [None] * 5 +set_frame_change = None +info, setLevel = [None] * 2 +draw_extra_addons = None +apply_theme, rebuild_color_cache, color_callback = [None] * 3 + + def get_params(prop_names_and_fallbacks, direct=False): """ This function returns an object which you can use the . op on. @@ -74,18 +78,17 @@ def get_param(prop_name, fallback): def apply_theme_if_necessary(): if get_param("apply_theme_on_open", False): - color_def.apply_theme() + apply_theme() print("applied theme.") # getDpiFactor and getDpi are lifted from Animation Nodes :) -def get_dpi_factor(): - return get_dpi() / 72 +def get_dpi_factor(factor=0.014): + system_preferences = bpy.context.preferences.system + retina_factor = getattr(system_preferences, "pixel_size", 1) + dpi = system_preferences.dpi * retina_factor + return dpi * factor -def get_dpi(): - systemPreferences = bpy.context.preferences.system - retinaFactor = getattr(systemPreferences, "pixel_size", 1) - return systemPreferences.dpi * retinaFactor class SvExPipInstall(bpy.types.Operator): """Install the package by calling pip install""" @@ -181,21 +184,19 @@ def execute(self, context): return {'FINISHED'} class SverchokPreferences(AddonPreferences): - - bl_idname = __package__ - - def update_debug_mode(self, context): - data_structure.DEBUG_MODE = self.show_debug + import sverchok + bl_idname = sverchok.__name__ def set_frame_change(self, context): - handlers.set_frame_change(self.frame_change_mode) + set_frame_change(self.frame_change_mode) def update_theme(self, context): - color_def.rebuild_color_cache() + rebuild_color_cache() if self.auto_apply_theme: - color_def.apply_theme() + apply_theme() - tab_modes = data_structure.enum_item_4(["General", "Node Defaults", "Extra Nodes", "Theme"]) + tab_modes = [(n.replace(' ', '_'), n, '', i) for i, n in + enumerate(["General", "Node Defaults", "Extra Nodes", "Theme"])] selected_tab: bpy.props.EnumProperty( items=tab_modes, @@ -203,13 +204,6 @@ def update_theme(self, context): default="General" ) - # debugish... - show_debug: BoolProperty( - name="Debug mode", # todo to remove, there is logging level for this - description="Deprecated", - default=False, subtype='NONE', - update=update_debug_mode) - no_data_color: FloatVectorProperty( name="No data", description='When a node can not get data', size=3, min=0.0, max=1.0, @@ -239,12 +233,16 @@ def update_theme(self, context): default = False) # theme settings + themes = [("default_theme", "Default", "Default"), + ("nipon_blossom", "Nipon Blossom", "Nipon Blossom"), + ("grey", "Grey", "Grey"), + ("darker", "Darker", "Darker")] sv_theme: EnumProperty( - items=color_def.themes, + items=themes, name="Theme preset", description="Select a theme preset", - update=color_def.color_callback, + update=color_callback, default="default_theme") auto_apply_theme: BoolProperty( @@ -360,8 +358,8 @@ def set_nodeview_render_params(self, context): # Logging settings def update_log_level(self, context): - logging.info("Setting log level to %s", self.log_level) - logging.setLevel(self.log_level) + info("Setting log level to %s", self.log_level) + setLevel(self.log_level) log_levels = [ ("DEBUG", "Debug", "Debug output", 0), @@ -416,7 +414,7 @@ def general_tab(self, layout): box.label(text="Export to Gist") box.prop(self, "github_token") box.label(text="To export node trees to gists, you have to create a GitHub API access token.") - box.label(text="For more information, visit " + TOKEN_HELP_URL) + box.label(text="For more information, visit " + "https://github.com/nortikin/sverchok/wiki/Set-up-GitHub-account-for-exporting-node-trees-from-Sverchok") box.operator("node.sv_github_api_token_help", text="Visit documentation page") col2 = col_split.split().column() @@ -426,7 +424,6 @@ def general_tab(self, layout): col2box = col2.box() col2box.label(text="Debug:") - col2box.prop(self, "show_debug") col2box.prop(self, "developer_mode") log_box = col2.box() diff --git a/ui/color_def.py b/ui/color_def.py index d5f465cea7..2939f34c2b 100644 --- a/ui/color_def.py +++ b/ui/color_def.py @@ -20,6 +20,7 @@ import bpy from bpy.props import StringProperty +import sverchok.settings as settings from sverchok.utils.logging import debug import sverchok from sverchok.utils.handle_blender_data import BlTrees @@ -27,11 +28,6 @@ colors_cache = {} -themes = [("default_theme", "Default", "Default"), - ("nipon_blossom", "Nipon Blossom", "Nipon Blossom"), - ("grey", "Grey", "Grey"), - ("darker", "Darker", "Darker")] - default_theme = { "Viz": (1, 0.589, 0.214), @@ -169,3 +165,8 @@ def register(): def unregister(): bpy.utils.unregister_class(SverchokApplyTheme) + + +settings.apply_theme = apply_theme +settings.rebuild_color_cache = rebuild_color_cache +settings.color_callback = color_callback diff --git a/utils/__init__.py b/utils/__init__.py index a3e361f0be..060c97a5f2 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,6 +1,6 @@ # GPL3 import bpy -import sverchok +import inspect node_classes = {} @@ -59,17 +59,13 @@ def unregister(): return register, unregister -def auto_gather_node_classes(start_module = None): + +def auto_gather_node_classes(start_module): """ this produces a dict with mapping from bl_idname to class reference at runtime f.ex node_classes = {SvBMeshViewerMk2: , .... } """ - - import inspect - if start_module is None: - start_module = sverchok.nodes - node_cats = inspect.getmembers(start_module, inspect.ismodule) for catname, nodecat in node_cats: node_files = inspect.getmembers(nodecat, inspect.ismodule) diff --git a/utils/logging.py b/utils/logging.py index 345b9f38dc..67db7bfe4f 100644 --- a/utils/logging.py +++ b/utils/logging.py @@ -11,6 +11,7 @@ import sverchok from sverchok.utils.development import get_version_string from sverchok.utils.context_managers import sv_preferences +import sverchok.settings as settings # Hardcoded for now log_format = "%(asctime)s [%(levelname)s] %(name)s: %(message)s" @@ -262,6 +263,8 @@ def is_enabled_for(log_level="DEBUG") -> bool: consoleHandler = None logger = logging.getLogger("logging") +settings.info = info +settings.setLevel = setLevel def register(): global consoleHandler diff --git a/utils/sv_extra_addons.py b/utils/sv_extra_addons.py index 411b5d93fe..7c9cbb58d2 100644 --- a/utils/sv_extra_addons.py +++ b/utils/sv_extra_addons.py @@ -15,16 +15,14 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # ##### END GPL LICENSE BLOCK ##### -import bpy + import os -import urllib -import urllib.request from zipfile import ZipFile import addon_utils import bpy -import sverchok +import sverchok.settings as settings from sverchok.utils import sv_requests as requests @@ -172,6 +170,8 @@ def execute(self, context): return {'FINISHED'} +settings.draw_extra_addons = draw_extra_addons + def register():