How to convert CFrame Anims to KeyframeSequence?

I’m trying to create a ModuleScript that will allow me to convert CFrame Anims into Poses for a KeyframeSequence. The issue that I am having is that the generated AnimationSequence does not reflect the initial CFrame Anim.

The only issue that I believe is wrong is the calculations for getting the correct Pose.CFrame value, but I’m a little math-illiterate.

I’m not sure how to calculate the Pose.CFrame from the Motor6D’s C0 and C1 values.

I’ve made it obvious where the Pose has it’s CFrame set in the module script.

Place file if needed:
Place1.rbxl (82.0 KB)

Here are my 2 scripts.

CFrame Anim script:

local PoseMOD = require(script.PoseRecorder)

local PoseRecorder = PoseMOD.new(workspace.Rig)

local function AdjustAngles(Data)
	for _, Motor : Motor6D in PoseRecorder.Rig:GetDescendants() do
		if Motor:IsA("Motor6D") then
			
			local Target = Motor.Part1 -- "Left Shoulder" = "Left Arm", etc.
			
			if Target then
				local Adjust_Data = Data[Target.Name]
				
				if Adjust_Data then
					print(Adjust_Data)
					local OffsetData = Adjust_Data.Offset or {0, 0, 0}
					local AnglesData = Adjust_Data.Angles or {0, 0, 0}
					
					local Offset = CFrame.new(OffsetData[1], OffsetData[2], OffsetData[3])
					
					local Angles = CFrame.Angles(
						math.rad(AnglesData[1]),
						math.rad(AnglesData[2]),
						math.rad(AnglesData[3])
					)
					
					Motor.C0 = (Motor.C0 * Offset) * Angles
				end
			end
		end
	end
	
	PoseRecorder:Record()
end

task.wait(3)

for looping = 1, 3 do
	if looping == 3 then
		AdjustAngles({
			["Left Arm"] = {
				["Angles"] = {-45, 0, 15},
				["Offset"] = {0, -0.5, 0}
			},

			["Right Arm"] = {
				["Angles"] = {-45, 0, -15},
				["Offset"] = {0, -0.5, 0}
			},
		})
		
	else
		AdjustAngles({
			["Left Arm"] = {
				["Angles"] = {0, 0, 30},
				["Offset"] = {0, 0, 0}
			},

			["Right Arm"] = {
				["Angles"] = {0, 0, -30},
				["Offset"] = {0, 0, 0}
			},
		})
	end
	
	task.wait(0.3)
end

print(PoseRecorder)

PoseRecorder:GenerateAnimationSequence()

ModuleScript:

local PoseRecorder = {}
PoseRecorder.__index = PoseRecorder


--// Functions
function PoseRecorder.new(rig: Model)
	local self = setmetatable({}, PoseRecorder)
	
	self.Rig = rig
	self.Keyframes = {} -- Motor6D values
	self.TimeLength = 0 -- Animation current time length
	
	return self
end

--// Methods
function PoseRecorder:Clean()
	self.Rig = nil
	
	if typeof(self.Keyframes) == "table" then
		table.clear(self.Keyframes)
	end
	
	self.TimeLength = nil
	self.Keyframes = nil
end

function PoseRecorder:Record()
	local Motor_Data = {}
	
	for _, Joint in self.Rig:GetDescendants() do
		if Joint:IsA("Motor6D") then
			local JointPart1 = Joint.Part1
			
			if not JointPart1 then
				continue
			end
			
			Motor_Data[JointPart1.Name] = {
				C0 = Joint.C0,
				C1 = Joint.C1,
				Part0 = Joint.Part0,
				Part1 = Joint.Part1
			}
		end
	end
	
	table.insert(self.Keyframes, {
		["Time"] = self.TimeLength,
		["Motor_Data"] = Motor_Data
	})
	
	self.TimeLength += 0.25
end

function PoseRecorder:GenerateAnimationSequence()
	local KeyframeSequence = Instance.new("KeyframeSequence")
	
	for _, KeyframeData in self.Keyframes do
		local Keyframe = Instance.new("Keyframe", KeyframeSequence)
		
		Keyframe.Time = KeyframeData.Time
		
		local PoseInstances = {}
		
		for JointName, JointData in KeyframeData.Motor_Data do
			local Pose = Instance.new("Pose")
			
			
			
			
			
			--// This right here is what I'm trying to work out!
			Pose.CFrame = JointData.C0:ToWorldSpace(JointData.C1) -----------
			-----------------------------------------------------------------
			
			
			
			
			Pose.Name = JointName

			PoseInstances[JointName] = {
				["Pose"] = Pose,
				["TargetParent"] = JointData.Part0 and JointData.Part0.Name or nil
			}
		end
		
		local Pose_HRP = Instance.new("Pose")
		Pose_HRP.Name = "HumanoidRootPart"
		Keyframe:AddPose(Pose_HRP)
		
		
		--// Organize Pose Instances Hierarchy
		for _, PoseData in PoseInstances do
			
			if PoseData.Pose and PoseData.Pose:IsA("Pose") then
				
				local Target : Pose = PoseInstances[PoseData.TargetParent]

				if Target then
					if Target.Pose then
						Target.Pose:AddSubPose(PoseData.Pose)
					end
				else
					Pose_HRP:AddSubPose(PoseData.Pose)
				end
			end
		end
		
		KeyframeSequence:AddKeyframe(Keyframe)
	end
	
	KeyframeSequence.Name = DateTime.now().UnixTimestamp
	
	KeyframeSequence.Parent = game.Lighting
	
	warn("Keyframe Sequence generated!", KeyframeSequence)
end



--// Script
return PoseRecorder

Results:


I tried playing around with it, Pose.CFrame changes the Motor6D.Transform and this depends on the initial C0 and C1 of the joint. So I stored the initial C0 and C1 and found the offset of new C0 and C1 to determine the Pose.CFrame

for _, Joint in self.Rig:GetDescendants() do
	if Joint:IsA("Motor6D") then
		self.Original[Joint.Name] = {
			C0 = Joint.C0,
			C1 = Joint.C1,
		}

	end
end

Added this in PoseRecorder.new
Also added a Name key in JointData, which stores the name of the joint, so we can access it later.

And finally the Pose.CFrame

Pose.CFrame = self.Original[JointData.Name].C0:ToObjectSpace(JointData.C0) * self.Original[JointData.Name].C1:ToObjectSpace(JointData.C1)

1 Like

Thank you. I really appreciate you taking the time to look into it haha. Gonna be honest, never would’ve figured out the math on my own, especially when I couldn’t find anything on it.

1 Like

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