diff --git a/NodeGraphQt/base/graph.py b/NodeGraphQt/base/graph.py index 8e6a0f65..23acd0d9 100644 --- a/NodeGraphQt/base/graph.py +++ b/NodeGraphQt/base/graph.py @@ -721,6 +721,8 @@ def _deserialize_context_menu(self, menu, menu_data): import sys import importlib.util + nodes_menu = self.get_context_menu('nodes') + def build_menu_command(menu, data): """ Create menu command from serialized data. @@ -745,7 +747,12 @@ def build_menu_command(menu, data): cmd_func = getattr(mod, data['function_name']) cmd_name = data.get('label') or '' cmd_shortcut = data.get('shortcut') - menu.add_command(cmd_name, cmd_func, cmd_shortcut) + cmd_kwargs = {'func': cmd_func, 'shortcut': cmd_shortcut} + + if menu == nodes_menu and data.get('node_type'): + cmd_kwargs['node_type'] = data['node_type'] + + menu.add_command(name=cmd_name, **cmd_kwargs) if isinstance(menu_data, dict): item_type = menu_data.get('type') @@ -780,7 +787,8 @@ def set_context_menu(self, menu_name, data): 'label': 'test command', 'file': '../path/to/my/test_module.py', 'function': 'run_test', - 'shortcut': 'Ctrl+b' + 'shortcut': 'Ctrl+b', + 'node_type': 'nodeGraphQt.nodes.MyNodeClass' }, ] @@ -806,6 +814,8 @@ def set_context_menu_from_file(self, file_path, menu=None): menu (str): name of the parent context menu to populate under. file_path (str): serialized menu commands json file. """ + file_path = os.path.abspath(file_path) + menu = menu or 'graph' if not os.path.isfile(file_path): return diff --git a/examples/basic_example.py b/examples/basic_example.py index 9ddf02e1..4d9d4fab 100644 --- a/examples/basic_example.py +++ b/examples/basic_example.py @@ -26,7 +26,9 @@ # create graph controller. graph = NodeGraph() - graph.set_context_menu_from_file('/Users/jchan/PycharmProjects/NodeGraphQt/examples/hotkeys/hotkeys.json') + + # set up context menu for the node graph. + graph.set_context_menu_from_file('../examples/hotkeys/hotkeys.json') # registered example nodes. graph.register_nodes([