From 6d89a0047a78877f27008a78c88b6a0131a62a01 Mon Sep 17 00:00:00 2001 From: mahid Date: Tue, 14 Mar 2023 21:09:06 -0500 Subject: [PATCH 1/3] Added a graceful exit to MTL reading There's been a issue recently where Mineways will name MTLs weirdly. For instance: - "Beach Town.obj" : "Beach_Town.mtl" - "hello there.obj" : "hello_there.mtl" The MTL conversion function simply replaces the extension of self.filepath, assuming that the MTL has the same name. However, since Mineways adds underscores, convert_mtl would throw an exception when attempting to read the file. The current workarounds are to: - Remove the underscores - Don't add spaces to OBJ names (ideally no one should be adding spaces to any filename because of issues like this) - Use jmc2OBJ, which as far as I know doesn't have this weird behavior This is more of a band-aid, and ideally we should check for underscores in the MTL name (we can take advantage of pathlib for this), but for now if an exception occurs due to a weirdly named MTL, we'll just print the error, return False, and call it a day. --- MCprep_addon/world_tools.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/MCprep_addon/world_tools.py b/MCprep_addon/world_tools.py index 8b66e4d5..bbee6368 100644 --- a/MCprep_addon/world_tools.py +++ b/MCprep_addon/world_tools.py @@ -170,8 +170,13 @@ def convert_mtl(filepath): mtl = filepath.rsplit(".", 1)[0] + '.mtl' lines = None copied_file = None - with open(mtl, 'r') as mtl_file: - lines = mtl_file.readlines() + + try: + with open(mtl, 'r') as mtl_file: + lines = mtl_file.readlines() + except Exception as e: + print(e) + return False if bpy.context.scene.view_settings.view_transform in BUILTIN_SPACES: return None From a85b3b6e70a0ad72008b78ba70c8090f5f090c99 Mon Sep 17 00:00:00 2001 From: mahid Date: Wed, 15 Mar 2023 17:38:45 -0500 Subject: [PATCH 2/3] Fixed typo --- MCprep_addon/world_tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MCprep_addon/world_tools.py b/MCprep_addon/world_tools.py index bbee6368..0c418fdc 100644 --- a/MCprep_addon/world_tools.py +++ b/MCprep_addon/world_tools.py @@ -213,7 +213,7 @@ def convert_mtl(filepath): for index, line in enumerate(lines): if line.startswith("map_d "): lines[index] = "# " + line - except Exception as e: + except FileNotFoundError as e: print(e) return False From ba194ab6f18fa41b9b4d46adf6ed7f715455fa1d Mon Sep 17 00:00:00 2001 From: mahid Date: Wed, 15 Mar 2023 17:56:52 -0500 Subject: [PATCH 3/3] Added an MTL existence check at the beginning By taking advantage of the pathlib module (Seriously, why don't we use it more? It makes file operations so much easier and is compatible with the os module), we check to see if the assumed MTL path is valid. If not, we then replace the spaces in the MTL filename with underscores. If still not, then we return False. This assumes that an MTL will always have either the same name as the corresponding OBJ, or that Mineways went ahead and added underscores. --- MCprep_addon/world_tools.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/MCprep_addon/world_tools.py b/MCprep_addon/world_tools.py index 0c418fdc..ac1abf0f 100644 --- a/MCprep_addon/world_tools.py +++ b/MCprep_addon/world_tools.py @@ -167,7 +167,16 @@ def convert_mtl(filepath): Returns: True if success or skipped, False if failed, or None if skipped """ - mtl = filepath.rsplit(".", 1)[0] + '.mtl' + # Check if the MTL exists. If not, then check if it + # uses underscores. If still not, then return False + mtl = Path(filepath.rsplit(".", 1)[0] + '.mtl') + if not mtl.exists(): + mtl_underscores = Path(mtl.parent.absolute()) / mtl.name.replace(" ", "_") + if mtl_underscores.exists(): + mtl = mtl_underscores + else: + return False + lines = None copied_file = None @@ -213,7 +222,7 @@ def convert_mtl(filepath): for index, line in enumerate(lines): if line.startswith("map_d "): lines[index] = "# " + line - except FileNotFoundError as e: + except Exception as e: print(e) return False