Skip to content

Commit a2ea878

Browse files
committed
jest_import_packed_atlas: fixed CLI and improved ergonomics
1 parent 41eb4de commit a2ea878

File tree

4 files changed

+123
-75
lines changed

4 files changed

+123
-75
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 05/23/2022
2+
### jest_import_packed_atlas
3+
* Fix CLI not working, forgot to merge in the code
4+
* Fix CLI readme instructions
5+
* Improved Ergonomics: We assume that the .json file is in the same directory as the .png file so you can just hit Ok if it is. This is the common case for me
6+
7+
### jest_import_existing_tags.lua
8+
* Removed finsihed TODO comment
9+
110
## 12/31/2020
211
- added merge all tabs script
312
- prefixed/renamed scripts with "jest" so I can find my scripts quicker

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ NOTE: This does not work with rotated atlases
2727

2828
This script can also be ran via cli:
2929
```
30-
aseprite.exe <SPRITE.png> --script-param json="C:\SPRITE.json" --script jest_import_packed_atlas.lua --save-as <RES.ase> --batch
30+
// png and json file don't have to be aboslute paths but script file most likely need absolute path. If it doesn't work just use absolute paths
31+
32+
aseprite.exe <C:\SPRITE.png> --script-param json="C:\SPRITE.json" --script "C:\jest_import_packed_atlas.lua" --batch
3133
```
3234

3335
note: if you see all the colors wrong, you did not have the texture atlas sprite active.

jest_import_existing_tags.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ end
398398

399399
-- start main
400400
local dlg = Dialog()
401-
-- TODO(jest): file picker always unnecessarily asks to overwrite json file. The json file never gets modified. Find a way to fix this
402401
dlg:file{ id="picker",
403402
label="select animation data file(json)",
404403
title="animimation tag importer",

jest_import_packed_atlas.lua

Lines changed: 111 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@ end
333333
It will also import tags if they exist in the json file
334334
335335
This script also has CLI support so you can mass convert your texture atlases:
336-
aseprite.exe <SPRITE.png> --script-param json="C:\SPRITE.json" --script jest_import_packed_atlas.lua --save-as <RES.ase> --batch
336+
NOTE THAT PATHS MUST BE ABSOLUTE, EG:
337+
WARNING! IT WILL SAVE IN THE SAME DIRECTORY AS THE PNG FILE! Becareful if you already have an .ase file in the same directory with the same name as the .png
338+
'--save-as' flag DOES NOT WORK and I'm too lazy to add an export script-param var
339+
png & json paths don't have to be absolute but script path has to, at least these are my problems. Use all absolute paths if you are having issues
340+
aseprite.exe <C:\SPRITE.png> --script-param json="C:\SPRITE.json" --script "C:\jest_import_packed_atlas.lua" --batch
337341
338342
Your .json file can be either in array form, e.g:
339343
@@ -432,88 +436,122 @@ local function is_array(hash)
432436
return res
433437
end
434438

435-
local dlg = Dialog()
436-
dlg:file{
437-
id = "picker",
438-
label = "select animation data file(json)",
439-
title = "animimation tag importer",
440-
load = true,
441-
open = true,
442-
filename = "",
443-
filetypes = {"json"},
444-
onchange = function()
445-
local filepath = dlg.data.picker -- matches id name
446-
local f = io.open(filepath, "r+"):read('a')
447-
local jsondata = json.decode(f)
448-
449-
if jsondata == nil then
450-
print("could not load file " .. filepath)
451-
print("check your json file for errors")
452-
453-
return 1
454-
end
439+
local function build(filepath)
440+
local f = io.open(filepath, "r+"):read('a')
441+
local jsondata = json.decode(f)
455442

456-
local image = app.activeImage
457-
local sprite = app.activeSprite
458-
if not is_array(jsondata.frames) then
459-
-- convert it so we can use it as an array
460-
jsondata.frames = jhash_to_jarray(jsondata.frames)
461-
end
443+
if jsondata == nil then
444+
print("could not load file " .. filepath)
445+
print("check your json file for errors")
462446

463-
local og_size = jsondata.frames[1].sourceSize
464-
local new_sprite = Sprite(og_size.w, og_size.h)
465-
new_sprite:setPalette(sprite.palettes[1])
466-
467-
local frame = new_sprite.frames[1]
468-
for index, aframe in pairs(jsondata.frames) do
469-
local src_loc = aframe.frame
470-
local place_loc = aframe.spriteSourceSize
471-
local dest_img = new_sprite.cels[index].image
472-
frame = new_sprite:newFrame()
473-
draw_section(image, dest_img, src_loc, place_loc, sprite.palettes[1])
474-
if aframe.duration ~= nil then
475-
frame.previous.duration = aframe.duration / 1000
476-
end
447+
return 1
448+
end
449+
450+
local image = app.activeImage
451+
local sprite = app.activeSprite
452+
if not is_array(jsondata.frames) then
453+
-- convert it so we can use it as an array
454+
jsondata.frames = jhash_to_jarray(jsondata.frames)
455+
end
456+
457+
local og_size = jsondata.frames[1].sourceSize
458+
local new_sprite = Sprite(og_size.w, og_size.h)
459+
new_sprite.filename = app.fs.fileTitle(filepath);
460+
new_sprite:setPalette(sprite.palettes[1])
461+
462+
local frame = new_sprite.frames[1]
463+
for index, aframe in pairs(jsondata.frames) do
464+
local src_loc = aframe.frame
465+
local place_loc = aframe.spriteSourceSize
466+
local dest_img = new_sprite.cels[index].image
467+
frame = new_sprite:newFrame()
468+
draw_section(image, dest_img, src_loc, place_loc, sprite.palettes[1])
469+
if aframe.duration ~= nil then
470+
frame.previous.duration = aframe.duration / 1000
477471
end
478-
-- # is the length operator, delete the extra empty frame
479-
new_sprite:deleteFrame(#new_sprite.frames)
472+
end
473+
-- # is the length operator, delete the extra empty frame
474+
new_sprite:deleteFrame(#new_sprite.frames)
480475

481-
-- IMPORTING FRAME TAGS
482-
if jsondata.meta ~= nil and jsondata.meta.frameTags then
483-
for index, tag_data in pairs(jsondata.meta.frameTags) do
484-
local name = tag_data.name
485-
local from = tag_data.from + 1
486-
local to = tag_data.to + 1
487-
local direction = tag_data.direction
476+
-- IMPORTING FRAME TAGS
477+
if jsondata.meta ~= nil and jsondata.meta.frameTags then
478+
for index, tag_data in pairs(jsondata.meta.frameTags) do
479+
local name = tag_data.name
480+
local from = tag_data.from + 1
481+
local to = tag_data.to + 1
482+
local direction = tag_data.direction
488483

489-
-- seems like exporting tags does not export their colors so no way to import them until aseprite starts exporting color of a tag in the output json file
484+
-- seems like exporting tags does not export their colors so no way to import them until aseprite starts exporting color of a tag in the output json file
490485

491-
local new_tag = new_sprite:newTag(from, to)
492-
new_tag.name = name
493-
new_tag.aniDir = direction
486+
local new_tag = new_sprite:newTag(from, to)
487+
new_tag.name = name
488+
new_tag.aniDir = direction
494489

495-
end
496490
end
491+
end
497492

498-
for index, frame_data in pairs(jsondata.frames) do
499-
if frame_data.duration then
500-
local duration = frame_data.duration
493+
for index, frame_data in pairs(jsondata.frames) do
494+
if frame_data.duration then
495+
local duration = frame_data.duration
501496

502-
local current_frame = app.activeFrame
503-
current_frame.duration = duration / 1000 -- duraction in the editor is in seconds, e.g 0.1
504-
app.command.GoToNextFrame()
505-
end
497+
local current_frame = app.activeFrame
498+
current_frame.duration = duration / 1000 -- duraction in the editor is in seconds, e.g 0.1
499+
app.command.GoToNextFrame()
506500
end
501+
end
507502

508-
-- FIXES CEL BOUNDS FROM BEING INCORRECT https://github.com/aseprite/aseprite/issues/3206
509-
app.command.CanvasSize {
510-
ui = false,
511-
left = 0,
512-
top = 0,
513-
right = 0,
514-
bottom = 0,
515-
trimOutside = true
516-
}
517-
dlg:close()
503+
-- FIXES CEL BOUNDS FROM BEING INCORRECT https://github.com/aseprite/aseprite/issues/3206
504+
app.command.CanvasSize {
505+
ui = false,
506+
left = 0,
507+
top = 0,
508+
right = 0,
509+
bottom = 0,
510+
trimOutside = true
511+
}
512+
end
513+
514+
local JKEY = "json"
515+
local from_cli_json_path = app.params[JKEY]
516+
517+
if from_cli_json_path ~= nil then
518+
build(from_cli_json_path)
519+
520+
-- weirdly filename must also have extension despite specifing it in 'filename-format' below
521+
local name = app.fs.filePathAndTitle(from_cli_json_path) .. ".ase"
522+
app.command.saveFileAs {["filename"] = name, ["filename-format"] = ".ase"}
523+
else
524+
local dlg = Dialog()
525+
526+
local PICKER = "picker"
527+
local sprite = app.activeSprite
528+
529+
if sprite == nil then
530+
print("you are not viewing a sprite on the active tab")
531+
return 1
532+
end
533+
534+
-- tries to guess that the png & json are in the same directory
535+
local json_filepath = app.fs.filePathAndTitle(sprite.filename) .. ".json"
536+
local exists_in_same_dir = app.fs.isFile(json_filepath)
537+
if exists_in_same_dir == false then
538+
json_filepath = "" -- not in same dir, look for it yourself
518539
end
519-
}:show()
540+
541+
dlg:file{
542+
id = PICKER,
543+
label = "select animation data file(json)",
544+
title = "animimation tag importer",
545+
load = true,
546+
open = true,
547+
filename = json_filepath,
548+
filetypes = {"json"}
549+
}:button{
550+
id = "Ok",
551+
text = "Ok",
552+
onclick = function()
553+
build(dlg.data[PICKER])
554+
dlg:close()
555+
end
556+
}:show()
557+
end

0 commit comments

Comments
 (0)