Meshroom/tests/test_nodeCallbacks.py
Yann Lanthony 90acb93ea8 [core] Trigger node desc creation callback only for explicit node creation
The desc.Node.onCreated callback is a hook in the Node API for developers to
write custom behavior on a node first initialization.
By being called in the core.Node constructor, this was triggered in several situations
that don't match this idea and with unpredictable side effects (graph loading, node re-creation on undo...).

* Make `onNodeCreated` callback an explicit method on desc.Node.
* Remove the call to node descriptor's `onNodeCreated` callback outside core.Node constructor.
* Trigger this callback on explicit node creation (adding new node to graph, init a graph from a template).
2025-02-14 11:42:22 +01:00

68 lines
2.3 KiB
Python

from meshroom.core import desc, registerNodeType, unregisterNodeType
from meshroom.core.node import Node
from meshroom.core.graph import Graph, loadGraph
class NodeWithCreationCallback(desc.InputNode):
"""Node defining an 'onNodeCreated' callback, triggered a new node is added to a Graph."""
inputs = [
desc.BoolParam(
name="triggered",
label="Triggered",
description="Attribute impacted by the `onNodeCreated` callback",
value=False,
),
]
@classmethod
def onNodeCreated(cls, node: Node):
"""Triggered when a new node is created within a Graph."""
node.triggered.value = True
class TestNodeCreationCallback:
@classmethod
def setup_class(cls):
registerNodeType(NodeWithCreationCallback)
@classmethod
def teardown_class(cls):
unregisterNodeType(NodeWithCreationCallback)
def test_notTriggeredOnNodeInstantiation(self):
node = Node(NodeWithCreationCallback.__name__)
assert node.triggered.value is False
def test_triggeredOnNewNodeCreationInGraph(self):
graph = Graph("")
node = graph.addNewNode(NodeWithCreationCallback.__name__)
assert node.triggered.value is True
def test_notTriggeredOnNodeDuplication(self):
graph = Graph("")
node = graph.addNewNode(NodeWithCreationCallback.__name__)
node.triggered.resetToDefaultValue()
duplicates = graph.duplicateNodes([node])
assert duplicates[node][0].triggered.value is False
def test_notTriggeredOnGraphLoad(self, graphSavedOnDisk):
graph: Graph = graphSavedOnDisk
node = graph.addNewNode(NodeWithCreationCallback.__name__)
node.triggered.resetToDefaultValue()
graph.save()
loadedGraph = loadGraph(graph.filepath)
assert loadedGraph.node(node.name).triggered.value is False
def test_triggeredOnGraphInitializationFromTemplate(self, graphSavedOnDisk):
graph: Graph = graphSavedOnDisk
node = graph.addNewNode(NodeWithCreationCallback.__name__)
node.triggered.resetToDefaultValue()
graph.save(template=True)
graphFromTemplate = Graph("")
graphFromTemplate.initFromTemplate(graph.filepath)
assert graphFromTemplate.node(node.name).triggered.value is True