Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Last active August 10, 2024 22:40
Show Gist options
  • Save MaximumADHD/831bc3ab4b4f8cad3c174fcb7d99884b to your computer and use it in GitHub Desktop.
Save MaximumADHD/831bc3ab4b4f8cad3c174fcb7d99884b to your computer and use it in GitHub Desktop.
--!strict
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local InsertService = game:GetService("InsertService")
local Selection = game:GetService("Selection")
local props = {
"Name",
"Color",
"CFrame",
"Anchored",
"Material",
"CanCollide",
"Transparency",
}
local function toMeshPart(part: Instance?): MeshPart?
if not (part and part:IsA("BasePart")) then
return nil
end
local mesh = part:FindFirstChildWhichIsA("FileMesh", true)
if not mesh then
return nil
end
local id = mesh.MeshId
local scale = mesh.Scale
local isHead = false
local doubleSide = false
local originalSize = part:FindFirstChild("OriginalSize")
if id == "" then
if mesh:IsA("SpecialMesh") and mesh.MeshType.Name == "Head" then
local size = part.Size
local y = (size.Y * scale.Y) / 1.25
local xz = math.min(size.X * scale.X, size.Z * scale.Z) / 1.25
id = "rbxasset://avatar/heads/head.mesh"
scale = Vector3.new(xz, y, xz)
mesh.MeshType = Enum.MeshType.FileMesh
mesh.Scale = scale
mesh.MeshId = id
isHead = true
else
return nil
end
end
local xMod = scale.X < 0 and -1 or 1
local yMod = scale.Y < 0 and -1 or 1
local zMod = scale.Z < 0 and -1 or 1
if xMod < 0 or yMod < 0 or zMod < 0 then
scale *= Vector3.new(xMod, yMod, zMod)
doubleSide = true
end
local meshPart = InsertService:CreateMeshPartAsync(mesh.MeshId, "Box", "Performance")
meshPart.Size = meshPart.MeshSize * scale
meshPart.TextureID = mesh.TextureId
meshPart.DoubleSided = doubleSide
if originalSize and originalSize:IsA("Vector3Value") then
local ratio = originalSize.Value / part.Size
originalSize.Value = meshPart.Size * ratio
end
for i, child in part:GetChildren() do
if child ~= mesh then
if child:IsA("Decal") and not isHead then
local success = pcall(function()
local surface = Instance.new("SurfaceAppearance")
surface.ColorMap = child.Texture
surface.Parent = meshPart
if part.Transparency >= 1 then
part.Transparency = 0
surface.AlphaMode = Enum.AlphaMode.Transparency
end
end)
if success then
continue
end
end
child.Parent = meshPart
end
end
for i, prop in props do
meshPart[prop] = (part :: any)[prop]
end
return meshPart
end
ChangeHistoryService:SetWaypoint("PreMeshPartConvert")
for i, desc in Selection:Get() do
if desc:IsA("Part") then
local meshPart = toMeshPart(desc)
if meshPart then
meshPart.Parent = desc.Parent
desc.Parent = nil
end
end
end
ChangeHistoryService:SetWaypoint("PostMeshPartConvert")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment