From eb9e7745922fd8c1fb9c74c14379143dcfc615db Mon Sep 17 00:00:00 2001 From: Pyogenics Date: Thu, 21 Nov 2024 19:05:25 +0000 Subject: [PATCH] Create fresh plugin using new extension system --- io_scene_a3d/A3D3Shared.py | 40 ------ io_scene_a3d/A3D3_2.py | 157 ----------------------- io_scene_a3d/A3D3_3.py | 157 ----------------------- io_scene_a3d/A3DIOTools.py | 38 ------ io_scene_a3d/__init__.py | 196 ----------------------------- io_scene_a3d/__main__.py | 38 ------ io_scene_a3d/blender_manifest.toml | 36 ++++++ 7 files changed, 36 insertions(+), 626 deletions(-) delete mode 100644 io_scene_a3d/A3D3Shared.py delete mode 100644 io_scene_a3d/A3D3_2.py delete mode 100644 io_scene_a3d/A3D3_3.py delete mode 100644 io_scene_a3d/A3DIOTools.py delete mode 100644 io_scene_a3d/__main__.py create mode 100644 io_scene_a3d/blender_manifest.toml diff --git a/io_scene_a3d/A3D3Shared.py b/io_scene_a3d/A3D3Shared.py deleted file mode 100644 index 81c9c30..0000000 --- a/io_scene_a3d/A3D3Shared.py +++ /dev/null @@ -1,40 +0,0 @@ -''' -Copyright (C) 2024 Pyogenics - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -class A3D3Mesh: - def __init__(self, coordinates, uv1, normals, uv2, colors, unknown, submeshes): - # Vertex data - self.coordinates = coordinates - self.uv1 = uv1 - self.normals = normals - self.uv2 = uv2 - self.colors = colors - self.unknown = unknown - - self.submeshes = submeshes - self.faces = [] # Aggregate of all submesh face data, easier for blender importing - - # Object data - self.name = "" - self.transform = None - -class A3D3Submesh: - def __init__(self, faces, smoothingGroups, material): - self.faces = faces - self.smoothingGroups = smoothingGroups - self.material = material - -class A3D3Transform: - def __init__(self, position, rotation, scale, name): - self.position = position - self.rotation = rotation - self.scale = scale - self.name = name - self.parentID = 0 \ No newline at end of file diff --git a/io_scene_a3d/A3D3_2.py b/io_scene_a3d/A3D3_2.py deleted file mode 100644 index 8903331..0000000 --- a/io_scene_a3d/A3D3_2.py +++ /dev/null @@ -1,157 +0,0 @@ -''' -Copyright (C) 2024 Pyogenics - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -from .A3DIOTools import unpackStream, readNullTerminatedString -from .A3D3Shared import A3D3Mesh, A3D3Submesh, A3D3Transform - -''' -A3D version 3 type 2 -''' -class A3D3_2: - def __init__(self): - # Object data - self.materialNames = [] # Used to lookup names from materialID - self.materials = {} - self.meshes = [] - self.transforms = [] - - ''' - IO - ''' - def readSubmesh(self, stream): - print("Reading submesh") - - faceCount, = unpackStream(" - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -from .A3DIOTools import unpackStream, readString, calculatePadding -from .A3D3Shared import A3D3Mesh, A3D3Submesh, A3D3Transform - -''' -A3D version 3 type 3 -''' -class A3D3_3: - def __init__(self): - self.materials = {} - self.materialNames = [] - self.meshes = [] - self.transforms = [] - - def readSubmesh(self, stream): - print("Reading submesh") - - indexCount, = unpackStream("= 0: - self.meshes[meshID].submeshes[materialI].material = self.materialNames[materialID] - - self.meshes[meshID].transform = self.transforms[transformID] - - def read(self, stream): - print("Reading A3D3 type 3") - - self.readMaterialBlock(stream) - self.readMeshBlock(stream) - self.readTransformBlock(stream) - self.readObjectBlock(stream) \ No newline at end of file diff --git a/io_scene_a3d/A3DIOTools.py b/io_scene_a3d/A3DIOTools.py deleted file mode 100644 index 1d0bd3d..0000000 --- a/io_scene_a3d/A3DIOTools.py +++ /dev/null @@ -1,38 +0,0 @@ -''' -Copyright (C) 2024 Pyogenics - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -from struct import unpack, calcsize - -def unpackStream(format, stream): - size = calcsize(format) - data = stream.read(size) - return unpack(format, data) - -def readNullTerminatedString(stream): - string = b"" - char = stream.read(1) - while char != b"\x00": - string += char - char = stream.read(1) - return string.decode("utf8") - -def calculatePadding(length): - # (it basically works with rounding) - paddingSize = (((length + 3) // 4) * 4) - length - return paddingSize - -def readString(stream): - length, = unpackStream(" - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -bl_info = { - "name": "Modern A3D", - "description": "Support for modern a3d models", - "author": "Pyogenics, https://www.github.com/Pyogenics", - "version": (1, 0, 0), - "blender": (4, 0, 0), - "location": "File > Import-Export", - "category": "Import-Export" -} - -import bmesh -import bpy -from bpy.types import Operator -from bpy.props import StringProperty -from bpy_extras.io_utils import ImportHelper - -from .A3D3_2 import A3D3_2 -from .A3D3_3 import A3D3_3 -from .A3DIOTools import unpackStream - -''' -Operators -''' -class ImportA3DModern(Operator, ImportHelper): - bl_idname = "import_scene.a3dmodern" - bl_label = "Import A3D" - bl_description = "Import an A3D model" - - filter_glob: StringProperty(default="*.a3d", options={'HIDDEN'}) - - def invoke(self, context, event): - return ImportHelper.invoke(self, context, event) - - def execute(self, context): - filepath = self.filepath - print(f"Importing A3D scene from {filepath}") - - with open(filepath, "rb") as file: - signature = file.read(4) - if signature != b"A3D\0": - raise RuntimeError(f"Invalid A3D signature: {signature}") - - variant, _, rootBlockMarker, _ = unpackStream("<2H2I", file) - if rootBlockMarker != 1: - raise RuntimeError(f"Invalid root block marker: {rootBlockMarker}") - - if variant == 3: - a3d = A3D3_3() - a3d.read(file) - - for mesh in a3d.meshes: - blenderMesh = self.createBlenderMeshMin(mesh) - blenderObject = bpy.data.objects.new(mesh.name, blenderMesh) - bpy.context.collection.objects.link(blenderObject) - elif variant == 2: - a3d = A3D3_2() - a3d.read(file) - - # Create our materials - materials = {} - for materialName in a3d.materialNames: - materials[materialName] = bpy.data.materials.new(materialName) - - a3dMesh = a3d.meshes[0] - blenderMesh = self.createBlenderMesh(a3dMesh, materials) - blenderObject = bpy.data.objects.new(a3dMesh.name, blenderMesh) - bpy.context.collection.objects.link(blenderObject) - elif variant == 1: - pass - else: - pass - - self.report({"INFO"}, f"Loaded A3D") - - return {"FINISHED"} - - def createBlenderMeshMin(self, mesh): - me = bpy.data.meshes.new(mesh.name) - bm = bmesh.new() - - for coord in mesh.coordinates: - bm.verts.new(coord) - bm.verts.ensure_lookup_table() - bm.verts.index_update() - for face in mesh.faces: - v1, v2, v3 = face - bm.faces.new([ - bm.verts[v1], - bm.verts[v2], - bm.verts[v3] - ]) - - layers = [] - if len(mesh.uv1) != 0: - layers.append( - (bm.loops.layers.uv.new("UV1"), mesh.uv1) - ) - print("has UV1") - if len(mesh.uv2) != 0: - layers.append( - (bm.loops.layers.uv.new("UV2"), mesh.uv2) - ) - print("has UV2") - for face in bm.faces: - for loop in face.loops: - for uvLayer, uvData in layers: loop[uvLayer].uv = uvData[loop.vert.index] - loop.vert.normal = mesh.normals[loop.vert.index] - - bm.to_mesh(me) - me.update() - - return me - - def createBlenderMesh(self, mesh, materials): - me = bpy.data.meshes.new(mesh.name) - bm = bmesh.new() - - for coord in mesh.coordinates: - bm.verts.new(coord) - bm.verts.ensure_lookup_table() - bm.verts.index_update() - for face in mesh.faces: - v1, v2, v3 = face - bm.faces.new([ - bm.verts[v1], - bm.verts[v2], - bm.verts[v3] - ]) - - layers = [] - if len(mesh.uv1) != 0: - layers.append( - (bm.loops.layers.uv.new("UV1"), mesh.uv1) - ) - print("has UV1") - if len(mesh.uv2) != 0: - layers.append( - (bm.loops.layers.uv.new("UV2"), mesh.uv2) - ) - print("has UV2") - for face in bm.faces: - for loop in face.loops: - for uvLayer, uvData in layers: loop[uvLayer].uv = uvData[loop.vert.index] - loop.vert.normal = mesh.normals[loop.vert.index] - - bm.to_mesh(me) - me.update() - - # Materials - for submesh in mesh.submeshes: - material = materials[submesh.material] - me.materials.append(material) - materialI = len(me.materials) - 1 - for polygon in me.polygons: - polygon.material_index = materialI - - return me - -''' -Menu -''' -def menu_func_import_a3d(self, context): - self.layout.operator(ImportA3DModern.bl_idname, text="A3D Modern") - -''' -Register -''' -classes = { - ImportA3DModern -} - -def register(): - # Register classes - for c in classes: - bpy.utils.register_class(c) - # File > Import-Export - bpy.types.TOPBAR_MT_file_import.append(menu_func_import_a3d) -# bpy.types.TOPBAR_MT_file_export.append(menu_func_export_dava) - -def unregister(): - # Unregister classes - for c in classes: - bpy.utils.unregister_class(c) - # Remove `File > Import-Export` - bpy.types.TOPBAR_MT_file_import.remove(menu_func_import_a3d) -# bpy.types.TOPBAR_MT_file_export.remove(menu_func_export_dava) \ No newline at end of file diff --git a/io_scene_a3d/__main__.py b/io_scene_a3d/__main__.py deleted file mode 100644 index e21e0d7..0000000 --- a/io_scene_a3d/__main__.py +++ /dev/null @@ -1,38 +0,0 @@ -''' -Copyright (C) 2024 Pyogenics - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -''' - -from .A3DIOTools import unpackStream -from .A3D3_2 import A3D3_2 -from .A3D3_3 import A3D3_3 - -def readA3D(file): - signature = file.read(4) - if signature != b"A3D\0": - raise RuntimeError(f"Invalid A3D signature: {signature}") - - variant, _, rootBlockMarker, _ = unpackStream("<2H2I", file) - if rootBlockMarker != 1: - raise RuntimeError(f"Invalid root block marker: {rootBlockMarker}") - - if variant == 3: - a3d = A3D3_3() - a3d.read(file) - elif variant == 2: - a3d = A3D3_2() - a3d.read(file) - elif variant == 1: - pass - else: - raise RuntimeError(f"Unknown A3D variant: {variant}") - -from sys import argv -if __name__ == "__main__": - with open(argv[1], "rb") as file: - readA3D(file) \ No newline at end of file diff --git a/io_scene_a3d/blender_manifest.toml b/io_scene_a3d/blender_manifest.toml new file mode 100644 index 0000000..2dfd308 --- /dev/null +++ b/io_scene_a3d/blender_manifest.toml @@ -0,0 +1,36 @@ +schema_version = "1.0.0" + +id = "alternativa3d_tanki_format" +version = "1.0.0" +name = "Alternativa3D file format (Tanki Online HTML5)" +tagline = "Import-Export Alternativa3D 3D models used by Tanki Online HTML5" +maintainer = "Pyogenics " +type = "add-on" + +website = "https://github.com/MapMakersAndProgrammers/io_scene_a3d" + +tags = ["Import-Export"] + +blender_version_min = "4.2.0" + +license = [ + "SPDX:MIT", +] +copyright = [ + "2024 Pyogenics", +] + +# wheels = [ +# ] + +[permissions] +files = "Import-Export Alternativa3D 3D model files" + +# [build] +# # These are the default build excluded patterns. +# # You only need to edit them if you want different options. +# paths_exclude_pattern = [ +# "__pycache__/", +# "/.git/", +# "/*.zip", +# ] \ No newline at end of file