MaterialX  1.38.5
Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Friends | List of all members
Element Class Reference

The base class for MaterialX elements. More...

#include <Element.h>

Inheritance diagram for Element:
Backdrop Collection CommentElement GenericElement GeomElement GeomPropDef Look LookGroup PropertySet TypeDef TypedElement Unit UnitDef UnitTypeDef VariantAssign VariantSet

Public Member Functions

 Element (const Element &)=delete
 
Elementoperator= (const Element &)=delete
 
bool operator== (const Element &rhs) const
 Return true if the given element tree, including all descendants, is identical to this one.
 
bool operator!= (const Element &rhs) const
 Return true if the given element tree, including all descendants, differs from this one.
 
Category
void setCategory (const string &category)
 Set the element's category string.
 
const string & getCategory () const
 Return the element's category string. More...
 
Name
void setName (const string &name)
 Set the element's name string. More...
 
const string & getName () const
 Return the element's name string.
 
string getNamePath (ConstElementPtr relativeTo=nullptr) const
 Return the element's hierarchical name path, relative to the root document. More...
 
ElementPtr getDescendant (const string &namePath) const
 Return the element specified by the given hierarchical name path, relative to the current element. More...
 
File Prefix
void setFilePrefix (const string &prefix)
 Set the element's file prefix string.
 
bool hasFilePrefix () const
 Return true if the given element has a file prefix string.
 
const string & getFilePrefix () const
 Return the element's file prefix string.
 
const string & getActiveFilePrefix () const
 Return the file prefix string that is active at the scope of this element, taking all ancestor elements into account.
 
Geom Prefix
void setGeomPrefix (const string &prefix)
 Set the element's geom prefix string.
 
bool hasGeomPrefix () const
 Return true if the given element has a geom prefix string.
 
const string & getGeomPrefix () const
 Return the element's geom prefix string.
 
const string & getActiveGeomPrefix () const
 Return the geom prefix string that is active at the scope of this element, taking all ancestor elements into account.
 
Color Space
void setColorSpace (const string &colorSpace)
 Set the element's color space string.
 
bool hasColorSpace () const
 Return true if the given element has a color space string.
 
const string & getColorSpace () const
 Return the element's color space string.
 
const string & getActiveColorSpace () const
 Return the color space string that is active at the scope of this element, taking all ancestor elements into account.
 
Inheritance
void setInheritString (const string &inherit)
 Set the inherit string of this element.
 
bool hasInheritString () const
 Return true if this element has an inherit string.
 
const string & getInheritString () const
 Return the inherit string of this element.
 
void setInheritsFrom (ConstElementPtr super)
 Set the element that this one directly inherits from.
 
ElementPtr getInheritsFrom () const
 Return the element, if any, that this one directly inherits from.
 
bool hasInheritedBase (ConstElementPtr base) const
 Return true if this element has the given element as an inherited base, taking the full inheritance chain into account.
 
bool hasInheritanceCycle () const
 Return true if the inheritance chain for this element contains a cycle.
 
Namespace
void setNamespace (const string &space)
 Set the namespace string of this element.
 
bool hasNamespace () const
 Return true if this element has a namespace string.
 
const string & getNamespace () const
 Return the namespace string of this element.
 
string getQualifiedName (const string &name) const
 Return a qualified version of the given name, taking the namespace at the scope of this element into account.
 
Documentation String
void setDocString (const string &doc)
 Set the documentation string of this element.
 
string getDocString () const
 Return the documentation string of this element.
 
Subclass
template<class T >
bool isA (const string &category=EMPTY_STRING) const
 Return true if this element belongs to the given subclass. More...
 
template<class T >
shared_ptr< T > asA ()
 Dynamic cast to an instance of the given subclass.
 
template<class T >
shared_ptr< const T > asA () const
 Dynamic cast to a const instance of the given subclass.
 
Child Elements
template<class T >
shared_ptr< T > addChild (const string &name=EMPTY_STRING)
 Add a child element of the given subclass and name. More...
 
ElementPtr addChildOfCategory (const string &category, string name=EMPTY_STRING)
 Add a child element of the given category and name. More...
 
ElementPtr changeChildCategory (ElementPtr child, const string &category)
 Change the category of the given child element. More...
 
ElementPtr getChild (const string &name) const
 Return the child element, if any, with the given name.
 
template<class T >
shared_ptr< T > getChildOfType (const string &name) const
 Return the child element, if any, with the given name and subclass. More...
 
const vector< ElementPtr > & getChildren () const
 Return a constant vector of all child elements. More...
 
template<class T >
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 by the given category string. More...
 
void setChildIndex (const string &name, int index)
 Set the index of the child, if any, with the given name. More...
 
int getChildIndex (const string &name) const
 Return the index of the child, if any, with the given name. More...
 
void removeChild (const string &name)
 Remove the child element, if any, with the given name.
 
template<class T >
void removeChildOfType (const string &name)
 Remove the child element, if any, with the given name and subclass. More...
 
Attributes
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.
 
const string & getAttribute (const string &attrib) const
 Return the value string of the given attribute. More...
 
const StringVecgetAttributeNames () const
 Return a vector of stored attribute names, in the order they were set.
 
template<class T >
void setTypedAttribute (const string &attrib, const T &data)
 Set the value of an implicitly typed attribute. More...
 
template<class T >
getTypedAttribute (const string &attrib) const
 Return the value of an implicitly typed attribute. More...
 
void removeAttribute (const string &attrib)
 Remove the given attribute, if present.
 
Self And Ancestor Elements
ElementPtr getSelf ()
 Return our self pointer.
 
ConstElementPtr getSelf () const
 Return our self pointer.
 
ElementPtr getParent ()
 Return our parent element.
 
ConstElementPtr getParent () const
 Return our parent element.
 
ElementPtr getRoot ()
 Return the root element of our tree.
 
ConstElementPtr getRoot () const
 Return the root element of our tree.
 
DocumentPtr getDocument ()
 Return the root document of our tree.
 
ConstDocumentPtr getDocument () const
 Return the root document of our tree.
 
template<class T >
shared_ptr< const T > getAncestorOfType () const
 Return the first ancestor of the given subclass, or an empty shared pointer if no ancestor of this subclass is found.
 
Traversal
TreeIterator traverseTree () const
 Traverse the tree from the given element to each of its descendants in depth-first order, using pre-order visitation. More...
 
GraphIterator traverseGraph () const
 Traverse the dataflow graph from the given element to each of its upstream sources in depth-first order, using pre-order visitation. More...
 
virtual Edge getUpstreamEdge (size_t index=0) const
 Return the Edge with the given index that lies directly upstream from this element in the dataflow graph. More...
 
virtual size_t getUpstreamEdgeCount () const
 Return the number of queriable upstream edges for this element.
 
ElementPtr getUpstreamElement (size_t index=0) const
 Return the Element with the given index that lies directly upstream from this one in the dataflow graph. More...
 
InheritanceIterator traverseInheritance () const
 Traverse the inheritance chain from the given element to each element from which it inherits. More...
 
Source URI
void setSourceUri (const string &sourceUri)
 Set the element's source URI. More...
 
bool hasSourceUri () const
 Return true if this element has a source URI.
 
const string & getSourceUri () const
 Return the element's source URI.
 
const string & getActiveSourceUri () const
 Return the source URI that is active at the scope of this element, taking all ancestor elements into account.
 
Validation
virtual bool validate (string *message=nullptr) const
 Validate that the given element tree, including all descendants, is consistent with the MaterialX specification.
 
Utility
void copyContentFrom (const ConstElementPtr &source)
 Copy all attributes and descendants from the given element to this one. More...
 
void clearContent ()
 Clear all attributes and descendants from this element.
 
string createValidChildName (string name) const
 Using the input name as a starting point, modify it to create a valid, unique name for a child element.
 
StringResolverPtr createStringResolver (const string &geom=EMPTY_STRING) const
 Construct a StringResolver at the scope of this element. More...
 
string asString () const
 Return a single-line description of this element, including its category, name, and attributes.
 
template<class T >
shared_ptr< T > resolveRootNameReference (const string &name) const
 Resolve a reference to a named element at the root scope of this document, taking the namespace at the scope of this element into account.
 

Static Public Attributes

static const string NAME_ATTRIBUTE = "name"
 
static const string FILE_PREFIX_ATTRIBUTE = "fileprefix"
 
static const string GEOM_PREFIX_ATTRIBUTE = "geomprefix"
 
static const string COLOR_SPACE_ATTRIBUTE = "colorspace"
 
static const string INHERIT_ATTRIBUTE = "inherit"
 
static const string NAMESPACE_ATTRIBUTE = "namespace"
 
static const string DOC_ATTRIBUTE = "doc"
 

Protected Types

using DocumentPtr = shared_ptr< Document >
 
using ConstDocumentPtr = shared_ptr< const Document >
 

Protected Member Functions

 Element (ElementPtr parent, const string &category, const string &name)
 
void validateRequire (bool expression, bool &res, string *message, const string &errorDesc) const
 
virtual void registerChildElement (ElementPtr child)
 
virtual void unregisterChildElement (ElementPtr child)
 
ElementPtr getSelfNonConst () const
 

Protected Attributes

string _category
 
string _name
 
string _sourceUri
 
ElementMap _childMap
 
vector< ElementPtr_childOrder
 
StringMap _attributeMap
 
StringVec _attributeOrder
 
weak_ptr< Element_parent
 
weak_ptr< Element_root
 

Friends

template<class T >
class ElementRegistry
 

Detailed Description

The base class for MaterialX elements.

An Element is a named object within a Document, which may possess any number of child elements and attributes.

Member Function Documentation

◆ addChild()

shared_ptr< T > addChild ( const string &  name = EMPTY_STRING)

Add a child element of the given subclass and name.

Parameters
nameThe name of the new child element. If no name is specified, then a unique name will automatically be generated.
Exceptions
Exceptionif a child of this element already possesses the given name.
Returns
A shared pointer to the new child element.

◆ addChildOfCategory()

ElementPtr addChildOfCategory ( const string &  category,
string  name = EMPTY_STRING 
)

Add a child element of the given category and name.

Parameters
categoryThe category string of the new child element. If the category string is recognized, then the correponding Element subclass is generated; otherwise, a GenericElement is generated.
nameThe name of the new child element. If no name is specified, then a unique name will automatically be generated.
Exceptions
Exceptionif a child of this element already possesses the given name.
Returns
A shared pointer to the new child element.

◆ changeChildCategory()

ElementPtr changeChildCategory ( ElementPtr  child,
const string &  category 
)

Change the category of the given child element.

Parameters
childThe child element that will be modified.
categoryThe new category string for the child element.
Returns
A shared pointer to a new child element, containing the contents of the original child but with a new category and subclass.

◆ copyContentFrom()

void copyContentFrom ( const ConstElementPtr source)

Copy all attributes and descendants from the given element to this one.

Parameters
sourceThe element from which content is copied.

◆ createStringResolver()

StringResolverPtr createStringResolver ( const string &  geom = EMPTY_STRING) const

Construct a StringResolver at the scope of this element.

The returned object may be used to apply substring modifiers to data values in the context of a specific element, geometry, and material.

Parameters
geomAn optional geometry name, which will be used to select the applicable set of geometry token substitutions. By default, no geometry token substitutions are applied. If the universal geometry name "/" is given, then all geometry token substitutions are applied,
Returns
A shared pointer to a StringResolver.

◆ getAttribute()

const string& getAttribute ( const string &  attrib) const
inline

Return the value string of the given attribute.

If the given attribute is not present, then an empty string is returned.

◆ getCategory()

const string& getCategory ( ) const
inline

Return the element's category string.

The category of a MaterialX element represents its role within the document, with common examples being "material", "nodegraph", and "image".

◆ getChildIndex()

int getChildIndex ( const string &  name) const

Return the index of the child, if any, with the given name.

If no child with the given name is found, then -1 is returned.

◆ getChildOfType()

shared_ptr<T> getChildOfType ( const string &  name) const
inline

Return the child element, if any, with the given name and subclass.

If a child with the given name exists, but belongs to a different subclass, then an empty shared pointer is returned.

◆ getChildren()

const vector<ElementPtr>& getChildren ( ) const
inline

Return a constant vector of all child elements.

The returned vector maintains the order in which children were added.

◆ getChildrenOfType()

vector<shared_ptr<T> > getChildrenOfType ( const string &  category = EMPTY_STRING) const
inline

Return a vector of all child elements that are instances of the given subclass, optionally filtered by the given category string.

The returned vector maintains the order in which children were added.

◆ getDescendant()

ElementPtr getDescendant ( const string &  namePath) const

Return the element specified by the given hierarchical name path, relative to the current element.

If the name path is empty then the current element is returned. If no element is found at the given path, then an empty shared pointer is returned.

Parameters
namePathThe relative name path of the specified element.

◆ getNamePath()

string getNamePath ( ConstElementPtr  relativeTo = nullptr) const

Return the element's hierarchical name path, relative to the root document.

The name of each ancestor will be prepended in turn, separated by forward slashes.

Parameters
relativeToIf a valid ancestor element is specified, then the returned path will be relative to this ancestor.

◆ getTypedAttribute()

T getTypedAttribute ( const string &  attrib) const
inline

Return the value of an implicitly typed attribute.

If the given attribute is not present, or cannot be converted to the given data type, then the zero value for the data type is returned.

◆ getUpstreamEdge()

Edge getUpstreamEdge ( size_t  index = 0) const
virtual

Return the Edge with the given index that lies directly upstream from this element in the dataflow graph.

Parameters
indexAn optional index of the edge to be returned, where the valid index range may be determined with getUpstreamEdgeCount.
Returns
The upstream Edge, if valid, or an empty Edge object.

Reimplemented in Node, and Output.

◆ getUpstreamElement()

ElementPtr getUpstreamElement ( size_t  index = 0) const

Return the Element with the given index that lies directly upstream from this one in the dataflow graph.

Parameters
indexAn optional index of the element to be returned, where the valid index range may be determined with getUpstreamEdgeCount.
Returns
The upstream Element, if valid, or an empty ElementPtr.

◆ isA()

bool isA ( const string &  category = EMPTY_STRING) const
inline

Return true if this element belongs to the given subclass.

If a category string is specified, then both subclass and category matches are required.

◆ removeChildOfType()

void removeChildOfType ( const string &  name)
inline

Remove the child element, if any, with the given name and subclass.

If a child with the given name exists, but belongs to a different subclass, then this method has no effect.

◆ setChildIndex()

void setChildIndex ( const string &  name,
int  index 
)

Set the index of the child, if any, with the given name.

If the given index is out of bounds, then an exception is thrown.

◆ setName()

void setName ( const string &  name)

Set the element's name string.

The name of a MaterialX element must be unique among all elements at the same scope.

Exceptions
Exceptionif an element at the same scope already possesses the given name.

◆ setSourceUri()

void setSourceUri ( const string &  sourceUri)
inline

Set the element's source URI.

Parameters
sourceUriA URI string representing the resource from which this element originates. This string may be used by serialization and deserialization routines to maintain hierarchies of include references.

◆ setTypedAttribute()

void setTypedAttribute ( const string &  attrib,
const T &  data 
)
inline

Set the value of an implicitly typed attribute.

Since an attribute stores no explicit type, the same type argument must be used in corresponding calls to getTypedAttribute.

◆ traverseGraph()

GraphIterator traverseGraph ( ) const

Traverse the dataflow graph from the given element to each of its upstream sources in depth-first order, using pre-order visitation.

Exceptions
ExceptionFoundCycleif a cycle is encountered.
Returns
A GraphIterator object. Example usage with an implicit iterator:
for (Edge edge : inputElem->traverseGraph())
{
ElementPtr upElem = edge.getUpstreamElement();
ElementPtr downElem = edge.getDownstreamElement();
cout << upElem->asString() << " lies upstream from " << downElem->asString() << endl;
}
Example usage with an explicit iterator:
for (mx::GraphIterator it = inputElem->traverseGraph().begin(); it != mx::GraphIterator::end(); ++it)
{
mx::ElementPtr elem = it.getUpstreamElement();
cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
}
See also
getUpstreamEdge
getUpstreamElement

◆ traverseInheritance()

InheritanceIterator traverseInheritance ( ) const

Traverse the inheritance chain from the given element to each element from which it inherits.

Exceptions
ExceptionFoundCycleif a cycle is encountered.
Returns
An InheritanceIterator object. Example usage:
ConstElementPtr derivedElem;
for (ConstElementPtr elem : inputElem->traverseInheritance())
{
if (derivedElem)
cout << derivedElem->asString() << " inherits from " << elem->asString() << endl;
derivedElem = elem;
}

◆ traverseTree()

TreeIterator traverseTree ( ) const

Traverse the tree from the given element to each of its descendants in depth-first order, using pre-order visitation.

Returns
A TreeIterator object. Example usage with an implicit iterator:
for (ElementPtr elem : inputElem->traverseTree())
{
cout << elem->asString() << endl;
}
Example usage with an explicit iterator:
for (mx::TreeIterator it = inputElem->traverseTree().begin(); it != mx::TreeIterator::end(); ++it)
{
mx::ElementPtr elem = it.getElement();
cout << elem->asString() << " at depth " << it.getElementDepth() << endl;
}

The documentation for this class was generated from the following files:
ConstElementPtr
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
Definition: Element.h:32
ElementPtr
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:30
Edge
An edge between two connected Elements, returned during graph traversal.
Definition: Traversal.h:30