Image
-- ************************************************************************************
-- "Rename part of selected layer(s) name(s)"
-- ************************************************************************************

-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "LK_DeleteOffLayers"

-- **************************************************
-- General information about this script
-- **************************************************

LK_DeleteOffLayers = {}

function LK_DeleteOffLayers:Name()
	return "Delete 'OFF' Layers"
end

function LK_DeleteOffLayers:Version()
	return "0.1"
end

function LK_DeleteOffLayers:Description()
	return "Deletes all layers that are turned 'OFF' from current document."
end

function LK_DeleteOffLayers:Creator()
	return "Lukas Krepel, Frame Order"
end

function LK_DeleteOffLayers:UILabel()
	return "Delete 'OFF' Layers"
end

-- **************************************************
-- The guts of this script
-- **************************************************

function LK_DeleteOffLayers:IsEnabled(moho)
	return true
end

LK_DeleteOffLayers.deleteRefLayers = false
LK_DeleteOffLayers.deleteStoryboardLayers = false
LK_DeleteOffLayers.deleteAudioLayers = false
LK_DeleteOffLayers.deleteEmptyGroups = true

function LK_DeleteOffLayers:ShouldDelete(layer)
	local deleteLayer = true
	if layer:IsVisible() then
		deleteLayer = false
	end
	-- * Extra checks:
	if deleteLayer then
		if layer:IsAudioType() then
			if not self.deleteAudioLayers then
				deleteLayer = false -- * Exclude Audio layers.
			end
		end
	end
	if deleteLayer then
		local lowercase = string.lower(layer:Name())
		if string.match(lowercase, "ref") then
			if not self.deleteRefLayers then
				deleteLayer = false -- * Exclude REF layers.
			end
		elseif string.match(lowercase, "stb") then
			if not self.deleteStoryboardLayers then
				deleteLayer = false -- * Exclude STB layers.
			end
		end
	end
	return deleteLayer
end

function LK_DeleteOffLayers:AddEmptyGroupsToTable(moho, deleteLayers)
	local layers = FO_Utilities:AllLayers(moho)
	for i = 1, #layers do
		local layer = layers[i]
		if layer:IsGroupType() then
			if layer:CountLayers() < 1 then
				if not (table.contains(deleteLayers, layer)) then
					table.insert(deleteLayers, layer)
				end
			end
		end
	end
end

function LK_DeleteOffLayers:Run(moho)
	moho.document:PrepUndo("", moho.layer, false)
	moho.document:SetDirty()
	local msg2 = ""
	local deleteLayers = {}
	local layers = {}
	local stack = {}
	local sp = 0
	for i=0, moho.document:CountLayers()-1 do
		local layer = moho.document:Layer(i)
		if self:ShouldDelete(layer) then
			table.insert(deleteLayers, layer)
		else
			local group = nil
			local layerID = 0
			while true do
				if (layer:IsGroupType()) then
					table.insert(stack, {group, layerID -1})
					sp = sp+1
					group = moho:LayerAsGroup(layer)
					layerID = group:CountLayers()
				end
				if (layerID > 0) then
					layerID = layerID -1
					layer = group:Layer--[[ByDepth]](layerID)
					if self:ShouldDelete(layer) then
						table.insert(deleteLayers, layer)
					end
				else
					layerID = -1
					while (sp > 0) do
						group, layerID = stack[sp][1], stack[sp][2]
						table.remove(stack)
						sp = sp -1
						if (layerID >= 0) then
							layer = group:Layer--[[ByDepth]](layerID)
							if self:ShouldDelete(layer) then
								table.insert(deleteLayers, layer)
							end
							break
						end
					end
				end
				if (layerID < 0) then
					break
				end
			end
		end
	end
	if self.deleteEmptyGroups then
		self:AddEmptyGroupsToTable(moho, deleteLayers)
	end
	for i = 1, #deleteLayers do
		local layer = deleteLayers[i]
		msg2 = msg2.."- "..layer:Name().."\n"
	end
	if #deleteLayers == 0 then
		FO_Utilities:Alert("Nothing to delete.")
		return
	else
		local msg1
		if #deleteLayers == 1 then
			msg1 = #deleteLayers.." layer is"
		else
			msg1 = #deleteLayers.." layers are"
		end
		msg1 = msg1.." turned off. Delete?"
		if FO_Utilities:YesNoQuestion("?", msg1, msg2) then
			-- * Yes:
			for i = 1, #deleteLayers do
				local layer = deleteLayers[i]
				if layer ~= nil then
					if i == #deleteLayers and moho.document:CountLayers()==1 then -- * In case the last layer needs to be deleted.
						moho:CreateNewLayer(MOHO.LT_VECTOR):SetName("Layer 1")
					end
					moho:DeleteLayer(layer)
				end
			end
		else
			-- * No:
			return
		end
	end
end

Delete 'OFF' Layers
Listed

Author: Lukas View Script
Script type: Button/Menu

Uploaded: May 26 2023, 07:51

Last modified: Sep 05 2023, 12:53

Script Version: 0.1

Deletes all layers that are set invisible in the Layers Panel
Deletes all layers that are set invisible in the Layers Panel.
(It checks if the eyeball icon is off, not animation property of a layer)
It's useful for getting rid of clutter in documents to speed Moho up, like of screen elements that are leftover from other scenes etc. First just turn all layers off, then run the script.
It gives a list of layers and ask if you want to delete them before it actually does.

Installation Options:

This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 867