Just tested "syn_rl_merge_meshinstance_final_4.lua" and it seems to be working well.
Code: Select all
--[[
MeshInstance: Instancing the positions of mesh points on vector layers
1. Use NAME for the name of the source layer, and NAME.<any or no name>.dup for all of the other layers
Like this: source: arm ---> instance: "arm.anyname.dup" or "arm.dup".
2. Set this script for all of the layers (ie. for the original and for the instances)
3. Modify always the source layer only.
]]--
-- written by Fazek -- modified by Synthsin75 (With help from Heyvern)
function LayerScript(moho)
local mesh= moho:Mesh()
if (mesh == nil) then return end
local ownLayer= moho.layer
local name= ownLayer:Name()
local tab
local mi= ownLayer.FA_MeshInst
local len= mesh:CountPoints()
local instName = ""
local a, b
local refname
local curve = nil
local curvePointID = -1
if(string.find(name, ".", 1, true) ~= nil) then
a, b = string.find(name, ".", 1, true)
instName = string.sub(name, b)
--print (instName)
end
if (string.find(name,instName, 1, true) ~= nil) then
--print (instName)
end
if (string.sub(name,-4) == ".dup") then
local refname
--copy instance
if (mi and ownLayer.FA_MeshInstName == name) then
--cached
if (mi[2] ~= moho.frame) then
print("MeshInstance: wrong order - swap the source and the copy!")
return
end
tab= mi[1]
if (mi[3] < len) then len= mi[3] end
for p= 1, len do
local pt= mesh:Point(p - 1)
pt.fPos.x= tab[p][1]
pt.fPos.y= tab[p][2]
--pt.SetCurvature(tab[p][3], moho.layerFrame)
for j = 0, pt:CountCurves() - 1 do --added by Ramon0 from here...
curve, curvePointID = pt:Curve(j, curvePointID)
if tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)) ~= tonumber(tab[p][3]) then
--print("Slave---"..tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)))
--print("Master---"..tonumber(tab[p][3]))
curve:SetCurvature(curvePointID, tab[p][3], moho.layerFrame)
end
end --...to here.
end
else
__,__,pref = string.find(name, "(.-)%.+") --name prefix
local __,__,sufx = string.find(name, "%..+(%..+)") --name suffix
local __,__,sufx2 = string.find(name, ".-(%..+)") --alternate suffix
if (sufx ~= nil) then
refname = pref..sufx --reference name
elseif (sufx == nil) then
refname = pref..sufx2
end
--print(refname)
local srcName= string.sub(refname,1,-5)
-- recursive search for the source
local stack= {}
local sp= 0
local document= moho.document
for i= 0, document:CountLayers() - 1 do
local layer= document:Layer(i)
local parent= nil
local sub= 0
while true do
if (layer:IsGroupType()) then
table.insert(stack, { parent, sub - 1 })
sp= sp + 1
parent= moho:LayerAsGroup(layer)
sub= parent:CountLayers()
elseif (layer:Name() == srcName) then
local mi= layer.FA_MeshInst
if (mi) then
if (mi[2] == moho.frame) then
tab= mi[1]
if (mi[3] < len) then len= mi[3] end
for p= 1, len do
local pt= mesh:Point(p - 1)
pt.fPos.x= tab[p][1]
pt.fPos.y= tab[p][2]
--print(tab[p][1])
for j = 0, pt:CountCurves() - 1 do --added by Ramon0 from here...
curve, curvePointID = pt:Curve(j, curvePointID)
if tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)) ~= tonumber(tab[p][3]) then
--print("Slave---"..tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)))
--print("Master---"..tonumber(tab[p][3]))
curve:SetCurvature(curvePointID, tab[p][3], moho.layerFrame)
end
end --...to here.
end
ownLayer.FA_MeshInst= mi
ownLayer.FA_MeshInstName= refname
return
end
end
end
if (sub > 0) then
sub= sub - 1
layer= parent:Layer(sub)
else
sub= -1
while (sp > 0) do
parent, sub= stack[sp][1], stack[sp][2]
table.remove(stack)
sp= sp - 1
if (sub >= 0) then
layer= parent:Layer(sub)
break
end
end
if (sub < 0) then
break
end
end
end
end
end
else
--source
tab= {}
for p= 0, len - 1 do
local pt= mesh:Point(p)
local curve = nil
local curvePointID = -1
for j = 0, pt:CountCurves() - 1 do --Added by Ramon0 from here...
curve, curvePointID = pt:Curve(j, curvePointID)
--table.insert(tab, { pt.fPos.x, pt.fPos.y, curve:GetCurvature(curvePointID, moho.layerFrame) })
--for k,v in ipairs(tab) do print(k,v) end
end --...to here.
table.insert(tab, { pt.fPos.x, pt.fPos.y, curve:GetCurvature(curvePointID, moho.layerFrame) })
--for k,v in ipairs(tab) do print(k,v) end
end
--trick: FA_MeshInst won't change in the address space (no orphans)
if (mi and not ownLayer.FA_MeshInstName == name)
then
--cached
mi[1]= tab
mi[2]= moho.frame
mi[3]= len
else
ownLayer.FA_MeshInst= { tab, moho.frame, len }
ownLayer.FA_MeshInstName= name
mi= ownLayer.FA_MeshInst
name= name..".dup"
--avoid orphaning: update all dependencies
-- recursive search for the copy
local stack= {}
local sp= 0
local document= moho.document
for i= 0, document:CountLayers() - 1 do
local layer= document:Layer(i)
local parent= nil
local sub= 0
while true do
if (layer:IsGroupType()) then
table.insert(stack, { parent, sub - 1 })
sp= sp + 1
parent= moho:LayerAsGroup(layer)
sub= parent:CountLayers()
elseif (layer:LayerType() == MOHO.LT_VECTOR and layer:Name() == name) then
layer.FA_MeshInst= mi
layer.FA_MeshInstName= name
end
if (sub > 0) then
sub= sub - 1
layer= parent:Layer(sub)
else
sub= -1
while (sp > 0) do
parent, sub= stack[sp][1], stack[sp][2]
table.remove(stack)
sp= sp - 1
if (sub >= 0) then
layer= parent:Layer(sub)
break
end
end
if (sub < 0) then
break
end
end
end
end
end
end
end