MaterialX 1.38.10
Loading...
Searching...
No Matches
Node.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_NODE_H
7#define MATERIALX_NODE_H
8
11
13
15
16MATERIALX_NAMESPACE_BEGIN
17
18class Node;
19class GraphElement;
20class NodeGraph;
21class Backdrop;
22
24using NodePtr = shared_ptr<Node>;
26using ConstNodePtr = shared_ptr<const Node>;
27
29using GraphElementPtr = shared_ptr<GraphElement>;
31using ConstGraphElementPtr = shared_ptr<const GraphElement>;
32
34using NodeGraphPtr = shared_ptr<NodeGraph>;
36using ConstNodeGraphPtr = shared_ptr<const NodeGraph>;
37
39using BackdropPtr = shared_ptr<Backdrop>;
41using ConstBackdropPtr = shared_ptr<const Backdrop>;
42
43// Predicate to test a node against some criteria whether
44// that criteria has passed
45using NodePredicate = std::function<bool(NodePtr node)>;
46
52class MX_CORE_API Node : public InterfaceElement
53{
54 public:
55 Node(ElementPtr parent, const string& name) :
56 InterfaceElement(parent, CATEGORY, name)
57 {
58 }
59 virtual ~Node() { }
60
63
67 void setConnectedNode(const string& inputName, ConstNodePtr node);
68
71 NodePtr getConnectedNode(const string& inputName) const;
72
75 void setConnectedNodeName(const string& inputName, const string& nodeName);
76
79 string getConnectedNodeName(const string& inputName) const;
80
84 void setConnectedOutput(const string& inputName, OutputPtr output);
85
88 OutputPtr getConnectedOutput(const string& inputName) const;
89
93
103 NodeDefPtr getNodeDef(const string& target = EMPTY_STRING,
104 bool allowRoughMatch = false) const;
105
109
117 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const
118 {
119 NodeDefPtr nodeDef = getNodeDef(target);
120 return nodeDef ? nodeDef->getImplementation(target) : InterfaceElementPtr();
121 }
122
126
129 Edge getUpstreamEdge(size_t index = 0) const override;
130
132 size_t getUpstreamEdgeCount() const override
133 {
134 return getInputCount();
135 }
136
142 OutputPtr getNodeDefOutput(ElementPtr connectingElement);
143
146 vector<PortElementPtr> getDownstreamPorts() const;
147
151
154 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override
155 {
156 return getNodeDef(target);
157 }
158
161 InputPtr addInputFromNodeDef(const string& inputName);
162
164 void addInputsFromNodeDef();
165
169
172 bool validate(string* message = nullptr) const override;
173
175
176 public:
177 static const string CATEGORY;
178};
179
182class MX_CORE_API GraphElement : public InterfaceElement
183{
184 protected:
185 GraphElement(ElementPtr parent, const string& category, const string& name) :
186 InterfaceElement(parent, category, name)
187 {
188 }
189
190 public:
191 virtual ~GraphElement() { }
192
195
203 NodePtr addNode(const string& category,
204 const string& name = EMPTY_STRING,
205 const string& type = DEFAULT_TYPE_STRING)
206 {
207 NodePtr node = addChild<Node>(name);
208 node->setCategory(category);
209 node->setType(type);
210 return node;
211 }
212
214 NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string& name = EMPTY_STRING)
215 {
216 NodePtr node = addNode(nodeDef->getNodeString(), name, nodeDef->getType());
217 node->setNodeDefString(nodeDef->getName());
218 return node;
219 }
220
222 NodePtr getNode(const string& name) const
223 {
224 return getChildOfType<Node>(name);
225 }
226
229 vector<NodePtr> getNodes(const string& category = EMPTY_STRING) const
230 {
231 return getChildrenOfType<Node>(category);
232 }
233
235 vector<NodePtr> getNodesOfType(const string& nodeType) const
236 {
237 vector<NodePtr> nodes;
238 for (auto node : getNodes())
239 {
240 if (node->getType() == nodeType)
241 {
242 nodes.push_back(node);
243 }
244 }
245 return nodes;
246 }
247
249 void removeNode(const string& name)
250 {
251 removeChildOfType<Node>(name);
252 }
253
257
260 NodePtr addMaterialNode(const string& name = EMPTY_STRING, ConstNodePtr shaderNode = nullptr);
261
263 vector<NodePtr> getMaterialNodes() const
264 {
265 return getNodesOfType(MATERIAL_TYPE_STRING);
266 }
267
271
273 BackdropPtr addBackdrop(const string& name = EMPTY_STRING)
274 {
275 return addChild<Backdrop>(name);
276 }
277
279 BackdropPtr getBackdrop(const string& name) const
280 {
281 return getChildOfType<Backdrop>(name);
282 }
283
285 vector<BackdropPtr> getBackdrops() const
286 {
287 return getChildrenOfType<Backdrop>();
288 }
289
291 void removeBackdrop(const string& name)
292 {
293 removeChildOfType<Backdrop>(name);
294 }
295
299
307 void flattenSubgraphs(const string& target = EMPTY_STRING, NodePredicate filter = nullptr);
308
311 vector<ElementPtr> topologicalSort() const;
312
315 NodePtr addGeomNode(ConstGeomPropDefPtr geomPropDef, const string& namePrefix);
316
323 string asStringDot() const;
324
326};
327
330class MX_CORE_API NodeGraph : public GraphElement
331{
332 public:
333 NodeGraph(ElementPtr parent, const string& name) :
334 GraphElement(parent, CATEGORY, name)
335 {
336 }
337 virtual ~NodeGraph() { }
338
341
343 vector<OutputPtr> getMaterialOutputs() const;
344
348
350 void setNodeDef(ConstNodeDefPtr nodeDef);
351
353 NodeDefPtr getNodeDef() const;
354
358 InterfaceElementPtr getImplementation() const;
359
363
366 vector<PortElementPtr> getDownstreamPorts() const;
367
371
374 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override;
375
379 void addInterfaceName(const string& inputPath, const string& interfaceName);
380
383 void removeInterfaceName(const string& inputPath);
384
388 void modifyInterfaceName(const string& inputPath, const string& interfaceName);
389
393
396 bool validate(string* message = nullptr) const override;
397
399
400 public:
401 static const string CATEGORY;
402};
403
406class MX_CORE_API Backdrop : public Element
407{
408 public:
409 Backdrop(ElementPtr parent, const string& name) :
410 Element(parent, CATEGORY, name)
411 {
412 }
413 virtual ~Backdrop() { }
414
417
419 void setContainsString(const string& contains)
420 {
421 setAttribute(CONTAINS_ATTRIBUTE, contains);
422 }
423
425 bool hasContainsString() const
426 {
427 return hasAttribute(CONTAINS_ATTRIBUTE);
428 }
429
431 string getContainsString() const
432 {
433 return getAttribute(CONTAINS_ATTRIBUTE);
434 }
435
439
441 void setWidth(float width)
442 {
443 setTypedAttribute<float>(WIDTH_ATTRIBUTE, width);
444 }
445
447 bool hasWidth() const
448 {
449 return hasAttribute(WIDTH_ATTRIBUTE);
450 }
451
453 float getWidth() const
454 {
455 return getTypedAttribute<float>(WIDTH_ATTRIBUTE);
456 }
457
461
463 void setHeight(float height)
464 {
465 setTypedAttribute<float>(HEIGHT_ATTRIBUTE, height);
466 }
467
469 bool hasHeight() const
470 {
471 return hasAttribute(HEIGHT_ATTRIBUTE);
472 }
473
475 float getHeight() const
476 {
477 return getTypedAttribute<float>(HEIGHT_ATTRIBUTE);
478 }
479
483
485 void setContainsElements(const vector<ConstTypedElementPtr>& nodes);
486
488 vector<TypedElementPtr> getContainsElements() const;
489
493
496 bool validate(string* message = nullptr) const override;
497
499
500 public:
501 static const string CATEGORY;
502 static const string CONTAINS_ATTRIBUTE;
503 static const string WIDTH_ATTRIBUTE;
504 static const string HEIGHT_ATTRIBUTE;
505};
506
507MATERIALX_NAMESPACE_END
508
509#endif
Definition element subclasses.
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< const GeomPropDef > ConstGeomPropDefPtr
A shared pointer to a const GeomPropDef.
Definition: Geom.h:50
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:41
shared_ptr< const InterfaceElement > ConstInterfaceElementPtr
A shared pointer to a const InterfaceElement.
Definition: Interface.h:43
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:31
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
Import and export declarations for the Core library.
shared_ptr< const NodeGraph > ConstNodeGraphPtr
A shared pointer to a const NodeGraph.
Definition: Node.h:36
shared_ptr< NodeGraph > NodeGraphPtr
A shared pointer to a NodeGraph.
Definition: Node.h:34
shared_ptr< Backdrop > BackdropPtr
A shared pointer to a Backdrop.
Definition: Node.h:39
shared_ptr< GraphElement > GraphElementPtr
A shared pointer to a GraphElement.
Definition: Node.h:29
shared_ptr< const Backdrop > ConstBackdropPtr
A shared pointer to a const Backdrop.
Definition: Node.h:41
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24
shared_ptr< const GraphElement > ConstGraphElementPtr
A shared pointer to a const GraphElement.
Definition: Node.h:31
A layout element used to contain, group and document nodes within a graph.
Definition: Node.h:407
void setContainsString(const string &contains)
Set the contains string for this backdrop.
Definition: Node.h:419
void setHeight(float height)
Set the height attribute of the backdrop.
Definition: Node.h:463
void setWidth(float width)
Set the width attribute of the backdrop.
Definition: Node.h:441
bool hasHeight() const
Return true if this backdrop has a height attribute.
Definition: Node.h:469
float getWidth() const
Return the width attribute of the backdrop.
Definition: Node.h:453
string getContainsString() const
Return the contains string for this backdrop.
Definition: Node.h:431
float getHeight() const
Return the height attribute of the backdrop.
Definition: Node.h:475
bool hasContainsString() const
Return true if this backdrop has a contains string.
Definition: Node.h:425
bool hasWidth() const
Return true if this backdrop has a width attribute.
Definition: Node.h:447
An edge between two connected Elements, returned during graph traversal.
Definition: Traversal.h:30
The base class for MaterialX elements.
Definition: Element.h:80
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition: Element.h:504
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
Definition: Element.cpp:191
virtual Edge getUpstreamEdge(size_t index=0) const
Return the Edge with the given index that lies directly upstream from this element in the dataflow gr...
Definition: Element.cpp:345
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:497
virtual bool validate(string *message=nullptr) const
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
Definition: Element.cpp:396
The base class for graph elements such as NodeGraph and Document.
Definition: Node.h:183
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:229
vector< BackdropPtr > getBackdrops() const
Return a vector of all Backdrop elements in the graph.
Definition: Node.h:285
BackdropPtr addBackdrop(const string &name=EMPTY_STRING)
Add a Backdrop to the graph.
Definition: Node.h:273
NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string &name=EMPTY_STRING)
Add a Node that is an instance of the given NodeDef.
Definition: Node.h:214
vector< NodePtr > getMaterialNodes() const
Return a vector of all material nodes.
Definition: Node.h:263
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:203
NodePtr getNode(const string &name) const
Return the Node, if any, with the given name.
Definition: Node.h:222
BackdropPtr getBackdrop(const string &name) const
Return the Backdrop, if any, with the given name.
Definition: Node.h:279
void removeNode(const string &name)
Remove the Node, if any, with the given name.
Definition: Node.h:249
void removeBackdrop(const string &name)
Remove the Backdrop, if any, with the given name.
Definition: Node.h:291
vector< NodePtr > getNodesOfType(const string &nodeType) const
Return a vector of nodes in the graph which have a given type.
Definition: Node.h:235
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:318
size_t getInputCount() const
Return the number of Input elements.
Definition: Interface.h:387
virtual ConstInterfaceElementPtr getDeclaration(const string &target=EMPTY_STRING) const
Return the first declaration of this interface, optionally filtered by the given target name.
Definition: Interface.cpp:609
OutputPtr getConnectedOutput(const string &inputName) const
Return the output connected to the given input.
Definition: Interface.cpp:465
void setConnectedOutput(const string &inputName, OutputPtr output)
Set the output to which the given input is connected, creating a child input if needed.
Definition: Interface.cpp:451
A node graph element within a Document.
Definition: Node.h:331
A node element within a NodeGraph or Document.
Definition: Node.h:53
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:117
size_t getUpstreamEdgeCount() const override
Return the number of queriable upstream edges for this element.
Definition: Node.h:132
ConstInterfaceElementPtr 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:154