-
Notifications
You must be signed in to change notification settings - Fork 3
/
io_scene_ignition.py
200 lines (154 loc) · 6.01 KB
/
io_scene_ignition.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
"""
This is a Blender plugin that adds an importer for Ignition track meshes.
Texture loading does not currently work (it kinda loads them, but doesn't apply them).
Patches welcome!
"""
from os.path import splitext
from itertools import chain
import bpy
import struct
from bpy_extras.io_utils import unpack_list
from bpy.props import (
StringProperty,
)
from bpy_extras.io_utils import (
ImportHelper,
path_reference_mode,
)
from bpy_extras.image_utils import load_image
from bpy_extras import node_shader_utils
bl_info = {
"name": "Ignition Track Importer",
"author": "Luke Benstead",
"version": (1, 0, 20220430),
"blender": (2, 80, 0),
"location": "File > Import-Export",
"description": "Import a track from the game Ignition",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
class ImportIgnitionTrack(bpy.types.Operator, ImportHelper):
bl_idname = "import_scene.ignition_track"
bl_label = 'Import Ignition Track'
bl_options = {'PRESET'}
filename_ext = ".MSH"
filter_glob = StringProperty(
default="*.MSH",
options={'HIDDEN'}
)
path_mode = path_reference_mode
check_extension = True
def _read_palette(self, filename):
with open(filename, "rb") as f:
data = f.read()
data = data[8:] # Skip file-length, and unknown
colours = []
for i in range(256):
buffer, data = data[:3], data[3:]
colour = struct.unpack("<BBB", buffer)
colours.append(
(colour[0], colour[1], colour[2], 0 if i == 0 else 255)
)
return colours
def _read_textures(self, filename, palette):
with open(filename, "rb") as f:
data = f.read()
textures = []
for i in range(16):
texture = []
for j in range(256 * 256):
buffer, data = data[:1], data[1:]
idx = struct.unpack("<B", buffer)[0]
colour = palette[idx]
texture.append(colour)
textures.append(texture)
scale = 1.0 / 255.0
for i, texture in enumerate(textures):
tex = bpy.data.images.new("Texture{}".format(i), 256, 256, alpha=True)
tex.pixels = list(chain(*[
(r * scale, g * scale, b * scale, a * scale) for r,g,b,a in texture
]))
mat = bpy.data.materials.new("Material{}".format(i))
mat.use_nodes = True
bsdf = bsdf = mat.node_tree.nodes["Principled BSDF"]
base_color_texture = mat.node_tree.nodes.new('ShaderNodeTexImage')
base_color_texture.image = tex
mat.node_tree.links.new(bsdf.inputs['Base Color'], base_color_texture.outputs['Color'])
def _read_mesh(self, filename, mesh_count, places):
vertices = []
faces = []
with open(filename, "rb") as f:
data = f.read()
msh = bpy.data.meshes.new("TrackMesh")
obj = bpy.data.objects.new("Track", msh)
scale = 1.0 / 10.0
for j in range(mesh_count):
buffer, data = data[:4], data[4:]
vertex_count = struct.unpack("<i", buffer)[0]
buffer, data = data[:4], data[4:]
poly_count = struct.unpack("<i", buffer)[0]
voffset = len(vertices)
for i in range(vertex_count):
buffer, data = data[:12], data[12:] # 3 ints
vertex = struct.unpack("<iii", buffer)
x = vertex[0] + places[j][2]
y = vertex[1] - places[j][3]
z = vertex[2] + places[j][4]
vertices.append((x * scale, z * scale, -y * scale))
for i in range(poly_count):
buffer, data = data[:44], data[44:]
poly = struct.unpack("<iiiiiiiiiihH", buffer)
faces.append((poly[1] + voffset, poly[2] + voffset, poly[3] + voffset))
msh.from_pydata(vertices, [], faces)
msh.update(calc_edges=True)
print("Read %d vertices from %d meshes\n" % (len(vertices), mesh_count))
return obj
def _read_places(self, filename):
with open(filename, "rb") as f:
data = f.read()
buffer, data = data[:4], data[4:]
mesh_count = struct.unpack("<i", buffer)[0]
places = []
for i in range(mesh_count):
size = 8 + (3 * 4)
buffer, data = data[:size], data[size:]
place = struct.unpack("<iiiii", buffer)
places.append(place)
return mesh_count, places
def execute(self, context):
mesh_name = self.filepath
basename = splitext(self.filepath)[0]
place_name = basename + ".PLC"
pal_name = basename + ".COL"
tex_name = basename + ".TEX"
mesh_count, places = self._read_places(place_name)
obj = self._read_mesh(mesh_name, mesh_count, places)
palette = self._read_palette(pal_name)
self._read_textures(tex_name, palette)
context.view_layer.active_layer_collection.collection.objects.link(obj)
return {'FINISHED'}
def menu_func_import_mesh(self, context):
self.layout.operator(
ImportIgnitionTrack.bl_idname, text="Ignition Track (.MSH)")
def make_annotations(cls):
"""Converts class fields to annotations if running with Blender 2.8"""
if bpy.app.version < (2, 80):
return cls
bl_props = {k: v for k, v in cls.__dict__.items() if isinstance(v, tuple)}
if bl_props:
if '__annotations__' not in cls.__dict__:
setattr(cls, '__annotations__', {})
annotations = cls.__dict__['__annotations__']
for k, v in bl_props.items():
annotations[k] = v
delattr(cls, k)
return cls
def register():
make_annotations(ImportIgnitionTrack)
bpy.utils.register_class(ImportIgnitionTrack)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import_mesh)
def unregister():
bpy.utils.unregister_class(ImportIgnitionTrack)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import_mesh)