From 5c34bbad38a3cd4b30162bfe6931cb6b2940fffe Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 3 Dec 2023 18:14:20 +0800 Subject: [PATCH] fix virtools export fatal errors --- bbp_ng/OP_EXPORT_virtools.py | 49 ++++++++++++++++----------------- bbp_ng/PyBMap/bmap.py | 2 +- bbp_ng/PyBMap/bmap_wrapper.py | 9 +++--- bbp_ng/UTIL_ballance_texture.py | 1 + 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/bbp_ng/OP_EXPORT_virtools.py b/bbp_ng/OP_EXPORT_virtools.py index c7df0e8..735a80c 100644 --- a/bbp_ng/OP_EXPORT_virtools.py +++ b/bbp_ng/OP_EXPORT_virtools.py @@ -89,7 +89,7 @@ def _export_virtools(file_name_: str, encodings_: tuple[str], compress_level_: i texture_crets: tuple[_TTexturePair, ...] = _export_virtools_materials( writer, progress, material_crets) # export texture - _export_virtools_textures(writer, progress, texture_crets) + _export_virtools_textures(writer, progress, vt_temp_folder, texture_crets) # save document _save_virtools_document( @@ -385,39 +385,36 @@ def _export_virtools_materials( def _export_virtools_textures( writer: bmap.BMFileWriter, progress: ProgressReport, + vt_temp_folder: str, texture_crets: tuple[_TTexturePair, ...] ) -> None: # start saving progress.enter_substeps(len(texture_crets), "Saving Textures") - # create another temp folder for non-ballance texture saving - with tempfile.TemporaryDirectory() as nonballance_temp: - print(f'Non-Ballance Texture Temp: {nonballance_temp}') + for tex, vttexture in texture_crets: + # set name + vttexture.set_name(tex.name) - for tex, vttexture in texture_crets: - # set name - vttexture.set_name(tex.name) + # set texture cfg + rawtex: PROP_virtools_texture.RawVirtoolsTexture = PROP_virtools_texture.get_raw_virtools_texture(tex) + vttexture.set_save_options(rawtex.mSaveOptions) + vttexture.set_video_format(rawtex.mVideoFormat) - # set texture cfg - rawtex: PROP_virtools_texture.RawVirtoolsTexture = PROP_virtools_texture.get_raw_virtools_texture(tex) - vttexture.set_save_options(rawtex.mSaveOptions) - vttexture.set_video_format(rawtex.mVideoFormat) + # save core texture + # load ballance textures to vt engine from external ref path + # load other textures to vt engine from temp folder. + # no need to distinguish save options + try_filepath: str | None = UTIL_ballance_texture.get_ballance_texture_filename( + UTIL_ballance_texture.get_texture_filepath(tex)) + if try_filepath is None: + # non-ballance file, save in temp and change file path to point to it. + try_filepath = UTIL_ballance_texture.generate_other_texture_save_path(tex, vt_temp_folder) + UTIL_ballance_texture.save_other_texture(tex, try_filepath) + # load into vt engine + vttexture.load_image(try_filepath) - # save core texture - # load ballance textures to vt engine from external ref path - # load other textures to vt engine from temp folder. - # no need to distinguish save options - try_filepath: str | None = UTIL_ballance_texture.get_ballance_texture_filename( - UTIL_ballance_texture.get_texture_filepath(tex)) - if try_filepath is None: - # non-ballance file, save in temp and change file path to point to it. - try_filepath = UTIL_ballance_texture.generate_other_texture_save_path(tex, nonballance_temp) - UTIL_ballance_texture.save_other_texture(tex, try_filepath) - # load into vt engine - vttexture.load_image(try_filepath) - - # step - progress.step() + # step + progress.step() # leave progress and return progress.leave_substeps() diff --git a/bbp_ng/PyBMap/bmap.py b/bbp_ng/PyBMap/bmap.py index 8dcab34..6a1bc44 100644 --- a/bbp_ng/PyBMap/bmap.py +++ b/bbp_ng/PyBMap/bmap.py @@ -78,7 +78,7 @@ elif sys.platform.startswith('darwin'): else: _g_BMapLibName = "BMap.bin" -_g_BMapModule: ctypes.CDLL = None +_g_BMapModule: ctypes.CDLL | None = None try: _g_BMapModule = ctypes.cdll.LoadLibrary( os.path.join(os.path.dirname(__file__), _g_BMapLibName) diff --git a/bbp_ng/PyBMap/bmap_wrapper.py b/bbp_ng/PyBMap/bmap_wrapper.py index 9a3168e..f4bf6b3 100644 --- a/bbp_ng/PyBMap/bmap_wrapper.py +++ b/bbp_ng/PyBMap/bmap_wrapper.py @@ -7,7 +7,7 @@ g_InvalidPtr: bmap.bm_void_p = bmap.bm_void_p(0) g_InvalidCKID: int = 0 g_BMapEncoding: str = "utf-8" -def python_callback(strl: bytes): +def _python_callback(strl: bytes): """ The Python type callback for BMFile. Simply add a prefix when output. @@ -17,6 +17,7 @@ def python_callback(strl: bytes): # i think Python do a auto convertion here. if strl is not None: print(f'[PyBMap] {strl.decode(g_BMapEncoding)}') +_g_RawCallback: bmap.bm_callback = bmap.bm_callback(_python_callback) class _AbstractPointer(): __mRawPointer: int @@ -594,7 +595,6 @@ class BMFileReader(_AbstractPointer): file_name: bmap.bm_CKSTRING = bmap.bm_CKSTRING(file_name_.encode(g_BMapEncoding)) temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding)) texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding)) - raw_callback: bmap.bm_callback = bmap.bm_callback(python_callback) encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_)) encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))( *(strl.encode(g_BMapEncoding) for strl in encodings_) @@ -602,7 +602,7 @@ class BMFileReader(_AbstractPointer): out_file: bmap.bm_void_p = bmap.bm_void_p() # exec bmap.BMFile_Load( - file_name, temp_folder, texture_folder, raw_callback, + file_name, temp_folder, texture_folder, _g_RawCallback, encoding_count, encodings, ctypes.byref(out_file) ) @@ -693,7 +693,6 @@ class BMFileWriter(_AbstractPointer): # create param temp_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(temp_folder_.encode(g_BMapEncoding)) texture_folder: bmap.bm_CKSTRING = bmap.bm_CKSTRING(texture_folder_.encode(g_BMapEncoding)) - raw_callback: bmap.bm_callback = bmap.bm_callback(python_callback) encoding_count: bmap.bm_CKDWORD = bmap.bm_CKDWORD(len(encodings_)) encodings: ctypes.Array = (bmap.bm_CKSTRING * len(encodings_))( *(strl.encode(g_BMapEncoding) for strl in encodings_) @@ -701,7 +700,7 @@ class BMFileWriter(_AbstractPointer): out_file: bmap.bm_void_p = bmap.bm_void_p() # exec bmap.BMFile_Create( - temp_folder, texture_folder, raw_callback, + temp_folder, texture_folder, _g_RawCallback, encoding_count, encodings, ctypes.byref(out_file) ) diff --git a/bbp_ng/UTIL_ballance_texture.py b/bbp_ng/UTIL_ballance_texture.py index 5b40261..8fabde0 100644 --- a/bbp_ng/UTIL_ballance_texture.py +++ b/bbp_ng/UTIL_ballance_texture.py @@ -350,6 +350,7 @@ def save_other_texture(tex: bpy.types.Image, filepath: str) -> None: @param tex[in] The saving texture @param filepath[in] The absolute path to saving file. """ + # MARK: must use keyword to assign param otherwise blender will throw error. tex.save(filepath = filepath) #endregion