jchanvfx / NodeGraphQt

Node graph framework that can be re-implemented into applications that supports PySide2
http://chantonic.com/NodeGraphQt/
MIT License
1.26k stars 256 forks source link

Ability to register custom context menus #127

Closed mikemalinowski closed 3 years ago

mikemalinowski commented 5 years ago

Currently when right clicking we always get the context menu generated by NodeGraphQt.base.actions.setup_context_menu, but it would be nice to either override or extend that on a per-node/per-port basis.

This would allow the graph to be utilised for a really wide variety of tools. (love the library btw!)

jchanvfx commented 4 years ago

Hi @mikemalinowski ,

Thanks for checking out the node graph framework, A new released is out v0.0.16 with the ability to register custom context menus on a per node type basis see example below 😸as for context menu on a per port basis that will have to be in a later version 😄

Cheers, Johnny

from NodeGraphQt import BaseNode, NodeGraph, QtWidgets, setup_context_menu

def my_func(graph, node):
    print('Command triggered on node: {}'.format(node.name()))

class MyNode(BaseNode):

    __identifier__ = 'com.chantasticvfx'
    NODE_NAME = 'my node'

    def __init__(self):
        super(MyNode, self).__init__()
        self.add_input('foo')
        self.add_output('bar')

if __name__ == '__main__':
    app = QtWidgets.QApplication([])

    graph = NodeGraph()
    graph.register_node(MyNode)
    graph.widget.resize(1100, 800)
    graph.widget.show()

    # setup default context menu.
    setup_context_menu(graph)

    # register node context menu.
    node_menu = graph.context_nodes_menu()
    node_menu.add_command('foo', my_func, node_type='com.chantasticvfx.MyNode')

    # create a couple nodes.
    node_a = graph.create_node('com.chantasticvfx.MyNode',
                               name='node foo',
                               pos=[0, 0])

    app.exec_()
ICWiener-1 commented 4 years ago

Hello, Editing my previous comment stating that node_id was None. Checked your git and the code and issue #142 was the problem, so removing my comment here. Thanks again for that awesome Node vibe