mirror of
https://github.com/alicevision/Meshroom.git
synced 2025-06-03 11:21:52 +02:00
[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).
This commit is contained in:
parent
6ee3b01177
commit
90acb93ea8
4 changed files with 97 additions and 36 deletions
68
tests/test_nodeCallbacks.py
Normal file
68
tests/test_nodeCallbacks.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue