Skip to content

Commit 9053518

Browse files
committed
Change path operations to respect user's file extension
1 parent ff0a43a commit 9053518

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

spritesheetExporter/se_ui.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def __init__(self):
6868
self.addRow(self.write_texture_atlas)
6969

7070
def apply_settings(self, exp: SpritesheetExporter):
71-
exp.export_name = self.name.text().split(".")[0]
72-
exp.export_dir = Path(self.directory.text())
71+
exp.export_path = Path(self.directory.text(), self.name.text())
7372
exp.export_individual_frames = self.export_frames.isChecked()
7473
exp.write_texture_atlas = self.write_texture_atlas.isChecked()
7574

@@ -166,7 +165,7 @@ def __init__(self):
166165
self.dialog.setWindowModality(Qt.NonModal)
167166
self.dialog.setMinimumSize(500, 100)
168167

169-
self.common_settings.name.setText(self.exp.export_name)
168+
self.common_settings.name.setText(self.exp.export_path.name)
170169
self.common_settings.change_dir.clicked.connect(self.change_export_dir)
171170
self.common_settings.reset_dir.clicked.connect(self.reset_export_dir)
172171

spritesheetExporter/spritesheet_exporter.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717

1818
class SpritesheetExporter:
19-
export_name = "spritesheet"
20-
export_dir = Path.home()
19+
export_path = Path.home().joinpath("spritesheet.png")
2120

2221
horizontal = True
2322
rows = DEFAULT_SPACE
@@ -121,22 +120,22 @@ def _set_frame_times(self, doc: Document):
121120
if def_start:
122121
self.start = 0
123122

124-
def make_export_path(self, suffix=""):
125-
return self.export_dir.joinpath(self.export_name + suffix)
126-
127123
def _make_frames_dir(self):
128-
frames_dir = self.make_export_path("_sprites")
124+
frames_dir = self.export_path.with_name(self.export_path.stem + "_sprites")
129125

130126
if frames_dir.exists():
131127
if self.force_new:
132128
export_num = 0
129+
frames_dir = self.export_path.with_suffix(
130+
self.export_path.stem + "_sprites0"
131+
)
133132

134-
def export_candidate():
135-
return self.make_export_path("_sprites" + str(export_num))
136-
137-
while export_candidate().exists():
133+
while frames_dir.exists():
138134
export_num += 1
139-
frames_dir = export_candidate()
135+
frames_dir = self.export_path.with_suffix(
136+
"".join([self.export_path.stem, "_sprites", str(export_num)])
137+
)
138+
140139
frames_dir.mkdir()
141140
else:
142141
frames_dir.mkdir()
@@ -195,7 +194,7 @@ def _process_frames(self, src: Document, dest: Document):
195194
name = layer.name()
196195

197196
if frames_dir is not None:
198-
file_name = f"sprite_{name.zfill(3)}.png"
197+
file_name = "".join(["sprite_", name.zfill(3), self.export_path.suffix])
199198
new_doc = KI.createDocument(
200199
width,
201200
height,
@@ -242,7 +241,7 @@ def _process_frames(self, src: Document, dest: Document):
242241
)
243242

244243
if texture_atlas is not None:
245-
with open(str(self.make_export_path(".json")), "w") as f:
244+
with self.export_path.with_suffix(".json").open("w") as f:
246245
json.dump(texture_atlas, f)
247246

248247
def export(self, debug=False):
@@ -263,18 +262,21 @@ def export(self, debug=False):
263262
width = doc.width()
264263
height = doc.height()
265264

265+
if self.export_path.suffix == "":
266+
self.export_path = self.export_path.with_suffix(".png")
267+
266268
# creating a new document where we'll put our sprites
267269
sheet = KI.createDocument(
268270
width,
269271
height,
270-
self.export_name + ".png",
272+
self.export_path.name,
271273
doc.colorModel(),
272274
doc.colorDepth(),
273275
doc.colorProfile(),
274276
doc.resolution(),
275277
)
276278

277-
sheet.setFileName(str(self.make_export_path(".png")))
279+
sheet.setFileName(str(self.export_path))
278280
num_frames = self._copy_frames(doc, sheet)
279281

280282
# getting a default value for rows and columns

0 commit comments

Comments
 (0)