|
| 1 | +import os |
| 2 | +import bpy |
| 3 | +import bpy_extras |
| 4 | +from bpy_extras.io_utils import ImportHelper, ExportHelper |
| 5 | +import xml.etree.ElementTree as ET |
| 6 | + |
| 7 | +bl_info = { |
| 8 | + "name" : "NierSarTools", |
| 9 | + "author" : "RaiderB", |
| 10 | + "description" : "", |
| 11 | + "blender" : (3, 0, 0), |
| 12 | + "version" : (0, 1, 0), |
| 13 | + "location" : "", |
| 14 | + "warning" : "", |
| 15 | + "category" : "Generic" |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +class ImportSar(bpy.types.Operator, ImportHelper): |
| 20 | + '''Load a Nier:Automata Sar (Skeleton) File.''' |
| 21 | + bl_idname = "import_scene.sar" |
| 22 | + bl_label = "Import Sar Data" |
| 23 | + bl_options = {'PRESET', 'UNDO'} |
| 24 | + filename_ext = ".sar" |
| 25 | + filter_glob: bpy.props.StringProperty(default="*.sar", options={'HIDDEN'}) |
| 26 | + |
| 27 | + tryApplyingOffsets: bpy.props.BoolProperty(name="Try Applying Offsets", default=False) |
| 28 | + |
| 29 | + onlyToXml: bpy.props.BoolProperty(name="Only Convert To XML", default=False) |
| 30 | + recursivelyImport: bpy.props.BoolProperty(name="Import all recursively", default=False) |
| 31 | + |
| 32 | + def doImport(self, filepath): |
| 33 | + from . import sarImporter |
| 34 | + from . import sar |
| 35 | + |
| 36 | + if self.onlyToXml: |
| 37 | + xml = sar.bxmToXml(filepath) |
| 38 | + with open(filepath + ".xml", "wb") as f: |
| 39 | + f.write(ET.tostring(xml)) |
| 40 | + else: |
| 41 | + sarImporter.importSar(filepath, self.tryApplyingOffsets) |
| 42 | + |
| 43 | + def execute(self, context): |
| 44 | + if self.recursivelyImport: |
| 45 | + directory = os.path.split(self.filepath)[0] |
| 46 | + for root, dirs, files in os.walk(directory): |
| 47 | + for file in files: |
| 48 | + if file.endswith(".sar"): |
| 49 | + self.doImport(root + '\\' + file) |
| 50 | + print("Imported all file!") |
| 51 | + else: |
| 52 | + self.doImport(self.filepath) |
| 53 | + |
| 54 | + return {'FINISHED'} |
| 55 | + |
| 56 | +class ExportSar(bpy.types.Operator, ExportHelper): |
| 57 | + '''Export a Nier:Automata Sar (Skeleton) File.''' |
| 58 | + bl_idname = "export_scene.sar" |
| 59 | + bl_label = "Export Sar Data" |
| 60 | + bl_options = {'PRESET', 'UNDO'} |
| 61 | + filename_ext = ".sar" |
| 62 | + filter_glob: bpy.props.StringProperty(default="*.sar", options={'HIDDEN'}) |
| 63 | + |
| 64 | + def execute(self, context): |
| 65 | + from . import sarExporter |
| 66 | + sarExporter.exportSar(self.filepath) |
| 67 | + return {'FINISHED'} |
| 68 | + |
| 69 | +def importMenuAdditions(self, context): |
| 70 | + self.layout.operator(ImportSar.bl_idname, text="Sar for Nier:Automata (.sar)") |
| 71 | + |
| 72 | +def exportMenuAdditions(self, context): |
| 73 | + self.layout.operator(ExportSar.bl_idname, text="Sar for Nier:Automata (.sar)") |
| 74 | + |
| 75 | +def register(): |
| 76 | + bpy.utils.register_class(ImportSar) |
| 77 | + bpy.utils.register_class(ExportSar) |
| 78 | + |
| 79 | + bpy.types.TOPBAR_MT_file_import.append(importMenuAdditions) |
| 80 | + bpy.types.TOPBAR_MT_file_export.append(exportMenuAdditions) |
| 81 | + |
| 82 | +def unregister(): |
| 83 | + bpy.utils.unregister_class(ImportSar) |
| 84 | + bpy.utils.unregister_class(ExportSar) |
| 85 | + |
| 86 | + bpy.types.TOPBAR_MT_file_import.remove(importMenuAdditions) |
| 87 | + bpy.types.TOPBAR_MT_file_export.remove(exportMenuAdditions) |
0 commit comments