is the document object
is the object of the currently selected layer
selects the i-th root level layer
returns the number of root level layers
Code: Select all
for i = 0, moho.document:CountLayers() - 1 do
local i = moho.document:Layer(i)
end
traverses the root layers
is a member function that returns true if the layer object is a group type
Code: Select all
local group = moho:LayerAsGroup(layer)
local n = group:CountLayers()
turns the object
layer into a group object (Group, Bone, Switch, or Particle layer), and stores the number of child layers of that group into
n.
This means that with the Layer member function, you only get access to the layers in the level you have selected. At the root, there is moho.document. If there is a group type layer in the root, you need to create a group object to get to the next level of layers.
- moho.document -> Layer(i) gives you the layers at the root, iterated over index value i
- group layer -> Layer(i) gives you the layers inside that group
You seem to want to select each layer directly, but you cannot do this. You need to select a layer through its "group parent" object:
Code: Select all
local layer
= moho:LayerAsGroup(
moho:LayerAsGroup(
moho.document:Layer(i)
):Layer(j)
):Layer(k)
will give you the k-th layer object inside the j-th layer object inside the i-th root layer object.
However, if you want to compare
moho.layer with some layer object, Lua throws an error, unless both objects are the same. The way around this is to use the
pcall function of Lua, as is demonstrated in this layer script:
Code: Select all
function LayerScript(moho)
local layer = moho.layer
for i = 0, moho.document:CountLayers() - 1 do
local layer = moho.document:Layer(i)
--[[
pcall(func, arg1, ...) calls the function 'func',
with arguments 'arg1', etc. and return two values
if no error occurred,
the first value is true,
and the second the return value of the function
if an error occurred,
the first value is false,
and the second value is the error message
--]]
local ok, val = pcall(CmpLayers, layer, moho.layer)
if ok and val then
print("found " .. layer:Name())
end
end
end
function CmpLayers(layer1, layer2)
if layer1 == layer2 then return true end
return false
end
To traverse all layers in the current document, you need to start by traversing the root level (
moho.document), and see if there are any group type layers with one or more child layers. If there are, you should traverse each group type layer, and see if there are any group type layers with one or more child layers. Etc. You can solve this, for instance, with a recursive algorithm and store the layers objects in a table, so you can access each layer object through a simple table without re-traversing the whole layer tree each time. I'm sure there are other methods than recursion. You could scan the current layer level, and push the current layer level to stack if you happen to find a group type layer with one or more child layers, create a new layer level, etc., until there are no more group type layers to scan, and then start popping the previous levels from stack, and continue where you left.
The trick is to device a data structure that captures the layer hierarchy structure, and is still easy to access. You could then add member functions to this object, e.g. to find the recipe how to traverse the layer hierarchy for the current layer (typically, the k-th layer of the j-th layer of the i-th root layer, or something similar).