Skip to content
Merged
22 changes: 22 additions & 0 deletions source/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ const string Backdrop::HEIGHT_ATTRIBUTE = "height";
// Node methods
//

void Node::setNameGlobal(const string& name)
{
vector<PortElementPtr> downStreamPorts = getDownstreamPorts();
setName(name);
const string& newName = getName();
for (PortElementPtr& port : downStreamPorts)
{
port->setNodeName(newName);
}
}

void Node::setConnectedNode(const string& inputName, ConstNodePtr node)
{
InputPtr input = getInput(inputName);
Expand Down Expand Up @@ -571,6 +582,17 @@ string GraphElement::asStringDot() const
// NodeGraph methods
//

void NodeGraph::setNameGlobal(const string& name)
{
vector<PortElementPtr> downStreamPorts = getDownstreamPorts();
setName(name);
const string& newName = getName();
for (PortElementPtr& port : downStreamPorts)
{
port->setNodeGraphString(newName);
}
}

vector<OutputPtr> NodeGraph::getMaterialOutputs() const
{
vector<OutputPtr> materialOutputs;
Expand Down
20 changes: 20 additions & 0 deletions source/MaterialXCore/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class MX_CORE_API Node : public InterfaceElement
}
virtual ~Node() { }

/// @name Name
/// @{

/// Set the name string of this Node, propagating the updated name to all
/// downstream ports.
/// @throws Exception if an element at the same scope already possesses the
/// given name.
void setNameGlobal(const string& name);

/// @}
/// @name Connections
/// @{

Expand Down Expand Up @@ -327,6 +337,16 @@ class MX_CORE_API NodeGraph : public GraphElement
}
virtual ~NodeGraph() { }

/// @name Name
/// @{

/// Set the name string of this NodeGraph, propagating the updated name to all
/// downstream ports.
/// @throws Exception if an element at the same scope already possesses the
/// given name.
void setNameGlobal(const string& name);

/// @}
/// @name Material References
/// @{

Expand Down
111 changes: 111 additions & 0 deletions source/MaterialXTest/MaterialXCore/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,114 @@ TEST_CASE("Node Definition Creation", "[nodedef]")

REQUIRE(doc->validate());
}

TEST_CASE("Set Name Global", "[node, nodegraph]")
{
mx::DocumentPtr doc = mx::createDocument();

const std::string type = "float";
const std::string new_name = "new_name";
SECTION("node")
{
// Upstream -> Downstream
SECTION("Node within NodeGraph -> NodeGraph output")
{
mx::NodeGraphPtr parentGraph = doc->addNodeGraph();

mx::NodePtr upstreamNode = parentGraph->addNode("constant", "upstreamNode", type);
upstreamNode->addOutput("output", type);

mx::PortElementPtr downstreamOutput = parentGraph->addOutput("out", type);
downstreamOutput->setNodeName(upstreamNode->getName());

upstreamNode->setNameGlobal(new_name);
REQUIRE(upstreamNode->getName() == new_name);
REQUIRE(downstreamOutput->getNodeName() == new_name);
}
SECTION("Free node")
{
SECTION("Free Node -> Free Node")
{
mx::NodePtr upstreamNode = doc->addNode("constant", "upstreamNode", type);
upstreamNode->addOutput("output", type);

mx::NodePtr downStreamNode = doc->addNode("downStreamNode");
mx::InputPtr downstreamInput = downStreamNode->addInput("in", type);
downstreamInput->setNodeName(upstreamNode->getName());
SECTION("Update references")
{
upstreamNode->setNameGlobal(new_name);
REQUIRE(upstreamNode->getName() == new_name);
REQUIRE(downstreamInput->getNodeName() == new_name);
}
}
SECTION("Free Node -> NodeGraph input")
{
mx::NodePtr upstreamNode = doc->addNode("constant", "upstreamNode", type);
upstreamNode->addOutput("output", type);

mx::NodeGraphPtr downstreamGraph = doc->addNodeGraph();
mx::InputPtr downstreamInput = downstreamGraph->addInput("input", type);
downstreamInput->setNodeName(upstreamNode->getName());

upstreamNode->setNameGlobal(new_name);
REQUIRE(upstreamNode->getName() == new_name);
REQUIRE(downstreamInput->getNodeName() == new_name);
}
}
SECTION("Node -> NodeDef output")
{
mx::NodeGraphPtr compoundNodeGraph = doc->addNodeGraph();
mx::OutputPtr compoundNodeGraphOutput = compoundNodeGraph->addOutput("output", type);

mx::NodePtr compoundNodeGraphNode = compoundNodeGraph->addNode("constant", "upstreamNode", type);
compoundNodeGraphNode->addOutput("output", type);
compoundNodeGraphOutput->setNodeName(compoundNodeGraphNode->getName());

std::string newNodeDefName = doc->createValidChildName("ND_" + compoundNodeGraph->getName());
std::string newGraphName = doc->createValidChildName("NG_" + compoundNodeGraph->getName());
mx::NodeDefPtr nodeDef = doc->addNodeDefFromGraph(compoundNodeGraph, newNodeDefName, "NODENAME", newGraphName);
mx::NodeGraphPtr functionalNodeGraph = nodeDef->getImplementation()->asA<mx::NodeGraph>();

mx::NodePtr upstreamNode = functionalNodeGraph->getChild(compoundNodeGraphNode->getName())->asA<mx::Node>();
REQUIRE(upstreamNode);

mx::OutputPtr downstreamOutput = functionalNodeGraph->getOutput(compoundNodeGraphOutput->getName());
REQUIRE(downstreamOutput);

upstreamNode->setNameGlobal(new_name);
REQUIRE(upstreamNode->getName() == new_name);
REQUIRE(downstreamOutput->getNodeName() == new_name);
}
}
SECTION("nodegraph")
{
mx::NodeGraphPtr upstreamGraph = doc->addNodeGraph();
mx::NodePtr upstreamGraphNode = upstreamGraph->addNode("constant", "upstreamNode", type);
mx::OutputPtr upstreamGraphOutput = upstreamGraph->addOutput("output", type);
upstreamGraphOutput->setNodeName(upstreamGraphNode->getName());

SECTION("Node Graph -> Node")
{
mx::NodePtr downstreamNode = doc->addNode("constant", "downstreamNode", type);
mx::InputPtr downstreamInput = downstreamNode->addInput("input", type);
downstreamInput->setNodeGraphString(upstreamGraph->getName());
downstreamInput->setOutputString(upstreamGraphOutput->getName());

upstreamGraph->setNameGlobal(new_name);
REQUIRE(upstreamGraph->getName() == new_name);
REQUIRE(downstreamInput->getNodeGraphString() == new_name);
}
SECTION("Node Graph -> Node Graph")
{
mx::NodeGraphPtr downstreamGraph = doc->addNodeGraph();
mx::InputPtr downstreamInput = downstreamGraph->addInput("input", type);
downstreamInput->setNodeGraphString(upstreamGraph->getName());
downstreamInput->setOutputString(upstreamGraphOutput->getName());

upstreamGraph->setNameGlobal(new_name);
REQUIRE(upstreamGraph->getName() == new_name);
REQUIRE(downstreamInput->getNodeGraphString() == new_name);
}
}
}