MaterialX 1.38.2
Definition.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_DEFINITION_H
7#define MATERIALX_DEFINITION_H
8
11
13
15
16namespace MaterialX
17{
18
19extern MX_CORE_API const string COLOR_SEMANTIC;
20extern MX_CORE_API const string SHADER_SEMANTIC;
21
22class NodeDef;
23class Implementation;
24class TypeDef;
25class TargetDef;
26class Member;
27class Unit;
28class UnitDef;
29class UnitTypeDef;
30class AttributeDef;
31
33using NodeDefPtr = shared_ptr<NodeDef>;
35using ConstNodeDefPtr = shared_ptr<const NodeDef>;
36
38using ImplementationPtr = shared_ptr<Implementation>;
40using ConstImplementationPtr = shared_ptr<const Implementation>;
41
43using TypeDefPtr = shared_ptr<TypeDef>;
45using ConstTypeDefPtr = shared_ptr<const TypeDef>;
46
48using TargetDefPtr = shared_ptr<TargetDef>;
50using ConstTargetDefPtr = shared_ptr<const TargetDef>;
51
53using MemberPtr = shared_ptr<Member>;
55using ConstMemberPtr = shared_ptr<const Member>;
56
58using UnitPtr = shared_ptr<Unit>;
60using ConstUnitPtr = shared_ptr<const Unit>;
61
63using UnitDefPtr = shared_ptr<UnitDef>;
65using ConstUnitDefPtr = shared_ptr<const UnitDef>;
66
68using UnitTypeDefPtr = shared_ptr<UnitTypeDef>;
70using ConstUnitTypeDefPtr = shared_ptr<const UnitTypeDef>;
71
73using AttributeDefPtr = shared_ptr<AttributeDef>;
75using AttributeDefDefPtr = shared_ptr<const AttributeDef>;
76
82class MX_CORE_API NodeDef : public InterfaceElement
83{
84 public:
85 NodeDef(ElementPtr parent, const string& name) :
86 InterfaceElement(parent, CATEGORY, name)
87 {
88 }
89 virtual ~NodeDef() { }
90
93
95 void setNodeString(const string& node)
96 {
97 setAttribute(NODE_ATTRIBUTE, node);
98 }
99
101 bool hasNodeString() const
102 {
103 return hasAttribute(NODE_ATTRIBUTE);
104 }
105
107 const string& getNodeString() const
108 {
109 return getAttribute(NODE_ATTRIBUTE);
110 }
111
113 const string& getType() const override;
114
118
120 void setNodeGroup(const string& category)
121 {
122 setAttribute(NODE_GROUP_ATTRIBUTE, category);
123 }
124
126 bool hasNodeGroup() const
127 {
128 return hasAttribute(NODE_GROUP_ATTRIBUTE);
129 }
130
132 const string& getNodeGroup() const
133 {
134 return getAttribute(NODE_GROUP_ATTRIBUTE);
135 }
136
140
148 InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const;
149
153
156 bool validate(string* message = nullptr) const override;
157
161
165 bool isVersionCompatible(const string& version) const;
166
169 ConstNodeDefPtr getDeclaration(const string& target = EMPTY_STRING) const override;
170
172
173 public:
174 static const string CATEGORY;
175 static const string NODE_ATTRIBUTE;
176 static const string NODE_GROUP_ATTRIBUTE;
177
178 static const string TEXTURE_NODE_GROUP;
179 static const string PROCEDURAL_NODE_GROUP;
180 static const string GEOMETRIC_NODE_GROUP;
181 static const string ADJUSTMENT_NODE_GROUP;
182 static const string CONDITIONAL_NODE_GROUP;
183 static const string ORGANIZATION_NODE_GROUP;
184 static const string TRANSLATION_NODE_GROUP;
185};
186
193class MX_CORE_API Implementation : public InterfaceElement
194{
195 public:
196 Implementation(ElementPtr parent, const string& name) :
197 InterfaceElement(parent, CATEGORY, name)
198 {
199 }
200 virtual ~Implementation() { }
201
204
206 void setFile(const string& file)
207 {
208 setAttribute(FILE_ATTRIBUTE, file);
209 }
210
212 bool hasFile() const
213 {
214 return hasAttribute(FILE_ATTRIBUTE);
215 }
216
218 const string& getFile() const
219 {
220 return getAttribute(FILE_ATTRIBUTE);
221 }
222
226
228 void setFunction(const string& function)
229 {
230 setAttribute(FUNCTION_ATTRIBUTE, function);
231 }
232
234 bool hasFunction() const
235 {
236 return hasAttribute(FUNCTION_ATTRIBUTE);
237 }
238
240 const string& getFunction() const
241 {
242 return getAttribute(FUNCTION_ATTRIBUTE);
243 }
244
248
250 void setNodeDef(ConstNodeDefPtr nodeDef);
251
253 NodeDefPtr getNodeDef() const;
254
258
261 bool validate(string* message = nullptr) const override;
262
266
269 ConstNodeDefPtr getDeclaration(const string& target = EMPTY_STRING) const override;
270
272
273 public:
274 static const string CATEGORY;
275 static const string FILE_ATTRIBUTE;
276 static const string FUNCTION_ATTRIBUTE;
277};
278
281class MX_CORE_API TypeDef : public Element
282{
283 public:
284 TypeDef(ElementPtr parent, const string& name) :
285 Element(parent, CATEGORY, name)
286 {
287 }
288 virtual ~TypeDef() { }
289
292
294 void setSemantic(const string& semantic)
295 {
296 setAttribute(SEMANTIC_ATTRIBUTE, semantic);
297 }
298
300 bool hasSemantic() const
301 {
302 return hasAttribute(SEMANTIC_ATTRIBUTE);
303 }
304
306 const string& getSemantic() const
307 {
308 return getAttribute(SEMANTIC_ATTRIBUTE);
309 }
310
314
316 void setContext(const string& context)
317 {
318 setAttribute(CONTEXT_ATTRIBUTE, context);
319 }
320
322 bool hasContext() const
323 {
324 return hasAttribute(CONTEXT_ATTRIBUTE);
325 }
326
328 const string& getContext() const
329 {
330 return getAttribute(CONTEXT_ATTRIBUTE);
331 }
332
336
342 MemberPtr addMember(const string& name = EMPTY_STRING)
343 {
344 return addChild<Member>(name);
345 }
346
348 MemberPtr getMember(const string& name) const
349 {
350 return getChildOfType<Member>(name);
351 }
352
354 vector<MemberPtr> getMembers() const
355 {
356 return getChildrenOfType<Member>();
357 }
358
360 void removeMember(const string& name)
361 {
362 removeChildOfType<Member>(name);
363 }
364
366
367 public:
368 static const string CATEGORY;
369 static const string SEMANTIC_ATTRIBUTE;
370 static const string CONTEXT_ATTRIBUTE;
371};
372
375class MX_CORE_API TargetDef : public TypedElement
376{
377 public:
378 TargetDef(ElementPtr parent, const string& name) :
379 TypedElement(parent, CATEGORY, name)
380 {
381 }
382 virtual ~TargetDef() { }
383
388 StringVec getMatchingTargets() const;
389
390 public:
391 static const string CATEGORY;
392};
393
396class MX_CORE_API Member : public TypedElement
397{
398 public:
399 Member(ElementPtr parent, const string& name) :
400 TypedElement(parent, CATEGORY, name)
401 {
402 }
403 virtual ~Member() { }
404
405 public:
406 static const string CATEGORY;
407};
408
411class MX_CORE_API Unit : public Element
412{
413 public:
414 Unit(ElementPtr parent, const string& name) :
415 Element(parent, CATEGORY, name)
416 {
417 }
418 virtual ~Unit() { }
419
420 public:
421 static const string CATEGORY;
422};
423
426class MX_CORE_API UnitDef : public Element
427{
428 public:
429 UnitDef(ElementPtr parent, const string& name) :
430 Element(parent, CATEGORY, name)
431 {
432 }
433 virtual ~UnitDef() { }
434
437
439 void setUnitType(const string& type)
440 {
441 setAttribute(UNITTYPE_ATTRIBUTE, type);
442 }
443
445 bool hasUnitType() const
446 {
447 return hasAttribute(UNITTYPE_ATTRIBUTE);
448 }
449
451 const string& getUnitType() const
452 {
453 return getAttribute(UNITTYPE_ATTRIBUTE);
454 }
455
459
464 UnitPtr addUnit(const string& name)
465 {
466 if (name.empty())
467 {
468 throw Exception("A unit definition name cannot be empty");
469 }
470 return addChild<Unit>(name);
471 }
472
474 UnitPtr getUnit(const string& name) const
475 {
476 return getChildOfType<Unit>(name);
477 }
478
480 vector<UnitPtr> getUnits() const
481 {
482 return getChildrenOfType<Unit>();
483 }
484
486 void removeUnit(const string& name)
487 {
488 removeChildOfType<Unit>(name);
489 }
490
492
493 public:
494 static const string CATEGORY;
495 static const string UNITTYPE_ATTRIBUTE;
496};
497
500class MX_CORE_API UnitTypeDef : public Element
501{
502 public:
503 UnitTypeDef(ElementPtr parent, const string& name) :
504 Element(parent, CATEGORY, name)
505 {
506 }
507 virtual ~UnitTypeDef() { }
508
510 vector<UnitDefPtr> getUnitDefs() const;
511
512 public:
513 static const string CATEGORY;
514};
515
518class MX_CORE_API AttributeDef : public TypedElement
519{
520 public:
521 AttributeDef(ElementPtr parent, const string& name) :
522 TypedElement(parent, CATEGORY, name)
523 {
524 }
525 virtual ~AttributeDef() { }
526
529
531 void setAttrName(const string& name)
532 {
533 setAttribute(ATTRNAME_ATTRIBUTE, name);
534 }
535
537 bool hasAttrName() const
538 {
539 return hasAttribute(ATTRNAME_ATTRIBUTE);
540 }
541
543 const string& getAttrName() const
544 {
545 return getAttribute(ATTRNAME_ATTRIBUTE);
546 }
547
551
553 void setValueString(const string& value)
554 {
555 setAttribute(VALUE_ATTRIBUTE, value);
556 }
557
559 bool hasValueString() const
560 {
561 return hasAttribute(VALUE_ATTRIBUTE);
562 }
563
565 const string& getValueString() const
566 {
567 return getAttribute(VALUE_ATTRIBUTE);
568 }
569
573
575 template<class T> void setValue(const T& value, const string& type = EMPTY_STRING)
576 {
577 setType(!type.empty() ? type : getTypeString<T>());
578 setValueString(toValueString(value));
579 }
580
582 void setValue(const char* value, const string& type = EMPTY_STRING)
583 {
584 setValue(value ? string(value) : EMPTY_STRING, type);
585 }
586
588 bool hasValue() const
589 {
590 return hasAttribute(VALUE_ATTRIBUTE);
591 }
592
599 {
600 if (!hasValue())
601 return ValuePtr();
602 return Value::createValueFromStrings(getValueString(), getType());
603 }
604
608
610 void setElements(const string& elements)
611 {
612 setAttribute(ELEMENTS_ATTRIBUTE, elements);
613 }
614
616 bool hasElements() const
617 {
618 return hasAttribute(ELEMENTS_ATTRIBUTE);
619 }
620
622 const string& getElements() const
623 {
624 return getAttribute(ELEMENTS_ATTRIBUTE);
625 }
626
630
632 void setExportable(bool value)
633 {
634 setTypedAttribute<bool>(EXPORTABLE_ATTRIBUTE, value);
635 }
636
639 bool getExportable() const
640 {
641 return getTypedAttribute<bool>(EXPORTABLE_ATTRIBUTE);
642 }
643
645
646 public:
647 static const string CATEGORY;
648 static const string ATTRNAME_ATTRIBUTE;
649 static const string VALUE_ATTRIBUTE;
650 static const string ELEMENTS_ATTRIBUTE;
651 static const string EXPORTABLE_ATTRIBUTE;
652};
653
654} // namespace MaterialX
655
656#endif
shared_ptr< TargetDef > TargetDefPtr
A shared pointer to a TargetDef.
Definition: Definition.h:48
shared_ptr< const UnitTypeDef > ConstUnitTypeDefPtr
A shared pointer to a const UnitTypeDef.
Definition: Definition.h:70
shared_ptr< UnitDef > UnitDefPtr
A shared pointer to a UnitDef.
Definition: Definition.h:63
shared_ptr< Unit > UnitPtr
A shared pointer to a Unit.
Definition: Definition.h:58
shared_ptr< const UnitDef > ConstUnitDefPtr
A shared pointer to a const UnitDef.
Definition: Definition.h:65
shared_ptr< Implementation > ImplementationPtr
A shared pointer to an Implementation.
Definition: Definition.h:38
shared_ptr< const Member > ConstMemberPtr
A shared pointer to a const Member.
Definition: Definition.h:55
shared_ptr< const Unit > ConstUnitPtr
A shared pointer to a const Unit.
Definition: Definition.h:60
shared_ptr< TypeDef > TypeDefPtr
A shared pointer to a TypeDef.
Definition: Definition.h:43
shared_ptr< const TargetDef > ConstTargetDefPtr
A shared pointer to a const TargetDef.
Definition: Definition.h:50
shared_ptr< const Implementation > ConstImplementationPtr
A shared pointer to a const Implementation.
Definition: Definition.h:40
shared_ptr< AttributeDef > AttributeDefPtr
A shared pointer to an AttributeDef.
Definition: Definition.h:73
shared_ptr< const NodeDef > ConstNodeDefPtr
A shared pointer to a const NodeDef.
Definition: Definition.h:35
shared_ptr< const TypeDef > ConstTypeDefPtr
A shared pointer to a const TypeDef.
Definition: Definition.h:45
shared_ptr< const AttributeDef > AttributeDefDefPtr
A shared pointer to a const AttributeDef.
Definition: Definition.h:75
shared_ptr< Member > MemberPtr
A shared pointer to a Member.
Definition: Definition.h:53
shared_ptr< NodeDef > NodeDefPtr
A shared pointer to a NodeDef.
Definition: Definition.h:33
shared_ptr< UnitTypeDef > UnitTypeDefPtr
A shared pointer to a UnitTypeDef.
Definition: Definition.h:68
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
Interface element subclasses.
shared_ptr< InterfaceElement > InterfaceElementPtr
A shared pointer to an InterfaceElement.
Definition: Interface.h:42
Import and export declarations for the Core library.
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
An attribute definition element within a Document.
Definition: Definition.h:519
ValuePtr getValue() const
Return the typed value of an element as a generic value object, which may be queried to access its da...
Definition: Definition.h:598
void setAttrName(const string &name)
Set the element's attrname string.
Definition: Definition.h:531
const string & getValueString() const
Get the value string of a element.
Definition: Definition.h:565
const string & getAttrName() const
Return the element's attrname string.
Definition: Definition.h:543
void setValue(const char *value, const string &type=EMPTY_STRING)
Set the typed value of an element from a C-style string.
Definition: Definition.h:582
bool getExportable() const
Return the exportable boolean for the element.
Definition: Definition.h:639
bool hasElements() const
Return true if the element has an elements string.
Definition: Definition.h:616
void setExportable(bool value)
Set the exportable boolean for the element.
Definition: Definition.h:632
void setValueString(const string &value)
Set the value string of an element.
Definition: Definition.h:553
bool hasAttrName() const
Return true if this element has an attrname string.
Definition: Definition.h:537
void setValue(const T &value, const string &type=EMPTY_STRING)
Set the typed value of an element.
Definition: Definition.h:575
void setElements(const string &elements)
Set the element's elements string.
Definition: Definition.h:610
const string & getElements() const
Return the element's elements string.
Definition: Definition.h:622
bool hasValueString() const
Return true if the given element has a value string.
Definition: Definition.h:559
bool hasValue() const
Return true if the element possesses a typed value.
Definition: Definition.h:588
The base class for MaterialX elements.
Definition: Element.h:75
The base class for exceptions that are propagated from the MaterialX library to the client applicatio...
Definition: Exception.h:23
An implementation element within a Document.
Definition: Definition.h:194
bool hasFile() const
Return true if the given Implementation has a file string.
Definition: Definition.h:212
bool hasFunction() const
Return true if the given Implementation has a function string.
Definition: Definition.h:234
const string & getFunction() const
Return the function string for the Implementation.
Definition: Definition.h:240
void setFunction(const string &function)
Set the function string for the Implementation.
Definition: Definition.h:228
const string & getFile() const
Return the file string for the Implementation.
Definition: Definition.h:218
void setFile(const string &file)
Set the file string for the Implementation.
Definition: Definition.h:206
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:317
A member element within a TypeDef.
Definition: Definition.h:397
A node definition element within a Document.
Definition: Definition.h:83
void setNodeString(const string &node)
Set the node string of the NodeDef.
Definition: Definition.h:95
const string & getNodeGroup() const
Return the node group of the NodeDef.
Definition: Definition.h:132
bool hasNodeGroup() const
Return true if the given NodeDef has a node group.
Definition: Definition.h:126
const string & getNodeString() const
Return the node string of the NodeDef.
Definition: Definition.h:107
bool hasNodeString() const
Return true if the given NodeDef has a node string.
Definition: Definition.h:101
void setNodeGroup(const string &category)
Set the node group of the NodeDef.
Definition: Definition.h:120
A definition of an implementation target.
Definition: Definition.h:376
A type definition element within a Document.
Definition: Definition.h:282
MemberPtr getMember(const string &name) const
Return the Member, if any, with the given name.
Definition: Definition.h:348
MemberPtr addMember(const string &name=EMPTY_STRING)
Add a Member to the TypeDef.
Definition: Definition.h:342
bool hasSemantic() const
Return true if the given TypeDef has a semantic string.
Definition: Definition.h:300
vector< MemberPtr > getMembers() const
Return a vector of all Member elements in the TypeDef.
Definition: Definition.h:354
bool hasContext() const
Return true if the given TypeDef has a context string.
Definition: Definition.h:322
void setContext(const string &context)
Set the context string of the TypeDef.
Definition: Definition.h:316
void setSemantic(const string &semantic)
Set the semantic string of the TypeDef.
Definition: Definition.h:294
void removeMember(const string &name)
Remove the Member, if any, with the given name.
Definition: Definition.h:360
const string & getContext() const
Return the context string of the TypeDef.
Definition: Definition.h:328
const string & getSemantic() const
Return the semantic string of the TypeDef.
Definition: Definition.h:306
The base class for typed elements.
Definition: Element.h:842
A unit definition element within a Document.
Definition: Definition.h:427
void removeUnit(const string &name)
Remove the Unit, if any, with the given name.
Definition: Definition.h:486
void setUnitType(const string &type)
Set the element's unittype string.
Definition: Definition.h:439
UnitPtr getUnit(const string &name) const
Return the Unit, if any, with the given name.
Definition: Definition.h:474
bool hasUnitType() const
Return true if the given element has a unittype string.
Definition: Definition.h:445
UnitPtr addUnit(const string &name)
Add a Unit to the UnitDef.
Definition: Definition.h:464
vector< UnitPtr > getUnits() const
Return a vector of all Unit elements in the UnitDef.
Definition: Definition.h:480
const string & getUnitType() const
Return the element's type string.
Definition: Definition.h:451
A unit declaration within a UnitDef.
Definition: Definition.h:412
A unit type definition element within a Document.
Definition: Definition.h:501
static ValuePtr createValueFromStrings(const string &value, const string &type)
Create a new value instance from value and type strings.
Definition: Value.cpp:211