Hi! I am very interested in this script that Lukas did and posted here: viewtopic.php?f=12&t=33324&hilit=y+sort
I'm not refering to the first script he talks about in that post (sort by Y position) but the second one, that is on the bottom of the page 1 (sort by z scale).
The script can be used to create the best layer sorting system I've seen, that can be controled by smartbones without blocking it like layer order does, and that's why I've always been interested in it.
Unfortunately, I keep getting the same console errors and I've been trying to find why but couldn't. The script itself is short and looks simple so maybe Lukas or, if he's still bussy, someone can help me to fix it.
The error I get is: LK_SortByZScale.lua:10 attempt to call method 'Layer' (a nil value) and I also have the same error but on the line 20, sometimes. The errors seem to happen randomly while the animation is playing and a different layer than the one with the script is selected. If the script layer is selected, I think it doesn't happen.
It happens with a frecuency that varies according to the complexity of the rig. The more complex, the more frecuent. From 1 error per 5 minutes to 1 error per second, and the rate is not fixed.
This can be used as a reproduction project. Here it happens each 2-4 minutes or so, while the animation is looping and you have one layer selected different than "grupo n"
https://1drv.ms/u/s!AiyKCw6Xey5TgZR7YbATmV42kdH5Hg
I'm using Moho 12.5 on Windows 10
Need help with "Sort by Z Scale" script
Moderators: Víctor Paredes, Belgarath, slowtiger
Re: Need help with "Sort by Z Scale" script
I believe it is because moho.layer is not properly cast to a group layer type (using LayerAsGroup) and occasionally this trips.
The edit below performs the cast to variable called thisLayer, then uses that variable in place of moho.layer throughout the remainder of the script.
The edit below performs the cast to variable called thisLayer, then uses that variable in place of moho.layer throughout the remainder of the script.
Code: Select all
-- **************************************************
-- Embed this layerscript to a group layer and it's child layers will be ordered by Y translation.
-- **************************************************
function LayerScript(moho)
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, layer, zScale = {}
local layerOrderChan = thisLayer:GetLayerOrdering()
for i = thisLayer:CountLayers()-1, 0, -1 do
layer = thisLayer:Layer(i)
zScale = layer.fScale.value.z
table.insert(order, {i, zScale})
end
table.sort(order,
function(a,b)
return (a[2]<b[2])
end)
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 ("''LK_SortByZScale.lua'' should only be embedded into group or bone layers, ''" .. moho.layer:Name() .. "'' is not a group or bone layer.")
end
end
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Sam
Re: Need help with "Sort by Z Scale" script
I think you did it! Thank you sooo much! I tested it for 10 minutes and didn't get a single error. I will test it more tomorrow but looks like its working perfectly 

- hayasidist
- Posts: 3841
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Need help with "Sort by Z Scale" script
good catch Sam .. I've added a section to the introduction to http://mohoscripting.com/ about "common scripting errors" with this sort of problem as the first (and so far only) entry.
Re: Need help with "Sort by Z Scale" script
Thanks Sam! I missed thatSimplSam wrote: ↑Sat Feb 06, 2021 6:05 pm I believe it is because moho.layer is not properly cast to a group layer type (using LayerAsGroup) and occasionally this trips.
The edit below performs the cast to variable called thisLayer, then uses that variable in place of moho.layer throughout the remainder of the script.
Code: Select all
-- ************************************************** -- Embed this layerscript to a group layer and it's child layers will be ordered by Y translation. -- ************************************************** function LayerScript(moho) 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, layer, zScale = {} local layerOrderChan = thisLayer:GetLayerOrdering() for i = thisLayer:CountLayers()-1, 0, -1 do layer = thisLayer:Layer(i) zScale = layer.fScale.value.z table.insert(order, {i, zScale}) end table.sort(order, function(a,b) return (a[2]<b[2]) end) 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 ("''LK_SortByZScale.lua'' should only be embedded into group or bone layers, ''" .. moho.layer:Name() .. "'' is not a group or bone layer.") end end
