Skip to content

Instantly share code, notes, and snippets.

@Madgvox
Last active October 7, 2020 20:26
Show Gist options
  • Save Madgvox/7eda9dec78c0095bda5ae4dbf629b320 to your computer and use it in GitHub Desktop.
Save Madgvox/7eda9dec78c0095bda5ae4dbf629b320 to your computer and use it in GitHub Desktop.
Generates layer and tag data (a la the --data cli option) for an aseprite file, but includes proper layer hierarchy information as well as tag colors.
local spr = app.activeSprite
if not spr then return app.alert "No active sprite" end
local function color_byte_to_string( col )
local hex = ''
while(col > 0)do
local index = math.fmod(col, 16) + 1
col = math.floor(col / 16)
hex = string.sub('0123456789abcdef', index, index) .. hex
end
if(string.len(hex) == 0)then
hex = '00'
elseif(string.len(hex) == 1)then
hex = '0' .. hex
end
return hex
end
local function color_to_string( color )
local hexadecimal = '#'
hexadecimal = hexadecimal .. color_byte_to_string( color.red )
hexadecimal = hexadecimal .. color_byte_to_string( color.green )
hexadecimal = hexadecimal .. color_byte_to_string( color.blue )
hexadecimal = hexadecimal .. color_byte_to_string( color.alpha )
return hexadecimal
end
local blend_mode_strings = {
[ BlendMode.NORMAL ] = "normal",
[ BlendMode.MULTIPLY ] = "multiply",
[ BlendMode.SCREEN ] = "screen",
[ BlendMode.OVERLAY ] = "overlay",
[ BlendMode.DARKEN ] = "darken",
[ BlendMode.LIGHTEN ] = "lighten",
[ BlendMode.COLOR_DODGE ] = "color_dodge",
[ BlendMode.COLOR_BURN ] = "color_burn",
[ BlendMode.HARD_LIGHT ] = "hard_light",
[ BlendMode.SOFT_LIGHT ] = "soft_light",
[ BlendMode.DIFFERENCE ] = "difference",
[ BlendMode.EXCLUSION ] = "exclusion",
[ BlendMode.HSL_HUE ] = "hsl_hue",
[ BlendMode.HSL_SATURATION ] = "hsl_saturation",
[ BlendMode.HSL_COLOR ] = "hsl_color",
[ BlendMode.HSL_LUMINOSITY ] = "hsl_luminosity",
[ BlendMode.ADDITION ] = "addition",
[ BlendMode.SUBTRACT ] = "subtract",
[ BlendMode.DIVIDE ] = "divide",
default = "unknown"
}
local anidir_strings = {
[ AniDir.FORWARD ] = "forward",
[ AniDir.REVERSE ] = "reverse",
[ AniDir.PING_PONG ] = "pingpong",
default = "unknown"
}
local function blend_mode_to_string( blendMode )
local val = blend_mode_strings[ blendMode ]
if val then
return val
else
return val[ "default" ]
end
end
local function anidir_to_string( anidir )
local val = anidir_strings[ anidir ]
if val then
return val
else
return val[ "default" ]
end
end
local indent = ''
local function print_layers( layers )
local old_indent = indent
indent = indent .. ' '
io.write "[\n"
local first = true
for _,l in ipairs( layers ) do
if first then first = false else io.write(',\n') end
local blendMode = l.blendMode
if blendMode == nil then
blendMode = ""
else
blendMode = '"blendMode": "' .. blend_mode_to_string( blendMode ) .. '", '
end
local color = l.color
if color == nil then
color = ""
else
if color.alpha == 0 then
color = ""
else
color = '"color": "' .. color_to_string( color ) .. '", '
end
end
local opacity = l.opacity
if opacity == nil then
opacity = ""
else
opacity = '"opacity": ' .. opacity .. ', '
end
io.write( indent .. '{ "visible": ' .. ( l.isVisible and "true" or "false" ) .. ', "userData": "' .. l.data .. '", ' .. blendMode .. color .. opacity .. '"name": "' .. l.name.. '"' )
if l.isGroup then
io.write( ', "layers": ' )
print_layers( l.layers )
io.write( indent .. '}' )
else
io.write( ' }' )
end
end
io.write( "\n" )
indent = old_indent
io.write( indent .. "]\n" )
end
local function print_tags( tags )
local old_indent = indent
indent = indent .. ' '
io.write "[\n"
local first = true
for _,s in ipairs( tags ) do
if first then first = false else io.write(',\n') end
local direction = s.aniDir
if direction == nil then
direction = ""
else
direction = anidir_to_string( direction )
end
local color = s.color
if color == nil then
color = ""
else
if color.alpha == 0 then
color = ""
else
color = '"color": "' .. color_to_string( color ) .. '", '
end
end
local fromFrame = s.fromFrame
if type(fromFrame) ~= "number" then -- support new Frame construct or legacy
fromFrame = fromFrame.frameNumber
end
local toFrame = s.toFrame
if type(toFrame) ~= "number" then -- support new Frame construct or legacy
toFrame = toFrame.frameNumber
end
io.write( indent .. '{ "name": "' .. s.name.. '", "from": ' .. fromFrame .. ', "to": ' .. toFrame .. ', ' .. color .. '"direction": "' .. direction .. '" }' )
end
io.write( "\n" )
indent = old_indent
io.write( indent .. "]\n" )
end
io.write( '{' )
io.write( '"frameTags":' )
print_tags( spr.tags )
io.write( ',' )
io.write( '"layers":' )
print_layers( spr.layers )
io.write( '}' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment