Class SoSwitch

  • All Implemented Interfaces:
    Direct Known Subclasses:
    SoBlinker, SoInteractiveSwitch

    public class SoSwitch
    extends SoGroup
    Group node that traverses one chosen child. This group node traverses exactly one, none or all of its children. It implements an operation similar to the switch statement in C. One can use this node to switch on and off the effects of some properties, to switch between different properties, to switch between different geometry or to control the visibility of geometry.

    The whichChild field specifies the index of the child to traverse, where the first child has index 0. There are several special values that can be used:

    • SO_SWITCH_NONE (-1, the default) means do not traverse any children.
    • SO_SWITCH_INHERIT (-2) allows the index to be inherited from a previously-encountered SoSwitch node or from certain other nodes (such as SoArray or SoMultipleCopy) that set the switch value.
    • SO_SWITCH_ALL (-3) traverses all children, making the switch behave exactly like a regular SoGroup.

    There are several extended versions of this node with additional/different behavior:

    • SoInteractiveSwitch: During a user interaction, for example moving the camera, traverses a different (or no) child specified by its whichInteractiveChild field.
    • SoMultiSwitch: Traverses any number of its children (specified in a list of indices).
    • SoPathSwitch: Only traverses its children if the current path (when it is traversed) matches or contains a path specified in its path field.
    • SoMultiPathSwitch: Only traverses its children if the current path matches or contains any of the paths specified in its paths field.


    • The default value for whichChild is SO_SWITCH_NONE, meaning that nothing is displayed by default. In most cases you will want to set some other initial value in this field.
    • When the value of whichChild is SO_SWITCH_INHERIT, if the inherited value is larger than the number of children in the current node, the actual value used is "modulo" the number of children.
    • Some actions have an option that allows them to ignore the value of whichChild. For example, calling setSearchingAll(true) on an SoSearchAction causes it to traverse all the children of SoSwitch nodes.
    • SoSwitch is commonly used to control the visibility of a geometry. For example an application might allow the user to create multiple slices through a volume (SoOrthoSlice) or mesh (MoLogicalSlice) data set, but also allow the user to control the visibility of each slice separately. Using SoSwitch with SO_SWITCH_NONE is much more efficient than removing geometry from the scene graph because previously built caches are preserved. Using SoSwitch is also much more efficient than using draw style INVISIBLE (SoDrawStyle) because the children are not traversed.
    • This node is an SoGroup, not an SoSeparator, and therefore does not provide render caching. Generally if the children contain geometry, it is good practice to make each child an SoSeparator to make render caching more effective.

    File format/default:

    Switch {

      whichChild -1

    Action behavior:

    SoGLRenderAction, SoCallbackAction, SoGetBoundingBoxAction, SoGetMatrixAction, SoHandleEventAction, SoRayPickAction
    Traverses the chosen child or children. Sets: SoSwitchElement

    If the action's Searching-All flag is set, always traverses all children. Otherwise, traverses just the chosen child or children.

    See Also:
    SoArray, SoLevelOfDetail, SoMultipleCopy, SoPathSwitch, SoMultiSwitch, SoInteractiveSwitch
    • Field Detail

      • whichChild

        public final SoSFInt32 whichChild
        Index of the child to traverse, or one of SO_SWITCH_NONE, SO_SWITCH_INHERIT, or SO_SWITCH_ALL.
    • Constructor Detail

      • SoSwitch

        public SoSwitch​(int nChildren)
        Constructor that takes approximate number of children.
      • SoSwitch

        public SoSwitch()
        Creates a switch node with default settings.