> For the complete documentation index, see [llms.txt](https://bkortbus.gitbook.io/unreal-python-recipe-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bkortbus.gitbook.io/unreal-python-recipe-book/actors/on-selection-changed-callback.md).

# On Selection Changed Callback

For any tools that rely on the current actor selection in the viewport

A neat property of the LevelEditorSubsystem is [get\_selection\_set()](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/LevelEditorSubsystem?application_version=5.2#unreal.LevelEditorSubsystem.get_selection_set), this is the Selection Set managed by the subsystem that keeps track of what's currently selected in the 3D Level / Editor World.

A callback we can make use of on this object in Python is [on\_selection\_changed](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/TypedElementSelectionSet?application_version=5.2#unreal.TypedElementSelectionSet.on_selection_change), which triggers any time the user changes their selection. This can be quite powerful for Editor Utility Widgets to be more dynamic with what ever the user has selected in their scene.

## Our Callback Function

For this callback, the LevelEditorSubsystem will pass along its Selection Set when triggered to our custom function. Our function will expect to be provided the Selection Set and can go directly into its relevant logic:

```python
def selection_tracker(selection_set: unreal.TypedElementSelectionSet):
    if selection_set.get_num_selected_elements():
        print(f"The following objects are currently selected:")
        for selected in selection_set.get_selected_objects():
             print(f"\t{selected.get_path_name()}")
    else:
        print("no objects selected!")
```

***

## Adding our Function to the Callback

To register our function, we can run the following logic:

```python
LevelEditorSubsystem = unreal.get_editor_subsystem(unreal.LevelEditorSubsystem)

selection_set = LevelEditorSubsystem.get_selection_set()
selection_set.on_selection_change.add_callable(selection_tracker)
```

This will get the selection set from the LevelEditorSubsystem and add our Python function to it

And now, any time we select something in the viewport we get&#x20;

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FHHBwORzmyfuvlkIAZAgP%2Fimage.png?alt=media&amp;token=2fdd1567-5a0a-43ab-a6c2-6c41f7b088e3" alt=""><figcaption></figcaption></figure>

***

## Removing our Function from the Callback

When we're done with our tool / use case we can use the `remove_callable` function to disconnect our callback function:

```python
selection_set.on_selection_change.remove_callable(selection_tracker)
```
