3.12. Node Types

Inventor provides runtime type-checking through the SoType class. Use the getTypeId() method on an instance to obtain the SoType for that instance. Runtime type-checking is available for most Inventor classes, including nodes, engines, actions, details, and events.

The SoType class has methods that enable you to find the parent class of a type ( getParent()), to create an instance of a particular type ( createInstance()), and to obtain an SbName for the class type ( getName()). For example, the following code returns a name, such as

Material or Group, which you could then use to print some information about the node:

node->getTypeId().getName();
  

The following two statements both return the SoType for an SoMaterial SoMaterial SoMaterial node (the first is more efficient):

// (1)
SoMaterial::getClassTypeId();
// (2)
SoType::fromName("Material");

To determine whether an instance is of a particular type, use the == operator, as follows:

if (myNode->getTypeId() == SoGroup::getClassTypeId())
      // Is this an SoGroup?

To determine whether an instance is of the same type or derived from a particular class, use the isOfType() method or the SoType::derivedFrom() method (the two methods have the same effects):

// (1)
if (myNode->isOfType(SoGroup::getClassTypeId()))
   // Is this an SoGroup, SoSeparator, SoSwitch, and so on
      
// (2)
if (myNode->getTypeId().isDerivedFrom(
      SoGroup::getClassTypeId()))

Also see the description in Chapter 8, Applying Actions of the SoSearchAction SoSearchAction SoSearchAction , which allows you to search the scene graph for nodes of a particular type, or derived from a type.