def export_arc(blender_object): '''Exports an arc file containing mod and tex files, among others from a previously imported arc.''' mods = {} try: saved_arc = Arc( file_path=BytesIO(blender_object.albam_imported_item.data)) except AttributeError: raise ExportError( 'Object {0} did not come from the original arc'.format( blender_object.name)) for child in blender_object.children: try: basename = posixpath.basename(child.name) folder = child.albam_imported_item.folder if os.sep == ntpath.sep: # Windows mod_filepath = ntpath.join(ensure_ntpath(folder), basename) else: mod_filepath = os.path.join(folder, basename) except AttributeError: raise ExportError( 'Object {0} did not come from the original arc'.format( child.name)) assert child.albam_imported_item.file_type == 'mtframework.mod' mod, textures = export_mod156(child) mods[mod_filepath] = (mod, textures) with tempfile.TemporaryDirectory() as tmpdir: tmpdir_slash_ending = tmpdir + os.sep if not tmpdir.endswith( os.sep) else tmpdir saved_arc.unpack(tmpdir) mod_files = [ os.path.join(root, f) for root, _, files in os.walk(tmpdir) for f in files if f.endswith('.mod') ] # tex_files = {os.path.join(root, f) for root, _, files in os.walk(tmpdir) # for f in files if f.endswith('.tex')} new_tex_files = set() for modf in mod_files: rel_path = modf.split(tmpdir_slash_ending)[1] try: new_mod = mods[rel_path] except KeyError: raise ExportError( "Can't export to arc, a mod file is missing: {}".format( rel_path)) with open(modf, 'wb') as w: w.write(new_mod[0]) mod_textures = new_mod[1] for texture in mod_textures: tex = Tex112.from_dds( file_path=bpy.path.abspath(texture.image.filepath)) try: tex.unk_float_1 = texture.albam_imported_texture_value_1 tex.unk_float_2 = texture.albam_imported_texture_value_2 tex.unk_float_3 = texture.albam_imported_texture_value_3 tex.unk_float_4 = texture.albam_imported_texture_value_4 except AttributeError: pass tex_name = os.path.basename(texture.image.filepath) tex_filepath = os.path.join(os.path.dirname(modf), tex_name.replace('.dds', '.tex')) new_tex_files.add(tex_filepath) with open(tex_filepath, 'wb') as w: w.write(tex) # probably other files can reference textures besides mod, this is in case # textures applied have other names. # TODO: delete only textures referenced from saved_mods at import time # unused_tex_files = tex_files - new_tex_files # for utex in unused_tex_files: # os.unlink(utex) new_arc = Arc.from_dir(tmpdir) return new_arc
def test_ensure_ntpath_from_ntpath(): path = 'foo\\bar\\spam\\eggs' assert ensure_ntpath(path) == 'foo\\bar\\spam\\eggs'
def test_ensure_ntpath_from_posixpath(): path = 'foo/bar/spam/eggs' assert ensure_ntpath(path) == 'foo\\bar\\spam\\eggs'
def export_arc(blender_object): '''Exports an arc file containing mod and tex files, among others from a previously imported arc.''' mods = {} try: saved_arc = Arc(file_path=BytesIO(blender_object.albam_imported_item.data)) except AttributeError: raise ExportError('Object {0} did not come from the original arc'.format(blender_object.name)) for child in blender_object.children: try: basename = posixpath.basename(child.name) folder = child.albam_imported_item.folder if os.sep == ntpath.sep: # Windows mod_filepath = ntpath.join(ensure_ntpath(folder), basename) else: mod_filepath = os.path.join(folder, basename) except AttributeError: raise ExportError('Object {0} did not come from the original arc'.format(child.name)) assert child.albam_imported_item.file_type == 'mtframework.mod' mod, textures = export_mod156(child) mods[mod_filepath] = (mod, textures) with tempfile.TemporaryDirectory() as tmpdir: tmpdir_slash_ending = tmpdir + os.sep if not tmpdir.endswith(os.sep) else tmpdir saved_arc.unpack(tmpdir) mod_files = [os.path.join(root, f) for root, _, files in os.walk(tmpdir) for f in files if f.endswith('.mod')] # tex_files = {os.path.join(root, f) for root, _, files in os.walk(tmpdir) # for f in files if f.endswith('.tex')} new_tex_files = set() for modf in mod_files: rel_path = modf.split(tmpdir_slash_ending)[1] try: new_mod = mods[rel_path] except KeyError: raise ExportError("Can't export to arc, a mod file is missing: {}".format(rel_path)) with open(modf, 'wb') as w: w.write(new_mod[0]) mod_textures = new_mod[1] for texture in mod_textures: tex = Tex112.from_dds(file_path=bpy.path.abspath(texture.image.filepath)) try: tex.unk_float_1 = texture.albam_imported_texture_value_1 tex.unk_float_2 = texture.albam_imported_texture_value_2 tex.unk_float_3 = texture.albam_imported_texture_value_3 tex.unk_float_4 = texture.albam_imported_texture_value_4 except AttributeError: pass tex_name = os.path.basename(texture.image.filepath) tex_filepath = os.path.join(os.path.dirname(modf), tex_name.replace('.dds', '.tex')) new_tex_files.add(tex_filepath) with open(tex_filepath, 'wb') as w: w.write(tex) # probably other files can reference textures besides mod, this is in case # textures applied have other names. # TODO: delete only textures referenced from saved_mods at import time # unused_tex_files = tex_files - new_tex_files # for utex in unused_tex_files: # os.unlink(utex) new_arc = Arc.from_dir(tmpdir) return new_arc