From c0c9c9d5bb2c129bcbd9095d13858ccc21ea11e4 Mon Sep 17 00:00:00 2001 From: zeffii Date: Sat, 13 May 2017 16:42:39 +0200 Subject: [PATCH 1/4] add boilerplate for custom defaults --- __init__.py | 5 +- core/node_defaults.py | 106 ++++++++++++++++++++++++++++++++++++++++++ node_tree.py | 16 +++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 core/node_defaults.py diff --git a/__init__.py b/__init__.py index bf034d811..8ab570c60 100755 --- a/__init__.py +++ b/__init__.py @@ -79,7 +79,7 @@ root_modules = [ core_modules = [ "monad_properties", - "handlers", "update_system", "upgrade_nodes", "upgrade_group", "monad", + "handlers", "update_system", "upgrade_nodes", "upgrade_group", "monad", "node_defaults" ] utils_modules = [ @@ -157,6 +157,7 @@ if reload_event: import bpy from sverchok.utils import ascii_print +from sverchok.core import node_defaults def register(): @@ -169,12 +170,14 @@ def register(): print("** version: ", bl_info['version']," **") print("** Have a nice day with sverchok **\n") ascii_print.logo() + node_defaults.register_defaults() if reload_event: data_structure.RELOAD_EVENT = True print("Sverchok is reloaded, press update") + def unregister(): for m in reversed(imported_modules + node_list): if hasattr(m, "unregister"): diff --git a/core/node_defaults.py b/core/node_defaults.py new file mode 100644 index 000000000..b4f668b72 --- /dev/null +++ b/core/node_defaults.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +import ast +import os +import importlib.util as getutil + +import bpy +import sverchok + +node_default_dict = {} +node_default_functions = {} + + +def get_dict(): + + datafiles = os.path.join(bpy.utils.user_resource('DATAFILES', path='sverchok', create=True)) + extra_path = ["node_defaults", "node_defaults.json"] + path_to_defaults = os.path.join(datafiles, *extra_path) + + if os.path.exists(path_to_defaults): + with open(path_to_defaults) as d: + print('loading default dict..') + my_dict = ''.join(d.readlines()) + return ast.literal_eval(my_dict) + + return {} + + +def get_function(func, node): + """ + this function takes func and decomposes it into filename (minus .py ) and functionname + it will then look at ../datafiles/node_defaults/filename.py for a function called functionname + assuming it is found, it will add the func (filename.functionnme) to " node_default_functions ". + + - each func lookup should only be needed once per session, + - repeats will be taken from " node_default_functions " dict + + """ + filename, functionname = func.split('.') + + datafiles = os.path.join(bpy.utils.user_resource('DATAFILES', path='sverchok', create=True)) + extra_path = ["node_defaults", filename + '.py'] + path_to_function = os.path.join(datafiles, *extra_path) + + if func in node_default_functions: + return node_default_functions[func] + + else: + if os.path.exists(path_to_function): + print('--- first time getting function path for ', node.bl_idname) + spec = getutil.spec_from_file_location(func, path_to_function) + macro_module = getutil.module_from_spec(spec) + spec.loader.exec_module(macro_module) + node_default_functions[func] = getattr(macro_module, functionname) + + +def set_defaults_if_defined(node): + print('A') + if not hasattr(node, 'bl_idname') and not len(node_default_dict): + return + + print('B') + print(node_default_dict) + node_settings = node_default_dict.get(node.bl_idname) + if not node_settings: + return + + print('C') + props = node_settings.get('props') + if props: + for prop, value in props: + setattr(node, prop, value) + + # execute! + func = node_settings.get('function') + if func: + got_function = get_function(func, node) + try: + got_function(node) + except Exception as err: + print('failed to load', node.bl_idname, '\'s custom default script..') + print('reason: ', repr(err)) + + +def register_defaults(): + print('called') + node_default_dict = get_dict() + print(node_default_dict) + diff --git a/node_tree.py b/node_tree.py index 4bec41d21..5394eaed1 100644 --- a/node_tree.py +++ b/node_tree.py @@ -50,6 +50,8 @@ from sverchok.core.socket_conversions import ( is_vector_to_matrix, is_matrix_to_vector) +from sverchok.core.node_defaults import set_defaults_if_defined + from sverchok.ui import color_def @@ -459,13 +461,26 @@ class SverchCustomTreeNode: self.create_sockets() def init(self, context): + """ + this function is triggered upon node creation, + - freezes the node + - delegates further initialization information to sv_init + - sets node color + - unfreezes the node + - sets custom defaults (nodes, and sockets) + + """ ng = self.id_data + ng.freeze() if hasattr(self, "sv_init"): self.sv_init(context) self.set_color() ng.unfreeze() + set_defaults_if_defined(self) + + def process_node(self, context): ''' Doesn't work as intended, inherited functions can't be used for bpy.props @@ -500,6 +515,7 @@ def register(): bpy.utils.register_class(SvDummySocket) + def unregister(): bpy.utils.unregister_class(SvDummySocket) bpy.utils.unregister_class(VerticesSocket) -- GitLab From 17e508655a0fd7f95258d9bb966f25764be842ce Mon Sep 17 00:00:00 2001 From: zeffii Date: Sat, 13 May 2017 16:58:07 +0200 Subject: [PATCH 2/4] remove debug prints --- core/node_defaults.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/node_defaults.py b/core/node_defaults.py index b4f668b72..fbf1c741e 100644 --- a/core/node_defaults.py +++ b/core/node_defaults.py @@ -28,6 +28,12 @@ node_default_dict = {} node_default_functions = {} +def register_defaults(): + # print('called') + node_default_dict.update(get_dict()) + # print('____', node_default_dict) + + def get_dict(): datafiles = os.path.join(bpy.utils.user_resource('DATAFILES', path='sverchok', create=True)) @@ -64,25 +70,22 @@ def get_function(func, node): else: if os.path.exists(path_to_function): - print('--- first time getting function path for ', node.bl_idname) + # print('--- first time getting function path for ', node.bl_idname) spec = getutil.spec_from_file_location(func, path_to_function) macro_module = getutil.module_from_spec(spec) spec.loader.exec_module(macro_module) node_default_functions[func] = getattr(macro_module, functionname) + return node_default_functions[func] def set_defaults_if_defined(node): - print('A') if not hasattr(node, 'bl_idname') and not len(node_default_dict): return - print('B') - print(node_default_dict) node_settings = node_default_dict.get(node.bl_idname) if not node_settings: return - print('C') props = node_settings.get('props') if props: for prop, value in props: @@ -99,8 +102,5 @@ def set_defaults_if_defined(node): print('reason: ', repr(err)) -def register_defaults(): - print('called') - node_default_dict = get_dict() - print(node_default_dict) + -- GitLab From 227a987aa38a35f127672a225c5e6fe28e692c83 Mon Sep 17 00:00:00 2001 From: zeffii Date: Sat, 13 May 2017 16:58:41 +0200 Subject: [PATCH 3/4] remove whitespace --- core/node_defaults.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/node_defaults.py b/core/node_defaults.py index fbf1c741e..b2bc37d31 100644 --- a/core/node_defaults.py +++ b/core/node_defaults.py @@ -100,7 +100,3 @@ def set_defaults_if_defined(node): except Exception as err: print('failed to load', node.bl_idname, '\'s custom default script..') print('reason: ', repr(err)) - - - - -- GitLab From 9af9b85f04dbcfeda0e5384311e79876c03c4354 Mon Sep 17 00:00:00 2001 From: zeffii Date: Sun, 14 May 2017 11:16:39 +0200 Subject: [PATCH 4/4] clarify comment --- core/node_defaults.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/node_defaults.py b/core/node_defaults.py index b2bc37d31..8956a8dc9 100644 --- a/core/node_defaults.py +++ b/core/node_defaults.py @@ -51,14 +51,16 @@ def get_dict(): def get_function(func, node): """ - this function takes func and decomposes it into filename (minus .py ) and functionname - it will then look at ../datafiles/node_defaults/filename.py for a function called functionname - assuming it is found, it will add the func (filename.functionnme) to " node_default_functions ". + this function takes the variable passed as (which is "filename.functionname") + and looks at ../datafiles/node_defaults/filename.py for a function called functionname. - - each func lookup should only be needed once per session, - - repeats will be taken from " node_default_functions " dict + assuming it is found, it will add the callable function reference to the + node_default_functions dict, (node_default_function[] = function_reference) + each func lookup should only be needed once per session, repeats will be taken directly from + the dict being built. """ + filename, functionname = func.split('.') datafiles = os.path.join(bpy.utils.user_resource('DATAFILES', path='sverchok', create=True)) -- GitLab