fixed filepath handling for Linux
authorAlexander N <alpha-beta-release@gmx.net>
Thu, 5 Sep 2013 17:37:51 +0000 (17:37 +0000)
committerAlexander N <alpha-beta-release@gmx.net>
Thu, 5 Sep 2013 17:37:51 +0000 (17:37 +0000)
- added toGoodFilePath, to conversion from Windows to Linux (MacOS?)

io_scene_fpx/__init__.py
io_scene_fpx/fpx_import.py
io_scene_fpx/fpx_resource.blend
io_scene_fpx/fpx_spec.py
io_scene_fpx/fpx_utils.py

index 114c536..cf6f7d0 100644 (file)
@@ -34,7 +34,7 @@ bl_info = {
     'name': "Future Pinball FPx format (.fpm/.fpl/.fpt)",
     'description': "Import Future Pinball Model, Library and Table files",
     'author': "Alexander Nussbaumer",
-    'version': (0, 0, 309032),
+    'version': (0, 0, 309051),
     'blender': (2, 68, 0),
     'location': "File > Import",
     'warning': "",
@@ -53,8 +53,10 @@ bl_info = {
 #       models got multiple materials assigned.
 #       models got christal material assigned instead texture.
 # - issue: some images could not be loaded to blender.
-#       IMB_ibImageFromMemory: unknown fileformat (e.g.:__grab__fpl__\bulb_trigger_star_v2\Bulb-Trigger-Star-v2.bmp)
-#       TGS x% missing?
+#       #DEBUG fpl images.load C:\Users\user\AppData\Local\Temp\__grab__fpl__\bulb_trigger_star_v2\Bulb-Trigger-Star-v2.bmp
+#       IMB_ibImageFromMemory: unknown fileformat (C:\Users\user\AppData\Local\Temp\__grab__fpl__\bulb_trigger_star_v2\Bulb-Trigger-Star-v2.bmp)
+#       #DEBUG fpl images.load C:\Users\user\AppData\Local\Temp\__grab__fpl__\gameover\GameOver.tga
+#       decodetarga: incomplete file, 7.9% missing
 #
 # - todo: delete all unused temporary scenes with its content.
 #       to shrink file size.
index a13e176..e608ba8 100644 (file)
@@ -488,13 +488,19 @@ class FplImporter():
                                     rename_active_fpm(self.__context, FORMAT_RESOURCE.format(file_name, key_name))
 
                             elif type == Fpl_Library_Type.TYPE_GRAPHIC:
-                                #print("#DEBUG", type, key_name)
+                                #print("#DEBUG fpl images.load", item_path)
+                                #print("#DEBUG", type, key_name, item_path)
                                 blend_image = self.__data.images.load(item_path)
-                                blend_image.name = FpxUtilities.toGoodName(FORMAT_RESOURCE.format(file_name, FORMAT_IMAGE.format(key_name)))
-                                blend_image.pack()
-                                blend_image.use_fake_user = True
-                                item_dir, item_file = path.split(item_path)
-                                blend_image.filepath_raw = "//unpacked_resource/{}".format(item_file)
+                                if blend_image:
+                                    blend_image.name = FpxUtilities.toGoodName(FORMAT_RESOURCE.format(file_name, FORMAT_IMAGE.format(key_name)))
+                                    if blend_image.has_data:
+                                        blend_image.update()
+                                    blend_image.pack()
+                                    blend_image.use_fake_user = True
+                                    item_dir, item_file = path.split(item_path)
+                                    blend_image.filepath_raw = "//unpacked_resource/{}".format(item_file)
+                                if not blend_image or not blend_image.has_data:
+                                    print("#DEBUG fpl images.load failed", item_path)
 
                         else:
                             pass
@@ -2714,6 +2720,7 @@ class FptImporter():
         #print("#DEBUG LoadFromPathLibrary", name, type, lib, folder)
 
         filepath = path.join(folder, lib)
+        filepath = FpxUtilities.toGoodFilePath(filepath)
         if path.exists(filepath):
             FplImporter(
                     report=self.report,
@@ -2759,8 +2766,9 @@ class FptImporter():
             if fpx_item.get_value("linked"):
                 #print("#DEBUG GetLinked linked > ", type, fpx_item_name)
 
-                linked_path = fpx_item.get_value("linked_path").lower()
-                library_file, file_name = linked_path.split(Fpt_File_Reader.SEPARATOR)
+                win_linked_path = fpx_item.get_value("linked_path").lower()
+                linked_path = FpxUtilities.toGoodFilePath(win_linked_path)
+                library_file, file_name = linked_path.split(path.sep) #Fpt_File_Reader.SEPARATOR)
                 ## "Sci-Fi Classic GFX.fpl"
                 ## library_file = FpxUtilities.toGoodName(library_file)
                 ## file_name = FpxUtilities.toGoodName(file_name)
@@ -2789,12 +2797,18 @@ class FptImporter():
                 if item_type == Fpt_PackedLibrary_Type.TYPE_IMAGE:
                     item_path = dst_sub_path_names.get(fpx_item_name)
                     if item_path:
+                        #print("#DEBUG fpt images.load", item_path)
                         blend_image = self.__data.images.load(item_path)
-                        blend_image.name = blender_resource_name
-                        blend_image.pack()
-                        #blend_image.use_fake_user = True
-                        item_dir, item_file = path.split(item_path)
-                        blend_image.filepath_raw = "//unpacked_resource/{}".format(item_file)
+                        if blend_image:
+                            blend_image.name = blender_resource_name
+                            if blend_image.has_data:
+                                blend_image.update()
+                            blend_image.pack()
+                            #blend_image.use_fake_user = True
+                            item_dir, item_file = path.split(item_path)
+                            blend_image.filepath_raw = "//unpacked_resource/{}".format(item_file)
+                        if not blend_image or not blend_image.has_data:
+                            print("#DEBUG fpt images.load failed", item_path)
                 elif item_type == Fpt_PackedLibrary_Type.TYPE_MODEL:
                     item_data = dst_sub_path_names.get("data_{}".format(fpx_item_name))
                     if item_data:
@@ -3036,7 +3050,7 @@ def remove_material(blender_context):
             texture_slot.texture = None
         material_slot.material = None
 
-    if ops.object.material_slot_remove.poll:
+    if ops.object.material_slot_remove.poll():
         ops.object.material_slot_remove()
 
     for material in used_materials:
index 2d212bb..3c24329 100644 (file)
Binary files a/io_scene_fpx/fpx_resource.blend and b/io_scene_fpx/fpx_resource.blend differ
index 952ad50..4bdf081 100644 (file)
@@ -2825,6 +2825,7 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
             if item_name:
                 dst_sub_path = path.join(dst_path, item_name)
                 makedirs(dst_sub_path, mode=0o777, exist_ok=True)
+                #print("#DEBUG", item_name, dst_path, dst_sub_path)
                 dst_sub_path_names["sub_dir"] = dst_sub_path
             else:
                 dst_sub_path = dst_path
@@ -2835,7 +2836,8 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
             dst_sub_path_names["type"] = item_type
 
             # grab preview
-            item_path = reader.get_value("preview_path")
+            win_item_path = reader.get_value("preview_path")
+            item_path = FpxUtilities.toGoodFilePath(win_item_path)
             item_data_len = reader.get_value("preview_data_len")
             item_data = reader.get_value("preview_data")
             if item_path and item_data:
@@ -2844,7 +2846,8 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
                 dst_sub_path_names["preview_data"] = full_path
 
             # grab primary_model
-            item_path = reader.get_value("primary_model_path")
+            win_item_path = reader.get_value("primary_model_path")
+            item_path = FpxUtilities.toGoodFilePath(win_item_path)
             item_data_len = reader.get_value("primary_model_data_len")
             item_data = reader.get_value("primary_model_data")
             if item_path and item_data:
@@ -2853,7 +2856,8 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
                 dst_sub_path_names["primary_model_data"] = full_path
 
             # grab secondary_model
-            item_path = reader.get_value("secondary_model_path")
+            win_item_path = reader.get_value("secondary_model_path")
+            item_path = FpxUtilities.toGoodFilePath(win_item_path)
             item_data_len = reader.get_value("secondary_model_data_len")
             item_data = reader.get_value("secondary_model_data")
             if item_path and item_data:
@@ -2862,7 +2866,8 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
                 dst_sub_path_names["secondary_model_data"] = full_path
 
             # grab mask_model
-            item_path = reader.get_value("mask_model_path")
+            win_item_path = reader.get_value("mask_model_path")
+            item_path = FpxUtilities.toGoodFilePath(win_item_path)
             item_data_len = reader.get_value("mask_model_data_len")
             item_data = reader.get_value("mask_model_data")
             if item_path and item_data:
@@ -2871,7 +2876,8 @@ class Fpm_File_Reader(Cfb_RawIO_Reader):
                 dst_sub_path_names["mask_model_data"] = full_path
 
             # grab reflection_model
-            item_path = reader.get_value("reflection_model_path")
+            win_item_path = reader.get_value("reflection_model_path")
+            item_path = FpxUtilities.toGoodFilePath(win_item_path)
             item_data_len = reader.get_value("reflection_model_data_len")
             item_data = reader.get_value("reflection_model_data")
             if item_path and item_data:
@@ -2976,7 +2982,9 @@ class Fpl_File_Reader(Cfb_RawIO_Reader):
             dst_sub_path_names["type_{}".format(item_name)] = value._FTYP
 
             # grab item
-            item_path=path.split(value.FPAT)[1]
+            win_file_path = value.FPAT
+            file_path = FpxUtilities.toGoodFilePath(win_file_path)
+            item_path=path.split(file_path)[1]
             item_data=value.FDAT
             if item_path and item_data:
                 full_path = path.normpath(path.join(dst_sub_path, item_path))
index a9766ff..927dd6a 100644 (file)
 from bpy import (
         ops,
         )
+from bpy.path import (
+        resolve_ncase,
+        )
+from os import (
+        path,
+        )
 
 
 class FpxUtilities:
@@ -51,6 +57,8 @@ class FpxUtilities:
 
     @staticmethod
     def dump_bin(dump, address=0, comment="", max_size=0x0000000FFFFF, sector_list=None, sector_size=None, add_tags=True):
+        if dump is None:
+            return
         tag_name = FpxUtilities.TAG_NAME
         gap_hex = "|| "
         default_marker_left = ' ('
@@ -71,6 +79,16 @@ class FpxUtilities:
             view_address_16 = view_address & 15
 
             # cut of value to char
+            if isinstance(value, str):
+                if (value >= chr(32) and value < chr(127)):
+                    value_chr = value
+                else:
+                    value_chr = '.'
+            else:
+                if (value >= 32 and value < 127):
+                    value_chr = chr(value)
+                else:
+                    value_chr = '.'
             if (value >= 32 and value < 127):
                 value_chr = chr(value)
             else:
@@ -100,7 +118,10 @@ class FpxUtilities:
                     view_output.append(gap_hex)
                     view_chr.append(" ")
 
-            view_output.append("{:02X} ".format(value))
+            if isinstance(value, str):
+                view_output.append("{:02X} ".format(ord(value)))
+            else:
+                view_output.append("{:02X} ".format(value))
             view_chr.append(value_chr)
 
             if index >= max_index or view_address_16 == 15:
@@ -156,6 +177,28 @@ class FpxUtilities:
             sx.append(c)
         return str().join(sx).lower().strip(". ")
 
+    ###########################################################################
+    @staticmethod
+    def toGoodFilePath(s):
+        """ source path/filenames are based on windows systems """
+        if not s:
+            return s
+
+        # detecting custom operating system
+        if path.sep != '\\':
+            # replace windows sep to custom os sep
+            s = s.replace('\\', path.sep)
+
+            # find and cutoff drive letter
+            i = s.find(':')
+            if i > -1:
+                s = s[i + 1:]
+
+        # try to handle case sensitive names in case of such os
+        s = resolve_ncase(s)
+
+        return s
+
     ###########################################################################
     @staticmethod
     def enable_edit_mode(enable, blender_context):