Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function centerActivePolygon(centerX, centerY, useCentroid)
- centerX = centerX-.5 or 0
- centerY = centerY-.5 or 0
- -- Par défaut, utilise la bounding box (false), sinon le centroïde (true)
- useCentroid = useCentroid or false
- local comp = fusion:GetCurrentComp()
- if not comp then
- print("Error : No Active Composition")
- return
- end
- local tool = comp.ActiveTool
- if not tool then
- print("Error : No Active Node")
- return
- end
- local toolID = tool:GetAttrs()["TOOLS_RegID"]
- if not toolID or not (toolID == "PolylineMask" or toolID == "BSplineMask" or toolID == "sPolygon") then
- print("Error : The node is not a polygon : ", tool:GetAttrs()["TOOLS_RegID"])
- return
- end
- local Center=tool.Center[0]
- centerX=Center[1]
- centerY=Center[2]
- if toolID ~= "sPolygon" then
- centerX = centerX-.5 or 0
- centerY = centerY-.5 or 0
- end
- local settings = tool:SaveSettings()
- local pointsTable = nil
- local function findPoints(tbl)
- for k, v in pairs(tbl) do
- if k == "Points" and type(v) == "table" then
- pointsTable = v
- return true
- elseif type(v) == "table" then
- if findPoints(v) then return true end
- end
- end
- return false
- end
- if not findPoints(settings) or not pointsTable then
- print("Error : No Points found")
- return
- end
- local centerPolyX, centerPolyY
- if useCentroid then
- -- Calcul du centroïde (centre géométrique)
- centerPolyX = 0
- centerPolyY = 0
- local numPoints = #pointsTable
- if numPoints == 0 then
- print("Error : No points in polygon")
- return
- end
- -- Somme de tous les points
- for _, pt in ipairs(pointsTable) do
- centerPolyX = centerPolyX + pt.X
- centerPolyY = centerPolyY + pt.Y
- end
- -- Moyenne pour obtenir le centroïde
- centerPolyX = centerPolyX / numPoints
- centerPolyY = centerPolyY / numPoints
- else
- -- Calcul du centre de la bounding box (méthode originale)
- local minX, maxX = math.huge, -math.huge
- local minY, maxY = math.huge, -math.huge
- for _, pt in ipairs(pointsTable) do
- if pt.X < minX then minX = pt.X end
- if pt.X > maxX then maxX = pt.X end
- if pt.Y < minY then minY = pt.Y end
- if pt.Y > maxY then maxY = pt.Y end
- end
- centerPolyX = (minX + maxX) / 2
- centerPolyY = (minY + maxY) / 2
- end
- -- Décalage nécessaire
- local dx = centerX - centerPolyX
- local dy = centerY - centerPolyY
- -- Application du décalage
- for _, pt in ipairs(pointsTable) do
- pt.X = pt.X + dx
- pt.Y = pt.Y + dy
- end
- tool:LoadSettings(settings)
- local method = useCentroid and "centroïde" or "bounding box"
- print(string.format("The Polygon is Centered (%s). Initial Center (%.3f, %.3f) → New Center: (%.3f, %.3f)",
- method, centerPolyX, centerPolyY, centerX, centerY))
- if toolID == "sPolygon" then
- tool.Center={0,0,0}
- else
- tool.Center={.5,.5,0}
- end
- end
- centerActivePolygon(0.5, 0.5, true) -- true means centered from centroid, else from bounding box
Advertisement
Add Comment
Please, Sign In to add comment