Here's the original script:
viewtopic.php?f=12&t=2506
It finds a layer with .sslv at the end and controls that from the layer with the script attached.
But I adjusted it to specifically look for a layer with the same name as well as that ending.
ie. If you attach the script to a layer named "Eyes" then it would control a layer named "Eyes.sslv"
That way you could have multiple sets of these connections in one file without them interfering with each other.
That worked fine, however I then wanted a system where it could look for a folder first and then search through that folder for a slave layer.
Because in plenty of files you might have multiple "Eyes" layers for different characters, rather than always naming them character specific things I wanted the script to be able to find the character folder and then in that folder locate the Eyes layer.
That ran into some performance issues. I am able to make the script work, but on our production with a lot of layers and characters, it just crashes from trying to search through all the files and find results. The curious thing is, it does fine when I don't specify a folder and it runs through all the layers in the whole file to find every matching slave name, but once I'm trying to get it to find the character folder and then the slave, it's much more resource heavy.
I'm new to working in Anime Studio so perhaps I'm unintentionally doing something that's draining on the program, so I was hoping some more savvy person from here could help me out. I'll include the code below and I cleaned it up a bit, but note it includes both code for searching for all results and code for searching to grab layers from certain characters.
Code: Select all
function LayerScript(moho)
--************************************************************
--SlaveSwitch (c)2005 B. Quinn
--V 1.0
--************************************************************
local frame = moho.frame
local layer = moho:LayerAsSwitch(moho.layer)
local target = layer:Name() .. ".sslv"
local targetChar = ""
local slaves = {}
local foundLayers = 1
--splitTarget
splitTarg = string.find(target,'@')
--Check if there's an @, as this indicates that it's seeking a character folder and then a slave
if (splitTarg ~= nil) then
targetChar = string.sub(target, 0, splitTarg - 1)
target = string.sub(target, splitTarg+1, -1)
if (charLayer == nil or charLayer:Name() ~= targetChar) then charLayer = searchOneResult(targetChar) end
end
--Function for adding slaves to the list
function addSlave(layer)
slaves[foundLayers] = layer
foundLayers = foundLayers + 1
end
--This method searches all layers for the target name and adds all answers to the slave list
function searchAllResults(currentLayer, targetName)
local layerCount = currentLayer:CountLayers()
for count = 0, layerCount - 1 do
local testLayer = currentLayer:Layer(count)
if ( testLayer:Name() == targetName ) then addSlave(testLayer) end
testLayer = moho:LayerAsGroup(testLayer)
if (testLayer ~= nil) then
local testCount = testLayer:CountLayers()
if (testCount > 0) then
searchAllResults(testLayer, target)
end
end
end
end
--This method searches for a specific unique layer result and returns it
function searchOneResult(targetName)
local layerCount = moho.document:CountLayers()
local testLayer = nil
for count = 0, layerCount - 1 do
testLayer = moho.document:Layer(count)
if ( testLayer:Name() == "CHARACTERS" ) then
--Now look for the intended character within that layer
testLayer = moho:LayerAsGroup(testLayer)
layerCount = testLayer:CountLayers()
for count = 0, layerCount - 1 do
current = testLayer(count)
if (current:Name() == targetName) then
return current
end
end
end
if (count == layerCount - 1) then return nil end
end
return nil
end
if (layer:LayerType() ~= MOHO.LT_SWITCH) then return end
local switch = layer:SwitchValues()
local ctrlLayer = nil
local error = false
--If there was no split, just search every layer and get every result
if (splitTarg == nil) then
for count = 0, moho.document:CountLayers() - 1 do
local currentLayer = moho.document:Layer(count)
if ( currentLayer:Name() == target ) then addSlave(currentLayer) end
currentLayerGroup = moho:LayerAsGroup(currentLayer)
if (currentLayerGroup ~= nil) then searchAllResults(currentLayerGroup, target) end
end
else
--If there was a split, use the search one result as your starting point and search all the way down from there
local charLayer = searchOneResult(targetChar)
charLayer = moho:LayerAsGroup(charLayer)
searchAllResults(charLayer, target)
end
if (slaves == {}) then return end --If no matching layers were found then it aborts
--And this is where it runs through all the found layers to actually adjust them
for i, slave in ipairs(slaves) do
if switch:HasKey(frame) then
local switchName = layer:SwitchValues():GetValue(frame)
slave:SwitchValues():SetValue(frame, switchName)
else
slave:SwitchValues():DeleteKey(frame)
end
end
end