-- ******************************************* -- Embedded script for a vector layer -- SORT THE SHAPES AUTOMATICALLY BASED IN THE -- X value of its associated bones. -- Version: 2.0 -- Created By Genete -- -- ******************************************* -- Some code need optimization. function LayerScript(moho) local mesh = moho:Mesh() local skel = moho:ParentSkeleton() if (mesh == nil) then return end if (skel == nil) then return end local maxshp = mesh:CountShapes() local maxbones = skel:CountBones() if (maxshp == 0) then return end if (maxbones == 0) then return end TBonesSet ={} for s = 0, maxshp-1 do TBonesSet[s]= -1 end for b = 0, maxbones-1 do local bname = skel:Bone(b):Name() local subName = string.sub(bname, -3) if (subName == ".sh") then local name = string.sub(bname, 1, -4) local sID = tonumber(name) if (sID ~= nil) then if (sID < maxshp) then TBonesSet[sID] = b end end end end local bool = false for b in TBonesSet do if (skel:Bone(TBonesSet[b]).fAnimPos:HasKey(moho.frame)) then bool = true break end end if (bool == false) then return end local jz = 0 local iz = 0 local bonei = nil local bonej = nil -- Perform the sort of the shapes for i=0, maxshp -2 do -- If there is only one shape there is not sort operation -- assume the 0 to i shapes are sorted local bID = nil biID = TBonesSet[i] iz = 0 if (biID >= 0) then iz = skel:Bone(biID).fPos.x -- that's the Z value end for j=i+1, maxshp -1 do --locate the next shapes in the stack. local bjID = nil bjID = TBonesSet[j] jz = 0 if (bjID >= 0) then jz = skel:Bone(bjID).fPos.x -- that's the Z value end if (jz < iz) then for k= 0, j-i-1 do -- print ("lowering shape : ".. j-k) local bonejk1ID local bonejk1 local bonejk mesh:LowerShape(j-k, false) -- lower the shape as many times to put it below i shape. bonejk1ID = TBonesSet[j-k-1] TBonesSet[j-k-1] = TBonesSet[j-k] TBonesSet[j-k] = bonejk1ID bonejk1 = TBonesSet[j-k-1] bonejk = TBonesSet[j-k] if (bonejk1 >= 0) then skel:Bone(bonejk1):SetName(j-k-1 .. ".sh") end if (bonejk >= 0) then skel:Bone(bonejk):SetName(j-k .. ".sh") end end end end end end -- function