Totox691

Davinci fusion - Polygon Centering

Sep 25th, 2025
109
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | Source Code | 0 0
  1. function centerActivePolygon(centerX, centerY, useCentroid)
  2.    local time=comp.CurrentTime
  3.  
  4.     centerX = centerX-.5 or 0
  5.     centerY = centerY-.5 or 0
  6.     -- Par défaut, utilise la bounding box (false), sinon le centroïde (true)
  7.     useCentroid = useCentroid or false
  8.  
  9.     local comp = fusion:GetCurrentComp()
  10.     if not comp then
  11.         print("Error : No Active Composition")
  12.         return
  13.     end
  14.  
  15.     local tool = comp.ActiveTool
  16.     if not tool then
  17.         print("Error : No Active Node")
  18.         return
  19.     end
  20.  
  21.     local toolID = tool.ID
  22.  
  23.  
  24.     if not toolID or not (toolID == "PolylineMask" or toolID == "BSplineMask" or toolID == "sPolygon") then
  25.         print("Error : The node is not a polygon : ", tool.ID)
  26.         return
  27.     end
  28.  
  29.  
  30.     local Center=tool.Center[0]
  31.  
  32.     centerX=Center[1]
  33.     centerY=Center[2]
  34.  
  35.     if toolID ~= "sPolygon" then
  36.         centerX = centerX-.5 or 0
  37.         centerY = centerY-.5 or 0        
  38.     end
  39.  
  40.  
  41.     local settings = tool:SaveSettings()
  42.     local pointsTable = nil
  43.     local keyframes={}
  44.  
  45.  
  46.     -- on s'assure que les keyframes trouvés sont ceux d'un polyline
  47.     function KeyframesisPolyLine(kf)
  48.         for k, v in pairs(kf) do
  49.             if not v.Value then return end -- pas de value, c'est pas une polyline
  50.             if v.Value.__ctor == "Polyline" then
  51.                 keyframes = kf
  52.                 return
  53.             end
  54.         end
  55.      
  56.     end
  57.  
  58.     -- recherche la table des points
  59.     local function findPoints(tbl)
  60.         for k, v in pairs(tbl) do
  61.             if k == "KeyFrames" then
  62.                 KeyframesisPolyLine(v)
  63.                
  64.                
  65.             end
  66.             if k == "Points" and type(v) == "table" then
  67.                 pointsTable = v
  68.                 return true
  69.             elseif type(v) == "table" then
  70.                 if findPoints(v) then return true end
  71.             end
  72.         end
  73.         return false
  74.     end
  75.  
  76.     if not findPoints(settings) or not pointsTable then
  77.         print("Error : No Points found")
  78.         return
  79.     end
  80.  
  81.     -- des keyframes sont posées, on utilise la keyframe courante pour extraire les points
  82.     if keyframes[time] then
  83.             pointsTable = keyframes[time].Value.Points
  84.              --dump(pointsTable)
  85.               --dump(keyframes[time].Value)
  86.     end
  87.  
  88.     local centerPolyX, centerPolyY
  89.  
  90.     if useCentroid then
  91.         -- Calcul du centroïde (centre géométrique)
  92.         centerPolyX = 0
  93.         centerPolyY = 0
  94.         local numPoints = #pointsTable
  95.        
  96.         if numPoints == 0 then
  97.             print("Error : No points in polygon")
  98.             return
  99.         end
  100.        
  101.         -- Somme de tous les points
  102.         for _, pt in ipairs(pointsTable) do
  103.             centerPolyX = centerPolyX + pt.X
  104.             centerPolyY = centerPolyY + pt.Y
  105.         end
  106.        
  107.         -- Moyenne pour obtenir le centroïde
  108.         centerPolyX = centerPolyX / numPoints
  109.         centerPolyY = centerPolyY / numPoints
  110.        
  111.     else
  112.         -- Calcul du centre de la bounding box (méthode originale)
  113.         local minX, maxX = math.huge, -math.huge
  114.         local minY, maxY = math.huge, -math.huge
  115.         for _, pt in ipairs(pointsTable) do
  116.             if pt.X < minX then minX = pt.X end
  117.             if pt.X > maxX then maxX = pt.X end
  118.             if pt.Y < minY then minY = pt.Y end
  119.             if pt.Y > maxY then maxY = pt.Y end
  120.         end
  121.  
  122.         centerPolyX = (minX + maxX) / 2
  123.         centerPolyY = (minY + maxY) / 2
  124.     end
  125.  
  126.     -- Décalage nécessaire
  127.     local dx = centerX - centerPolyX
  128.     local dy = centerY - centerPolyY
  129.  
  130.     -- Application du décalage
  131.     for _, pt in ipairs(pointsTable) do
  132.         pt.X = pt.X + dx
  133.         pt.Y = pt.Y + dy
  134.     end
  135.  
  136.     tool:LoadSettings(settings)
  137.    
  138.     local method = useCentroid and "centroïde" or "bounding box"
  139.     print(string.format("The Polygon is Centered (%s). Initial Center  (%.3f, %.3f) → New Center: (%.3f, %.3f)",
  140.           method, centerPolyX, centerPolyY, centerX, centerY))
  141.  
  142.        
  143.      if toolID == "sPolygon" then
  144.         tool.Center={0,0,0}
  145.      else
  146.         tool.Center={.5,.5,0}
  147.  
  148.     end
  149.  
  150. end
  151.  
  152.  
  153.  
  154. centerActivePolygon(0.5, 0.5, true) -- true means centered from centroid, else from bounding box
  155.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment