-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathiqe_import_simple.py
236 lines (202 loc) · 6.71 KB
/
iqe_import_simple.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
228
229
230
231
232
233
234
235
236
# Inter-Quake Import
#
# Simple version that only imports positions, normals, texture coords and vertex colors.
# Armatures, blend weights, and custom attributes are all ignored.
#
bl_info = {
"name": "Import Inter-Quake Export Simple (.iqe)",
"description": "Import Inter-Quake Export Simple.",
"author": "Tor Andersson",
"version": (2012, 12, 21),
"blender": (2, 6, 5),
"location": "File > Import > Inter-Quake Model",
"wiki_url": "http://github.com/ccxvii/asstools",
"category": "Import-Export",
}
import bpy, bmesh, shlex, os, sys, glob
from bpy.props import *
from bpy_extras.io_utils import ImportHelper, unpack_list, unpack_face_list
from bpy_extras.image_utils import load_image
def reorder(f, ft, fc):
# see bpy_extras.io_utils.unpack_face_list()
if len(f) == 3:
if f[2] == 0:
f = f[1], f[2], f[0]
ft = ft[1], ft[2], ft[0]
fc = fc[1], fc[2], fc[0]
else: # assume quad
if f[3] == 0 or f[2] == 0:
f = f[2], f[3], f[0], f[1]
ft = ft[2], ft[3], ft[0], ft[1]
fc = fc[2], fc[3], fc[0], fc[1]
return f, ft, fc
def import_material(matname):
texname = matname.split("+")[-1]
imgname = texname + ".png"
if imgname in bpy.data.images:
img = bpy.data.images[imgname]
else:
img = load_image("textures/" + imgname, place_holder=True)
if texname in bpy.data.textures:
tex = bpy.data.textures[texname]
else:
tex = bpy.data.textures.new(texname, type='IMAGE')
tex.image = img
tex.use_alpha = True
if matname in bpy.data.materials:
mat = bpy.data.materials[matname]
else:
mat = bpy.data.materials.new(matname)
mat.game_settings.alpha_blend = 'CLIP'
mat.diffuse_intensity = 1.0
mat.specular_intensity = 0.0
mat.use_transparency = True
mat.use_object_color = True
slot = mat.texture_slots.create(0)
slot.texture = tex
slot.texture_coords = 'UV'
slot.uv_layer = "UVMap"
slot.use_map_color_diffuse = True
slot.use_map_alpha = True
slot.blend_type = 'MULTIPLY'
return mat, img
def isdegenerate(f):
if len(f) == 3:
a, b, c = f
return a == b or a == c or b == c
if len(f) == 4:
a, b, c, d = f
return a == b or a == c or a == d or b == c or b == d
return True
def import_mesh(filename):
print("importing", filename)
name = filename.split("/")[-1].split("\\")[-1].split(".")[0]
file = open(filename)
line = file.readline()
if not line.startswith("# Inter-Quake Export"):
raise Exception("Not an IQE file!")
has_vc = has_vt = False
faces = []
mat, img = None, None
for line in file.readlines():
if "#" in line or "\"" in line:
line = shlex.split(line, "#")
else:
line = line.split()
if len(line) == 0: pass
elif line[0] == "mesh": in_vp, in_vn, in_vt, in_vc = [], [], [], []
elif line[0] == "material": mat, img = import_material(line[1])
elif line[0] == "vp": in_vp.append((float(line[1]), float(line[2]), float(line[3])))
elif line[0] == "vn": in_vn.append((float(line[1]), float(line[2]), float(line[3])))
elif line[0] == "vt": has_vt = True; in_vt.append((float(line[1]), 1.0 - float(line[2])))
elif line[0] == "vc": has_vc = True; in_vc.append((float(line[1]), float(line[2]), float(line[3])))
elif line[0] == "fm":
verts = []
for f in [int(x) for x in line[1:]]:
p, n = in_vp[f], in_vn[f]
t = in_vt[f] if f < len(in_vt) else (0,0)
c = in_vc[f] if f < len(in_vc) else (1,1,1)
verts.insert(0, (p, n, t, c))
faces.append((verts, mat, img))
vertex_map = {}
out_vp, out_vn, out_f, out_ft, out_fc, out_fm = [], [], [], [], [], []
for verts, mat, img in faces:
f, ft, fc = [], [], []
for p, n, t, c in verts:
if not (p,n) in vertex_map:
vertex_map[(p,n)] = len(out_vp)
out_vp.append(p)
out_vn.append(n)
f.append(vertex_map[p,n])
ft.append(t)
fc.append(c)
f, ft, fc = reorder(f, ft, fc)
if isdegenerate(f):
print("degenerate face", f)
continue
out_f.append(f)
out_ft.append(ft)
out_fc.append(fc)
out_fm.append((mat, img))
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
grp = bpy.data.groups.new(name)
grp.objects.link(obj)
mesh.show_double_sided = False
mesh.vertices.add(len(out_vp))
mesh.vertices.foreach_set("co", unpack_list(out_vp))
mesh.tessfaces.add(len(out_f))
mesh.tessfaces.foreach_set("vertices_raw", unpack_face_list(out_f))
if has_vt: uvlayer = mesh.tessface_uv_textures.new()
if has_vc: vclayer = mesh.tessface_vertex_colors.new()
for i, face in enumerate(mesh.tessfaces):
mat, img = out_fm[i]
if mat.name not in mesh.materials:
mesh.materials.append(mat)
face.material_index = mesh.materials.find(mat.name)
face.use_smooth = True
if has_vt:
uvlayer.data[i].image = img
uvlayer.data[i].uv1 = out_ft[i][0]
uvlayer.data[i].uv2 = out_ft[i][1]
uvlayer.data[i].uv3 = out_ft[i][2]
uvlayer.data[i].uv4 = out_ft[i][3] if len(out_ft[i]) == 4 else (0,0)
if has_vc:
vclayer.data[i].color1 = out_fc[i][0]
vclayer.data[i].color2 = out_fc[i][1]
vclayer.data[i].color3 = out_fc[i][2]
vclayer.data[i].color4 = out_fc[i][3] if len(out_fc[i]) == 4 else (1,1,1)
for mat in mesh.materials:
mat.use_vertex_color_paint = has_vc
mesh.update()
# Must set normals after mesh.update() or they will be recalculated
mesh.vertices.foreach_set("normal", unpack_list(out_vn))
bpy.context.scene.objects.link(obj)
bpy.context.scene.objects.active = obj
#
# Register addon
#
class ImportIQESimple(bpy.types.Operator, ImportHelper):
bl_idname = "import.iqm"
bl_label = "Import IQE Simple"
filename_ext = ".iqe"
filter_glob = StringProperty(default="*.iqe", options={'HIDDEN'})
filepath = StringProperty(name="File Path", maxlen=1024, default="")
def execute(self, context):
import_iqm(self.properties.filepath, self.bone_axis)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(ImportIQESimple.bl_idname, text="Inter-Quake Export Simple (.iqe)")
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_func)
def batch_zap():
if "Cube" in bpy.data.objects:
obj = bpy.data.objects['Cube']
bpy.context.scene.objects.unlink(obj)
bpy.data.objects.remove(obj)
def batch(input):
batch_zap()
output = os.path.splitext(input)[0] + ".blend"
import_mesh(input)
print("Saving", output)
bpy.ops.wm.save_mainfile(filepath=output, check_existing=False)
def batch_many(input_list):
batch_zap()
output = "output.blend"
for input in input_list:
import_mesh(input)
print("Saving", output)
bpy.ops.wm.save_mainfile(filepath=output, check_existing=False)
if __name__ == "__main__":
register()
if len(sys.argv) > 4 and sys.argv[-2] == '--':
if "*" in sys.argv[-1]:
batch_many(glob.glob(sys.argv[-1]))
else:
batch(sys.argv[-1])
elif len(sys.argv) > 4 and sys.argv[4] == '--':
batch_many(sys.argv[5:])