|
| 1 | +NodeGraphQt in Nuke |
| 2 | +################### |
| 3 | + |
| 4 | +Creating a custom node graph panel in NUKE |
| 5 | + |
| 6 | +.. image:: ../_images/app_nuke_example.png |
| 7 | + :width: 800px |
| 8 | + |
| 9 | +Here is an example where the :attr:`NodeGraph.widget` can be registered as a |
| 10 | +panel in the compositing application NUKE. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | + :linenos: |
| 16 | +
|
| 17 | + from nukescripts import panels |
| 18 | +
|
| 19 | + from Qt import QtWidgets, QtCore |
| 20 | + from NodeGraphQt import NodeGraph, BaseNode |
| 21 | +
|
| 22 | +
|
| 23 | + # create a simple test node class. |
| 24 | + class TestNode(BaseNode): |
| 25 | + """ |
| 26 | + Simple test node object. |
| 27 | + """ |
| 28 | +
|
| 29 | + __identifier__ = 'nodes.nuke' |
| 30 | + NODE_NAME = 'test node' |
| 31 | +
|
| 32 | + def __init__(self): |
| 33 | + super(TestNode, self).__init__() |
| 34 | + self.add_input('in') |
| 35 | + self.add_output('out 1') |
| 36 | + self.add_output('out 2') |
| 37 | +
|
| 38 | +
|
| 39 | + # create the node graph controller and register our "TestNode". |
| 40 | + graph = NodeGraph() |
| 41 | + graph.register_node(TestNode) |
| 42 | +
|
| 43 | + # create nodes. |
| 44 | + node_1 = graph.create_node('nodes.nuke.TestNode') |
| 45 | + node_2 = graph.create_node('nodes.nuke.TestNode') |
| 46 | + node_3 = graph.create_node('nodes.nuke.TestNode') |
| 47 | +
|
| 48 | + # connect the nodes. |
| 49 | + node_1.set_output(0, node_2.input(0)) |
| 50 | + node_2.set_output(1, node_3.input(0)) |
| 51 | +
|
| 52 | + # auto layout the nodes. |
| 53 | + graph.auto_layout_nodes() |
| 54 | +
|
| 55 | + # create a backdrop node and wrap it to node_1 and node_2. |
| 56 | + backdrop = graph.create_node('Backdrop') |
| 57 | + backdrop.wrap_nodes([node_1, node_2]) |
| 58 | +
|
| 59 | +
|
| 60 | + # create the wrapper widget. |
| 61 | + class CustomNodeGraph(QtWidgets.QWidget): |
| 62 | + """ |
| 63 | + Custom node graph widget to be registered as a panel in Nuke. |
| 64 | + """ |
| 65 | +
|
| 66 | + def __init__(self, parent=None): |
| 67 | + super(CustomNodeGraph, self).__init__(parent) |
| 68 | + layout = QtWidgets.QVBoxLayout(self) |
| 69 | + layout.setContentsMargins(0, 0, 0, 0) |
| 70 | + layout.addWidget(graph.widget) |
| 71 | +
|
| 72 | + @staticmethod |
| 73 | + def _set_nuke_zero_margin(widget_object): |
| 74 | + """ |
| 75 | + Foundry Nuke hack for "nukescripts.panels.registerWidgetAsPanel" to |
| 76 | + remove the widget contents margin. |
| 77 | +
|
| 78 | + sourced from: https://gist.github.com/maty974/4739917 |
| 79 | +
|
| 80 | + Args: |
| 81 | + widget_object (QtWidgets.QWidget): widget object. |
| 82 | + """ |
| 83 | + if widget_object: |
| 84 | + parent_widget = widget_object.parentWidget().parentWidget() |
| 85 | + target_widgets = set() |
| 86 | + target_widgets.add(parent_widget) |
| 87 | + target_widgets.add(parent_widget.parentWidget().parentWidget()) |
| 88 | + for widget_layout in target_widgets: |
| 89 | + widget_layout.layout().setContentsMargins(0, 0, 0, 0) |
| 90 | +
|
| 91 | + def event(self, event): |
| 92 | + if event.type() == QtCore.QEvent.Type.Show: |
| 93 | + try: |
| 94 | + self._set_nuke_zero_margin(self) |
| 95 | + except Exception: |
| 96 | + pass |
| 97 | + return super(CustomNodeGraph, self).event(event) |
| 98 | +
|
| 99 | + # register the wrapper widget as a panel in Nuke. |
| 100 | + panels.registerWidgetAsPanel( |
| 101 | + widget='CustomNodeGraph', |
| 102 | + name='Custom Node Graph', |
| 103 | + id='nodegraphqt.graph.CustomNodeGraph' |
| 104 | + ) |
0 commit comments