Skip to content

Include bone support, and fix names with periods not being resolved properly #16

@riesegarder

Description

@riesegarder

Currently, Moonlite doesn't support bones.
I changed the MoonJoint type to include it:

export type MoonJointInfo = {
	Name: string,
	Joint: Motor6D | Bone,
	Parent: MoonJointInfo?,

	Children: {
		[string]: MoonJointInfo,
	},
}

Then I realized joints with periods in names weren't being resolved, because most bones have them. I fixed it by changing the resolveJoints function and the compileItem function:

resolveJoints

local function resolveJoints(target: Instance)
	local joints = {} :: {
		[string]: MoonJointInfo,
	}
	
	local jointsHier = {} :: {
		[string]: MoonJointInfo,
	}
	
	for i, desc: Instance in target:GetDescendants() do
		if desc:IsA("Motor6D") and desc.Active then
			local part1 = desc.Part1
			local name = part1 and part1.Name

			if name then
				joints[name] = {
					Name = name,
					Joint = desc,
					Children = {},
				}
			end
		elseif desc:IsA("Bone") then 
			local name = desc.Name
			if name then
				joints[name] = {
					Name = name,
					Joint = desc,
					Children = {},
				}
			end
		end
	end

	for name1, data1 in joints do
		local joint = data1.Joint
		if joint:IsA("Motor6D") then
			local part0 = joint.Part0

			if part0 then
				local name0 = part0.Name
				local data0 = joints[name0]

				if data0 then
					data0.Children[name1] = data1
					data1.Parent = data0
				end
			end
		elseif joint:IsA("Bone") then 
			local jointParent = joint.Parent

			if jointParent then
				local name0 = jointParent.Name
				local data0 = joints[name0]

				if data0 then
					data0.Children[name1] = data1
					data1.Parent = data0
				end
			end
		end
	end

	for jointName, jointData in joints do 
		local hier = jointData.Name
		local parent = jointData.Parent
		while parent do 
			hier = `{parent.Name}.{hier}`
			parent = parent.Parent
		end
		jointsHier[hier] = jointData
	end

	return jointsHier
end

compileItem (just the part where it deals with rigs)

local function compileItem(self: MoonTrack, item: MoonAnimItem, targets: MoonTarget)
-- snip
	if rig and itemType == "Rig" then
		local jointsHier = resolveJoints(target)

		for i, jointData in rig:GetChildren() do
			if jointData.Name ~= "_joint" then
				continue
			end

			local hier = jointData:FindFirstChild("_hier")
			local default: any = jointData:FindFirstChild("default")
			local keyframes = jointData:FindFirstChild("_keyframes")

			if default then
				default = readValue(default)
			end

			if hier and keyframes then
				local tree = readValue(hier)
				local data = jointsHier[tree]

				if data then
					local joint = data.Joint

					local props: any = {
						Transform = {
							Default = CFrame.identity,
							Static = false,
							Sequence = unpackKeyframes(keyframes, function(c1: CFrame)
								return if joint:IsA("Motor6D") then c1:Inverse() * default else c1
							end),
						},
					}

					targets[joint] = {
						Props = props,
						Target = joint,
					}
				end
			end
		end
	else
-- snip
end

Sorry for putting this here, I don't use github for scripts and don't have the setup to issue a pull request. But I hope it helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions