Skip to content

Commit

Permalink
Avoid importing half of Sverchok from settings. Also, it should fix #…
Browse files Browse the repository at this point in the history
…4281

Settings were purposely imported via addon_folder_name.settings instead of sverchok.settings. I assume this was done to be able to write this: class SvPreferences: bl_idname = __package__. Because it seems that bl_idname of add-on settings should always be equal to name of add-on folder.
  • Loading branch information
Durman committed Dec 13, 2022
1 parent e930504 commit 1e545f3
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 120 deletions.
10 changes: 5 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
52 changes: 21 additions & 31 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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:
Expand All @@ -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"
Expand All @@ -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)


Expand All @@ -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
Expand All @@ -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)


Expand Down
5 changes: 4 additions & 1 deletion core/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions core/socket_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
30 changes: 0 additions & 30 deletions data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import numpy as np


DEBUG_MODE = False
RELOAD_EVENT = False

# this is set correctly later.
Expand Down Expand Up @@ -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! ################
#####################################################
Expand Down
8 changes: 8 additions & 0 deletions dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
71 changes: 34 additions & 37 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -181,35 +184,26 @@ 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,
description="pick viewing mode",
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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions ui/color_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
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
from sverchok.ui.nodeview_space_menu import add_node_menu

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),
Expand Down Expand Up @@ -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
Loading

0 comments on commit 1e545f3

Please sign in to comment.