MaterialX 1.38.2
Node.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_NODE_H
7#define MATERIALX_NODE_H
8
11
13
15
16namespace MaterialX
17{
18
19class Node;
20class GraphElement;
21class NodeGraph;
22class Backdrop;
23
25using NodePtr = shared_ptr<Node>;
27using ConstNodePtr = shared_ptr<const Node>;
28
30using GraphElementPtr = shared_ptr<GraphElement>;
32using ConstGraphElementPtr = shared_ptr<const GraphElement>;
33
35using NodeGraphPtr = shared_ptr<NodeGraph>;
37using ConstNodeGraphPtr = shared_ptr<const NodeGraph>;
38
40using BackdropPtr = shared_ptr<Backdrop>;
42using ConstBackdropPtr = shared_ptr<const Backdrop>;
43
44// Predicate to test a node against some criteria whether
45// that criteria has passed
46using NodePredicate = std::function<bool(NodePtr node)>;
47
53class MX_CORE_API Node : public InterfaceElement
54{
55 public:
56 Node(ElementPtr parent, const string& name) :
57 InterfaceElement(parent, CATEGORY, name)
58 {
59 }
60 virtual ~Node() { }
61
64
68 void setConnectedNode(const string& inputName, NodePtr node);
69
72 NodePtr getConnectedNode(const string& inputName) const;
73
76 void setConnectedNodeName(const string& inputName, const string& nodeName);
77
80 string getConnectedNodeName(const string& inputName) const;
81
85 void setConnectedOutput(const string& inputName, OutputPtr output);
86
89 OutputPtr getConnectedOutput(const string& inputName) const;
90
94
101 NodeDefPtr getNodeDef(const string& target = EMPTY_STRING) const;
102
106
114 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const
115 {
116 NodeDefPtr nodeDef = getNodeDef(target);
117 return nodeDef ? nodeDef->getImplementation(target) : InterfaceElementPtr();
118 }
119
123
126 Edge getUpstreamEdge(size_t index = 0) const override;
127
129 size_t getUpstreamEdgeCount() const override
130 {
131 return getInputCount();
132 }
133
139 OutputPtr getNodeDefOutput(ElementPtr connectingElement);
140
143 vector<PortElementPtr> getDownstreamPorts() const;
144
148
151 ConstNodeDefPtr getDeclaration(const string& target = EMPTY_STRING) const override
152 {
153 return getNodeDef(target);
154 }
155
158 InputPtr addInputFromNodeDef(const string& name);
159
163
166 bool validate(string* message = nullptr) const override;
167
169
170 public:
171 static const string CATEGORY;
172};
173
176class MX_CORE_API GraphElement : public InterfaceElement
177{
178 protected:
179 GraphElement(ElementPtr parent, const string& category, const string& name) :
180 InterfaceElement(parent, category, name)
181 {
182 }
183 public:
184 virtual ~GraphElement() { }
185
188
196 NodePtr addNode(const string& category,
197 const string& name = EMPTY_STRING,
198 const string& type = DEFAULT_TYPE_STRING)
199 {
200 NodePtr node = addChild<Node>(name);
201 node->setCategory(category);
202 node->setType(type);
203 return node;
204 }
205
207 NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string& name = EMPTY_STRING)
208 {
209 NodePtr node = addNode(nodeDef->getNodeString(), name, nodeDef->getType());
210 node->setNodeDefString(nodeDef->getName());
211 return node;
212 }
213
215 NodePtr getNode(const string& name) const
216 {
217 return getChildOfType<Node>(name);
218 }
219
222 vector<NodePtr> getNodes(const string& category = EMPTY_STRING) const
223 {
224 return getChildrenOfType<Node>(category);
225 }
226
228 vector<NodePtr> getNodesOfType(const string& nodeType) const
229 {
230 vector<NodePtr> nodes;
231 for (auto node : getNodes())
232 {
233 if (node->getType() == nodeType)
234 {
235 nodes.push_back(node);
236 }
237 }
238 return nodes;
239 }
240
242 void removeNode(const string& name)
243 {
244 removeChildOfType<Node>(name);
245 }
246
250
253 NodePtr addMaterialNode(const string& name = EMPTY_STRING, ConstNodePtr shaderNode = nullptr);
254
256 vector<NodePtr> getMaterialNodes() const
257 {
258 return getNodesOfType(MATERIAL_TYPE_STRING);
259 }
260
264
266 BackdropPtr addBackdrop(const string& name = EMPTY_STRING)
267 {
268 return addChild<Backdrop>(name);
269 }
270
272 BackdropPtr getBackdrop(const string& name) const
273 {
274 return getChildOfType<Backdrop>(name);
275 }
276
278 vector<BackdropPtr> getBackdrops() const
279 {
280 return getChildrenOfType<Backdrop>();
281 }
282
284 void removeBackdrop(const string& name)
285 {
286 removeChildOfType<Backdrop>(name);
287 }
288
292
295 void flattenSubgraphs(const string& target = EMPTY_STRING, NodePredicate filter=nullptr);
296
300 vector<ElementPtr> topologicalSort() const;
301
308 string asStringDot() const;
309
311};
312
315class MX_CORE_API NodeGraph : public GraphElement
316{
317 public:
318 NodeGraph(ElementPtr parent, const string& name) :
319 GraphElement(parent, CATEGORY, name)
320 {
321 }
322 virtual ~NodeGraph() { }
323
326
328 void setNodeDef(ConstNodeDefPtr nodeDef);
329
331 NodeDefPtr getNodeDef() const;
332
336 InterfaceElementPtr getImplementation() const;
337
341
344 ConstNodeDefPtr getDeclaration(const string& target = EMPTY_STRING) const override;
345
349 void addInterfaceName(const string& inputPath, const string& interfaceName);
350
353 void removeInterfaceName(const string& inputPath);
354
358 void modifyInterfaceName(const string& inputPath, const string& interfaceName);
359
363
366 bool validate(string* message = nullptr) const override;
367
369
370 public:
371 static const string CATEGORY;
372};
373
376class MX_CORE_API Backdrop : public Element
377{
378 public:
379 Backdrop(ElementPtr parent, const string& name) :
380 Element(parent, CATEGORY, name)
381 {
382 }
383 virtual ~Backdrop() { }
384
387
389 void setContainsString(const string& contains)
390 {
391 setAttribute(CONTAINS_ATTRIBUTE, contains);
392 }
393
395 bool hasContainsString() const
396 {
397 return hasAttribute(CONTAINS_ATTRIBUTE);
398 }
399
401 string getContainsString() const
402 {
403 return getAttribute(CONTAINS_ATTRIBUTE);
404 }
405
409
411 void setWidth(float width)
412 {
413 setTypedAttribute<float>(WIDTH_ATTRIBUTE, width);
414 }
415
417 bool hasWidth() const
418 {
419 return hasAttribute(WIDTH_ATTRIBUTE);
420 }
421
423 float getWidth() const
424 {
425 return getTypedAttribute<float>(WIDTH_ATTRIBUTE);
426 }
427
431
433 void setHeight(float height)
434 {
435 setTypedAttribute<float>(HEIGHT_ATTRIBUTE, height);
436 }
437
439 bool hasHeight() const
440 {
441 return hasAttribute(HEIGHT_ATTRIBUTE);
442 }
443
445 float getHeight() const
446 {
447 return getTypedAttribute<float>(HEIGHT_ATTRIBUTE);
448 }
449
453
455 void setContainsElements(const vector<ConstTypedElementPtr>& nodes);
456
458 vector<TypedElementPtr> getContainsElements() const;
459
463
466 bool validate(string* message = nullptr) const override;
467
469
470 public:
471 static const string CATEGORY;
472 static const string CONTAINS_ATTRIBUTE;
473 static const string WIDTH_ATTRIBUTE;
474 static const string HEIGHT_ATTRIBUTE;
475};
476
477} // namespace MaterialX
478
479#endif
Definition element subclasses.
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:42
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:32
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:37
Import and export declarations for the Core library.
shared_ptr< const NodeGraph > ConstNodeGraphPtr
A shared pointer to a const NodeGraph.
Definition: Node.h:37
shared_ptr< NodeGraph > NodeGraphPtr
A shared pointer to a NodeGraph.
Definition: Node.h:35
shared_ptr< Backdrop > BackdropPtr
A shared pointer to a Backdrop.
Definition: Node.h:40
shared_ptr< GraphElement > GraphElementPtr
A shared pointer to a GraphElement.
Definition: Node.h:30
shared_ptr< const Backdrop > ConstBackdropPtr
A shared pointer to a const Backdrop.
Definition: Node.h:42
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:27
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:25
shared_ptr< const GraphElement > ConstGraphElementPtr
A shared pointer to a const GraphElement.
Definition: Node.h:32
A layout element used to contain, group and document nodes within a graph.
Definition: Node.h:377
void setContainsString(const string &contains)
Set the contains string for this backdrop.
Definition: Node.h:389
void setHeight(float height)
Set the height attribute of the backdrop.
Definition: Node.h:433
void setWidth(float width)
Set the width attribute of the backdrop.
Definition: Node.h:411
bool hasHeight() const
Return true if this backdrop has a height attribute.
Definition: Node.h:439
float getWidth() const
Return the width attribute of the backdrop.
Definition: Node.h:423
string getContainsString() const
Return the contains string for this backdrop.
Definition: Node.h:401
float getHeight() const
Return the height attribute of the backdrop.
Definition: Node.h:445
bool hasContainsString() const
Return true if this backdrop has a contains string.
Definition: Node.h:395
bool hasWidth() const
Return true if this backdrop has a width attribute.
Definition: Node.h:417
An edge between two connected Elements, returned during graph traversal.
Definition: Traversal.h:31
The base class for MaterialX elements.
Definition: Element.h:75
The base class for graph elements such as NodeGraph and Document.
Definition: Node.h:177
vector< NodePtr > getNodes(const string &category=EMPTY_STRING) const
Return a vector of all Nodes in the graph, optionally filtered by the given category string.
Definition: Node.h:222
vector< BackdropPtr > getBackdrops() const
Return a vector of all Backdrop elements in the graph.
Definition: Node.h:278
BackdropPtr addBackdrop(const string &name=EMPTY_STRING)
Add a Backdrop to the graph.
Definition: Node.h:266
NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string &name=EMPTY_STRING)
Add a Node that is an instance of the given NodeDef.
Definition: Node.h:207
vector< NodePtr > getMaterialNodes() const
Return a vector of all material nodes.
Definition: Node.h:256
NodePtr addNode(const string &category, const string &name=EMPTY_STRING, const string &type=DEFAULT_TYPE_STRING)
Add a Node to the graph.
Definition: Node.h:196
NodePtr getNode(const string &name) const
Return the Node, if any, with the given name.
Definition: Node.h:215
BackdropPtr getBackdrop(const string &name) const
Return the Backdrop, if any, with the given name.
Definition: Node.h:272
void removeNode(const string &name)
Remove the Node, if any, with the given name.
Definition: Node.h:242
void removeBackdrop(const string &name)
Remove the Backdrop, if any, with the given name.
Definition: Node.h:284
vector< NodePtr > getNodesOfType(const string &nodeType) const
Return a vector of nodes in the graph which have a given type.
Definition: Node.h:228
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:317
A node graph element within a Document.
Definition: Node.h:316
A node element within a NodeGraph or Document.
Definition: Node.h:54
InterfaceElementPtr getImplementation(const string &target=EMPTY_STRING) const
Return the first implementation for this node, optionally filtered by the given target and language n...
Definition: Node.h:114
size_t getUpstreamEdgeCount() const override
Return the number of queriable upstream edges for this element.
Definition: Node.h:129
ConstNodeDefPtr getDeclaration(const string &target=EMPTY_STRING) const override
Return the first declaration of this interface, optionally filtered by the given target name.
Definition: Node.h:151