Re: Group layer Depth Sort: Sort layers by Y-Position
Posted: Mon Feb 08, 2021 1:11 pm
Thank you! It means a lot coming from some of the people I've learnt from (:
Oh nice! Thanks SimplSamhayasidist wrote: ↑Sun Feb 07, 2021 11:06 amI think SimplSam has cracked the "attempt to call method 'Layer' (a nil value)" issue .. http://www.lostmarble.com/forum/viewtop ... 12&t=33764
Code: Select all
-- **************************************************
-- Script Name: AC_SortByTag.lua
-- Original Script: "LK_SortByYPos" and "LK_SortByZScale" by Lukas Krepel
-- Modified and Extended by: Alexandre Cousin
-- Assistance Provided by: ChatGPT
-- Description: This script allows sorting of child layers within a group or bone layer based on X, Y, Z position, or Z scale, determined by a tag in the group's name or a preset mode. It can also reverse the order if a '_Reverse' tag is present.
-- **************************************************
function LayerScript(moho)
-- Check the layer type
if (moho.layer:LayerType() == MOHO.LT_GROUP or moho.layer:LayerType() == MOHO.LT_BONE) then
local thisLayer = moho:LayerAsGroup(moho.layer)
thisLayer:EnableLayerOrdering(true)
local order = {}
local layerOrderChan = thisLayer:GetLayerOrdering()
-- Sorting mode - default is ZScale, or determined by a tag in the layer's name
local sortMode = "ZScale"
local layerName = moho.layer:Name()
-- Conflict resolution: check for ZScale before Z
if string.find(layerName, "_ZScale") then
sortMode = "ZScale"
elseif string.find(layerName, "_X") then
sortMode = "X"
elseif string.find(layerName, "_Y") then
sortMode = "Y"
elseif string.find(layerName, "_Z") then
sortMode = "Z"
end
-- Check if reverse order is needed
local reverseOrder = false
if string.find(layerName, "_Reverse") then
reverseOrder = true
end
for i = thisLayer:CountLayers() - 1, 0, -1 do
local layer = thisLayer:Layer(i)
local sortValue = 0
-- Choose the sorting value based on the selected mode
if sortMode == "X" then
sortValue = layer.fTranslation.value.x
elseif sortMode == "Y" then
sortValue = layer.fTranslation.value.y
elseif sortMode == "Z" then
sortValue = layer.fTranslation.value.z
elseif sortMode == "ZScale" then
sortValue = layer.fScale.value.z
end
table.insert(order, {i, sortValue})
end
-- Sort the layers based on the selected value, with optional reverse
table.sort(order,
function(a, b)
if reverseOrder then
return (a[2] > b[2])
else
return (a[2] < b[2])
end
end)
-- Apply the new layer order
local newOrder = ""
for i, v in ipairs(order) do
newOrder = newOrder .. thisLayer:Layer(v[1]):UUID() .. "|"
end
layerOrderChan:SetValue(0, newOrder)
order = nil
else
print("''AC_SortByTag.lua'' should only be embedded into group or bone layers, ''" .. moho.layer:Name() .. "'' is not a group or bone layer.")
end
end