forked from FOTMarut/j2a-extract
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathj2a-extract.py
More file actions
86 lines (69 loc) · 3.12 KB
/
Copy pathj2a-extract.py
File metadata and controls
86 lines (69 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
readme = """
Unpack a J2A file
Will unpack a J2A file insto a given folder. The resulting folder structure will
look like this:
[target folder] ->
set-001 ->
animation-001 ->
frame-001.png
frame-002.png
...
animation.settings
animation-002 ->
...
set-002 ->
...
...
Existing files will be overwritten.
"""
import argparse
import pathlib
import yaml
import os
from j2a import J2A
cli = argparse.ArgumentParser(description=readme, prog="J2A Unpacker", formatter_class=argparse.RawDescriptionHelpFormatter)
cli.add_argument("--palettefile", "-p", default="Diamondus_2.pal", help="Palette file to use")
cli.add_argument("j2afile", help="The J2A file to extract")
cli.add_argument("--folder", "-f", default=".",
help="Where to extract the animation data. Defaults to current working directory.")
args = cli.parse_args()
# check if all files we need exist and can be opened properly
destination_folder = pathlib.Path(args.folder)
source_file = pathlib.Path(args.j2afile)
palette_file = pathlib.Path(args.palettefile)
for check_file in (source_file, palette_file):
if not check_file.exists():
print("File '%s' does not exist." % str(check_file))
exit(1)
try:
j2afile = J2A(str(source_file), palette=str(palette_file)).read()
except Exception:
print("Could not open J2A file %s. Is it a valid J2A file?" % source_file.name)
exit(1)
# loop through all animations and unpack their frames
for set_index, set in enumerate(j2afile.sets):
print("Importing set %i..." % set_index)
set_folder = destination_folder.joinpath("set-%s" % str(set_index).zfill(3))
if not set_folder.exists():
os.makedirs(set_folder)
for animation_index, animation in enumerate(set.animations):
print("Unpacking animation %i..." % animation_index)
animation_folder = set_folder.joinpath("animation-%s" % str(animation_index).zfill(3))
if not animation_folder.exists():
os.makedirs(animation_folder)
settings_file = animation_folder.joinpath("animation.settings")
settings = {
"default": {"origin": "0,0", "coldspot": "0,0", "gunspot": "0,0", "tagged": 0, "fps": animation.fps}}
for frame_index, frame in enumerate(animation.frames):
frame_file = animation_folder.joinpath("frame-%s.png" % str(frame_index).zfill(3))
frame_name = frame_file.stem
settings[frame_name] = {
"origin": ",".join([str(coordinate) for coordinate in frame.origin]),
"coldspot": ",".join([str(coordinate) for coordinate in frame.coldspot]),
"gunspot": ",".join([str(coordinate) for coordinate in frame.gunspot]),
"tagged": {False: 0, True: 1}[frame.tagged]
}
j2afile.render_paletted_pixelmap(frame).save(str(frame_file))
with settings_file.open("w") as settings_output:
yaml.dump(settings, settings_output)
print("Done!")