Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions NodeGraphQt/base/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os
import re
from pathlib import Path

from Qt import QtCore, QtWidgets

Expand Down Expand Up @@ -775,14 +776,15 @@ def get_context_menu(self, menu):
"""
return self._context_menu.get(menu)

def _deserialize_context_menu(self, menu, menu_data):
def _deserialize_context_menu(self, menu, menu_data, anchor_path=None):
"""
Populate context menu from a dictionary.

Args:
menu (NodeGraphQt.NodeGraphMenu or NodeGraphQt.NodesMenu):
parent context menu.
menu_data (list[dict] or dict): serialized menu data.
anchor_path (str or None): directory to interpret file paths relative to (optional)
"""
if not menu:
raise ValueError('No context menu named: "{}"'.format(menu))
Expand All @@ -792,6 +794,10 @@ def _deserialize_context_menu(self, menu, menu_data):

nodes_menu = self.get_context_menu('nodes')

anchor = Path(anchor_path).resolve()
if anchor.is_file():
anchor = anchor.parent

def build_menu_command(menu, data):
"""
Create menu command from serialized data.
Expand All @@ -801,14 +807,16 @@ def build_menu_command(menu, data):
menu object.
data (dict): serialized menu command data.
"""
full_path = os.path.abspath(data['file'])
base_dir, file_name = os.path.split(full_path)
base_name = os.path.basename(base_dir)
file_name, _ = file_name.split('.')
func_path = Path(data['file'])
if not func_path.is_absolute():
func_path = anchor.joinpath(func_path)

base_name = func_path.parent.name
file_name = func_path.stem

mod_name = '{}.{}'.format(base_name, file_name)

spec = importlib.util.spec_from_file_location(mod_name, full_path)
spec = importlib.util.spec_from_file_location(mod_name, func_path)
mod = importlib.util.module_from_spec(spec)
sys.modules[mod_name] = mod
spec.loader.exec_module(mod)
Expand All @@ -832,12 +840,12 @@ def build_menu_command(menu, data):
elif item_type == 'menu':
sub_menu = menu.add_menu(menu_data['label'])
items = menu_data.get('items', [])
self._deserialize_context_menu(sub_menu, items)
self._deserialize_context_menu(sub_menu, items, anchor_path)
elif isinstance(menu_data, list):
for item_data in menu_data:
self._deserialize_context_menu(menu, item_data)
self._deserialize_context_menu(menu, item_data, anchor_path)

def set_context_menu(self, menu_name, data):
def set_context_menu(self, menu_name, data, anchor_path=None):
"""
Populate a context menu from serialized data.

Expand Down Expand Up @@ -875,11 +883,12 @@ def run_test(graph):
Args:
menu_name (str): name of the parent context menu to populate under.
data (dict): serialized menu data.
anchor_path (str or None): directory to interpret file paths relative to (optional)
"""
context_menu = self.get_context_menu(menu_name)
self._deserialize_context_menu(context_menu, data)
self._deserialize_context_menu(context_menu, data, anchor_path)

def set_context_menu_from_file(self, file_path, menu=None):
def set_context_menu_from_file(self, file_path, menu='graph'):
"""
Populate a context menu from a serialized json file.

Expand All @@ -892,16 +901,16 @@ 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)
file = Path(file_path).resolve()

menu = menu or 'graph'
if not os.path.isfile(file_path):
raise IOError('file doesn\'t exists: "{}"'.format(file_path))
if not file.is_file():
raise IOError('file doesn\'t exist: "{}"'.format(file))

with open(file_path) as f:
with file.open() as f:
data = json.load(f)
context_menu = self.get_context_menu(menu)
self._deserialize_context_menu(context_menu, data)
self._deserialize_context_menu(context_menu, data, file)

def disable_context_menu(self, disabled=True, name='all'):
"""
Expand Down
20 changes: 12 additions & 8 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import signal
from pathlib import Path

from Qt import QtCore, QtWidgets

Expand All @@ -12,11 +12,12 @@
NodesPaletteWidget
)

# import example nodes from the "example_nodes" package
from nodes import basic_nodes, custom_ports_node, group_node, widget_nodes
# import example nodes from the "nodes" sub-package
from examples.nodes import basic_nodes, custom_ports_node, group_node, widget_nodes

if __name__ == '__main__':
BASE_PATH = Path(__file__).parent.resolve()

def main():
# handle SIGINT to make the app terminate on CTRL+C
signal.signal(signal.SIGINT, signal.SIG_DFL)

Expand All @@ -26,7 +27,8 @@
graph = NodeGraph()

# set up context menu for the node graph.
graph.set_context_menu_from_file('../examples/hotkeys/hotkeys.json')
hotkey_path = Path(BASE_PATH, 'hotkeys', 'hotkeys.json')
graph.set_context_menu_from_file(hotkey_path, 'graph')

# registered example nodes.
graph.register_nodes([
Expand All @@ -53,9 +55,7 @@
# create node and set a custom icon.
n_basic_b = graph.create_node(
'nodes.basic.BasicNodeB', name='custom icon')
n_basic_b.set_icon(
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'star.png')
)
n_basic_b.set_icon(Path(BASE_PATH, 'star.png'))

# create node with the custom port shapes.
n_custom_ports = graph.create_node(
Expand Down Expand Up @@ -140,3 +140,7 @@ def display_properties_bin(node):
# nodes_palette.show()

app.exec()


if __name__ == '__main__':
main()
6 changes: 6 additions & 0 deletions examples/hotkeys/hotkey_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def clear_session(graph):
if graph.question_dialog('Clear Current Session?', 'Clear Session'):
graph.clear_session()

def quit_qt(graph):
"""
Quit the Qt application.
"""
from Qt import QtCore
QtCore.QCoreApplication.quit()

def clear_undo(graph):
"""
Expand Down
Loading