def read(self, blender_context, filepath): """ read ms3d file and convert ms3d content to bender content """ t1 = time() t2 = None self.has_textures = False try: # setup environment pre_setup_environment(self, blender_context) # inject splitted filepath self.directory_name, self.file_name = path.split(filepath) # create an empty ms3d template ms3d_model = Ms3dModel(self.file_name) try: # open ms3d file with io.FileIO(filepath, 'rb') as raw_io: # read and inject ms3d data from disk to internal structure 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 in Ms3dUi.VERBOSE_MAXIMAL: ms3d_model.print_internal() t2 = time() is_valid, statistics = ms3d_model.is_valid() if is_valid: # inject ms3d data to blender self.to_blender(blender_context, ms3d_model) post_setup_environment(self, blender_context) if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: print() print("##########################################################") print("Import from MS3D to Blender") print(statistics) print("##########################################################") except Ms3dHeader.HeaderError: msg = "read - invalid file format." if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: print(msg) if self.report: self.report({'WARNING', 'ERROR', }, msg) return False except Exception: type, value, traceback = exc_info() 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 self.report: self.report({'WARNING', 'ERROR', }, "read - exception.") if t2 is None: t2 = time() return False else: pass t3 = time() if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: print(ms3d_str['SUMMARY_IMPORT'].format( (t3 - t1), (t2 - t1), (t3 - t2))) return True
def write(self, blender_context, filepath): """convert bender content to ms3d content and write it to file""" t1 = time() t2 = None try: # setup environment pre_setup_environment(self, blender_context) # create an empty ms3d template ms3d_model = Ms3dModel() # inject blender data to ms3d file self.from_blender(blender_context, ms3d_model) t2 = time() try: # write ms3d file to disk with io.FileIO(filepath, "wb") as 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 in Ms3dUi.VERBOSE_MAXIMAL: ms3d_model.print_internal() post_setup_environment(self, blender_context) # restore active object blender_context.scene.objects.active = self.active_object if ((not blender_context.scene.objects.active) and (blender_context.selected_objects)): blender_context.scene.objects.active \ = blender_context.selected_objects[0] # restore pre operator undo state blender_context.preferences.edit.use_global_undo = self.undo is_valid, statistics = ms3d_model.is_valid() 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() 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 self.report: self.report({ 'WARNING', 'ERROR', }, "write - exception.") if t2 is None: t2 = time() return False else: pass t3 = time() if self.options_verbose in Ms3dUi.VERBOSE_NORMAL: print(ms3d_str['SUMMARY_EXPORT'].format((t3 - t1), (t2 - t1), (t3 - t2))) return True