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

ScriptName = "SZ_TopLayersToPngs"

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

SZ_TopLayersToPngs = {}

function SZ_TopLayersToPngs:Name()
	return self:Localize('UILabel')
end

function SZ_TopLayersToPngs:Version()
	return '1.0'
end

function SZ_TopLayersToPngs:UILabel()
	return self:Localize('UILabel')
end

function SZ_TopLayersToPngs:Creator()
	return 'Stan'
end

function SZ_TopLayersToPngs:Description()
	return self:Localize('Description')
end

-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************

function SZ_TopLayersToPngs:IsRelevant(moho)
	return true
end

function SZ_TopLayersToPngs:IsEnabled(moho)
	return true
end

-- **************************************************
-- SZ_TopLayersToPngsDialog
-- **************************************************

local SZ_TopLayersToPngsDialog = {}

function SZ_TopLayersToPngsDialog:new()
	local d = LM.GUI.SimpleDialog(SZ_TopLayersToPngs:Localize('UILabel'), SZ_TopLayersToPngsDialog)
	local l = d:GetLayout()

	d.text1 = LM.GUI.DynamicText(SZ_TopLayersToPngs:Localize('text1'), 0)
	l:AddChild(d.text1, LM.GUI.ALIGN_LEFT, 0)

	d.text2 = LM.GUI.DynamicText(SZ_TopLayersToPngs:Localize('text2'), 0)
	l:AddChild(d.text2, LM.GUI.ALIGN_LEFT, 0)
	return d
end

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

function SZ_TopLayersToPngs:Run(moho)
	local dlog = SZ_TopLayersToPngsDialog:new(moho)
	if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then return end
	
	local folder = LM.GUI.SaveFile(SZ_TopLayersToPngs:Localize('saveFile'))
	
	if (folder == "") then return end
	
	if not os.execute('mkdir "' .. folder .. '"') then return end
	
	
	local countLayers = moho.document:CountLayers()
	local length = string.len(tostring(countLayers))
	for i = countLayers-1, 0, -1 do
		local layer = moho.document:Layer(i)
		-- turn off all layers:
		self:SetAllLayersVisible(moho, false)
		-- turn on current layer:
		layer:SetVisible(true)
		local layerName = layer:Name()
		local ordinal = countLayers - i
		moho:FileRender(folder .. '\\' .. string.format('%0'..length..'d - %s.png', ordinal, layerName))
	end
	-- turn on all layers:
	self:SetAllLayersVisible(moho, true)	
end

function SZ_TopLayersToPngs:SetAllLayersVisible(moho, b)
	for n = 0, moho.document:CountLayers()-1 do
		local layer = moho.document:Layer(n)
		layer:SetVisible(b)
	end
end

-- **************************************************
-- Localization
-- **************************************************

function SZ_TopLayersToPngs:Localize(text)
	local phrase = {}

	phrase['Description'] = 'Export top-level layers as separate PNG files'
	phrase['UILabel'] = 'Export Top Layers to PNGs'
	
	phrase['saveFile'] = 'Select where to save files'

	phrase['text1'] = 'This will render the top-level layers as separate PNG files.'
	phrase['text2'] = 'Click OK to continue'

	local fileWord = MOHO.Localize("/Menus/File/File=File")
	if fileWord == "Файл" then
		phrase['Description'] = 'Экспортирует верхние слои проекта в виде отдельных PNG'
		phrase['UILabel'] = 'Экспорт верхних слоев в PNG'
		
		phrase['saveFile'] = 'Укажите папку для сохранения'

		phrase['text1'] = 'Верхние слои проекта будут экспортированы в виде PNG файлов.'
		phrase['text2'] = 'Нажмите OK чтобы продолжить.'
	end

	return phrase[text]
end

Top Layers to PNGs
Listed

Author: Stan View Script
Script type: Button/Menu

Uploaded: Dec 23 2020, 19:02

Export the top layers of the project as separate PNG files with transparency. This allows you to import your project into Photoshop as separate raster layers.
Image

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: 50