MaterialX 1.38.2
Interface.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_INTERFACE_H
7#define MATERIALX_INTERFACE_H
8
11
13
14#include <MaterialXCore/Geom.h>
15
16namespace MaterialX
17{
18
19class PortElement;
20class Input;
21class Output;
22class InterfaceElement;
23class Node;
24class NodeDef;
25
27using PortElementPtr = shared_ptr<PortElement>;
29using ConstPortElementPtr = shared_ptr<const PortElement>;
30
32using InputPtr = shared_ptr<Input>;
34using ConstInputPtr = shared_ptr<const Input>;
35
37using OutputPtr = shared_ptr<Output>;
39using ConstOutputPtr = shared_ptr<const Output>;
40
42using InterfaceElementPtr = shared_ptr<InterfaceElement>;
44using ConstInterfaceElementPtr = shared_ptr<const InterfaceElement>;
45
46using CharSet = std::set<char>;
47
52class MX_CORE_API PortElement : public ValueElement
53{
54 protected:
55 PortElement(ElementPtr parent, const string& category, const string& name) :
56 ValueElement(parent, category, name)
57 {
58 }
59 public:
60 virtual ~PortElement() { }
61
62 protected:
63 using NodePtr = shared_ptr<Node>;
64
65 public:
68
71 void setNodeName(const string& node)
72 {
73 setAttribute(NODE_NAME_ATTRIBUTE, node);
74 }
75
77 bool hasNodeName() const
78 {
79 return hasAttribute(NODE_NAME_ATTRIBUTE);
80 }
81
83 const string& getNodeName() const
84 {
85 return getAttribute(NODE_NAME_ATTRIBUTE);
86 }
87
91
93 void setNodeGraphString(const string& node)
94 {
95 setAttribute(NODE_GRAPH_ATTRIBUTE, node);
96 }
97
99 bool hasNodeGraphString() const
100 {
101 return hasAttribute(NODE_GRAPH_ATTRIBUTE);
102 }
103
105 const string& getNodeGraphString() const
106 {
107 return getAttribute(NODE_GRAPH_ATTRIBUTE);
108 }
109
113
115 void setOutputString(const string& output)
116 {
117 setAttribute(OUTPUT_ATTRIBUTE, output);
118 }
119
121 bool hasOutputString() const
122 {
123 return hasAttribute(OUTPUT_ATTRIBUTE);
124 }
125
127 const string& getOutputString() const
128 {
129 return getAttribute(OUTPUT_ATTRIBUTE);
130 }
131
135
138 void setChannels(const string& channels)
139 {
140 setAttribute(CHANNELS_ATTRIBUTE, channels);
141 }
142
144 bool hasChannels() const
145 {
146 return hasAttribute(CHANNELS_ATTRIBUTE);
147 }
148
150 const string& getChannels() const
151 {
152 return getAttribute(CHANNELS_ATTRIBUTE);
153 }
154
157 static bool validChannelsCharacters(const string &channels, const string &sourceType);
158
161 static bool validChannelsString(const string& channels, const string& sourceType, const string& destinationType);
162
166
170 void setConnectedNode(NodePtr node);
171
173 virtual NodePtr getConnectedNode() const;
174
178
181 bool validate(string* message = nullptr) const override;
182
184
185 public:
186 static const string NODE_NAME_ATTRIBUTE;
187 static const string NODE_GRAPH_ATTRIBUTE;
188 static const string OUTPUT_ATTRIBUTE;
189 static const string CHANNELS_ATTRIBUTE;
190
191 private:
192 static const std::unordered_map<string, CharSet> CHANNELS_CHARACTER_SET;
193 static const std::unordered_map<string, size_t> CHANNELS_PATTERN_LENGTH;
194};
195
201class MX_CORE_API Input : public PortElement
202{
203 public:
204 Input(ElementPtr parent, const string& name) :
205 PortElement(parent, CATEGORY, name)
206 {
207 }
208 virtual ~Input() { }
209
210 public:
213
215 void setDefaultGeomPropString(const string& geomprop)
216 {
217 setAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE, geomprop);
218 }
219
222 {
223 return hasAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE);
224 }
225
227 const string& getDefaultGeomPropString() const
228 {
229 return getAttribute(DEFAULT_GEOM_PROP_ATTRIBUTE);
230 }
231
233 GeomPropDefPtr getDefaultGeomProp() const;
234
238
240 NodePtr getConnectedNode() const override;
241
244 void setConnectedOutput(ConstOutputPtr output);
245
247 virtual OutputPtr getConnectedOutput() const;
248
251 InputPtr getInterfaceInput() const;
252
256
259 bool validate(string* message = nullptr) const override;
260
262
263 public:
264 static const string CATEGORY;
265 static const string DEFAULT_GEOM_PROP_ATTRIBUTE;
266};
267
270class MX_CORE_API Output : public PortElement
271{
272 public:
273 Output(ElementPtr parent, const string& name) :
274 PortElement(parent, CATEGORY, name)
275 {
276 }
277 virtual ~Output() { }
278
279 public:
282
285 Edge getUpstreamEdge(size_t index = 0) const override;
286
288 size_t getUpstreamEdgeCount() const override
289 {
290 return 1;
291 }
292
294 bool hasUpstreamCycle() const;
295
299
302 bool validate(string* message = nullptr) const override;
303
305
306 public:
307 static const string CATEGORY;
308 static const string DEFAULT_INPUT_ATTRIBUTE;
309};
310
316class MX_CORE_API InterfaceElement : public TypedElement
317{
318 protected:
319 InterfaceElement(ElementPtr parent, const string& category, const string& name) :
320 TypedElement(parent, category, name),
321 _inputCount(0),
322 _outputCount(0)
323 {
324 }
325 public:
326 virtual ~InterfaceElement() { }
327
328 protected:
329 using NodeDefPtr = shared_ptr<NodeDef>;
330 using ConstNodeDefPtr = shared_ptr<const NodeDef>;
331
332 public:
335
337 void setNodeDefString(const string& nodeDef)
338 {
339 setAttribute(NODE_DEF_ATTRIBUTE, nodeDef);
340 }
341
343 bool hasNodeDefString() const
344 {
345 return hasAttribute(NODE_DEF_ATTRIBUTE);
346 }
347
349 const string& getNodeDefString() const
350 {
351 return getAttribute(NODE_DEF_ATTRIBUTE);
352 }
353
357
364 InputPtr addInput(const string& name = EMPTY_STRING,
365 const string& type = DEFAULT_TYPE_STRING)
366 {
367 InputPtr child = addChild<Input>(name);
368 child->setType(type);
369 return child;
370 }
371
373 InputPtr getInput(const string& name) const
374 {
375 return getChildOfType<Input>(name);
376 }
377
379 vector<InputPtr> getInputs() const
380 {
381 return getChildrenOfType<Input>();
382 }
383
385 size_t getInputCount() const
386 {
387 return _inputCount;
388 }
389
391 void removeInput(const string& name)
392 {
393 removeChildOfType<Input>(name);
394 }
395
398 InputPtr getActiveInput(const string& name) const;
399
402 vector<InputPtr> getActiveInputs() const;
403
407
414 OutputPtr addOutput(const string& name = EMPTY_STRING,
415 const string& type = DEFAULT_TYPE_STRING)
416 {
417 OutputPtr output = addChild<Output>(name);
418 output->setType(type);
419 return output;
420 }
421
423 OutputPtr getOutput(const string& name) const
424 {
425 return getChildOfType<Output>(name);
426 }
427
429 vector<OutputPtr> getOutputs() const
430 {
431 return getChildrenOfType<Output>();
432 }
433
435 size_t getOutputCount() const
436 {
437 return _outputCount;
438 }
439
441 void removeOutput(const string& name)
442 {
443 removeChildOfType<Output>(name);
444 }
445
448 OutputPtr getActiveOutput(const string& name) const;
449
452 vector<OutputPtr> getActiveOutputs() const;
453
457 void setConnectedOutput(const string& inputName, OutputPtr output);
458
461 OutputPtr getConnectedOutput(const string& inputName) const;
462
466
472 TokenPtr addToken(const string& name = EMPTY_STRING)
473 {
474 return addChild<Token>(name);
475 }
476
478 TokenPtr getToken(const string& name) const
479 {
480 return getChildOfType<Token>(name);
481 }
482
484 vector<TokenPtr> getTokens() const
485 {
486 return getChildrenOfType<Token>();
487 }
488
490 void removeToken(const string& name)
491 {
492 removeChildOfType<Token>(name);
493 }
494
497 TokenPtr getActiveToken(const string& name) const;
498
501 vector<TokenPtr> getActiveTokens() const;
502
506
508 ValueElementPtr getValueElement(const string& name) const
509 {
510 return getChildOfType<ValueElement>(name);
511 }
512
516 ValueElementPtr getActiveValueElement(const string& name) const;
517
521 vector<ValueElementPtr> getActiveValueElements() const;
522
526
529 template<class T> InputPtr setInputValue(const string& name,
530 const T& value,
531 const string& type = EMPTY_STRING);
532
541 ValuePtr getInputValue(const string& name, const string& target = EMPTY_STRING) const;
542
545 TokenPtr setTokenValue(const string& name, const string& value)
546 {
547 TokenPtr token = getToken(name);
548 if (!token)
549 token = addToken(name);
550 token->setValue<string>(value);
551 return token;
552 }
553
556 string getTokenValue(const string& name)
557 {
558 TokenPtr token = getToken(name);
559 return token ? token->getValueString() : EMPTY_STRING;
560 }
561
565
567 void setTarget(const string& target)
568 {
569 setAttribute(TARGET_ATTRIBUTE, target);
570 }
571
573 bool hasTarget() const
574 {
575 return hasAttribute(TARGET_ATTRIBUTE);
576 }
577
579 const string& getTarget() const
580 {
581 return getAttribute(TARGET_ATTRIBUTE);
582 }
583
587
589 void setVersionString(const string& version)
590 {
591 setAttribute(VERSION_ATTRIBUTE, version);
592 }
593
595 bool hasVersionString() const
596 {
597 return hasAttribute(VERSION_ATTRIBUTE);
598 }
599
601 const string& getVersionString() const
602 {
603 return getAttribute(VERSION_ATTRIBUTE);
604 }
605
607 void setVersionIntegers(int majorVersion, int minorVersion);
608
610 virtual std::pair<int, int> getVersionIntegers() const;
611
615
617 void setDefaultVersion(bool defaultVersion)
618 {
619 setTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE, defaultVersion);
620 }
621
623 bool getDefaultVersion() const
624 {
625 return getTypedAttribute<bool>(DEFAULT_VERSION_ATTRIBUTE);
626 }
627
631
638 virtual ConstNodeDefPtr getDeclaration(const string& target = EMPTY_STRING) const;
639
647 bool isTypeCompatible(ConstInterfaceElementPtr declaration) const;
648
650
651 public:
652 static const string NODE_DEF_ATTRIBUTE;
653 static const string TARGET_ATTRIBUTE;
654 static const string VERSION_ATTRIBUTE;
655 static const string DEFAULT_VERSION_ATTRIBUTE;
656
657 protected:
658 void registerChildElement(ElementPtr child) override;
659 void unregisterChildElement(ElementPtr child) override;
660
661 private:
662 size_t _inputCount;
663 size_t _outputCount;
664};
665
666template<class T> InputPtr InterfaceElement::setInputValue(const string& name,
667 const T& value,
668 const string& type)
669{
670 InputPtr input = getChildOfType<Input>(name);
671 if (!input)
672 input = addInput(name);
673 input->setValue(value, type);
674 return input;
675}
676
677} // namespace MaterialX
678
679#endif
shared_ptr< const NodeDef > ConstNodeDefPtr
A shared pointer to a const NodeDef.
Definition: Definition.h:35
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:49
shared_ptr< const PortElement > ConstPortElementPtr
A shared pointer to a const PortElement.
Definition: Interface.h:29
shared_ptr< const Output > ConstOutputPtr
A shared pointer to a const Output.
Definition: Interface.h:39
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:42
shared_ptr< PortElement > PortElementPtr
A shared pointer to a PortElement.
Definition: Interface.h:27
shared_ptr< const InterfaceElement > ConstInterfaceElementPtr
A shared pointer to a const InterfaceElement.
Definition: Interface.h:44
shared_ptr< const Input > ConstInputPtr
A shared pointer to a const Input.
Definition: Interface.h:34
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< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:25
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
An edge between two connected Elements, returned during graph traversal.
Definition: Traversal.h:31
An input element within a Node or NodeDef.
Definition: Interface.h:202
const string & getDefaultGeomPropString() const
Return the defaultgeomprop string for the input.
Definition: Interface.h:227
void setDefaultGeomPropString(const string &geomprop)
Set the defaultgeomprop string for the input.
Definition: Interface.h:215
bool hasDefaultGeomPropString() const
Return true if the given input has a defaultgeomprop string.
Definition: Interface.h:221
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:317
OutputPtr addOutput(const string &name=EMPTY_STRING, const string &type=DEFAULT_TYPE_STRING)
Add an Output to this interface.
Definition: Interface.h:414
OutputPtr getOutput(const string &name) const
Return the Output, if any, with the given name.
Definition: Interface.h:423
bool hasNodeDefString() const
Return true if the given interface has a NodeDef string.
Definition: Interface.h:343
TokenPtr getToken(const string &name) const
Return the Token, if any, with the given name.
Definition: Interface.h:478
vector< InputPtr > getInputs() const
Return a vector of all Input elements.
Definition: Interface.h:379
InputPtr addInput(const string &name=EMPTY_STRING, const string &type=DEFAULT_TYPE_STRING)
Add an Input to this interface.
Definition: Interface.h:364
size_t getInputCount() const
Return the number of Input elements.
Definition: Interface.h:385
void removeInput(const string &name)
Remove the Input, if any, with the given name.
Definition: Interface.h:391
void setVersionString(const string &version)
Set the version string of this interface.
Definition: Interface.h:589
vector< TokenPtr > getTokens() const
Return a vector of all Token elements.
Definition: Interface.h:484
InputPtr getInput(const string &name) const
Return the Input, if any, with the given name.
Definition: Interface.h:373
const string & getVersionString() const
Return the version string of this interface.
Definition: Interface.h:601
ValueElementPtr getValueElement(const string &name) const
Return the ValueElement, if any, with the given name.
Definition: Interface.h:508
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:545
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:666
TokenPtr addToken(const string &name=EMPTY_STRING)
Add a Token to this interface.
Definition: Interface.h:472
void setNodeDefString(const string &nodeDef)
Set the NodeDef string for the interface.
Definition: Interface.h:337
vector< OutputPtr > getOutputs() const
Return a vector of all Output elements.
Definition: Interface.h:429
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:556
bool hasVersionString() const
Return true if this interface has a version string.
Definition: Interface.h:595
bool getDefaultVersion() const
Return the default version flag of this element.
Definition: Interface.h:623
const string & getNodeDefString() const
Return the NodeDef string for the interface.
Definition: Interface.h:349
void removeToken(const string &name)
Remove the Token, if any, with the given name.
Definition: Interface.h:490
void removeOutput(const string &name)
Remove the Output, if any, with the given name.
Definition: Interface.h:441
void setTarget(const string &target)
Set the target string of this interface.
Definition: Interface.h:567
void setDefaultVersion(bool defaultVersion)
Set the default version flag of this element.
Definition: Interface.h:617
bool hasTarget() const
Return true if the given interface has a target string.
Definition: Interface.h:573
size_t getOutputCount() const
Return the number of Output elements.
Definition: Interface.h:435
const string & getTarget() const
Return the target string of this interface.
Definition: Interface.h:579
A spatially-varying output element within a NodeGraph or NodeDef.
Definition: Interface.h:271
size_t getUpstreamEdgeCount() const override
Return the number of queriable upstream edges for this element.
Definition: Interface.h:288
The base class for port elements such as Input and Output.
Definition: Interface.h:53
bool hasOutputString() const
Return true if this element has an output string.
Definition: Interface.h:121
const string & getNodeGraphString() const
Return the node graph string of this element.
Definition: Interface.h:105
void setChannels(const string &channels)
Set the channels string of this element, defining a channel swizzle that will be applied to the upstr...
Definition: Interface.h:138
const string & getOutputString() const
Return the output string of this element.
Definition: Interface.h:127
bool hasChannels() const
Return true if this element has a channels string.
Definition: Interface.h:144
void setNodeGraphString(const string &node)
Set the node graph string of this element.
Definition: Interface.h:93
bool hasNodeGraphString() const
Return true if this element has a node graph string.
Definition: Interface.h:99
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:71
void setOutputString(const string &output)
Set the output string of this element.
Definition: Interface.h:115
const string & getChannels() const
Return the channels string of this element.
Definition: Interface.h:150
const string & getNodeName() const
Return the node name string of this element.
Definition: Interface.h:83
bool hasNodeName() const
Return true if this element has a node name string.
Definition: Interface.h:77
The base class for typed elements.
Definition: Element.h:842
The base class for elements that support typed values.
Definition: Element.h:899