diff --git a/io_scene_ms3d/__init__.py b/io_scene_ms3d/__init__.py index 735341770d57c640bc440167e8bd38a3221a60e6..35f7c41e6f37e3f48eb8f1744530cc9dcb3b54cf 100644 --- a/io_scene_ms3d/__init__.py +++ b/io_scene_ms3d/__init__.py @@ -23,7 +23,7 @@ bl_info = { 'description': "Import / Export MilkShape3D MS3D files"\ " (conform with MilkShape3D v1.8.4)", 'author': "Alexander Nussbaumer", - 'version': (0, 96, 0), + 'version': (0, 97, 0), 'blender': (2, 66, 0), 'location': "File > Import & File > Export", 'warning': "", diff --git a/io_scene_ms3d/ms3d_export.py b/io_scene_ms3d/ms3d_export.py index 1967558efc7b460eb622a9eade4607d807727482..d29f3e2c21e3f147f39bb4fa6880f9210c79c39b 100644 --- a/io_scene_ms3d/ms3d_export.py +++ b/io_scene_ms3d/ms3d_export.py @@ -94,7 +94,7 @@ class Ms3dExporter(): """ def __init__(self, report, - verbose=False, + verbose='NONE', use_blender_names=True, use_blender_materials=False, apply_transform=True, @@ -142,14 +142,17 @@ class Ms3dExporter(): try: # write ms3d file to disk with io.FileIO(filepath, "wb") as raw_io: - ms3d_model.write(raw_io) + debug_out = ms3d_model.write(raw_io) raw_io.flush() raw_io.close() + + if self.options_verbose in Ms3dUi.VERBOSE_MAXIMAL: + print(debug_out) finally: pass # if option is set, this time will enlargs the io time - if self.options_verbose: + if self.options_verbose in Ms3dUi.VERBOSE_MAXIMAL: ms3d_model.print_internal() post_setup_environment(self, blender_context) @@ -165,16 +168,18 @@ class Ms3dExporter(): blender_context.user_preferences.edit.use_global_undo = self.undo is_valid, statistics = ms3d_model.is_valid() - print() - print("##########################################################") - print("Export from Blender to MS3D") - print(statistics) - print("##########################################################") + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print() + print("##########################################################") + print("Export from Blender to MS3D") + print(statistics) + print("##########################################################") except Exception: type, value, traceback = exc_info() - print("write - exception in try block\n type: '{0}'\n" - " value: '{1}'".format(type, value, traceback)) + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print("write - exception in try block\n type: '{0}'\n" + " value: '{1}'".format(type, value, traceback)) if t2 is None: t2 = time() @@ -185,8 +190,9 @@ class Ms3dExporter(): pass t3 = time() - print(ms3d_str['SUMMARY_EXPORT'].format( - (t3 - t1), (t2 - t1), (t3 - t2))) + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print(ms3d_str['SUMMARY_EXPORT'].format( + (t3 - t1), (t2 - t1), (t3 - t2))) return {"FINISHED"} diff --git a/io_scene_ms3d/ms3d_import.py b/io_scene_ms3d/ms3d_import.py index a9bfed1904792727fc16ae9c11300ce11b3a4c82..c9e87c231159ea80b9d2b18b432dde78141ea842 100644 --- a/io_scene_ms3d/ms3d_import.py +++ b/io_scene_ms3d/ms3d_import.py @@ -87,7 +87,7 @@ class Ms3dImporter(): """ def __init__(self, report, - verbose=False, + verbose='NONE', use_extended_normal_handling=False, use_animation=True, use_quaternion_rotation=False, @@ -131,13 +131,16 @@ class Ms3dImporter(): # open ms3d file with io.FileIO(filepath, 'rb') as raw_io: # read and inject ms3d data from disk to internal structure - ms3d_model.read(raw_io) + debug_out = ms3d_model.read(raw_io) raw_io.close() + + if self.options_verbose in Ms3dUi.VERBOSE_MAXIMAL: + print(debug_out) finally: pass # if option is set, this time will enlargs the io time - if self.options_verbose: + if self.options_verbose in Ms3dUi.VERBOSE_MAXIMAL: ms3d_model.print_internal() t2 = time() @@ -148,23 +151,20 @@ class Ms3dImporter(): # inject ms3d data to blender self.to_blender(blender_context, ms3d_model) - blender_scene = blender_context.scene - - # finalize/restore environment - blender_scene.update() - post_setup_environment(self, blender_context) - print() - print("##########################################################") - print("Import from MS3D to Blender") - print(statistics) - print("##########################################################") + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print() + print("##########################################################") + print("Import from MS3D to Blender") + print(statistics) + print("##########################################################") except Exception: type, value, traceback = exc_info() - print("read - exception in try block\n type: '{0}'\n" - " value: '{1}'".format(type, value, traceback)) + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print("read - exception in try block\n type: '{0}'\n" + " value: '{1}'".format(type, value, traceback)) if t2 is None: t2 = time() @@ -175,50 +175,12 @@ class Ms3dImporter(): pass t3 = time() - print(ms3d_str['SUMMARY_IMPORT'].format( - (t3 - t1), (t2 - t1), (t3 - t2))) - - return {"FINISHED"} - - - def internal_read(self, blender_context, raw_io): - try: - # setup environment - pre_setup_environment(self, blender_context) - - try: - ms3d_model.read(raw_io) - finally: - pass - # if option is set, this time will enlargs the io time - if self.options_verbose: - ms3d_model.print_internal() - - is_valid, statistics = ms3d_model.is_valid() + if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: + print(ms3d_str['SUMMARY_IMPORT'].format( + (t3 - t1), (t2 - t1), (t3 - t2))) - if is_valid: - # inject ms3d data to blender - blender_empty_object, blender_mesh_object = self.to_blender(blender_context, ms3d_model) - - blender_scene = blender_context.scene - - # finalize/restore environment - blender_scene.update() - - post_setup_environment(self, blender_context) - - except Exception: - type, value, traceback = exc_info() - print("read - exception in try block\n type: '{0}'\n" - " value: '{1}'".format(type, value, traceback)) - - raise - - else: - pass - - return blender_empty_object, blender_mesh_object + return {"FINISHED"} ########################################################################### @@ -629,7 +591,7 @@ class Ms3dImporter(): # end BMesh stuff #################################################### - blender_mesh.validate(self.options_verbose) + blender_mesh.validate(self.options_verbose in Ms3dUi.VERBOSE_MAXIMAL) return blender_mesh_object diff --git a/io_scene_ms3d/ms3d_spec.py b/io_scene_ms3d/ms3d_spec.py index 740b333c6da0a44da7914f0dcc6702e160ea98a4..d5d5d7875076bed558d588a57443c5e84cde7180 100644 --- a/io_scene_ms3d/ms3d_spec.py +++ b/io_scene_ms3d/ms3d_spec.py @@ -290,6 +290,8 @@ class Ms3dIo: if not buffer: raise EOFError() eol = buffer.find(Ms3dSpec.STRING_TERMINATION) + if eol < 0: + eol = len(buffer) register_error(Ms3dSpec.STRING_MS3D_REPLACE, Ms3dIo.ms3d_replace) s = buffer[:eol].decode(encoding=Ms3dSpec.STRING_ENCODING, errors=Ms3dSpec.STRING_ERROR) return s @@ -1733,13 +1735,15 @@ class Ms3dModel: add content to blender scene """ + debug_out = [] + self.header.read(raw_io) if (self.header != Ms3dHeader()): - print("\nwarning, invalid file header") + debug_out.append("\nwarning, invalid file header\n") _number_vertices = Ms3dIo.read_word(raw_io) if (_number_vertices > Ms3dSpec.MAX_VERTICES): - print("\nwarning, invalid count: number_vertices: {}".format( + debug_out.append("\nwarning, invalid count: number_vertices: {}\n".format( _number_vertices)) self._vertices = [] for i in range(_number_vertices): @@ -1747,7 +1751,7 @@ class Ms3dModel: _number_triangles = Ms3dIo.read_word(raw_io) if (_number_triangles > Ms3dSpec.MAX_TRIANGLES): - print("\nwarning, invalid count: number_triangles: {}".format( + debug_out.append("\nwarning, invalid count: number_triangles: {}\n".format( _number_triangles)) self._triangles = [] for i in range(_number_triangles): @@ -1755,7 +1759,7 @@ class Ms3dModel: _number_groups = Ms3dIo.read_word(raw_io) if (_number_groups > Ms3dSpec.MAX_GROUPS): - print("\nwarning, invalid count: number_groups: {}".format( + debug_out.append("\nwarning, invalid count: number_groups: {}\n".format( _number_groups)) self._groups = [] for i in range(_number_groups): @@ -1763,7 +1767,7 @@ class Ms3dModel: _number_materials = Ms3dIo.read_word(raw_io) if (_number_materials > Ms3dSpec.MAX_MATERIALS): - print("\nwarning, invalid count: number_materials: {}".format( + debug_out.append("\nwarning, invalid count: number_materials: {}\n".format( _number_materials)) self._materials = [] for i in range(_number_materials): @@ -1782,7 +1786,7 @@ class Ms3dModel: _number_joints = Ms3dIo.read_word(raw_io) _progress.add('NUMBER_JOINTS') if (_number_joints > Ms3dSpec.MAX_JOINTS): - print("\nwarning, invalid count: number_joints: {}".format( + debug_out.append("\nwarning, invalid count: number_joints: {}\n".format( _number_joints)) self._joints = [] for i in range(_number_joints): @@ -1794,61 +1798,61 @@ class Ms3dModel: _number_group_comments = Ms3dIo.read_dword(raw_io) _progress.add('NUMBER_GROUP_COMMENTS') if (_number_group_comments > Ms3dSpec.MAX_GROUPS): - print("\nwarning, invalid count:"\ - " number_group_comments: {}".format( + debug_out.append("\nwarning, invalid count:"\ + " number_group_comments: {}\n".format( _number_group_comments)) if _number_group_comments > _number_groups: - print("\nwarning, invalid count:"\ - " number_group_comments: {}, number_groups: {}".format( + debug_out.append("\nwarning, invalid count:"\ + " number_group_comments: {}, number_groups: {}\n".format( _number_group_comments, _number_groups)) for i in range(_number_group_comments): item = Ms3dCommentEx().read(raw_io) if item.index >= 0 and item.index < _number_groups: self.groups[item.index]._comment_object = item else: - print("\nwarning, invalid index:"\ - " group_index: {}, number_groups: {}".format( + debug_out.append("\nwarning, invalid index:"\ + " group_index: {}, number_groups: {}\n".format( item.index, _number_groups)) _progress.add('GROUP_COMMENTS') _number_material_comments = Ms3dIo.read_dword(raw_io) _progress.add('NUMBER_MATERIAL_COMMENTS') if (_number_material_comments > Ms3dSpec.MAX_MATERIALS): - print("\nwarning, invalid count:"\ - " number_material_comments: {}".format( + debug_out.append("\nwarning, invalid count:"\ + " number_material_comments: {}\n".format( _number_material_comments)) if _number_material_comments > _number_materials: - print("\nwarning, invalid count:"\ + debug_out.append("\nwarning, invalid count:"\ " number_material_comments:"\ - " {}, number_materials: {}".format( + " {}, number_materials: {}\n".format( _number_material_comments, _number_materials)) for i in range(_number_material_comments): item = Ms3dCommentEx().read(raw_io) if item.index >= 0 and item.index < _number_materials: self.materials[item.index]._comment_object = item else: - print("\nwarning, invalid index:"\ + debug_out.append("\nwarning, invalid index:"\ " material_index: {}, number_materials:"\ - " {}".format(item.index, _number_materials)) + " {}\n".format(item.index, _number_materials)) _progress.add('MATERIAL_COMMENTS') _number_joint_comments = Ms3dIo.read_dword(raw_io) _progress.add('NUMBER_JOINT_COMMENTS') if (_number_joint_comments > Ms3dSpec.MAX_JOINTS): - print("\nwarning, invalid count:"\ - " number_joint_comments: {}".format( + debug_out.append("\nwarning, invalid count:"\ + " number_joint_comments: {}\n".format( _number_joint_comments)) if _number_joint_comments > _number_joints: - print("\nwarning, invalid count:"\ - " number_joint_comments: {}, number_joints: {}".format( + debug_out.append("\nwarning, invalid count:"\ + " number_joint_comments: {}, number_joints: {}\n".format( _number_joint_comments, _number_joints)) for i in range(_number_joint_comments): item = Ms3dCommentEx().read(raw_io) if item.index >= 0 and item.index < _number_joints: self.joints[item.index]._comment_object = item else: - print("\nwarning, invalid index:"\ - " joint_index: {}, number_joints: {}".format( + debug_out.append("\nwarning, invalid index:"\ + " joint_index: {}, number_joints: {}\n".format( item.index, _number_joints)) _progress.add('JOINT_COMMENTS') @@ -1872,8 +1876,8 @@ class Ms3dModel: elif self.sub_version_vertex_extra == 3: item = Ms3dVertexEx3() else: - print("\nwarning, invalid version:"\ - " sub_version_vertex_extra: {}".format( + debug_out.append("\nwarning, invalid version:"\ + " sub_version_vertex_extra: {}\n".format( sub_version_vertex_extra)) continue self.vertices[i]._vertex_ex_object = item.read(raw_io) @@ -1894,13 +1898,13 @@ class Ms3dModel: except EOFError: # reached end of optional data. - print("Ms3dModel.read - optional data read: {}".format(_progress)) + debug_out.append("Ms3dModel.read - optional data read: {}\n".format(_progress)) pass except Exception: type, value, traceback = exc_info() - print("Ms3dModel.read - exception in optional try block," - " _progress={0}\n type: '{1}'\n value: '{2}'".format( + debug_out.append("Ms3dModel.read - exception in optional try block," + " _progress={0}\n type: '{1}'\n value: '{2}'\n".format( _progress, type, value, traceback)) else: @@ -1935,7 +1939,7 @@ class Ms3dModel: self.sub_version_model_extra = 0 self._model_ex_object = Ms3dModelEx() - return + return "".join(debug_out) def write(self, raw_io): @@ -1944,6 +1948,8 @@ class Ms3dModel: creates, writes MS3D file. """ + debug_out = [] + self.header.write(raw_io) Ms3dIo.write_word(raw_io, self.number_vertices) @@ -2005,15 +2011,15 @@ class Ms3dModel: except Exception: type, value, traceback = exc_info() - print("Ms3dModel.write - exception in optional try block" - "\n type: '{0}'\n value: '{1}'".format( + debug_out.append("Ms3dModel.write - exception in optional try block" + "\n type: '{0}'\n value: '{1}'\n".format( type, value, traceback)) pass else: pass - return + return "".join(debug_out) def is_valid(self): diff --git a/io_scene_ms3d/ms3d_strings.py b/io_scene_ms3d/ms3d_strings.py index 986587600a02a1a7cf75ef85f9c7d4326be443dd..0a0f8b55b61c28f311c7fbe981c6438734a8b342 100644 --- a/io_scene_ms3d/ms3d_strings.py +++ b/io_scene_ms3d/ms3d_strings.py @@ -190,6 +190,12 @@ ms3d_str = { 'PROP_NAME_TRANSPARENCY': "Transparency", 'PROP_NAME_TRANSPARENCY_MODE': "Transp. Mode", 'PROP_NAME_VERBOSE': "Verbose", + 'ENUM_VERBOSE_NONE_1': "None", + 'ENUM_VERBOSE_NONE_2': "", + 'ENUM_VERBOSE_NORMAL_1': "Normal", + 'ENUM_VERBOSE_NORMAL_2': "", + 'ENUM_VERBOSE_MAXIMALIMAL_1': "Maximal", + 'ENUM_VERBOSE_MAXIMALIMAL_2': "", 'PROP_SMOOTHING_GROUP_INDEX': "Smoothing group id", 'PROP_NAME_ROTATION_MODE' : "Bone Rotation Mode", 'PROP_DESC_ROTATION_MODE' : "set the preferred rotation mode of bones", diff --git a/io_scene_ms3d/ms3d_ui.py b/io_scene_ms3d/ms3d_ui.py index 46d8a50055de927f57ad831997bdff9f7bd79f7c..8506b61df740f69f09db9fbb0af9ae76837ea8d3 100644 --- a/io_scene_ms3d/ms3d_ui.py +++ b/io_scene_ms3d/ms3d_ui.py @@ -90,7 +90,16 @@ from bpy.app import ( class Ms3dUi: - DEFAULT_VERBOSE = debug + VERBOSE_MODE_NONE = 'NONE' + VERBOSE_MODE_NORMAL = 'NORMAL' + VERBOSE_MODE_MAXIMAL = 'MAXIMAL' + + VERBOSE_NONE = {} + VERBOSE_NORMAL = {True, VERBOSE_MODE_NORMAL, VERBOSE_MODE_MAXIMAL, } + VERBOSE_MAXIMAL = {True, VERBOSE_MODE_MAXIMAL, } + + DEFAULT_VERBOSE = VERBOSE_MODE_NONE + ########################################################################### FLAG_TEXTURE_COMBINE_ALPHA = 'COMBINE_ALPHA' @@ -262,9 +271,22 @@ class Ms3dImportOperator(Operator, ImportHelper): options={'HIDDEN', } ) - verbose = BoolProperty( + verbose = EnumProperty( name=ms3d_str['PROP_NAME_VERBOSE'], description=ms3d_str['PROP_DESC_VERBOSE'], + items=( (Ms3dUi.VERBOSE_MODE_NONE, + ms3d_str['ENUM_VERBOSE_NONE_1'], + ms3d_str['ENUM_VERBOSE_NONE_2'], + ), + (Ms3dUi.VERBOSE_MODE_NORMAL, + ms3d_str['ENUM_VERBOSE_NORMAL_1'], + ms3d_str['ENUM_VERBOSE_NORMAL_2'], + ), + (Ms3dUi.VERBOSE_MODE_MAXIMAL, + ms3d_str['ENUM_VERBOSE_MAXIMALIMAL_1'], + ms3d_str['ENUM_VERBOSE_MAXIMALIMAL_2'], + ), + ), default=Ms3dUi.PROP_DEFAULT_VERBOSE, ) @@ -370,7 +392,7 @@ class Ms3dImportOperator(Operator, ImportHelper): def execute(self, blender_context): """ start executing """ from io_scene_ms3d.ms3d_import import (Ms3dImporter, ) - return Ms3dImporter( + Ms3dImporter( report=self.report, verbose=self.verbose, use_extended_normal_handling=self.use_extended_normal_handling, @@ -384,6 +406,9 @@ class Ms3dImportOperator(Operator, ImportHelper): self.filepath ) + blender_context.scene.update() + return {"FINISHED"} + def invoke(self, blender_context, event): blender_context.window_manager.fileselect_add(self) return {'RUNNING_MODAL', } @@ -410,9 +435,22 @@ class Ms3dExportOperator(Operator, ExportHelper): options={'HIDDEN', } ) - verbose = BoolProperty( + verbose = EnumProperty( name=ms3d_str['PROP_NAME_VERBOSE'], description=ms3d_str['PROP_DESC_VERBOSE'], + items=( (Ms3dUi.VERBOSE_MODE_NONE, + ms3d_str['ENUM_VERBOSE_NONE_1'], + ms3d_str['ENUM_VERBOSE_NONE_2'], + ), + (Ms3dUi.VERBOSE_MODE_NORMAL, + ms3d_str['ENUM_VERBOSE_NORMAL_1'], + ms3d_str['ENUM_VERBOSE_NORMAL_2'], + ), + (Ms3dUi.VERBOSE_MODE_MAXIMAL, + ms3d_str['ENUM_VERBOSE_MAXIMALIMAL_1'], + ms3d_str['ENUM_VERBOSE_MAXIMALIMAL_2'], + ), + ), default=Ms3dUi.PROP_DEFAULT_VERBOSE, ) @@ -557,7 +595,7 @@ class Ms3dExportOperator(Operator, ExportHelper): def execute(self, blender_context): """start executing""" from io_scene_ms3d.ms3d_export import (Ms3dExporter, ) - return Ms3dExporter( + Ms3dExporter( self.report, verbose=self.verbose, use_blender_names=self.use_blender_names, @@ -574,6 +612,9 @@ class Ms3dExportOperator(Operator, ExportHelper): self.filepath ) + blender_context.scene.update() + return {"FINISHED"} + # def invoke(self, blender_context, event): blender_context.window_manager.fileselect_add(self) diff --git a/io_scene_ms3d/ms3d_utils.py b/io_scene_ms3d/ms3d_utils.py index c44b19ca85fbd879ce23eda60b25f76112b67611..82a75223f324706b325c25b65c5ca90656e60b58 100644 --- a/io_scene_ms3d/ms3d_utils.py +++ b/io_scene_ms3d/ms3d_utils.py @@ -114,8 +114,6 @@ def pre_setup_environment(porter, blender_context): # change back to object mode enable_edit_mode(False, blender_context) - blender_context.scene.update() - ############################################################################### def post_setup_environment(porter, blender_context):