|
| 1 | +// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. |
| 2 | +#include "GPMeshExporterPrivatePCH.h" |
| 3 | +#include "AnimExporterGP.h" |
| 4 | +#include "Animation/AnimSequence.h" |
| 5 | + |
| 6 | +UAnimExporterGP::UAnimExporterGP(const FObjectInitializer& ObjectInitializer) |
| 7 | + : Super(ObjectInitializer) |
| 8 | +{ |
| 9 | + SupportedClass = UAnimSequence::StaticClass(); |
| 10 | + bText = true; |
| 11 | + PreferredFormatIndex = 0; |
| 12 | + FormatExtension.Add(TEXT("gpanim")); |
| 13 | + FormatDescription.Add(TEXT("GP Animation File")); |
| 14 | +} |
| 15 | + |
| 16 | +bool UAnimExporterGP::ExportText(const FExportObjectInnerContext* Context, UObject* Object, const TCHAR* Type, FOutputDevice& Ar, FFeedbackContext* Warn, uint32 PortFlags /*= 0*/) |
| 17 | +{ |
| 18 | + UAnimSequence* AnimSeq = CastChecked<UAnimSequence>(Object); |
| 19 | + |
| 20 | + USkeleton* Skeleton = AnimSeq->GetSkeleton(); |
| 21 | + const FReferenceSkeleton& RefSkeleton = Skeleton->GetReferenceSkeleton(); |
| 22 | + USkeletalMesh* SkelMesh = Skeleton->GetPreviewMesh(); |
| 23 | + if (AnimSeq->SequenceLength == 0.f) |
| 24 | + { |
| 25 | + // something is wrong |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + int32 NumFrames = AnimSeq->GetRawNumberOfFrames(); |
| 30 | + const float FrameRate = NumFrames / AnimSeq->SequenceLength; |
| 31 | + |
| 32 | + // Open another archive |
| 33 | + FArchive* File = IFileManager::Get().CreateFileWriter(*UExporter::CurrentFilename); |
| 34 | + |
| 35 | + // Let's try the header... |
| 36 | + File->Logf(TEXT("{")); |
| 37 | + File->Logf(TEXT("\t\"version\":1,")); |
| 38 | + |
| 39 | + File->Logf(TEXT("\t\"sequence\":{")); |
| 40 | + File->Logf(TEXT("\t\t\"frames\":%d,"), NumFrames); |
| 41 | + File->Logf(TEXT("\t\t\"length\":%f,"), AnimSeq->SequenceLength); |
| 42 | + File->Logf(TEXT("\t\t\"bonecount\":%d,"), RefSkeleton.GetNum()); |
| 43 | + File->Logf(TEXT("\t\t\"tracks\":[")); |
| 44 | + |
| 45 | + bool firstOutput = false; |
| 46 | + |
| 47 | + for (int32 BoneIndex = 0; BoneIndex < RefSkeleton.GetNum(); ++BoneIndex) |
| 48 | + { |
| 49 | + //int32 BoneTreeIndex = Skeleton->GetSkeletonBoneIndexFromMeshBoneIndex(SkelMesh, BoneIndex); |
| 50 | + int32 BoneTrackIndex = Skeleton->GetAnimationTrackIndex(BoneIndex, AnimSeq, true); |
| 51 | + |
| 52 | + if (BoneTrackIndex == INDEX_NONE && BoneIndex != 0) |
| 53 | + { |
| 54 | + // If this sequence does not have a track for the current bone, then skip it |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (firstOutput) |
| 59 | + { |
| 60 | + File->Logf(TEXT("\t\t\t},")); |
| 61 | + } |
| 62 | + |
| 63 | + firstOutput = true; |
| 64 | + |
| 65 | + File->Logf(TEXT("\t\t\t{")); |
| 66 | + File->Logf(TEXT("\t\t\t\t\"bone\":%d,"), BoneIndex); |
| 67 | + File->Logf(TEXT("\t\t\t\t\"transforms\":[")); |
| 68 | + float AnimTime = 0.0f; |
| 69 | + float AnimEndTime = AnimSeq->SequenceLength; |
| 70 | + // Subtracts 1 because NumFrames includes an initial pose for 0.0 second |
| 71 | + double TimePerKey = (AnimSeq->SequenceLength / (NumFrames - 1)); |
| 72 | + const float AnimTimeIncrement = TimePerKey; |
| 73 | + |
| 74 | + bool bLastKey = false; |
| 75 | + // Step through each frame and add the bone's transformation data |
| 76 | + while (!bLastKey) |
| 77 | + { |
| 78 | + const TArray<FBoneNode>& BoneTree = Skeleton->GetBoneTree(); |
| 79 | + |
| 80 | + FTransform BoneAtom; |
| 81 | + if (BoneTrackIndex != INDEX_NONE) |
| 82 | + { |
| 83 | + AnimSeq->GetBoneTransform(BoneAtom, BoneTrackIndex, AnimTime, true); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + BoneAtom.SetIdentity(); |
| 88 | + } |
| 89 | + |
| 90 | + bLastKey = AnimTime >= AnimEndTime; |
| 91 | + |
| 92 | + File->Logf(TEXT("\t\t\t\t\t{")); |
| 93 | + |
| 94 | + FQuat rot = BoneAtom.GetRotation(); |
| 95 | + // For the root bone, we need to fix-up the rotation because Unreal exports |
| 96 | + // animations with Y-forward for some reason (maybe because Maya?) |
| 97 | + if (BoneIndex == 0) |
| 98 | + { |
| 99 | + FQuat addRot(FVector(0.0f, 0.0f, 1.0f), -1.57f); |
| 100 | + rot = addRot * rot; |
| 101 | + } |
| 102 | + File->Logf(TEXT("\t\t\t\t\t\t\"rot\":[%f,%f,%f,%f],"), rot.X, rot.Y, rot.Z, rot.W); |
| 103 | + FVector trans = BoneAtom.GetTranslation(); |
| 104 | + |
| 105 | + // Sanjay: If it's skeleton retargeting, change the translation to be from the ref pose skeleton |
| 106 | + if (BoneTree[BoneIndex].TranslationRetargetingMode == EBoneTranslationRetargetingMode::Skeleton) |
| 107 | + { |
| 108 | + const FTransform& BoneTransform = RefSkeleton.GetRefBonePose()[BoneIndex]; |
| 109 | + trans = BoneTransform.GetTranslation(); |
| 110 | + } |
| 111 | + |
| 112 | + File->Logf(TEXT("\t\t\t\t\t\t\"trans\":[%f,%f,%f]"), trans.X, trans.Y, trans.Z); |
| 113 | + |
| 114 | + if (!bLastKey) |
| 115 | + { |
| 116 | + File->Logf(TEXT("\t\t\t\t\t},")); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + File->Logf(TEXT("\t\t\t\t\t}")); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + AnimTime += AnimTimeIncrement; |
| 125 | + } |
| 126 | + |
| 127 | + File->Logf(TEXT("\t\t\t\t]"), BoneIndex); |
| 128 | + } |
| 129 | + |
| 130 | + File->Logf(TEXT("\t\t\t}")); |
| 131 | + File->Logf(TEXT("\t\t]")); |
| 132 | + File->Logf(TEXT("\t}")); |
| 133 | + |
| 134 | + File->Logf(TEXT("}")); |
| 135 | + delete File; |
| 136 | + |
| 137 | + return true; |
| 138 | +} |
0 commit comments