[c++] Get Asset List For Actor
Another option we can expose from C++
Another option that might be worth pursuing is exposing C++ functionality to Python. Sometimes, if a Python option just isn't exposed or it's too inconvenient in Python, I'll try to find the C++ code for relevant actions in the Editor.
Unreal has a Right Click Menu option called Browse To Asset which will focus the Content Browser to the underlying asset of an Actor:

A lazy thing I'll do is find the relevant code in the C++ code and expose it to Python in a BP Function Library, here is an example c++ function we could add to gain access to this Menu's functionality:
The .h code:
/** Get the Asset Data List for the provided Actor
* Sourced from UEditorEngine::GetAssetsToSyncToContentBrowser
* @param Actor the actor to query
*/
UFUNCTION(BlueprintCallable, Category = "Python | Utils")
static void GetAssetsForActor(TArray<FAssetData>& Assets, const AActor* Actor);
The .cpp code:
void
UPythonUtilsLibrary::GetAssetsForActor(TArray<FAssetData>& Assets, const AActor* Actor)
{
TArray<UObject*> Objects;
Actor->GetReferencedContentObjects(Objects);
for (UObject* Object : Objects)
{
Assets.Add(FAssetData(Object));
}
TArray<FSoftObjectPath> SoftObjects;
Actor->GetSoftReferencedContentObjects(SoftObjects);
if (SoftObjects.Num())
{
IAssetRegistry& AssetRegistry = IAssetRegistry::GetChecked();
for (const FSoftObjectPath& SoftObject : SoftObjects)
{
FAssetData AssetData = AssetRegistry.GetAssetByObjectPath(SoftObject);
if (AssetData.IsValid())
{
Assets.Add(AssetData);
}
}
}
}
Calling it in Python
With our c++ function compiled and the Plugin enabled, we can use the following logic to call it:
unreal.PythonUtilsLibrary.get_assets_for_actor(actor)
And here is an example inspecting the current 3D Level selection:
EditorActorSubsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
actors = EditorActorSubsystem.get_selected_level_actors()
for actor in actors:
asset_references = unreal.PythonUtilsLibrary.get_assets_for_actor(actor)
if asset_references:
print(
f"Found {len(asset_references)} Content Browser Asset references"
f" for Actor {actor.get_actor_label()}:"
)
for reference in asset_references:
print(
f"\t{reference.package_name} ({reference.asset_class_path.asset_name})"
)
else:
print(f"No Content Browser Assets found for {actor.get_actor_label()}")

Last updated