Skip to content

Instantly share code, notes, and snippets.

@jovannic
Last active September 29, 2020 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovannic/0e8c87445eda82559011ed53aab6eb19 to your computer and use it in GitHub Desktop.
Save jovannic/0e8c87445eda82559011ed53aab6eb19 to your computer and use it in GitHub Desktop.
Hacky C0/C1 CFrameEditor Plugin
local Selection = game:GetService("Selection")
local CoreGui = game:GetService("CoreGui")
local joints = {}
local attachments = {}
local conCFrame = {}
local conParent = {}
-- Event to deduplicate registered callbacks if plugins are reloaded.
local killmename = "CFrameEditor.Plugin.RefreshCleanupEvent"
local killme = CoreGui:FindFirstChild(killmename)
if killme and not killme:IsA("BindableEvent") then
error(killmename .. " wasn't a BindableEvent. WHY.")
end
if not killme then
killme = Instance.new("BindableEvent")
killme.Name = killmename
killme.Archivable = false
killme.Parent = CoreGui
end
killme:Fire()
local function reset()
-- Disconnect property changed before changing parent
for attachment,v in pairs(attachments) do
conCFrame[attachment]:Disconnect()
conParent[attachment]:Disconnect()
end
conCFrame = {}
conParent = {}
for attachment,v in pairs(attachments) do
attachment:Destroy()
end
attachments = {}
joints = {}
end
local function createAttachment(joint, partProp, cframeProp)
local a = Instance.new("Attachment")
a.Archivable = false
a.Name = joint.Name .. "." .. cframeProp
a.CFrame = joint[cframeProp]
a.Parent = joint[partProp]
conCFrame[a] = a:GetPropertyChangedSignal("CFrame"):Connect(function()
joint[cframeProp] = a.CFrame
end)
conParent[a] = a:GetPropertyChangedSignal("Parent"):Connect(function()
joint[partProp] = a.Parent
end)
attachments[a] = true
end
local valid = false
local selectionConnection = Selection.SelectionChanged:Connect(function()
local selection = Selection:Get()
valid = false
for i,instance in ipairs(selection) do
if instance and instance:IsA("JointInstance") then
if not joints[instance] then
createAttachment(instance, "Part0", "C0")
createAttachment(instance, "Part1", "C1")
joints[instance] = true
end
valid = true
elseif attachments[instance] then
valid = true
end
end
if not valid then
-- Selecting an attachment in 3d view clears selection first. Check again next frame before resetting.
wait()
if not valid then
reset()
end
end
end)
-- Register for cleanup on refresh
local rip
rip = killme.Event:Connect(function()
reset()
selectionConnection:Disconnect()
rip:Disconnect()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment