|
333 | 333 | It will also import tags if they exist in the json file
|
334 | 334 |
|
335 | 335 | 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 |
337 | 341 |
|
338 | 342 | Your .json file can be either in array form, e.g:
|
339 | 343 |
|
@@ -432,88 +436,122 @@ local function is_array(hash)
|
432 | 436 | return res
|
433 | 437 | end
|
434 | 438 |
|
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) |
455 | 442 |
|
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") |
462 | 446 |
|
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 |
477 | 471 | 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) |
480 | 475 |
|
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 |
488 | 483 |
|
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 |
490 | 485 |
|
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 |
494 | 489 |
|
495 |
| - end |
496 | 490 | end
|
| 491 | + end |
497 | 492 |
|
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 |
501 | 496 |
|
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() |
506 | 500 | end
|
| 501 | + end |
507 | 502 |
|
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 |
518 | 539 | 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