# Walking The Actor Hierarchy

This page covers how to walk the Actors in a 3D Level akin to the Outliner:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FvVxKdNjLytBPEdBpglk6%2Fimage.png?alt=media&#x26;token=b7194710-bf99-4dfc-a4bf-c8ec5494451d" alt="" width="336"><figcaption></figcaption></figure>

### Understanding Spawned Vs Nested Actors

Before we get to the actor hierarchy it's important to be aware of how an actor was spawned in the 3D level. <mark style="color:green;">**actors can spawn other actors**</mark>. I'll be using the following terms to refer to the two possible ways in which an Actor is attached to another Actor:

* <mark style="color:green;">Spawned Actors</mark> are Actors added to the 3D Level usually by an [ActorComponent ](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/ActorComponent?application_version=5.2#unreal-actorcomponent)on a Blueprint Asset. They are very limited in what can be changed from the 3D level / Python. We cannot change display names or hierarchies as they are managed within the compiled Blueprint Asset that generated them. These Actors are owned by other Actors.
* <mark style="color:green;">Nested Actors</mark> are actors in the 3D Level attached to each other in the 3D level. We can change displays names, hierarchies, and any exposed properties directly. These Actors are owned by the 3D Level.

Actors spawned by other Actors aren't as editable as Actors added directly to the 3D Level

***

## Getting the Top-Most Level Actors

Building off of the `get_all_actors()` function from [#expanded-options](https://bkortbus.gitbook.io/unreal-python-recipe-book/get-all-actors-in-3d-level#expanded-options "mention"), we can use the following to get the list of top-most Actors in the 3D Level:

```python
def get_root_actors():
    return [
        actor
        for actor in get_all_actors()
        if not actor.get_attach_parent_actor() 
        and not actor.get_parent_actor()
    ]
```

This will give us the top-most actors we can then walk through

***

## Walking The Actor hierarchy

In order to walk the Actor Hierarchy we'll need a recursive function to step through the nested Actors.

This will rely on two functions in particular on the Actor Class:

1. [get\_all\_child\_actors ](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/Actor?application_version=5.3#unreal.Actor.get_all_child_actors)- this function gets the list of Actors spawned by the current Actor
2. [get\_attached\_actors ](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/Actor?application_version=5.3#unreal.Actor.get_attached_actors)- this function gets the list of Actors attached to the current Actor, which also includes actors spawned by the current Actor as well

We can use these two functions to get the separate lists of Spawned Actors and Nested Actors:

```python
def walk_actor(actor, indent=0):
    """walk the given actor to print its hierarchy"""
    print(f"{'  ' * indent}{actor.get_actor_label()}")

    # separate the nested actors from the spawned actors:
    spawned_actors = actor.get_all_child_actors()
    nested_actors = [
        child 
        for child in actor.get_attached_actors()
        if child not in spawned_actors          
    ]
    
    # print the spawned actors
    for child in spawned_actors:
        print(f"{'  ' * (indent+2)}{child.get_actor_label()}  (SPAWNED)")
    
    # walk the nested actors
    for child in nested_actors:
        walk_actor(child, indent+2)
```

## Results

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FmmRXmt33caAImp1O7KKY%2Fimage.png?alt=media&#x26;token=868e9426-a28c-4028-a6ee-00f73c89752e" alt="" width="563"><figcaption></figcaption></figure>

The ordering may differ from the Outliner UI, but it is properly displaying which actors are Nested versus which actors are Spawned


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bkortbus.gitbook.io/unreal-python-recipe-book/actors/walking-the-actor-hierarchy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
