So im creating a Skeletal Mesh Asset inside C++ and i keep getting a crash after reopening the editor, with the error saying
Invalid number of texture coordinates
Here is my code for creating the Skeletal Mesh Asset.
// Extract the common/shared skeleton
FBoneHierarchyBuilder HierarchyBuilder;
HierarchyBuilder.ProcessHierarchy(Entity);
// Create the skeletal mesh
const FString TargetMeshName = Entity.Name + TEXT("_SkelMesh");
const FString TargetMeshPath = LongPackagePath;
USkeletalMesh* SkeletalMesh = CastChecked<USkeletalMesh>(CreateNewAsset(USkeletalMesh::StaticClass(), TargetMeshPath, TargetMeshName, Flags));
// Create the skeleton
const FString TargetSkeletonName = Entity.Name + TEXT("_Skeleton");
const FString TargetSkeletonPath = LongPackagePath;
USkeleton* EntitySkeleton = CastChecked<USkeleton>(CreateNewAsset(USkeleton::StaticClass(), TargetSkeletonPath, TargetSkeletonName, Flags));
// Initialize the mesh asset
SkeletalMesh->Materials.Add(UMaterial::GetDefaultMaterial(MD_Surface));
FSkeletalMeshResource* ImportedResource = SkeletalMesh->GetImportedResource();
check(ImportedResource->LODModels.Num() == 0);
ImportedResource->LODModels.Empty();
FStaticLODModel& LODModel = *new (ImportedResource->LODModels) FStaticLODModel();
SkeletalMesh->LODInfo.Empty();
SkeletalMesh->LODInfo.AddZeroed();
SkeletalMesh->LODInfo[0].LODHysteresis = 0.02f;
FSkeletalMeshOptimizationSettings Settings;
// set default reduction settings values
SkeletalMesh->LODInfo[0].ReductionSettings = Settings;
// Pass the number of texture coordinate sets to the LODModel. Ensure there is at least one UV coord
LODModel.NumTexCoords = 1;
// Create the reference skeleton and update LOD0
FReferenceSkeleton& RefSkeleton = SkeletalMesh->RefSkeleton;
HierarchyBuilder.CopyToRefSkeleton(RefSkeleton);
SkeletalMesh->CalculateRequiredBones(LODModel, RefSkeleton, /*BonesToRemove=*/ nullptr);
SkeletalMesh->CalculateInvRefMatrices();
// Initialize the skeleton asset and create sockets for each bone
EntitySkeleton->MergeAllBonesToBoneTree(SkeletalMesh);
TArray<class USkeletalMeshSocket*>& Sockets = EntitySkeleton->Sockets;
for (const FName& BoneName : HierarchyBuilder.AllBones)
{
if (!BoneName.IsNone())
{
USkeletalMeshSocket* Socket = NewObject<USkeletalMeshSocket>(EntitySkeleton, BoneName);
Socket->SocketName = BoneName;
Socket->BoneName = BoneName;
Sockets.Add(Socket);
}
}
// Point the mesh and skeleton at each other
SkeletalMesh->Skeleton = EntitySkeleton;
EntitySkeleton->SetPreviewMesh(SkeletalMesh);
Any help with correctly setting up this Skeletal Mesh Asset is appreciated!