Totox691

Untitled

Sep 24th, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. function centerActivePolygon(centerX, centerY, useCentroid)
  2.  
  3.     centerX = centerX-.5 or 0
  4.     centerY = centerY-.5 or 0
  5.     -- Par défaut, utilise la bounding box (false), sinon le centroïde (true)
  6.     useCentroid = useCentroid or false
  7.  
  8.     local comp = fusion:GetCurrentComp()
  9.     if not comp then
  10.         print("Error : No Active Composition")
  11.         return
  12.     end
  13.  
  14.     local tool = comp.ActiveTool
  15.     if not tool then
  16.         print("Error : No Active Node")
  17.         return
  18.     end
  19.  
  20.     local toolID = tool:GetAttrs()["TOOLS_RegID"]
  21.     if not toolID or not (toolID == "PolylineMask" or toolID == "BSplineMask" or toolID == "sPolygon") then
  22.         print("Error : The node is not a polygon : ", tool:GetAttrs()["TOOLS_RegID"])
  23.         return
  24.     end
  25.  
  26.     local Center=tool.Center[0]
  27.  
  28.     centerX=Center[1]
  29.     centerY=Center[2]
  30.  
  31.     if toolID ~= "sPolygon" then
  32.         centerX = centerX-.5 or 0
  33.         centerY = centerY-.5 or 0        
  34.     end
  35.  
  36.  
  37.     local settings = tool:SaveSettings()
  38.     local pointsTable = nil
  39.  
  40.     local function findPoints(tbl)
  41.         for k, v in pairs(tbl) do
  42.             if k == "Points" and type(v) == "table" then
  43.                 pointsTable = v
  44.                 return true
  45.             elseif type(v) == "table" then
  46.                 if findPoints(v) then return true end
  47.             end
  48.         end
  49.         return false
  50.     end
  51.  
  52.     if not findPoints(settings) or not pointsTable then
  53.         print("Error : No Points found")
  54.         return
  55.     end
  56.  
  57.     local centerPolyX, centerPolyY
  58.  
  59.     if useCentroid then
  60.         -- Calcul du centroïde (centre géométrique)
  61.         centerPolyX = 0
  62.         centerPolyY = 0
  63.         local numPoints = #pointsTable
  64.        
  65.         if numPoints == 0 then
  66.             print("Error : No points in polygon")
  67.             return
  68.         end
  69.        
  70.         -- Somme de tous les points
  71.         for _, pt in ipairs(pointsTable) do
  72.             centerPolyX = centerPolyX + pt.X
  73.             centerPolyY = centerPolyY + pt.Y
  74.         end
  75.        
  76.         -- Moyenne pour obtenir le centroïde
  77.         centerPolyX = centerPolyX / numPoints
  78.         centerPolyY = centerPolyY / numPoints
  79.        
  80.     else
  81.         -- Calcul du centre de la bounding box (méthode originale)
  82.         local minX, maxX = math.huge, -math.huge
  83.         local minY, maxY = math.huge, -math.huge
  84.         for _, pt in ipairs(pointsTable) do
  85.             if pt.X < minX then minX = pt.X end
  86.             if pt.X > maxX then maxX = pt.X end
  87.             if pt.Y < minY then minY = pt.Y end
  88.             if pt.Y > maxY then maxY = pt.Y end
  89.         end
  90.  
  91.         centerPolyX = (minX + maxX) / 2
  92.         centerPolyY = (minY + maxY) / 2
  93.     end
  94.  
  95.     -- Décalage nécessaire
  96.     local dx = centerX - centerPolyX
  97.     local dy = centerY - centerPolyY
  98.  
  99.     -- Application du décalage
  100.     for _, pt in ipairs(pointsTable) do
  101.         pt.X = pt.X + dx
  102.         pt.Y = pt.Y + dy
  103.     end
  104.  
  105.     tool:LoadSettings(settings)
  106.    
  107.     local method = useCentroid and "centroïde" or "bounding box"
  108.     print(string.format("The Polygon is Centered (%s). Initial Center  (%.3f, %.3f) → New Center: (%.3f, %.3f)",
  109.           method, centerPolyX, centerPolyY, centerX, centerY))
  110.  
  111.        
  112.      if toolID == "sPolygon" then
  113.         tool.Center={0,0,0}
  114.      else
  115.         tool.Center={.5,.5,0}
  116.  
  117.     end
  118.  
  119. end
  120.  
  121.  
  122.  
  123. centerActivePolygon(0.5, 0.5, true) -- true means centered from centroid, else from bounding box
  124.  
Advertisement
Add Comment
Please, Sign In to add comment