Make a shape follow any existing shape bounds not work when source layer has non-zero origin AND non-one scale
Posted: Sat Jun 03, 2023 1:07 pm
Hi all, I try to write a script that create shape that follow the bounds of another layer, I have get this working. But strangely if the layer has both non-zero origin AND non-one scale, the result with be incorrect. But if just either factor is true, the result is also correct. I somehow know that the origin affects the bounds x and y, and no idea how the result should depend on the origin and scale. Can anyone tell me the solution and the relationship of shape bounds, layer scale and translation and origin? Thank you very much!
function FollowBounds:Run(moho)
local document = moho.document
local noTransformLayer = document:LayerByName("No Transform")
noTransformLayer = moho:LayerAsVector(noTransformLayer)
local withTransformLayer = document:LayerByName("With Transform")
withTransformLayer = moho:LayerAsVector(withTransformLayer)
local frameZero = 0
local noTransformLayerBounds = noTransformLayer:Bounds(frameZero)
local noTransformLayerBoundsMinimumX = noTransformLayerBounds.fMin.x
local noTransformLayerBoundsMinimumY = noTransformLayerBounds.fMin.y
local noTransformLayerBoundsMaximumX = noTransformLayerBounds.fMax.x
local noTransformLayerBoundsMaximumY = noTransformLayerBounds.fMax.y
local withTransformLayerBounds = withTransformLayer:Bounds(frameZero)
local withTransformLayerBoundsMinimumX = withTransformLayerBounds.fMin.x
local withTransformLayerBoundsMinimumY = withTransformLayerBounds.fMin.y
local withTransformLayerBoundsMaximumX = withTransformLayerBounds.fMax.x
local withTransformLayerBoundsMaximumY = withTransformLayerBounds.fMax.y
local withTransformLayerScale = withTransformLayer.fScale:GetValue(frameZero)
local withTransformLayerScaleX = withTransformLayerScale.x
local withTransformLayerScaleY = withTransformLayerScale.y
local withTransformLayerTranslation = withTransformLayer.fTranslation:GetValue(frameZero)
local withTransformLayerTranslateX = withTransformLayerTranslation.x
local withTransformLayerTranslateY = withTransformLayerTranslation.y
local withTransformLayerOrigin = withTransformLayer:Origin()
local withTransformLayerOriginX = withTransformLayerOrigin.x
local withTransformLayerOriginY = withTransformLayerOrigin.y
local newShapeLayer = document:LayerByName("New Shape")
newShapeLayer = moho:LayerAsVector(newShapeLayer)
local newShapeLayerOrigin = newShapeLayer:Origin()
local newShapeLayerOriginX = newShapeLayerOrigin.x
local newShapeLayerOriginY = newShapeLayerOrigin.y
local newShapeLayerMesh = newShapeLayer:Mesh()
local pointPosition = LM.Vector2:new_local()
local function setPointPosition(x, y)
pointPosition.x = x
pointPosition.y = y
end
local function addPointToNewShapeLayer(x, y)
setPointPosition(x, y)
newShapeLayerMesh:AddPoint(pointPosition, -1, frameZero)
end
local function appendPointToNewShapeLayer(x, y)
setPointPosition(x, y)
newShapeLayerMesh:AppendPoint(pointPosition, frameZero)
end
addPointToNewShapeLayer(noTransformLayerBoundsMinimumX, noTransformLayerBoundsMaximumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMaximumX, noTransformLayerBoundsMaximumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMaximumX, noTransformLayerBoundsMinimumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMinimumX, noTransformLayerBoundsMinimumY)
addPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMinimumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMaximumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMaximumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMaximumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMaximumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMinimumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMinimumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMinimumY * withTransformLayerScaleY)
end

function FollowBounds:Run(moho)
local document = moho.document
local noTransformLayer = document:LayerByName("No Transform")
noTransformLayer = moho:LayerAsVector(noTransformLayer)
local withTransformLayer = document:LayerByName("With Transform")
withTransformLayer = moho:LayerAsVector(withTransformLayer)
local frameZero = 0
local noTransformLayerBounds = noTransformLayer:Bounds(frameZero)
local noTransformLayerBoundsMinimumX = noTransformLayerBounds.fMin.x
local noTransformLayerBoundsMinimumY = noTransformLayerBounds.fMin.y
local noTransformLayerBoundsMaximumX = noTransformLayerBounds.fMax.x
local noTransformLayerBoundsMaximumY = noTransformLayerBounds.fMax.y
local withTransformLayerBounds = withTransformLayer:Bounds(frameZero)
local withTransformLayerBoundsMinimumX = withTransformLayerBounds.fMin.x
local withTransformLayerBoundsMinimumY = withTransformLayerBounds.fMin.y
local withTransformLayerBoundsMaximumX = withTransformLayerBounds.fMax.x
local withTransformLayerBoundsMaximumY = withTransformLayerBounds.fMax.y
local withTransformLayerScale = withTransformLayer.fScale:GetValue(frameZero)
local withTransformLayerScaleX = withTransformLayerScale.x
local withTransformLayerScaleY = withTransformLayerScale.y
local withTransformLayerTranslation = withTransformLayer.fTranslation:GetValue(frameZero)
local withTransformLayerTranslateX = withTransformLayerTranslation.x
local withTransformLayerTranslateY = withTransformLayerTranslation.y
local withTransformLayerOrigin = withTransformLayer:Origin()
local withTransformLayerOriginX = withTransformLayerOrigin.x
local withTransformLayerOriginY = withTransformLayerOrigin.y
local newShapeLayer = document:LayerByName("New Shape")
newShapeLayer = moho:LayerAsVector(newShapeLayer)
local newShapeLayerOrigin = newShapeLayer:Origin()
local newShapeLayerOriginX = newShapeLayerOrigin.x
local newShapeLayerOriginY = newShapeLayerOrigin.y
local newShapeLayerMesh = newShapeLayer:Mesh()
local pointPosition = LM.Vector2:new_local()
local function setPointPosition(x, y)
pointPosition.x = x
pointPosition.y = y
end
local function addPointToNewShapeLayer(x, y)
setPointPosition(x, y)
newShapeLayerMesh:AddPoint(pointPosition, -1, frameZero)
end
local function appendPointToNewShapeLayer(x, y)
setPointPosition(x, y)
newShapeLayerMesh:AppendPoint(pointPosition, frameZero)
end
addPointToNewShapeLayer(noTransformLayerBoundsMinimumX, noTransformLayerBoundsMaximumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMaximumX, noTransformLayerBoundsMaximumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMaximumX, noTransformLayerBoundsMinimumY)
appendPointToNewShapeLayer(noTransformLayerBoundsMinimumX, noTransformLayerBoundsMinimumY)
addPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMinimumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMaximumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMaximumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMaximumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMaximumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMinimumY * withTransformLayerScaleY)
appendPointToNewShapeLayer(withTransformLayerTranslateX + withTransformLayerBoundsMinimumX * withTransformLayerScaleX, withTransformLayerTranslateY + withTransformLayerBoundsMinimumY * withTransformLayerScaleY)
end