MaterialX 1.39.4
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
68 void setNameGlobal(const string& name);
69
73
77 void setConnectedNode(const string& inputName, ConstNodePtr node);
78
81 NodePtr getConnectedNode(const string& inputName) const;
82
85 void setConnectedNodeName(const string& inputName, const string& nodeName);
86
89 string getConnectedNodeName(const string& inputName) const;
90
94
104 NodeDefPtr getNodeDef(const string& target = EMPTY_STRING,
105 bool allowRoughMatch = false) const;
106
110
118 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const
119 {
120 NodeDefPtr nodeDef = getNodeDef(target);
121 return nodeDef ? nodeDef->getImplementation(target) : InterfaceElementPtr();
122 }
123
127
130 Edge getUpstreamEdge(size_t index = 0) const override;
131
133 size_t getUpstreamEdgeCount() const override
134 {
135 return getInputCount();
136 }
137
144
147 vector<PortElementPtr> getDownstreamPorts() const;
148
152
155 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override
156 {
157 return getNodeDef(target);
158 }
159
162 InputPtr addInputFromNodeDef(const string& inputName);
163
166
170
173 bool validate(string* message = nullptr) const override;
174
176
177 public:
178 static const string CATEGORY;
179};
180
183class MX_CORE_API GraphElement : public InterfaceElement
184{
185 protected:
186 GraphElement(ElementPtr parent, const string& category, const string& name) :
187 InterfaceElement(parent, category, name)
188 {
189 }
190
191 public:
192 virtual ~GraphElement() { }
193
196
204 NodePtr addNode(const string& category,
205 const string& name = EMPTY_STRING,
206 const string& type = DEFAULT_TYPE_STRING)
207 {
208 NodePtr node = addChild<Node>(name);
209 node->setCategory(category);
210 node->setType(type);
211 return node;
212 }
213
215 NodePtr addNodeInstance(ConstNodeDefPtr nodeDef, const string& name = EMPTY_STRING)
216 {
217 NodePtr node = addNode(nodeDef->getNodeString(), name, nodeDef->getType());
218 node->setNodeDefString(nodeDef->getName());
219 return node;
220 }
221
223 NodePtr getNode(const string& name) const
224 {
225 return getChildOfType<Node>(name);
226 }
227
230 vector<NodePtr> getNodes(const string& category = EMPTY_STRING) const
231 {
232 return getChildrenOfType<Node>(category);
233 }
234
236 vector<NodePtr> getNodesOfType(const string& nodeType) const
237 {
238 vector<NodePtr> nodes;
239 for (auto node : getNodes())
240 {
241 if (node->getType() == nodeType)
242 {
243 nodes.push_back(node);
244 }
245 }
246 return nodes;
247 }
248
250 void removeNode(const string& name)
251 {
253 }
254
258
261 NodePtr addMaterialNode(const string& name = EMPTY_STRING, ConstNodePtr shaderNode = nullptr);
262
264 vector<NodePtr> getMaterialNodes() const
265 {
266 return getNodesOfType(MATERIAL_TYPE_STRING);
267 }
268
272
274 BackdropPtr addBackdrop(const string& name = EMPTY_STRING)
275 {
276 return addChild<Backdrop>(name);
277 }
278
280 BackdropPtr getBackdrop(const string& name) const
281 {
282 return getChildOfType<Backdrop>(name);
283 }
284
286 vector<BackdropPtr> getBackdrops() const
287 {
289 }
290
292 void removeBackdrop(const string& name)
293 {
295 }
296
300
308 void flattenSubgraphs(const string& target = EMPTY_STRING, NodePredicate filter = nullptr);
309
313
316 NodePtr addGeomNode(ConstGeomPropDefPtr geomPropDef, const string& namePrefix);
317
324 string asStringDot() const;
325
327};
328
331class MX_CORE_API NodeGraph : public GraphElement
332{
333 public:
334 NodeGraph(ElementPtr parent, const string& name) :
335 GraphElement(parent, CATEGORY, name)
336 {
337 }
338 virtual ~NodeGraph() { }
339
342
347 void setNameGlobal(const string& name);
348
352
354 vector<OutputPtr> getMaterialOutputs() const;
355
359
361 void setNodeDef(ConstNodeDefPtr nodeDef);
362
364 NodeDefPtr getNodeDef() const;
365
370
374
377 vector<PortElementPtr> getDownstreamPorts() const;
378
382
385 ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const override;
386
391 InputPtr addInterfaceName(const string& inputPath, const string& interfaceName);
392
395 void removeInterfaceName(const string& inputPath);
396
400 void modifyInterfaceName(const string& inputPath, const string& interfaceName);
401
405
408 bool validate(string* message = nullptr) const override;
409
411
412 public:
413 static const string CATEGORY;
414};
415
418class MX_CORE_API Backdrop : public Element
419{
420 public:
421 Backdrop(ElementPtr parent, const string& name) :
422 Element(parent, CATEGORY, name)
423 {
424 }
425 virtual ~Backdrop() { }
426
429
431 void setContainsString(const string& contains)
432 {
433 setAttribute(CONTAINS_ATTRIBUTE, contains);
434 }
435
437 bool hasContainsString() const
438 {
439 return hasAttribute(CONTAINS_ATTRIBUTE);
440 }
441
443 string getContainsString() const
444 {
445 return getAttribute(CONTAINS_ATTRIBUTE);
446 }
447
451
453 void setWidth(float width)
454 {
455 setTypedAttribute<float>(WIDTH_ATTRIBUTE, width);
456 }
457
459 bool hasWidth() const
460 {
461 return hasAttribute(WIDTH_ATTRIBUTE);
462 }
463
465 float getWidth() const
466 {
467 return getTypedAttribute<float>(WIDTH_ATTRIBUTE);
468 }
469
473
475 void setHeight(float height)
476 {
477 setTypedAttribute<float>(HEIGHT_ATTRIBUTE, height);
478 }
479
481 bool hasHeight() const
482 {
483 return hasAttribute(HEIGHT_ATTRIBUTE);
484 }
485
487 float getHeight() const
488 {
489 return getTypedAttribute<float>(HEIGHT_ATTRIBUTE);
490 }
491
495
497 void setContainsElements(const vector<ConstTypedElementPtr>& nodes);
498
500 vector<TypedElementPtr> getContainsElements() const;
501
505
508 bool validate(string* message = nullptr) const override;
509
511
512 public:
513 static const string CATEGORY;
514 static const string CONTAINS_ATTRIBUTE;
515 static const string WIDTH_ATTRIBUTE;
516 static const string HEIGHT_ATTRIBUTE;
517};
518
519MATERIALX_NAMESPACE_END
520
521#endif
Definition element subclasses.
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition Element.h:31
vector< ElementPtr > ElementVec
A vector of elements.
Definition Element.h:69
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:419
void setContainsString(const string &contains)
Set the contains string for this backdrop.
Definition Node.h:431
void setHeight(float height)
Set the height attribute of the backdrop.
Definition Node.h:475
void setWidth(float width)
Set the width attribute of the backdrop.
Definition Node.h:453
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:481
float getWidth() const
Return the width attribute of the backdrop.
Definition Node.h:465
string getContainsString() const
Return the contains string for this backdrop.
Definition Node.h:443
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:487
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:437
bool hasWidth() const
Return true if this backdrop has a width attribute.
Definition Node.h:459
An edge between two connected Elements, returned during graph traversal.
Definition Traversal.h:30
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition Element.h:492
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Add a child element of the given subclass and name.
Definition Element.h:1402
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
void setTypedAttribute(const string &attrib, const T &data)
Set the value of an implicitly typed attribute.
Definition Element.h:507
T getTypedAttribute(const string &attrib) const
Return the value of an implicitly typed attribute.
Definition Element.h:515
shared_ptr< T > getChildOfType(const string &name) const
Return the child element, if any, with the given name and subclass.
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition Element.h:485
void removeChildOfType(const string &name)
Remove the child element, if any, with the given name and subclass.
Definition Element.h:471
vector< shared_ptr< T > > getChildrenOfType(const string &category=EMPTY_STRING) const
Return a vector of all child elements that are instances of the given subclass, optionally filtered b...
The base class for graph elements such as NodeGraph and Document.
Definition Node.h:184
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:230
vector< BackdropPtr > getBackdrops() const
Return a vector of all Backdrop elements in the graph.
Definition Node.h:286
BackdropPtr addBackdrop(const string &name=EMPTY_STRING)
Add a Backdrop to the graph.
Definition Node.h:274
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:215
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:264
ElementVec 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:204
NodePtr getNode(const string &name) const
Return the Node, if any, with the given name.
Definition Node.h:223
BackdropPtr getBackdrop(const string &name) const
Return the Backdrop, if any, with the given name.
Definition Node.h:280
void removeNode(const string &name)
Remove the Node, if any, with the given name.
Definition Node.h:250
void removeBackdrop(const string &name)
Remove the Backdrop, if any, with the given name.
Definition Node.h:292
vector< NodePtr > getNodesOfType(const string &nodeType) const
Return a vector of nodes in the graph which have a given type.
Definition Node.h:236
size_t getInputCount() const
Return the number of Input elements.
Definition Interface.h:381
A node graph element within a Document.
Definition Node.h:332
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...
void setNameGlobal(const string &name)
Set the name string of this NodeGraph, propagating the updated name to all downstream ports.
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:118
size_t getUpstreamEdgeCount() const override
Return the number of queryable upstream edges for this element.
Definition Node.h:133
void setNameGlobal(const string &name)
Set the name string of this Node, propagating the updated name to all downstream ports.
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:155