Download

How to save loaded skeletal mesh actor as .uasset

As the title suggests I am spawning a skeletal mesh actor, loading the skeleton and mesh data at runtime from a using a glTFRuntime plugin (for loading .glb/.gltf files from web).
It works fine loading into the scene however when trying to save/store as an asset it either crashes, or it saves but as an empty .uasset (1kb) file.

I have tried a few different approaches but nothing seems to work.

Here is first approach.

void UReadyPlayerMeFunctionLibrary::SaveAsAsset(USkeletalMesh* skeletalMesh, FString path)
{
	UPackage* Package = Cast<UPackage>(skeletalMesh->GetOuter());
	if (Package && Package != GetTransientPackage())
	{
		const FString Filename = FPackageName::LongPackageNameToFilename(SkeletalMeshContext->SkeletalMeshConfig.SaveToPackage, FPackageName::GetAssetPackageExtension());
		if (UPackage::SavePackage(Package, nullptr, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *Filename))
		{
			FAssetRegistryModule::AssetCreated(SkeletalMeshContext->SkeletalMesh);
		}
	}
}```
Here is second approach. This one almost works, it saves the asset. But as soon as you double click/open it in unreal to inspect the blueprint it crashes the editor. 

void UReadyPlayerMeFunctionLibrary::SaveAsAssetB(USkeletalMesh* skeletalMesh, FString path)
{
//path = “/Game/Avatars/Avatar”
UPackage* Package = CreatePackage(NULL, path);
auto flags = EObjectFlags::RF_Public | EObjectFlags::RF_Standalone;
USkeletalMesh
asset = NewObject(Package, USkeletalMesh::StaticClass(), *FString(“Actor”), flags);
asset = skeletalMesh;
FAssetRegistryModule::AssetCreated(asset);
asset->MarkPackageDirty();
}