Access Layer Info in Python

Hi, I’m trying to just get access to the layer names in the editor. I basically just want to select objects in the level and print the layer name associated.

I can’t seem to get any info from the layers system for some reason. Sorry, very new to python.

This is what I have:

import unreal

def print_all_actors_and_layers():
    # Get the LayersSubsystem
    layers_subsystem = unreal.LayersSubsystem()
    
    # Get all actors in the current level
    all_actors = unreal.EditorLevelLibrary.get_all_level_actors()
    
    # Get all layer names
    all_layer_names = []
    layers_subsystem.add_all_layer_names_to(all_layer_names)
    
    # Iterate over all actors
    for actor in all_actors:
        actor_label = actor.get_actor_label()
        actor_layers = []
        
        # Check which layers contain the actor
        for layer_name in all_layer_names:
            layer_actors = layers_subsystem.get_actors_from_layer(layer_name)
            if actor in layer_actors:
                actor_layers.append(layer_name)
        
        # Convert actor_layers to strings before joining
        layer_names_str = [layer.to_string() for layer in actor_layers]
        print(f"Actor: {actor_label}, Layers: {', '.join(layer_names_str)}")

# Execute the function
print_all_actors_and_layers()

i dont know python well enough yet to be able to edit this to fix the issue, can anybody explain to me why this doesnt work and how i might fix it?

You shouldn’t have to use the LayersSubsystem for this, Actors have a editor property that can get you the layers it belongs to by calling:

for actor in unreal.EditorLevelLibrary.get_all_level_actors()
   this_actor_layers = actor.get_editor_property('layers')