Access mesh component of a socket preview asset

Hi everyone,

I want to draw some debug data based on the socket preview asset attached to one of the sockets during animation preview. Imagine having a character skeletal mesh animation and then attach a sword or gun to one of the character’s socket. Now, when I add anim notify to the animation I would like to get the sword or gun skeletal/static mesh component to process some data.

Does anyone know how to do that? I checked if there is anything useful in UDebugSkelMeshComponent and checked FPreviewAssetAttachContainer but none contained any information about the preview asset.

Appreciate any help, cheers!

1 Like

Up, I’m also interested !

Edit : after digging in, here is how I got a preview asset Mesh :slight_smile:

#if WITH_EDITOR
	if (GIsEditor)
	{
		// Make preview work in anim preview window.
		if (!PreviewWeaponSocket)
		{
			AAnimationEditorPreviewActor* PreviewActor = Cast<AAnimationEditorPreviewActor>(OwnerActor);
			if (PreviewActor)
			{
				FPreviewAssetAttachContainer AttachedAssets = MeshComp->GetSkeletalMeshAsset()->GetSkeleton()->PreviewAttachedAssetContainer;
				for (auto Iter = AttachedAssets.CreateIterator(); Iter; ++Iter)
				{
					FPreviewAttachedObjectPair& Pair = (*Iter);
					USkeletalMesh* AttachedSkeletalMesh = Cast<USkeletalMesh>(Pair.GetAttachedObject());
					if (!AttachedSkeletalMesh)
						continue;

					USkeletalMeshSocket* Socket = AttachedSkeletalMesh->FindSocket(SocketName);
					if (!Socket)
						continue;

					PreviewWeaponSocket = Socket;
					break;
				}
			}
		}
	}
#endif

The only problem now is to get the transform of this AttachedSkeletalMesh since we only have a pointer to the asset and not a pointer to the scene component.

EDIT #2 :

I found something way more easy, and I don’t know why I did not think about it…
This piece of code lives in a UAnimNotifyState::NotifyTick so you have access to MeshComp which is a USkeletalMeshComponent.

		if (!PreviewWeaponComponent)
		{
			TArray<USceneComponent*> ChildrenComponents;
			MeshComp->GetChildrenComponents(true, ChildrenComponents);

			for (const USceneComponent* Children : ChildrenComponents)
			{
				if (Children->GetAttachSocketName() == TEXT("weapon_rSocket"))
				{
					if (Children->DoesSocketExist(SocketName))
					{
						PreviewWeaponComponent = Children;
					}
				}
			}
		}

		if (PreviewWeaponComponent)
		{
			AnchorTransform = PreviewWeaponComponent->GetSocketTransform(SocketName);
		}
		else
		{
			UE_LOG(LogAnimMontage, Warning, TEXT("HitCollider notify is set to `WeaponMesh` but no weapon detected. Add a preview mesh to the weapon socket."));
		}

I did this to find the transform of a socket on a weapon attached as a preview on the skeleton in the anim montage editor.

1 Like

Wow, I will give this a try. I’m pretty sure I tried something very similar before but perhaps I missed something. Anyway, thank you for sharing!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.