MaterialX 1.39.1
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
94 NodeDefPtr getNodeDef(const string& target = EMPTY_STRING,
95 bool allowRoughMatch = false) const;
96
100
108 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const
109 {
110 NodeDefPtr nodeDef = getNodeDef(target);
111 return nodeDef ? nodeDef->getImplementation(target) : InterfaceElementPtr();
112 }
113
117
120 Edge getUpstreamEdge(size_t index = 0) const override;
121
123 size_t getUpstreamEdgeCount() const override
124 {
125 return getInputCount();
126 }
127
134
137 vector<PortElementPtr> getDownstreamPorts() const;
138
142
145 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override
146 {
147 return getNodeDef(target);
148 }
149
152 InputPtr addInputFromNodeDef(const string& inputName);
153
156
160
163 bool validate(string* message = nullptr) const override;
164
166
167 public:
168 static const string CATEGORY;
169};
170
173class MX_CORE_API GraphElement : public InterfaceElement
174{
175 protected:
176 GraphElement(ElementPtr parent, const string& category, const string& name) :
177 InterfaceElement(parent, category, name)
178 {
179 }
180
181 public:
182 virtual ~GraphElement() { }
183
186
194 NodePtr addNode(const string& category,
195 const string& name = EMPTY_STRING,
196 const string& type = DEFAULT_TYPE_STRING)
197 {
198 NodePtr node = addChild<Node>(name);
199 node->setCategory(category);
200 node->setType(type);
201 return node;
202 }
203
205 NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string& name = EMPTY_STRING)
206 {
207 NodePtr node = addNode(nodeDef->getNodeString(), name, nodeDef->getType());
208 node->setNodeDefString(nodeDef->getName());
209 return node;
210 }
211
213 NodePtr getNode(const string& name) const
214 {
215 return getChildOfType<Node>(name);
216 }
217
220 vector<NodePtr> getNodes(const string& category = EMPTY_STRING) const
221 {
222 return getChildrenOfType<Node>(category);
223 }
224
226 vector<NodePtr> getNodesOfType(const string& nodeType) const
227 {
228 vector<NodePtr> nodes;
229 for (auto node : getNodes())
230 {
231 if (node->getType() == nodeType)
232 {
233 nodes.push_back(node);
234 }
235 }
236 return nodes;
237 }
238
240 void removeNode(const string& name)
241 {
242 removeChildOfType<Node>(name);
243 }
244
248
251 NodePtr addMaterialNode(const string& name = EMPTY_STRING, ConstNodePtr shaderNode = nullptr);
252
254 vector<NodePtr> getMaterialNodes() const
255 {
256 return getNodesOfType(MATERIAL_TYPE_STRING);
257 }
258
262
264 BackdropPtr addBackdrop(const string& name = EMPTY_STRING)
265 {
266 return addChild<Backdrop>(name);
267 }
268
270 BackdropPtr getBackdrop(const string& name) const
271 {
272 return getChildOfType<Backdrop>(name);
273 }
274
276 vector<BackdropPtr> getBackdrops() const
277 {
278 return getChildrenOfType<Backdrop>();
279 }
280
282 void removeBackdrop(const string& name)
283 {
284 removeChildOfType<Backdrop>(name);
285 }
286
290
298 void flattenSubgraphs(const string& target = EMPTY_STRING, NodePredicate filter = nullptr);
299
302 vector<ElementPtr> topologicalSort() const;
303
306 NodePtr addGeomNode(ConstGeomPropDefPtr geomPropDef, const string& namePrefix);
307
314 string asStringDot() const;
315
317};
318
321class MX_CORE_API NodeGraph : public GraphElement
322{
323 public:
324 NodeGraph(ElementPtr parent, const string& name) :
325 GraphElement(parent, CATEGORY, name)
326 {
327 }
328 virtual ~NodeGraph() { }
329
332
334 vector<OutputPtr> getMaterialOutputs() const;
335
339
341 void setNodeDef(ConstNodeDefPtr nodeDef);
342
344 NodeDefPtr getNodeDef() const;
345
350
354
357 vector<PortElementPtr> getDownstreamPorts() const;
358
362
365 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override;
366
371 InputPtr addInterfaceName(const string& inputPath, const string& interfaceName);
372
375 void removeInterfaceName(const string& inputPath);
376
380 void modifyInterfaceName(const string& inputPath, const string& interfaceName);
381
385
388 bool validate(string* message = nullptr) const override;
389
391
392 public:
393 static const string CATEGORY;
394};
395
398class MX_CORE_API Backdrop : public Element
399{
400 public:
401 Backdrop(ElementPtr parent, const string& name) :
402 Element(parent, CATEGORY, name)
403 {
404 }
405 virtual ~Backdrop() { }
406
409
411 void setContainsString(const string& contains)
412 {
413 setAttribute(CONTAINS_ATTRIBUTE, contains);
414 }
415
417 bool hasContainsString() const
418 {
419 return hasAttribute(CONTAINS_ATTRIBUTE);
420 }
421
423 string getContainsString() const
424 {
425 return getAttribute(CONTAINS_ATTRIBUTE);
426 }
427
431
433 void setWidth(float width)
434 {
435 setTypedAttribute<float>(WIDTH_ATTRIBUTE, width);
436 }
437
439 bool hasWidth() const
440 {
441 return hasAttribute(WIDTH_ATTRIBUTE);
442 }
443
445 float getWidth() const
446 {
447 return getTypedAttribute<float>(WIDTH_ATTRIBUTE);
448 }
449
453
455 void setHeight(float height)
456 {
457 setTypedAttribute<float>(HEIGHT_ATTRIBUTE, height);
458 }
459
461 bool hasHeight() const
462 {
463 return hasAttribute(HEIGHT_ATTRIBUTE);
464 }
465
467 float getHeight() const
468 {
469 return getTypedAttribute<float>(HEIGHT_ATTRIBUTE);
470 }
471
475
477 void setContainsElements(const vector<ConstTypedElementPtr>& nodes);
478
480 vector<TypedElementPtr> getContainsElements() const;
481
485
488 bool validate(string* message = nullptr) const override;
489
491
492 public:
493 static const string CATEGORY;
494 static const string CONTAINS_ATTRIBUTE;
495 static const string WIDTH_ATTRIBUTE;
496 static const string HEIGHT_ATTRIBUTE;
497};
498
499MATERIALX_NAMESPACE_END
500
501#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:399
void setContainsString(const string &contains)
Set the contains string for this backdrop.
Definition: Node.h:411
void setHeight(float height)
Set the height attribute of the backdrop.
Definition: Node.h:455
void setWidth(float width)
Set the width attribute of the backdrop.
Definition: Node.h:433
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
bool hasHeight() const
Return true if this backdrop has a height attribute.
Definition: Node.h:461
float getWidth() const
Return the width attribute of the backdrop.
Definition: Node.h:445
string getContainsString() const
Return the contains string for this backdrop.
Definition: Node.h:423
vector< TypedElementPtr > getContainsElements() const
Return the vector of elements that this backdrop contains.
float getHeight() const
Return the height attribute of the backdrop.
Definition: Node.h:467
void setContainsElements(const vector< ConstTypedElementPtr > &nodes)
Set the vector of elements that this backdrop contains.
bool hasContainsString() const
Return true if this backdrop has a contains string.
Definition: Node.h:417
bool hasWidth() const
Return true if this backdrop has a width attribute.
Definition: Node.h:439
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.
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition: Element.h:497
The base class for graph elements such as NodeGraph and Document.
Definition: Node.h:174
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:220
vector< BackdropPtr > getBackdrops() const
Return a vector of all Backdrop elements in the graph.
Definition: Node.h:276
BackdropPtr addBackdrop(const string &name=EMPTY_STRING)
Add a Backdrop to the graph.
Definition: Node.h:264
void flattenSubgraphs(const string &target=EMPTY_STRING, NodePredicate filter=nullptr)
Flatten all subgraphs at the root scope of this graph element, recursively replacing each graph-defin...
NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string &name=EMPTY_STRING)
Add a Node that is an instance of the given NodeDef.
Definition: Node.h:205
string asStringDot() const
Convert this graph to a string in the DOT language syntax.
NodePtr addMaterialNode(const string &name=EMPTY_STRING, ConstNodePtr shaderNode=nullptr)
Add a material node to the graph, optionally connecting it to the given shader node.
vector< NodePtr > getMaterialNodes() const
Return a vector of all material nodes.
Definition: Node.h:254
vector< ElementPtr > topologicalSort() const
Return a vector of all children (nodes and outputs) sorted in topological order.
NodePtr addGeomNode(ConstGeomPropDefPtr geomPropDef, const string &namePrefix)
If not yet present, add a geometry node to this graph matching the given property definition and name...
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:194
NodePtr getNode(const string &name) const
Return the Node, if any, with the given name.
Definition: Node.h:213
BackdropPtr getBackdrop(const string &name) const
Return the Backdrop, if any, with the given name.
Definition: Node.h:270
void removeNode(const string &name)
Remove the Node, if any, with the given name.
Definition: Node.h:240
void removeBackdrop(const string &name)
Remove the Backdrop, if any, with the given name.
Definition: Node.h:282
vector< NodePtr > getNodesOfType(const string &nodeType) const
Return a vector of nodes in the graph which have a given type.
Definition: Node.h:226
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:312
size_t getInputCount() const
Return the number of Input elements.
Definition: Interface.h:381
A node graph element within a Document.
Definition: Node.h:322
vector< PortElementPtr > getDownstreamPorts() const
Return a vector of all downstream ports that connect to this graph, ordered by the names of the port ...
void removeInterfaceName(const string &inputPath)
Remove an interface name from an existing NodeDef associated with this NodeGraph.
InputPtr addInterfaceName(const string &inputPath, const string &interfaceName)
Add an interface name to an existing NodeDef associated with this NodeGraph.
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
vector< OutputPtr > getMaterialOutputs() const
Return all material-type outputs of the nodegraph.
void modifyInterfaceName(const string &inputPath, const string &interfaceName)
Modify the interface name on an existing NodeDef associated with this NodeGraph.
InterfaceElementPtr getImplementation() const
Return the first implementation for this node graph.
NodeDefPtr getNodeDef() const
Return the NodeDef element referenced by this NodeGraph.
void setNodeDef(ConstNodeDefPtr nodeDef)
Set the NodeDef element referenced by this NodeGraph.
ConstInterfaceElementPtr getDeclaration(const string &target=EMPTY_STRING) const override
Return the first declaration of this interface, optionally filtered by the given target name.
A node element within a NodeGraph or Document.
Definition: Node.h:53
vector< PortElementPtr > getDownstreamPorts() const
Return a vector of all downstream ports that connect to this node, ordered by the names of the port e...
Edge getUpstreamEdge(size_t index=0) const override
Return the Edge with the given index that lies directly upstream from this element in the dataflow gr...
void setConnectedNodeName(const string &inputName, const string &nodeName)
Set the name of the Node connected to the given input, creating a child element for the input if need...
OutputPtr getNodeDefOutput(ElementPtr connectingElement)
Given a connecting element (Input or Output) return the NodeDef output corresponding to the output th...
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
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:108
size_t getUpstreamEdgeCount() const override
Return the number of queriable upstream edges for this element.
Definition: Node.h:123
NodePtr getConnectedNode(const string &inputName) const
Return the Node connected to the given input.
void addInputsFromNodeDef()
Add inputs based on the corresponding associated node definition.
NodeDefPtr getNodeDef(const string &target=EMPTY_STRING, bool allowRoughMatch=false) const
Return the first NodeDef that declares this node, optionally filtered by the given target name.
string getConnectedNodeName(const string &inputName) const
Return the name of the Node connected to the given input.
void setConnectedNode(const string &inputName, ConstNodePtr node)
Set the node to which the given input is connected, creating a child input if needed.
InputPtr addInputFromNodeDef(const string &inputName)
Add an input based on the corresponding input for the associated node definition.
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:145