animatable shape stack order (poetbeware)

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
cuebit
Posts: 23
Joined: Tue Jun 06, 2006 11:54 am

animatable shape stack order (poetbeware)

Post by cuebit »

Hi everybody,

I'm looking for the file stack.lua which was posted a few years ago on this forum by poetbeware
It's a script to animate the stack order of shapes in a layer.

The link is dead so.... anybody got this script?

regards,

Maarten
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

There is a HUGE download link for a whole pile of scripts. I will be trying to go through these and put them into some kind of logical order at some point. When I get a chance I will post this script for download.

For now here is the text of the script. Copy this, paste it into a text document and save it as "stack.lua" and you are good to go:

I tried using "size" so this doesn't take up so much space but apparently it doesn't work with the "code" tag.

Code: Select all

function pj_findStackSwitch(moho)
	local current = moho.layer
	local targetName = current:Name().." Stack"
	local parent = current:Parent()
	if (parent == nil) then
		local doc = moho.document
		for i=0,doc:CountLayers() - 1 do
			if (doc:Layer(i):Name() == targetName) then
				return doc:Layer(i)
			end	
		end
	else
		for i=0,parent:CountLayers() do
			if (parent:Layer(i):Name() == targetName) then
				return parent:Layer(i)
			end
		end
	end
	return nil
end



function pj_extractShapeNames(fileName, layerName)
	if (fileName == nil) then
		print("Nil fileName!")
	end
	local f = io.open(fileName, "r")
	if (f == nil) then
		if (pj_global_filename == nil) then
			return nil
		end
		f = io.open(pj_global_filename, "r")
		if (f == nil) then
			return nil
		end
	else
		pj_global_filename = fileName
	end
	local line = f:read("*line")
	local results = {}

	while (true) do
		local line = f:read("*line")

		-- If EOF, return.
		if (line == nil) then
			f:close()
			return results
		end

		-- If the line says layer_type 1, then it's a definition
		-- of a vector sublayer.  See if it has the shapes we want.
		start,finish = string.find(line, "^%s*layer_type 1$")
		if (start ~= nil) then
			pj_processVectorLayer(f, results, layerName)
		end

		-- Otherwise, if the line starts with layer_type, then it's
		-- some other kind of sublayer.  Recurse.
		start, finish = string.find(line, "^%s*layer_type")
		if (start ~= nil) then
			pj_processLayer(f, results, layerName)
		end
	end
end


function pj_processLayer(f, results, layerName)
	while (true) do
		local line = f:read("*line")

		-- If EOF, return.
		if (line == nil) then
			return
		end

		-- If the line contains a single }, it is the end
		-- of the layer structure; return;
		local start,finish
		start,finish = string.find(line, "^%s*%}$")
		if (start ~= nil) then
			return
		end

		-- If the line says layer_type 1, then it's a definition
		-- of a vector sublayer.  See if it has the shapes we want.
		start,finish = string.find(line, "^%s*layer_type 1$")
		if (start ~= nil) then
			pj_processVectorLayer(f, results, layerName)
		end

		-- Otherwise, if the line starts with layer_type, then it's
		-- some other kind of sublayer.  Recurse.
		start,finish = string.find(line, "^%s*layer_type")
		if (start ~= nil) then
			pj_processLayer(f, results, layerName)
		end

	end
end



function pj_processVectorLayer(f, results, layerName)
	while (true) do
		local line = f:read("*line")

		-- If EOF, return.
		if (line == nil) then
			return
		end

		-- If the line contains a single }, it is the end
		-- of the layer structure; return;
		local start,finish
		start,finish = string.find(line, "^%s*%}$")
		if (start ~= nil) then
			return
		end

		-- FIXME: I am assuming that Vector Layers have no child layers.
		-- Check if assumption is valid.

		-- If the line starts with name, then it's the layer's name.
		-- Check to see if the layer's name is the same as the 
		-- given layerName.  If so, we need to scan this layer for
		-- shapes.
		local name
		start,finish,name = string.find(line, "^%s*name \"(.-)\"")
		if (start ~= nil) then
			if (name == layerName) then
				pj_processShapes(f, results)
				return
			end
		end
	end
end


function pj_processShapes(f, results)
	while (true) do
		local line = f:read("*line")

		-- If EOF, return.
		if (line == nil) then
			return
		end

		-- If the line contains a single }, it is the end
		-- of the layer structure; return.
		local start,finish
		start,finish = string.find(line, "^%s*%}$")
		if (start ~= nil) then
			return
		end
		
		-- If the line starts with "shapes" then 
		start,finish = string.find(line, "^%s*shapes ")
		if (start ~= nil) then
			pj_processShapes2(f, results)
			return
		end
	end	
end


function pj_processShapes2(f, results)
	while (true) do
		local line = f:read("*line")
		-- If EOF, return.
		if (line == nil) then
			return
		end

		-- If the line contains a single }, it is the end
		-- of the layer structure; return.
		local start,finish
		start,finish = string.find(line, "^%s*%}$")
		if (start ~= nil) then
			return
		end

		-- If the line looks like this...
		--
		--   "shape1" true false false false
		--
		-- ...then it is finally, finally the name of a shape!
		--
		local name
		start,finish,name = string.find(line, "^%s*\"(.-)\" %a+ %a+ %a+ %a+")
		if (start ~= nil) then
			local last = table.getn(results) + 1
			results[last] = name
		end
	end
end


function pj_findShape(layer, mesh, name)
	for i=0,mesh:CountShapes() - 1 do
		if (mesh:Shape(i).pj_name == name) then
			return i
		end
	end
	return nil
--	idTable = pj_global_shape_id[layer:Name()]
--	if (idTable == nil) then
--		return nil
--	end

--	nameTable = pj_global_shape_name[layer:Name()]
--	if (nameTable == nil) then
--		return nil
--	end

--	local i = 1
--	while (nameTable[i] ~= nil) do
--		if (nameTable[i] == name) then
--			for j=0,mesh:CountShapes() - 1 do
--				local x1 = mesh:Shape(j).fName
--				local x2 = idTable[i]
--				x1 = tostring(x1)
--				x2 = tostring(x2)
--				if (x2 == x1) then
--					return j
--				end
--			end
--			return nil
--		end
--		i = i + 1
--	end
--	return nil
end


function pj_getShapeNames(moho)
	local mesh = moho:Mesh()
	local current = moho.layer
	local shapeNames
	shapeNames = pj_extractShapeNames(moho.document:Path(), current:Name())
--	local idTable = {}
	if (shapeNames == nil) then
		return
	end
	local i = 1
	while (shapeNames[i] ~= nil) do
--		local n = mesh:Shape(i - 1).fName
--		idTable[i] = n
		mesh:Shape(i - 1).pj_name = shapeNames[i]
		i = i + 1
	end
--	pj_global_shape_id[current:Name()] = idTable
--	pj_global_shape_name[current:Name()] = shapeNames
	current.pj_shapes = true
end

function LayerScript(moho)
	local current = moho.layer
	local mesh = moho:Mesh()

	local stackSwitch = pj_findStackSwitch(moho)
	if (stackSwitch == nil) then
		return
	end

	stackSwitch = moho:LayerAsSwitch(stackSwitch)

--	if (pj_global_shape_id == nil) then
--		pj_global_shape_id = {}
--		pj_global_shape_name = {}
--	end

--	if (pj_global_shape_id[current:Name()] == nil) then
--		pj_getShapeNames(moho)
--	end

	if (current.pj_shapes == nil) then
		pj_getShapeNames(moho)
	end

	local value = stackSwitch:GetValue(moho.frame)
	for w in string.gfind(value, "([%a_]+),") do
		local id = pj_findShape(current, mesh, w)
		if (id ~= nil) then
			mesh:RaiseShape(id, true)
		end
	end

end
User avatar
Víctor Paredes
Site Admin
Posts: 5707
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Post by Víctor Paredes »

hi, for what is this script?, just curiosity
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

It controls shape ordering inside a vector layer using a switch layer as a sort of look up reference.

The layers inside the switch don't really have any content. The names of the layers are the names of shapes in the order you want them. So you can change the order of shapes in another layer by selecting different layers in the switch whose name tells the script what order the shapes are in.

I don't think I ever used it myself.

-vern
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Post by chucky »

Oh wow.... that's just the best, I hope it works. happy happy Joy. :D
cuebit
Posts: 23
Joined: Tue Jun 06, 2006 11:54 am

Great

Post by cuebit »

Great Vern,

Thanx alot
Post Reply