Working with the scene graph

Scene graph and nodes

A scene graph is a tree of scene nodes. Each node is a hub of information for objects in the scene, and it can be responsible for the following:

Traversing the scene graph

Core.GetRootNode() provides the root node of the scene. For each INode, you can obtain its Name and Children via the INode properties. The example below (demoSceneGraph.py) demonstrates how to traverse through the scene graph and print the name of each node and that of its children.

'''
    Creates a simple text representation of the scene graph
'''
import MaxPlus
import sys

def outputNode(n, indent = ''):
    print indent, n.Name
    for c in n.Children:
        outputNode(c, indent + '--')
        
if __name__ == '__main__':
    outputNode(MaxPlus.Core.GetRootNode())

Selection list

While traversing the scene graph, you may find it useful to obtain the list of selected objects.

SelectionManager.Nodes provides an iterator for the selection list.

You can now do the following:

for n in MaxPlus.SelectionManager.Nodes:
    doThisWithNode(n)

This method takes a material m as input and assigns the material to only the selected objects:

def applyMaterialToNodes(m):
    for n in MaxPlus.SelectionManager.Nodes:
        n.Material = m

However, because this is an iterator, you cannot directly write to its indexed elements. For example, you cannot write MaxPlus.SelectionManager.Nodes[3]. You must first create a copy of the selection list by converting the iterator to an indexable collection, such as list(MaxPlus.SelectionManager.Nodes) or tuple(MaxPlus.SelectionManager.Nodes). Each element in the list is now a handle to the actual node.

Querying the types of objects that exist in the scene

The example demoTypeCasting.py demonstrates how to iterate through the scene and print out what class each object belongs to. For each node in the scene, it first obtains the super class ID for each object, then casts it to the correct type and prints it out.