To integrate NodeGraphQt into Foundry Nuke, you can register the NodeGraph.widget property as a Nuke panel.
Because Nuke's panels.registerWidgetAsPanel often adds unwanted margins to the registered widget, it is recommended to wrap the graph.widget in a custom QtWidgets.QWidget and use a margin-resetting hack (via setContentsMargins(0, 0, 0, 0)) on the parent hierarchy when the widget is shown. This ensures the node graph fills the entire panel area.
from nukescripts import panels
from Qt import QtWidgets, QtCore
from NodeGraphQt import NodeGraph, BaseNode
# 1. Define your nodes
class TestNode(BaseNode):
__identifier__ = 'nodes.nuke'
NODE_NAME = 'test node'
def __init__(self):
super(TestNode, self).__init__()
self.add_input('in')
self.add_output('out 1')
self.add_output('out 2')
# 2. Initialize the graph and register nodes
graph = NodeGraph()
graph.register_node(TestNode)
# 3. Create a wrapper widget to handle Nuke-specific layout issues
class CustomNodeGraph(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CustomNodeGraph, self).__init__(parent)
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(graph.widget)
def event(self, event):
if event.type() == QtCore.QEvent.Type.Show:
# Hack to remove margins from Nuke's panel container
try:
parent_widget = self.parentWidget().parentWidget()
target_widgets = {parent_widget, parent_widget.parentWidget().parentWidget()}
for widget_layout in target_widgets:
widget_layout.layout().setContentsMargins(0, 0, 0, 0)
except Exception:
pass
return super(CustomNodeGraph, self).event(event)
# 4. Register with Nuke
panels.registerWidgetAsPanel(
widget='CustomNodeGraph',
name='Custom Node Graph',
id='nodegraphqt.graph.CustomNodeGraph'
)