Skip to content

Commit cad97bc

Browse files
committed
Add create options and unit test.
1 parent c52561b commit cad97bc

4 files changed

Lines changed: 63 additions & 13 deletions

File tree

source/MaterialXCore/Document.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ void Document::initialize()
166166
}
167167

168168
NodeDefPtr Document::addNodeDefFromGraph(NodeGraphPtr nodeGraph, const string& nodeDefName,
169-
const string& category, const string& newGraphName)
169+
const string& category, const string& newGraphName,
170+
DefinitionOptions *options)
170171
{
171172
if (category.empty())
172173
{
@@ -178,26 +179,34 @@ NodeDefPtr Document::addNodeDefFromGraph(NodeGraphPtr nodeGraph, const string& n
178179
throw Exception("Cannot create duplicate nodedef: " + nodeDefName);
179180
}
180181

181-
if (getNodeGraph(newGraphName))
182+
bool addAsChild = options ? options->addImplementationAsChild : false;
183+
184+
if (!addAsChild && getNodeGraph(newGraphName))
182185
{
183186
throw Exception("Cannot create duplicate nodegraph: " + newGraphName);
184187
}
185188

189+
// Create a new nodedef and set its category
190+
NodeDefPtr nodeDef = addNodeDef(nodeDefName, EMPTY_STRING);
191+
nodeDef->setNodeString(category);
192+
186193
// Create a new functional nodegraph, and copy over the
187194
// contents from the compound nodegraph
188-
NodeGraphPtr graph = addNodeGraph(newGraphName);
195+
NodeGraphPtr graph = !addAsChild ? addNodeGraph(newGraphName) : nodeDef->addChild<NodeGraph>(newGraphName);
189196
graph->copyContentFrom(nodeGraph);
190197

191198
for (auto graphChild : graph->getChildren())
192199
{
193200
graphChild->removeAttribute(Element::XPOS_ATTRIBUTE);
194201
graphChild->removeAttribute(Element::YPOS_ATTRIBUTE);
195202
}
196-
graph->setNodeDefString(nodeDefName);
197203

198-
// Create a new nodedef and set its category
199-
NodeDefPtr nodeDef = addNodeDef(nodeDefName, EMPTY_STRING);
200-
nodeDef->setNodeString(category);
204+
// Reference from nodegraph to nodedef is not required
205+
// as the graph is a child of the nodedef.
206+
if (!addAsChild)
207+
{
208+
graph->setNodeDefString(nodeDefName);
209+
}
201210

202211
// Expose any existing interfaces from the graph.
203212
// Any connection attributes ("nodegraph", "nodename", "interfacename") on the

source/MaterialXCore/Document.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ using DocumentPtr = shared_ptr<Document>;
2323
/// A shared pointer to a const Document
2424
using ConstDocumentPtr = shared_ptr<const Document>;
2525

26+
class DefinitionOptions;
27+
2628
/// @class Document
2729
/// A MaterialX document, which represents the top-level element in the
2830
/// MaterialX ownership hierarchy.
@@ -363,7 +365,8 @@ class MX_CORE_API Document : public GraphElement
363365
/// @param category Category of the new NodeDef
364366
/// @return New declaration if successful.
365367
NodeDefPtr addNodeDefFromGraph(NodeGraphPtr nodeGraph, const string& nodeDefName,
366-
const string& category, const string& newGraphName);
368+
const string& category, const string& newGraphName,
369+
DefinitionOptions * options = nullptr);
367370

368371
/// Return the NodeDef, if any, with the given name.
369372
NodeDefPtr getNodeDef(const string& name) const
@@ -708,6 +711,15 @@ class MX_CORE_API Document : public GraphElement
708711
std::unique_ptr<Cache> _cache;
709712
};
710713

714+
/// @class DefinitionOptions
715+
/// Options for defining a NodeDef from an immplementation
716+
class MX_CORE_API DefinitionOptions
717+
{
718+
public:
719+
/// Add implementation as child of NodeDef as opposed a sibliing.
720+
bool addImplementationAsChild = false;
721+
};
722+
711723
/// Create a new Document.
712724
/// @relates Document
713725
MX_CORE_API DocumentPtr createDocument();

source/MaterialXTest/MaterialXCore/Node.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ TEST_CASE("Organization", "[nodegraph]")
675675
CHECK(nodeGraph->getBackdrops().empty());
676676
}
677677

678-
TEST_CASE("Node Definition Creation", "[nodedef]")
678+
void testNodeDefCreationFromGraph(mx::DefinitionOptions options)
679679
{
680680
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
681681
mx::DocumentPtr stdlib = mx::createDocument();
@@ -714,10 +714,12 @@ TEST_CASE("Node Definition Creation", "[nodedef]")
714714
bool isDefaultVersion = false;
715715
const std::string NODENAME = graph->getName();
716716

717+
717718
// Create a new functional graph and definition from a compound graph
719+
bool addAsChild = options.addImplementationAsChild;
718720
std::string newNodeDefName = doc->createValidChildName("ND_" + graph->getName());
719721
std::string newGraphName = doc->createValidChildName("NG_" + graph->getName());
720-
mx::NodeDefPtr nodeDef = doc->addNodeDefFromGraph(graph, newNodeDefName, NODENAME, newGraphName);
722+
mx::NodeDefPtr nodeDef = doc->addNodeDefFromGraph(graph, newNodeDefName, NODENAME, newGraphName, &options);
721723
REQUIRE(nodeDef != nullptr);
722724
nodeDef->setVersionString(VERSION1);
723725
nodeDef->setDefaultVersion(isDefaultVersion);
@@ -759,9 +761,16 @@ TEST_CASE("Node Definition Creation", "[nodedef]")
759761
}
760762

761763
// Check validity of new functional nodegraph
762-
mx::NodeGraphPtr newGraph = doc->getNodeGraph(newGraphName);
764+
mx::NodeGraphPtr newGraph = !addAsChild ? doc->getNodeGraph(newGraphName) : nodeDef->getChildOfType<mx::NodeGraph>(newGraphName);
763765
REQUIRE(newGraph != nullptr);
764-
REQUIRE(newGraph->getNodeDefString() == newNodeDefName);
766+
if (!addAsChild)
767+
{
768+
REQUIRE(newGraph->getNodeDefString() == newNodeDefName);
769+
}
770+
else
771+
{
772+
REQUIRE(newGraph->getNodeDefString().empty());
773+
}
765774
mx::ConstInterfaceElementPtr decl = newGraph->getDeclaration();
766775
REQUIRE(decl->getName() == nodeDef->getName());
767776
REQUIRE(doc->validate());
@@ -826,6 +835,23 @@ TEST_CASE("Node Definition Creation", "[nodedef]")
826835
REQUIRE(doc->validate());
827836
}
828837

838+
TEST_CASE("Node Definition Creation", "[nodedef_create]")
839+
{
840+
mx::DefinitionOptions defOptions;
841+
842+
SECTION("Without implementation as child")
843+
{
844+
defOptions.addImplementationAsChild = false;
845+
testNodeDefCreationFromGraph(defOptions);
846+
}
847+
848+
SECTION("With implementation as child")
849+
{
850+
defOptions.addImplementationAsChild = true;
851+
testNodeDefCreationFromGraph(defOptions);
852+
}
853+
}
854+
829855
TEST_CASE("Set Name Global", "[node, nodegraph]")
830856
{
831857
mx::DocumentPtr doc = mx::createDocument();

source/PyMaterialX/PyMaterialXCore/PyDocument.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ void bindPyDocument(py::module& mod)
2626
{
2727
mod.def("createDocument", &mx::createDocument);
2828

29+
py::class_<mx::DefinitionOptions>(mod, "DefinitionOptions")
30+
.def_readwrite("addImplementationAsChild", &mx::DefinitionOptions::addImplementationAsChild);
31+
2932
py::class_<mx::Document, mx::DocumentPtr, mx::GraphElement>(mod, "Document")
3033
.def("initialize", &mx::Document::initialize)
3134
.def("copy", &mx::Document::copy)
@@ -74,7 +77,7 @@ void bindPyDocument(py::module& mod)
7477
.def("removeTypeDef", &mx::Document::removeTypeDef)
7578
.def("addNodeDef", &mx::Document::addNodeDef,
7679
py::arg("name") = mx::EMPTY_STRING, py::arg("type") = mx::DEFAULT_TYPE_STRING, py::arg("node") = mx::EMPTY_STRING)
77-
.def("addNodeDefFromGraph", (mx::NodeDefPtr (mx::Document::*)(mx::NodeGraphPtr, const std::string&, const std::string&, const std::string&)) & mx::Document::addNodeDefFromGraph)
80+
.def("addNodeDefFromGraph", (mx::NodeDefPtr (mx::Document::*)(mx::NodeGraphPtr, const std::string&, const std::string&, const std::string&, mx::DefinitionOptions*)) & mx::Document::addNodeDefFromGraph)
7881
.def("addNodeDefFromGraph", (mx::NodeDefPtr(mx::Document::*)(mx::NodeGraphPtr, const std::string&, const std::string&, const std::string&,
7982
bool, const std::string&, const std::string& )) & PyBindDocument::old_addNodeDefFromGraph)
8083
.def("getNodeDef", &mx::Document::getNodeDef)

0 commit comments

Comments
 (0)