Skip to content

Commit

Permalink
Add fileselect filepath validation check
Browse files Browse the repository at this point in the history
When the filename field loses focus it will now be checked for validity.
Filenames with no or the wrong suffix will be corrected. If the filename
is cleared, it will be reset to the initial value when the fileselect
was invoked.

Fixes #7.
  • Loading branch information
Road-hog123 committed Aug 30, 2022
1 parent 760ae50 commit cc03506
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions io_export_o3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,33 @@ def invoke(self, context: Context, _: Event) -> set[str]:
# change stem
path = path.with_stem(stem)

# store initial name for use later
self.initial_name = path.name

# set filepath
self.filepath = str(path)
# show the file selector
context.window_manager.fileselect_add(self)
# prevent the operator terminating while in the file selector
return {'RUNNING_MODAL'}

def check(self, _: Context) -> bool:
path = Path(self.filepath)
# check if user cleared the filename box
if path.is_dir():
# reset filename to initial value
path /= self.initial_name
# ensure suffix
path = path.with_suffix(self.filename_ext)
# check for differences
if path == Path(self.filepath):
# nothing to redraw
return False
# set filepath
self.filepath = str(path)
# change to redraw
return True

def draw(self, _: Context) -> None:
pass

Expand Down

0 comments on commit cc03506

Please sign in to comment.