Export cropped layers

test excercise, work in progress and there are couple of issues:

  1. compatible only with v1.3-beta21 and higher since it uses new export option
  2. layer names are NOT sanitized before used as file names. if the layer name contains . / \ it’s bad, but if it contains : expect to see export of empty files.
  3. the script doesn’t care whether layer.isImage is regular layer, reference or background. this may or may not cause problems.

other than that it seems to work ok. if i get to update this thing one day, i’ll post it in this thread and github later.

-- export cropped layers  
-- v0.1 
-- b236 

-- WARNING: compatible with v1.3-beta21 and higher 

-- roadmap/changelog: 
-- TODO: sanitize layer names (seriously) 
-- TODO: option to treat groups as flattened layers 
-- TODO: option to store exported files to user-defined location if the sprite file path is empty 
-- TODO: option to export each frame 


-- TEMP (delete later): 
app.alert("make sure sprite is saved and layer names contain only valid characters") 


-- SETUP: 

local ttl = "export cropped layers v0.1   "

local s = app.sprite 
-- local l = app.layer 
local f = app.frame  
local fn = s.filename 
local dir = app.fs.filePath(fn) --current folder 
local sep = app.fs.pathSeparator --get system separator
local dest = "export" --default output location 

local ok = true 
if dir == '' then -- check if path to file is empty 
	app.alert("save the sprite first") --TODO: delete this and add alternative to set path in dialog 
	ok = false 
end 

-- FUNCTIONs: 

function clean_up(dirty) 
	--!!! this better be doing something! 
	local clean = dirty 
	return clean 
end 

function do_the_thing(src) 
	local sel = src:cel(f) 
	local img = sel.image 
	local fldr = win.data.dest --overwrite default destination with user-provided input 
	local nomen = clean_up(src.name) 
	local filet = dir .. sep .. fldr .. sep .. nomen .. ".png" --!!! src.name can contain bad characters 
	app.command.SaveFileCopyAs {
		ui = false, 
		filename = filet, 
		fromFrame = f, 
		toFrame = f, 
		ignoreEmpty = true, 
		bounds = sel.bounds --available since v1.3-beta21
	}
end 

function do_another_thing(src) 
	-- if we wanted to treat groups differently, 
	-- we should do it here 
end 

function iterate_layers(stck)
	
	for i, layer in ipairs(stck.layers) do 
		-- make sure we're at the right spot: 
		app.layer = layer 
		-- we MUST select range of layers and frames 
		-- to be able to harvest their sweet sweet cels: 
		app.range.frames = f 
		app.range.layers = layer 
		
		if layer.isImage and layer.isVisible and #app.range.cels > 0 then 
			do_the_thing(layer) 
		end 
		
		if layer.isGroup and layer.isVisible then 
			-- do_another_thing(layer) 
			-- or call itself: 
			iterate_layers(layer) 
		end
	end 
	
end

-- DIALOG: 

if ok then 
	win = Dialog{ title = ttl } 
	win:entry{ id = "dest", 
			   label = "folder:", 
			   text = dest, 
			   focus = true 
			   }
	win:newrow() 
	win:button{ text = "RUN!",
				onclick = function()
					iterate_layers(s)
					win:close()
				end 
	}
	win:button{ text = "done",
				onclick = function()
					win:close()
				end 
	}

	win:show{
		wait=false
	}
end 
1 Like