Return ALL Nodes in 3DS MAX

There are times when you need to get all the nodes in a scene. In 3DS MAX, the most common way I’ve seen to get all the nodes is the following:

from MaxPlus import Core

nodes = Core.GetRootNode().Children

This line of code only gets you the top-level nodes. If there are any nested nodes, such as those found in groups, then only the top-most node (i.e. the “parent”) will be returned.

Scene Explorer

Scene Explorer

Scene

Scene

Only “Boxes”, “Pyramid001”, and “Sphere001” will be returned with the above code. The individual “Box00#” objects will NOT be returned. This is the native behavior of 3DS MAX. The parent of the group (which is considered a “Helper“ class) may have some actions done to it, but those actions may not cascade down into the children nodes.

For instance, if you’d like to hide all nodes in the scene, you’d THINK hiding the parent node of a group would hide the children as well.

from MaxPlus import Core

nodes = Core.GetRootNode().Children

for n in nodes:
    n.Hide = True
Updated scene

Updated scene

Updated scene explorer

Updated scene explorer

This doesn’t feel like the expected behavior, but is, in fact, what happens. So we also need to hide the children nodes, no matter how deep into nested groups they are. So when I say we need to get all the nodes…

… I mEAN ALL the nodes.

The way I’ve come up with is to list all children nodes recursively.

from MaxPlus import Core
  
def get_all_nodes(nodes=None):
    """Returns all descendants of a list of nodes.
    If None is provided, it will return all nodes in the scene.

    Args:
        nodes (list[MaxPlus.INode]|None): Nodes from which to find descendants.

    Returns:
        list[MaxPlus.INode]: List of all nodes.
    """
    all_nodes_list = []
    if nodes is None:
        nodes = Core.GetRootNode().Children
    for node in nodes:
        if node.GetNumChildren():
            all_nodes_list.extend(get_all_nodes(node.Children))
        all_nodes_list.append(node)

    return all_nodes_list

This allows you to get ALL nodes. It also allows you to get all nodes within in a group or several groups supplied in a list.