# Walking Components

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

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FSZcQuwWtd4nLLInVg6s1%2Fimage.png?alt=media&#x26;token=f562ca2c-948f-46b6-a460-a59865a059f0" alt="" width="368"><figcaption></figcaption></figure>

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

```python
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 <mark style="color:green;">root\_component</mark> reference:

```python
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:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FDMIcGE7WVCo4dABaAOSb%2Fimage.png?alt=media&#x26;token=44bb5368-5b31-4b24-a6d3-8a0f20830c65" alt="" width="563"><figcaption></figcaption></figure>

This is because the <mark style="color:green;">function\_test</mark> Blueprint Asset spawns child actors. If we only want to view the immediate components of our given actor we can add a [get\_owner()](https://docs.unrealengine.com/5.2/en-US/PythonAPI/class/ActorComponent.html#unreal.ActorComponent.get_owner) check to confirm the component is directly managed by the given actor:

```python
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:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2F5tTEcGBZfGnAlgaLiC9f%2Fimage.png?alt=media&#x26;token=75f5db05-7f0f-419c-a796-6615bbb42c8a" alt="" width="518"><figcaption></figcaption></figure>
