(*) I'm not sure if it would work. Shapes are sort based on its average Zdepth based on the point bones virtual Zdepth it has attached to. If a point of shape has not parent bone or ist parent bone has a nill Z value then it returns. That means that a shape can have some points 3D rigged and some not. Even a shape can have all its points with not valid parent bone and its total average Z depth is 0.0. So if a shape is not bound to a valid bone that has a Z vlaue it has always a value of 0.0.
If you want a shape to be always over or below the 3D rig place it in another vector layer in the bone folder. Otherwise you cannot grantee that the 3D model is not "cut" by the static shape.
Code: Select all
-- *******************************************
-- Embedded script for a vector layer
-- SORT THE SHAPES AUTOMATICALLY BASED IN THE
-- AVERAGE Z VALUE OF ITS ASOCIATED POINTS
-- Version: 9.0
-- Created By Genete
-- Usage in combination with 3Dgrid9.lua or 3Dgrid9.lua embedded script for the bone layer.
-- *******************************************
-- Some code need optimization.
function LayerScript(moho)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end
if (mesh:CountShapes() == 0) then
return
end
for i = 0, mesh:CountShapes() -1 do
local cvID, lastID = -1,-1
local segID = -1
local ptmax = 0
local cve
local selectedID
local point
local shp = mesh:Shape(i)
----------------------------------------
shp.Z = 0 -- initializes the Z value of the shape to 0.
----------------------------------------
edmax= shp:CountEdges()-1
if (shp.fFillAllowed) then -- closed shape --> Every point is called two times.
for j=0, edmax do -- for all the edges of the shape
cvID, segID = shp:GetEdge(j,cvID, segID) -- find the curve and the edge (edge = two points)
cve = mesh:Curve(cvID)-- take the curve
ptmax = cve:CountPoints() -1 -- calculate the maximum number of points of the curve
point = cve:Point(segID) -- take the first point
AddZToShape(shp, point, edmax, moho)
if (segID < ptmax) then -- decides if takes the following or the first of the curve.
point = cve:Point(segID+1)
selectedID = segID +1
else
point = cve:Point(0)
selectedID = 0
end
AddZToShape(shp, point, edmax, moho)
end
else --not fillable shape (open curve)
cvID, segID = shp:GetEdge(0,cvID, segID) -- take first curve and edge (0)
cve = mesh:Curve(cvID)-- take the curve
point = cve:Point(segID) -- take the first point
ptmax = cve:CountPoints() -1 -- calculate the maximum number of points of the curve
-- Call the fucntion here two times.
AddZToShape(shp, point, edmax+1, moho)
AddZToShape(shp, point, edmax+1, moho)
if (segID < ptmax) then -- decides if takes the the folowing or the 0.
point = cve:Point(segID+1)
selectedID = segID +1
else
point = cve:Point(0)
selectedID = 0
end
AddZToShape(shp, point, edmax+1, moho)
for j=1, edmax-1 do -- for all the edges except first and last
cvID, segID = shp:GetEdge(j,cvID, segID) -- -- find the curve and the edge (edge = two points)
cve = mesh:Curve(cvID)-- take the curve
ptmax = cve:CountPoints() -1 -- calculate the maximum number of points of the curve
point = cve:Point(segID) -- take the first point
AddZToShape(shp, point, edmax+1, moho)
if (segID < ptmax) then -- decide si toma el siguiente o el cero.
point = cve:Point(segID+1)
selectedID = segID +1
else
point = cve:Point(0)
selectedID = 0
end
AddZToShape(shp, point, edmax+1, moho)
end
cvID, segID = shp:GetEdge(edmax,cvID, segID) -- take last curve and edge
cve = mesh:Curve(cvID)-- take the curve
ptmax = cve:CountPoints() -1 -- calculate the maximum number of points of the curve
point = cve:Point(segID) -- take the first point
AddZToShape(shp, point, edmax+1, moho)
if (segID < ptmax) then -- decide if takes the last point or the beginning of the curve.
point = cve:Point(segID+1)
selectedID = segID +1
else
point = cve:Point(0)
selectedID = 0
end
-- Call the function here two times for the last point .
AddZToShape(shp, point, edmax+1, moho)
AddZToShape(shp, point, edmax+1, moho)
end -- if then else
end -- for i
SortShapes(moho)
end -- function
function AddZToShape(shp, point, edmax, moho)
local zparent = 0
local ptbone = moho:ParentSkeleton():Bone(point.fParent) -- localize the bone parent of the point
if(ptbone == nil) then return end
if(ptbone.fPos.z == nil) then return end
local ptboneparent = moho:ParentSkeleton():Bone(ptbone.fParent)
while (string.find(string.sub(ptboneparent:Name(), -3), ".pt")) -- while its parent were a pt bone
do
zparent = zparent + ptboneparent.fPos.z
ptboneparent = moho:ParentSkeleton():Bone(ptboneparent.fParent) -- localize its parent and assing to it.
end
shp.Z = shp.Z + zparent/edmax/2 + ptbone.fPos.z/edmax/2 --stores the z value divided by edmax and by two because it is called two times.
end
function SortShapes(moho)
local mesh = moho:Mesh()
local maxshp = mesh:CountShapes()
for i=0, maxshp -2 do
local ishp = mesh:Shape(i) -- assume the 0 to i shapes are sorted
for j=i+1, maxshp -1 do --locate the next shapes in the stack.
local jshp = mesh:Shape(j)
if (jshp.Z < ishp.Z) then
for k= 0, j-i-1 do
mesh:LowerShape(j-k, false) -- lower the shape as many times to put it below i shape.
end
end
end
end
end
I can believe I have written that code...
