jchanvfx / NodeGraphQt

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

Layout direction bug when duplicating nodes #398

Open aloytag opened 9 months ago

aloytag commented 9 months ago

If the layout direction in a node is changed to vertical, then duplicating that node causes a strange behavior. For example, consider node_a in vertical layout. Then use the NodeGraph.duplicate_nodes() method to duplicate it. The result: The duplicated node and the original node (node_a) are both in horizontal layout.

Basic example case:

import sys
from Qt import QtWidgets
from NodeGraphQt import NodeGraph, BaseNode

class SimpleNode(BaseNode):
    __identifier__ = 'SimpleNode'
    NODE_NAME = 'SimpleNode'

    def __init__(self):
        super().__init__()

        self.add_input(name='', multi_input=True)
        self.add_output(name='')

if __name__=='__main__':
    app = QtWidgets.QApplication(sys.argv)

    graph = NodeGraph()
    graph.register_node(SimpleNode)

    graph_widget = graph.widget
    graph_widget.show()

    node_a = graph.create_node('SimpleNode.SimpleNode', name='Node 0')
    node_a.set_layout_direction(1)  # Vertical layout

    graph.duplicate_nodes([node_a])

    app.exec_()

I think the problem may be in one of the NodeGraph._serialize() or NodeGraph._deserialize() methods. I've been using these methods in my application in order to save and load a graph into/from a file, and have been experiencing some errors with layout directions.

Regards.

NodeGraphQt version: 0.6.29 Qt.py version: 1.3.7 PySide2 version: 5.15.2.1 Python version: 3.9.2

aloytag commented 9 months ago

I found the problem in NodeGraph._serialize() and NodeGraph._deserialize() methods.

Remove this line in NodeGraph._serialize():

serial_data['graph']['layout_direction'] = self.layout_direction()

With that line, the graph layout setting overwrites every node layout. But instead, we want to respect particular node layouts.

And add this line in NodeGraph._deserialize() when building the nodes:

node.set_layout_direction(n_data['layout_direction'])

This way, the new (deserialized) nodes copy the layout direction from the original (serialized) nodes.

Attached is a file with the modified methods. modified_graph.zip