-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_scene_tanki.py
227 lines (181 loc) · 7.05 KB
/
io_scene_tanki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
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.
'''
import bpy
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
import xml.etree.ElementTree as ET
from pathlib import Path
bl_info = {
"name": "Tanki map importer",
"description": "Tool to import Tanki Online maps into blender",
"author": "Pyogenics https://www.github.com/Pyogenics",
"version": (1, 0, 0),
"blender": (4, 0, 0),
"location": "File > Import",
"doc_url": "https://github.com/Pyogenics/tankiMapBIN2XML",
"tracker_url": "https://github.com/Pyogenics/tankiMapBIN2XML",
"support": "COMMUNITY",
"category": "Import",
}
'''
File IO
'''
class XMLProp:
def __init__(self):
self.libraryName = ""
self.groupName = ""
self.name = ""
self.rotationZ = 0.0
self.textureName = ""
self.position = (0.0, 0.0, 0.0)
def read(self, xmlData):
self.libraryName = xmlData.attrib["library-name"]
self.groupName = xmlData.attrib["group-name"]
self.name = xmlData.attrib["name"]
rotationData = xmlData.find("rotation")
self.rotationZ = float(rotationData.find("z").text)
self.textureName = xmlData.find("texture-name").text
positionData = xmlData.find("position")
positionX = float(positionData.find("x").text)
positionY = float(positionData.find("y").text)
positionZ = float(positionData.find("z").text)
self.position = (positionX, positionY, positionZ)
class XMLMap:
def __init__(self):
self.staticGeometry = []
self.collisionGeometry = []
def parseVersion1(self, xmlData):
print(f"Parse version 1")
staticGeometryData = xmlData.find("static-geometry")
for propData in staticGeometryData:
prop = XMLProp()
prop.read(propData)
self.staticGeometry.append(
prop
)
def read(self, xmlData):
print("Reading XML map")
version = xmlData.attrib["version"] # XXX: Handle maps with no version attrib
print(xmlData.attrib)
print(f"Found version {version}")
if version == "1.0.Light":
self.parseVersion1(xmlData)
else:
raise RuntimeError(f"Unsupported map XML version: {version}")
class XMLLibraryProp:
def __init__(self):
self.meshPath = None
def parse(self, folderPath, xmlData):
# TODO: some props contain <sprite> instead!
meshData = xmlData.find("mesh")
if meshData != None:
meshFile = meshData.attrib["file"]
self.meshPath = folderPath / meshFile
# TODO: parse textures
class XMLLibrary:
def __init__(self):
# Contains {"libraryName": {"propGroup": {"propName": prop, "propName": prop}}}
self.libraries = {}
def getProp(self, libraryName, groupName, propName):
return self.libraries[libraryName][groupName][propName]
def loadLibrary(self, folderPath, xmlPath):
xmlData = ET.parse(xmlPath).getroot()
libraryName = xmlData.attrib["name"]
# Read prop groups and their props
library = {}
for propGroupData in xmlData:
propGroupName = propGroupData.attrib["name"]
library[propGroupName] = {}
for propData in propGroupData:
propName = propData.attrib["name"]
prop = XMLLibraryProp()
prop.parse(folderPath, propData)
library[propGroupName][propName] = prop
self.libraries[libraryName] = library
# Recursive function
def loadChildren(self, path):
# Check for library.xml and load it
xmlPath = path / "library.xml"
if xmlPath.exists():
self.loadLibrary(path, xmlPath)
# TODO: images.xml
# Check sub folders
for subPath in path.iterdir():
if subPath.is_dir(): self.loadChildren(subPath)
def load(self, path):
print(f"Loading library from {path}")
self.loadChildren(path)
print(f"Loaded {len(self.libraries)} libraries")
'''
Blender IO
'''
class ImportTanki(bpy.types.Operator, ImportHelper):
bl_idname = "tankionline.importmap"
bl_label = "Import map"
filter_glob: StringProperty(default="*.xml", options={'HIDDEN'})
def invoke(self, context, event):
return ImportHelper.invoke(self, context, event)
def execute(self, context):
print("Begin map import")
# Library setup
print("Setup libraries")
libraryPath = context.preferences.addons[__name__].preferences.libraryPath
libraryPath = Path(libraryPath)
library = XMLLibrary()
library.load(libraryPath)
with open(self.filepath, "r") as file:
mapData = ET.parse(self.filepath).getroot()
print(mapData)
tankiMap = XMLMap()
tankiMap.read(mapData)
# Begin loading the map into blender
for mapProp in tankiMap.staticGeometry:
propData = library.getProp(mapProp.libraryName, mapProp.groupName, mapProp.name)
if propData.meshPath != None:
meshPath = str(propData.meshPath)
print(meshPath)
bpy.ops.import_scene.max3ds(filepath=meshPath)
return {"FINISHED"}
'''
UI
'''
class TankiImporterPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
libraryPath: StringProperty(
name="Library folder",
subtype='FILE_PATH',
)
def draw(self, context):
layout = self.layout
layout.label(text="Map importer settings")
layout.prop(self, "libraryPath")
def menu_func_import(self, context):
self.layout.operator(ImportTanki.bl_idname, text="Tanki Online map (.xml)")
classes = [
ImportTanki,
TankiImporterPreferences
]
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)