forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.cpp
More file actions
110 lines (91 loc) · 4.51 KB
/
Document.cpp
File metadata and controls
110 lines (91 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXCore/Document.h>
namespace mx = MaterialX;
TEST_CASE("Document", "[document]")
{
// Create a document.
mx::DocumentPtr doc = mx::createDocument();
// Create a node graph with a constant color output.
mx::NodeGraphPtr nodeGraph = doc->addNodeGraph();
mx::NodePtr constant = nodeGraph->addNode("constant");
constant->setParameterValue("value", mx::Color3(0.5f));
mx::OutputPtr output = nodeGraph->addOutput();
output->setConnectedNode(constant);
REQUIRE(doc->validate());
// Create and test a type mismatch in a connection.
output->setType("float");
REQUIRE(!doc->validate());
output->setType("color3");
REQUIRE(doc->validate());
// Test hierarchical name paths.
REQUIRE(constant->getNamePath() == "nodegraph1/node1");
REQUIRE(constant->getNamePath(nodeGraph) == "node1");
// Test getting elements by path
REQUIRE(doc->getDescendant("") == doc);
REQUIRE(doc->getDescendant("nodegraph1") == nodeGraph);
REQUIRE(doc->getDescendant("nodegraph1/node1") == constant);
REQUIRE(doc->getDescendant("missingElement") == mx::ElementPtr());
REQUIRE(doc->getDescendant("nodegraph1/missingNode") == mx::ElementPtr());
REQUIRE(nodeGraph->getDescendant("") == nodeGraph);
REQUIRE(nodeGraph->getDescendant("node1") == constant);
REQUIRE(nodeGraph->getDescendant("missingNode") == mx::ElementPtr());
// Create a simple shader interface.
mx::NodeDefPtr shader = doc->addNodeDef("", "surfaceshader", "simpleSrf");
mx::InputPtr diffColor = shader->addInput("diffColor", "color3");
mx::InputPtr specColor = shader->addInput("specColor", "color3");
mx::ParameterPtr roughness = shader->addParameter("roughness", "float");
// Create a material that instantiates the shader.
mx::MaterialPtr material = doc->addMaterial();
mx::ShaderRefPtr shaderRef = material->addShaderRef("", "simpleSrf");
// Bind the diffuse color input to the constant color output.
mx::BindInputPtr bindInput = shaderRef->addBindInput("diffColor");
bindInput->setConnectedOutput(output);
REQUIRE(diffColor->getUpstreamElement(material) == output);
// Bind the roughness parameter to a value.
mx::BindParamPtr bindParam = shaderRef->addBindParam("roughness");
bindParam->setValue(0.5f);
REQUIRE(roughness->getBoundValue(material)->asA<float>() == 0.5f);
// Create and test a type mismatch in a data binding.
bindParam->setValue(5);
REQUIRE(!doc->validate());
bindParam->setValue(0.5f);
REQUIRE(doc->validate());
// Create a collection
mx::CollectionPtr collection = doc->addCollection();
REQUIRE(doc->getCollections().size() == 1);
REQUIRE(doc->getCollection(collection->getName()));
doc->removeCollection(collection->getName());
REQUIRE(doc->getCollections().size() == 0);
// Create a property set
mx::PropertySetPtr propertySet = doc->addPropertySet();
REQUIRE(doc->getPropertySets().size() == 1);
REQUIRE(doc->getPropertySet(propertySet->getName()) != nullptr);
doc->removePropertySet(propertySet->getName());
REQUIRE(doc->getPropertySets().size() == 0);
// Validate the document.
REQUIRE(doc->validate());
// Create a namespaced custom library.
mx::DocumentPtr customLibrary = mx::createDocument();
customLibrary->setNamespace("custom");
mx::NodeGraphPtr customNodeGraph = customLibrary->addNodeGraph("NG_custom");
mx::NodeDefPtr customNodeDef = customLibrary->addNodeDef("ND_simpleSrf", "surfaceshader", "simpleSrf");
mx::ImplementationPtr customImpl = customLibrary->addImplementation("IM_custom");
mx::NodePtr customNode = customNodeGraph->addNodeInstance(customNodeDef, "custom1");
customImpl->setNodeDef(customNodeDef);
REQUIRE(customLibrary->validate());
// Import the custom library.
doc->importLibrary(customLibrary);
mx::NodeGraphPtr importedNodeGraph = doc->getNodeGraph("custom:NG_custom");
mx::NodeDefPtr importedNodeDef = doc->getNodeDef("custom:ND_simpleSrf");
mx::ImplementationPtr importedImpl = doc->getImplementation("custom:IM_custom");
mx::NodePtr importedNode = importedNodeGraph->getNode("custom1");
REQUIRE(importedNodeDef != nullptr);
REQUIRE(importedNode->getNodeDef() == importedNodeDef);
REQUIRE(importedImpl->getNodeDef() == importedNodeDef);
// Validate the combined document.
REQUIRE(doc->validate());
}