A potentially desirable Actor interaction is getting its Content Browser Asset, the source asset of the actor currently in our scene.
Frustratingly, I don't believe a magic bullet option is provided by the Unreal Python API directly that will work for any Actor type. We can still get the Content Browser Asset from just about any Actor, but we will need to make our own convenience function to achieve it.
Getting The Asset By Actor Type
There are a number of different actor classes in Unreal, each with their own method of getting the underlying asset. Here is a collection of several such functions, depending on the actor type.
But first, here is a quick snippet to get the first selected actor in the viewport:
EditorActorSubsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
selection = EditorActorSubsystem.get_selected_level_actors()
actor = selection[0] if selection else None
With an actor reference, here are some of the more common code snippets to get their Content browser Asset:
Blueprint Actor
if isinstance(actor.get_class(), unreal.BlueprintGeneratedClass):
asset_path = actor.get_class().get_outer().get_path_name()
asset = unreal.load_asset(asset_path)
Static Mesh Actor
if isinstance(actor, unreal.StaticMeshActor):
asset = actor.static_mesh_component.static_mesh
Skeletal Mesh Actor
if isinstance(actor, unreal.SkeletalMeshActor):
asset = actor.skeletal_mesh_component.skeletal_mesh_asset
Niagara Actor
if isinstance(actor, unreal.NiagaraActor):
asset = actor.niagara_component.get_asset()
Level Instances Actor
if isinstance(actor, unreal.LevelInstance):
asset = actor.get_world_asset()
Note: In earlier versions of UE5 it was unstable to hold onto unreal.World object references in Python, the full function demo will return this as a string path
Full Function Example
def get_asset_from_actor(actor):
"""
Get the content browser asset path of the given actor,
support must be added for each asset type
parameters:
actor: the actor to process
returns:
the actor's source asset (if supported & found)
"""
asset = None
# the source asset is usually stored on the root component
# and is generally unique per component class type,
# support will need to be added for each Actor class (joy)
if isinstance(actor.get_class(), unreal.BlueprintGeneratedClass):
asset_path = actor.get_class().get_outer().get_path_name()
asset = unreal.load_asset(asset_path)
elif isinstance(actor, unreal.StaticMeshActor):
asset = actor.static_mesh_component.static_mesh
elif isinstance(actor, unreal.SkeletalMeshActor):
asset = actor.skeletal_mesh_component.skeletal_mesh_asset
elif isinstance(actor, unreal.NiagaraActor):
asset = actor.niagara_component.get_asset()
elif isinstance(actor, unreal.LevelInstance):
asset = actor.get_world_asset().get_outer().get_path_name()
else:
unreal.log_warning(
f"\n\tActor {actor.get_actor_label()} has an unknown or unsupported source asset ({actor.get_class()})"
"\n\t\tEither the actor does not have a source asset in the Content Browser"
"\n\t\tor get_asset_from_actor() does not yet support its class type"
)
return asset