MaterialX 1.39.0
Loading...
Searching...
No Matches
Interface.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_INTERFACE_H
7#define MATERIALX_INTERFACE_H
8
11
13
14#include <MaterialXCore/Geom.h>
15
16MATERIALX_NAMESPACE_BEGIN
17
18class PortElement;
19class Input;
20class Output;
22class Node;
23class NodeDef;
24
26using PortElementPtr = shared_ptr<PortElement>;
28using ConstPortElementPtr = shared_ptr<const PortElement>;
29
31using InputPtr = shared_ptr<Input>;
33using ConstInputPtr = shared_ptr<const Input>;
34
36using OutputPtr = shared_ptr<Output>;
38using ConstOutputPtr = shared_ptr<const Output>;
39
41using InterfaceElementPtr = shared_ptr<InterfaceElement>;
43using ConstInterfaceElementPtr = shared_ptr<const InterfaceElement>;
44
45using CharSet = std::set<char>;
46
51class MX_CORE_API PortElement : public ValueElement
52{
53 protected:
54 PortElement(ElementPtr parent, const string& category, const string& name) :
55 ValueElement(parent, category, name)
56 {
57 }
58
59 public:
60 virtual ~PortElement() { }
61
62 protected:
63 using NodePtr = shared_ptr<Node>;
64 using ConstNodePtr = shared_ptr<const Node>;
65
66 public:
69
72 void setNodeName(const string& node)
73 {
74 setAttribute(NODE_NAME_ATTRIBUTE, node);
75 }
76
78 bool hasNodeName() const
79 {
80 return hasAttribute(NODE_NAME_ATTRIBUTE);
81 }
82
84 const string& getNodeName() const
85 {
86 return getAttribute(NODE_NAME_ATTRIBUTE);
87 }
88
92
94 void setNodeGraphString(const string& node)
95 {
96 setAttribute(NODE_GRAPH_ATTRIBUTE, node);
97 }
98
101 {
102 return hasAttribute(NODE_GRAPH_ATTRIBUTE);
103 }
104
106 const string& getNodeGraphString() const
107 {
108 return getAttribute(NODE_GRAPH_ATTRIBUTE);
109 }
110
114
116 void setOutputString(const string& output)
117 {
118 setAttribute(OUTPUT_ATTRIBUTE, output);
119 }
120
122 bool hasOutputString() const
123 {
124 return hasAttribute(OUTPUT_ATTRIBUTE);
125 }
126
130
133
135 const string& getOutputString() const
136 {
137 return getAttribute(OUTPUT_ATTRIBUTE);
138 }
139
143
147 void setConnectedNode(ConstNodePtr node);
148
150 virtual NodePtr getConnectedNode() const;
151
155
158 bool validate(string* message = nullptr) const override;
159
161
162 public:
163 static const string NODE_NAME_ATTRIBUTE;
164 static const string NODE_GRAPH_ATTRIBUTE;
165 static const string OUTPUT_ATTRIBUTE;
166};
167
173class MX_CORE_API Input : public PortElement
174{
175 public:
176 Input(ElementPtr parent, const string& name) :
177 PortElement(parent, CATEGORY, name)
178 {
179 }
180 virtual ~Input() { }
181
182 public:
185
187 void setDefaultGeomPropString(const string& geomprop)
188 {
189 setAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE, geomprop);
190 }
191
194 {
195 return hasAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE);
196 }
197
199 const string& getDefaultGeomPropString() const
200 {
201 return getAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE);
202 }
203
206
210
212 NodePtr getConnectedNode() const override;
213
216 void setConnectedInterfaceName(const string& interfaceName);
217
221
225
228 bool validate(string* message = nullptr) const override;
229
231
232 public:
233 static const string CATEGORY;
234 static const string DEFAULT_GEOM_PROP_ATTRIBUTE;
235};
236
239class MX_CORE_API Output : public PortElement
240{
241 public:
242 Output(ElementPtr parent, const string& name) :
243 PortElement(parent, CATEGORY, name)
244 {
245 }
246 virtual ~Output() { }
247
248 public:
251
254 Edge getUpstreamEdge(size_t index = 0) const override;
255
257 size_t getUpstreamEdgeCount() const override
258 {
259 return 1;
260 }
261
263 bool hasUpstreamCycle() const;
264
268
271 bool validate(string* message = nullptr) const override;
272
274
275 public:
276 static const string CATEGORY;
277 static const string DEFAULT_INPUT_ATTRIBUTE;
278};
279
285class MX_CORE_API InterfaceElement : public TypedElement
286{
287 protected:
288 InterfaceElement(ElementPtr parent, const string& category, const string& name) :
289 TypedElement(parent, category, name),
290 _inputCount(0),
291 _outputCount(0)
292 {
293 }
294
295 public:
296 virtual ~InterfaceElement() { }
297
298 protected:
299 using NodeDefPtr = shared_ptr<NodeDef>;
300 using ConstNodeDefPtr = shared_ptr<const NodeDef>;
301
302 public:
305
307 void setNodeDefString(const string& nodeDef)
308 {
309 setAttribute(NODE_DEF_ATTRIBUTE, nodeDef);
310 }
311
313 bool hasNodeDefString() const
314 {
315 return hasAttribute(NODE_DEF_ATTRIBUTE);
316 }
317
319 const string& getNodeDefString() const
320 {
321 return getAttribute(NODE_DEF_ATTRIBUTE);
322 }
323
327
334 InputPtr addInput(const string& name = EMPTY_STRING,
335 const string& type = DEFAULT_TYPE_STRING)
336 {
337 InputPtr child = addChild<Input>(name);
338 child->setType(type);
339 return child;
340 }
341
343 InputPtr getInput(const string& name) const
344 {
345 return getChildOfType<Input>(name);
346 }
347
349 vector<InputPtr> getInputs() const
350 {
351 return getChildrenOfType<Input>();
352 }
353
355 size_t getInputCount() const
356 {
357 return _inputCount;
358 }
359
361 void removeInput(const string& name)
362 {
363 removeChildOfType<Input>(name);
364 }
365
368 InputPtr getActiveInput(const string& name) const;
369
372 vector<InputPtr> getActiveInputs() const;
373
377
384 OutputPtr addOutput(const string& name = EMPTY_STRING,
385 const string& type = DEFAULT_TYPE_STRING)
386 {
387 OutputPtr output = addChild<Output>(name);
388 output->setType(type);
389 return output;
390 }
391
393 OutputPtr getOutput(const string& name) const
394 {
395 return getChildOfType<Output>(name);
396 }
397
399 vector<OutputPtr> getOutputs() const
400 {
401 return getChildrenOfType<Output>();
402 }
403
405 size_t getOutputCount() const
406 {
407 return _outputCount;
408 }
409
411 void removeOutput(const string& name)
412 {
413 removeChildOfType<Output>(name);
414 }
415
418 OutputPtr getActiveOutput(const string& name) const;
419
422 vector<OutputPtr> getActiveOutputs() const;
423
427 void setConnectedOutput(const string& inputName, OutputPtr output);
428
431 OutputPtr getConnectedOutput(const string& inputName) const;
432
436
442 TokenPtr addToken(const string& name = EMPTY_STRING)
443 {
444 return addChild<Token>(name);
445 }
446
448 TokenPtr getToken(const string& name) const
449 {
450 return getChildOfType<Token>(name);
451 }
452
454 vector<TokenPtr> getTokens() const
455 {
456 return getChildrenOfType<Token>();
457 }
458
460 void removeToken(const string& name)
461 {
462 removeChildOfType<Token>(name);
463 }
464
467 TokenPtr getActiveToken(const string& name) const;
468
471 vector<TokenPtr> getActiveTokens() const;
472
476
478 ValueElementPtr getValueElement(const string& name) const
479 {
480 return getChildOfType<ValueElement>(name);
481 }
482
486 ValueElementPtr getActiveValueElement(const string& name) const;
487
491 vector<ValueElementPtr> getActiveValueElements() const;
492
496
499 template <class T> InputPtr setInputValue(const string& name,
500 const T& value,
501 const string& type = EMPTY_STRING);
502
511 ValuePtr getInputValue(const string& name, const string& target = EMPTY_STRING) const;
512
515 TokenPtr setTokenValue(const string& name, const string& value)
516 {
517 TokenPtr token = getToken(name);
518 if (!token)
519 token = addToken(name);
520 token->setValue<string>(value);
521 return token;
522 }
523
526 string getTokenValue(const string& name)
527 {
528 TokenPtr token = getToken(name);
529 return token ? token->getValueString() : EMPTY_STRING;
530 }
531
535
537 void setTarget(const string& target)
538 {
539 setAttribute(TARGET_ATTRIBUTE, target);
540 }
541
543 bool hasTarget() const
544 {
545 return hasAttribute(TARGET_ATTRIBUTE);
546 }
547
549 const string& getTarget() const
550 {
551 return getAttribute(TARGET_ATTRIBUTE);
552 }
553
557
559 void setVersionString(const string& version)
560 {
561 setAttribute(VERSION_ATTRIBUTE, version);
562 }
563
565 bool hasVersionString() const
566 {
567 return hasAttribute(VERSION_ATTRIBUTE);
568 }
569
571 const string& getVersionString() const
572 {
573 return getAttribute(VERSION_ATTRIBUTE);
574 }
575
577 void setVersionIntegers(int majorVersion, int minorVersion);
578
580 virtual std::pair<int, int> getVersionIntegers() const;
581
585
587 void setDefaultVersion(bool defaultVersion)
588 {
589 setTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE, defaultVersion);
590 }
591
593 bool getDefaultVersion() const
594 {
595 return getTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE);
596 }
597
601
608 virtual ConstInterfaceElementPtr getDeclaration(const string& target = EMPTY_STRING) const;
609
611 void clearContent() override;
612
619 bool hasExactInputMatch(ConstInterfaceElementPtr declaration, string* message = nullptr) const;
620
622
623 public:
624 static const string NODE_DEF_ATTRIBUTE;
625 static const string TARGET_ATTRIBUTE;
626 static const string VERSION_ATTRIBUTE;
627 static const string DEFAULT_VERSION_ATTRIBUTE;
628
629 protected:
630 void registerChildElement(ElementPtr child) override;
631 void unregisterChildElement(ElementPtr child) override;
632
633 private:
634 size_t _inputCount;
635 size_t _outputCount;
636};
637
638template <class T> InputPtr InterfaceElement::setInputValue(const string& name,
639 const T& value,
640 const string& type)
641{
642 InputPtr input = getChildOfType<Input>(name);
643 if (!input)
644 input = addInput(name);
645 input->setValue(value, type);
646 return input;
647}
648
649MATERIALX_NAMESPACE_END
650
651#endif
shared_ptr< Token > TokenPtr
A shared pointer to a Token.
Definition: Element.h:46
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
Definition: Element.h:41
Geometric element subclasses.
shared_ptr< GeomPropDef > GeomPropDefPtr
A shared pointer to a GeomPropDef.
Definition: Geom.h:48
shared_ptr< const PortElement > ConstPortElementPtr
A shared pointer to a const PortElement.
Definition: Interface.h:28
shared_ptr< const Output > ConstOutputPtr
A shared pointer to a const Output.
Definition: Interface.h:38
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:41
shared_ptr< PortElement > PortElementPtr
A shared pointer to a PortElement.
Definition: Interface.h:26
shared_ptr< const InterfaceElement > ConstInterfaceElementPtr
A shared pointer to a const InterfaceElement.
Definition: Interface.h:43
shared_ptr< const Input > ConstInputPtr
A shared pointer to a const Input.
Definition: Interface.h:33
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< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:29
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: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
An input element within a Node or NodeDef.
Definition: Interface.h:174
GeomPropDefPtr getDefaultGeomProp() const
Return the GeomPropDef element to use, if defined for this input.
const string & getDefaultGeomPropString() const
Return the defaultgeomprop string for the input.
Definition: Interface.h:199
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
void setDefaultGeomPropString(const string &geomprop)
Set the defaultgeomprop string for the input.
Definition: Interface.h:187
InputPtr getInterfaceInput() const
Return the input on the parent graph corresponding to the interface name for this input.
bool hasDefaultGeomPropString() const
Return true if the given input has a defaultgeomprop string.
Definition: Interface.h:193
NodePtr getConnectedNode() const override
Return the node, if any, to which this input is connected.
void setConnectedInterfaceName(const string &interfaceName)
Connects this input to a corresponding interface with the given name.
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:286
OutputPtr addOutput(const string &name=EMPTY_STRING, const string &type=DEFAULT_TYPE_STRING)
Add an Output to this interface.
Definition: Interface.h:384
ValuePtr getInputValue(const string &name, const string &target=EMPTY_STRING) const
Return the typed value of an input by its name, taking both the calling element and its declaration i...
OutputPtr getOutput(const string &name) const
Return the Output, if any, with the given name.
Definition: Interface.h:393
bool hasNodeDefString() const
Return true if the given interface has a NodeDef string.
Definition: Interface.h:313
TokenPtr getToken(const string &name) const
Return the Token, if any, with the given name.
Definition: Interface.h:448
vector< InputPtr > getInputs() const
Return a vector of all Input elements.
Definition: Interface.h:349
InputPtr addInput(const string &name=EMPTY_STRING, const string &type=DEFAULT_TYPE_STRING)
Add an Input to this interface.
Definition: Interface.h:334
size_t getInputCount() const
Return the number of Input elements.
Definition: Interface.h:355
void removeInput(const string &name)
Remove the Input, if any, with the given name.
Definition: Interface.h:361
void setVersionString(const string &version)
Set the version string of this interface.
Definition: Interface.h:559
virtual std::pair< int, int > getVersionIntegers() const
Return the major and minor versions as an integer pair.
vector< TokenPtr > getTokens() const
Return a vector of all Token elements.
Definition: Interface.h:454
vector< ValueElementPtr > getActiveValueElements() const
Return a vector of all value elements that belong to this interface, taking inheritance into account.
InputPtr getInput(const string &name) const
Return the Input, if any, with the given name.
Definition: Interface.h:343
const string & getVersionString() const
Return the version string of this interface.
Definition: Interface.h:571
vector< OutputPtr > getActiveOutputs() const
Return a vector of all Output elements that belong to this interface, taking inheritance into account...
void setVersionIntegers(int majorVersion, int minorVersion)
Set the major and minor versions as an integer pair.
ValueElementPtr getValueElement(const string &name) const
Return the ValueElement, if any, with the given name.
Definition: Interface.h:478
void clearContent() override
Clear all attributes and descendants from this element.
TokenPtr getActiveToken(const string &name) const
Return the first Token with the given name that belongs to this interface, taking interface inheritan...
OutputPtr getConnectedOutput(const string &inputName) const
Return the output connected to the given input.
TokenPtr setTokenValue(const string &name, const string &value)
Set the string value of a Token by its name, creating a child element to hold the Token if needed.
Definition: Interface.h:515
OutputPtr getActiveOutput(const string &name) const
Return the first Output with the given name that belongs to this interface, taking interface inherita...
InputPtr setInputValue(const string &name, const T &value, const string &type=EMPTY_STRING)
Set the typed value of an input by its name, creating a child element to hold the input if needed.
Definition: Interface.h:638
TokenPtr addToken(const string &name=EMPTY_STRING)
Add a Token to this interface.
Definition: Interface.h:442
void setNodeDefString(const string &nodeDef)
Set the NodeDef string for the interface.
Definition: Interface.h:307
ValueElementPtr getActiveValueElement(const string &name) const
Return the first value element with the given name that belongs to this interface,...
vector< InputPtr > getActiveInputs() const
Return a vector of all Input elements that belong to this interface, taking inheritance into account.
bool hasExactInputMatch(ConstInterfaceElementPtr declaration, string *message=nullptr) const
Return true if this instance has an exact input match with the given declaration, where each input of...
vector< OutputPtr > getOutputs() const
Return a vector of all Output elements.
Definition: Interface.h:399
string getTokenValue(const string &name)
Return the string value of a Token by its name, or an empty string if the given Token is not present.
Definition: Interface.h:526
virtual ConstInterfaceElementPtr getDeclaration(const string &target=EMPTY_STRING) const
Return the first declaration of this interface, optionally filtered by the given target name.
void setConnectedOutput(const string &inputName, OutputPtr output)
Set the output to which the given input is connected, creating a child input if needed.
bool hasVersionString() const
Return true if this interface has a version string.
Definition: Interface.h:565
bool getDefaultVersion() const
Return the default version flag of this element.
Definition: Interface.h:593
const string & getNodeDefString() const
Return the NodeDef string for the interface.
Definition: Interface.h:319
void removeToken(const string &name)
Remove the Token, if any, with the given name.
Definition: Interface.h:460
void removeOutput(const string &name)
Remove the Output, if any, with the given name.
Definition: Interface.h:411
void setTarget(const string &target)
Set the target string of this interface.
Definition: Interface.h:537
InputPtr getActiveInput(const string &name) const
Return the first Input with the given name that belongs to this interface, taking interface inheritan...
void setDefaultVersion(bool defaultVersion)
Set the default version flag of this element.
Definition: Interface.h:587
bool hasTarget() const
Return true if the given interface has a target string.
Definition: Interface.h:543
size_t getOutputCount() const
Return the number of Output elements.
Definition: Interface.h:405
const string & getTarget() const
Return the target string of this interface.
Definition: Interface.h:549
vector< TokenPtr > getActiveTokens() const
Return a vector of all Token elements that belong to this interface, taking inheritance into account.
A node definition element within a Document.
Definition: Definition.h:82
A node element within a NodeGraph or Document.
Definition: Node.h:53
A spatially-varying output element within a NodeGraph or NodeDef.
Definition: Interface.h:240
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...
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
size_t getUpstreamEdgeCount() const override
Return the number of queriable upstream edges for this element.
Definition: Interface.h:257
bool hasUpstreamCycle() const
Return true if a cycle exists in any upstream path from this element.
The base class for port elements such as Input and Output.
Definition: Interface.h:52
bool hasOutputString() const
Return true if this element has an output string.
Definition: Interface.h:122
const string & getNodeGraphString() const
Return the node graph string of this element.
Definition: Interface.h:106
const string & getOutputString() const
Return the output string of this element.
Definition: Interface.h:135
virtual OutputPtr getConnectedOutput() const
Return the output, if any, to which this input is connected.
virtual NodePtr getConnectedNode() const
Return the node, if any, to which this element is connected.
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
void setNodeGraphString(const string &node)
Set the node graph string of this element.
Definition: Interface.h:94
bool hasNodeGraphString() const
Return true if this element has a node graph string.
Definition: Interface.h:100
void setConnectedOutput(ConstOutputPtr output)
Set the output to which this input is connected.
void setNodeName(const string &node)
Set the node name string of this element, creating a connection to the Node with the given name withi...
Definition: Interface.h:72
void setConnectedNode(ConstNodePtr node)
Set the node to which this element is connected.
void setOutputString(const string &output)
Set the output string of this element.
Definition: Interface.h:116
const string & getNodeName() const
Return the node name string of this element.
Definition: Interface.h:84
bool hasNodeName() const
Return true if this element has a node name string.
Definition: Interface.h:78
The base class for typed elements.
Definition: Element.h:854
The base class for elements that support typed values.
Definition: Element.h:918