Walking Components

how to inspect and walk the Actor Component Hierarchy

This page covers how to walk the Actor Components s in a 3D Level akin to the Details panel:

Compared to the Actor Hierarchy, Components appear deceptively simple to iterate over with a recursive function:

def walk_component(component, indent=2):
    """walk the given component to print its hierarchy"""
    print(f"{' '*indent}{component.get_name()}")

    # recurse through any immediate children
    for child in component.get_children_components(False):
        walk_component(child, indent+2)

After getting our Actor reference, we can start from the root_component reference:

EditorActorSubsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)

selection = EditorActorSubsystem.get_selected_level_actors()
actor = selection[0] if selection else None

if actor:
    print(actor.get_actor_label())
    walk_component(actor.root_component)

If we compare the results of this function to the Details Panel, we'll notice quite a few extra lines:

This is because the function_test Blueprint Asset spawns child actors. If we only want to view the immediate components of our given actor we can add a get_owner() check to confirm the component is directly managed by the given actor:

def walk_component(component, owner=None, indent=2):
    """walk the given component to print its hierarchy"""
    if not component:
        return
    
    # validate the component's owner
    if not owner:
        owner = component.get_owner()
    if component.get_owner() != owner:
        return
    
    print(f"{' '*indent}{component.get_name()}")

    # recurse through any immediate children
    for child in component.get_children_components(False):
        walk_component(child, owner , indent+2)

This added step gives us a printout that better matches the Details Panel:

Last updated