2011-09-14 10:33:51 +00:00
|
|
|
# vim:ts=4:et
|
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
|
|
|
# <pep8 compliant>
|
|
|
|
|
|
|
|
from struct import unpack
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
from bpy_extras.object_utils import object_data_add
|
|
|
|
from mathutils import Vector,Matrix
|
|
|
|
|
2011-09-15 02:29:54 +00:00
|
|
|
from . import quakepal
|
|
|
|
|
2011-09-14 10:33:51 +00:00
|
|
|
class MDL:
|
|
|
|
pass
|
2011-09-15 02:29:54 +00:00
|
|
|
class skin:
|
|
|
|
pass
|
2011-09-14 10:33:51 +00:00
|
|
|
class stvert:
|
|
|
|
pass
|
|
|
|
class tri:
|
|
|
|
pass
|
|
|
|
class frame:
|
|
|
|
pass
|
|
|
|
class vert:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def load_mdl(filepath):
|
|
|
|
data = open(filepath, "rb").read()
|
|
|
|
m = unpack("<4s i 3f 3f f 3f i i i i i i i i f", data[:84])
|
|
|
|
data = data[84:]
|
|
|
|
mdl = MDL()
|
2011-09-15 02:29:54 +00:00
|
|
|
mdl.name = filepath.split('/')[-1]
|
|
|
|
mdl.name = mdl.name.split('.')[0]
|
2011-09-14 10:33:51 +00:00
|
|
|
mdl.ident = m[0]
|
|
|
|
mdl.version = m[1]
|
|
|
|
mdl.scale = Vector(m[2:5])
|
|
|
|
mdl.scale_origin = Vector(m[5:8])
|
|
|
|
mdl.boundingradius = m[8]
|
|
|
|
mdl.eyeposition = Vector(m[9:12])
|
|
|
|
mdl.numskins = m[12]
|
|
|
|
mdl.skinwidth = m[13]
|
|
|
|
mdl.skinheight = m[14]
|
|
|
|
mdl.numverts = m[15]
|
|
|
|
mdl.numtris = m[16]
|
|
|
|
mdl.numframes = m[17]
|
|
|
|
mdl.synctype = m[18]
|
|
|
|
mdl.flags = m[19]
|
|
|
|
mdl.size = m[20]
|
|
|
|
# read in the skin data
|
|
|
|
size = mdl.skinwidth * mdl.skinheight
|
|
|
|
mdl.skins = []
|
|
|
|
for i in range(mdl.numskins):
|
2011-09-15 02:29:54 +00:00
|
|
|
s = skin()
|
|
|
|
mdl.skins.append(s)
|
|
|
|
s.type = unpack ("<i", data[:4])[0]
|
2011-09-14 10:33:51 +00:00
|
|
|
data = data[4:]
|
2011-09-15 02:29:54 +00:00
|
|
|
if s.type == 0:
|
2011-09-14 10:33:51 +00:00
|
|
|
# single skin
|
2011-09-15 02:29:54 +00:00
|
|
|
s.pixels = data[:size]
|
2011-09-14 10:33:51 +00:00
|
|
|
data = data[size:]
|
|
|
|
else:
|
|
|
|
# skin group
|
2011-09-15 02:29:54 +00:00
|
|
|
s.numskins = unpack ("<i", data[:4])[0]
|
2011-09-14 10:33:51 +00:00
|
|
|
data = data[4:]
|
2011-09-15 02:29:54 +00:00
|
|
|
s.times = unpack("<" + repr(n) + "f", data[:n * 4])
|
2011-09-14 10:33:51 +00:00
|
|
|
data = data[n * 4:]
|
2011-09-15 02:29:54 +00:00
|
|
|
s.skins = []
|
2011-09-14 10:33:51 +00:00
|
|
|
for j in range(n):
|
2011-09-15 02:29:54 +00:00
|
|
|
ss = skin()
|
|
|
|
ss.type = 0
|
|
|
|
ss.pixels = data[:size]
|
2011-09-14 10:33:51 +00:00
|
|
|
data = data[size:]
|
2011-09-15 02:29:54 +00:00
|
|
|
s.skins.append[ss]
|
2011-09-14 10:33:51 +00:00
|
|
|
#read in the st verts (uv map)
|
|
|
|
mdl.stverts = []
|
|
|
|
for i in range(mdl.numverts):
|
|
|
|
st = stvert ()
|
|
|
|
st.onseam, st.s, st.t = unpack ("<i i i", data[:12])
|
|
|
|
data = data[12:]
|
|
|
|
mdl.stverts.append(st)
|
|
|
|
#read in the tris
|
|
|
|
mdl.tris = []
|
|
|
|
for i in range(mdl.numtris):
|
|
|
|
t = unpack("<i 3i", data[:16])
|
|
|
|
data = data[16:]
|
|
|
|
mdl.tris.append(tri())
|
|
|
|
mdl.tris[-1].facesfront = t[0]
|
|
|
|
mdl.tris[-1].verts = t[1:]
|
|
|
|
#read in the frames
|
|
|
|
mdl.frames = []
|
|
|
|
for i in range(mdl.numframes):
|
|
|
|
f = frame()
|
|
|
|
f.type = unpack("<i", data[:4])[0]
|
|
|
|
data = data[4:]
|
|
|
|
if f.type == 0:
|
|
|
|
x = unpack("<3B B 3B B 16s", data[:24])
|
|
|
|
data = data[24:]
|
|
|
|
f.mins = x[0:3]
|
|
|
|
f.maxs = x[4:7]
|
|
|
|
name = x[8]
|
|
|
|
if b"\0" in name:
|
|
|
|
name = name[:name.index(b"\0")]
|
|
|
|
f.name = name
|
|
|
|
f.verts = []
|
|
|
|
for j in range(mdl.numverts):
|
|
|
|
x = unpack("<3B B", data[:4])
|
|
|
|
data = data[4:]
|
|
|
|
v = vert()
|
|
|
|
v.r = x[:3]
|
|
|
|
v.ni = x[3]
|
|
|
|
f.verts.append(v)
|
|
|
|
else:
|
|
|
|
g = f
|
|
|
|
x = unpack("<i 3B B 3B B", data[:12])
|
|
|
|
data = data[12:]
|
|
|
|
g.numframes = n = x[0]
|
|
|
|
g.mins = x[1:4]
|
|
|
|
g.maxs = x[5:8]
|
|
|
|
g.times = unpack("<" + repr(n) + "f", data[:n * 4])
|
|
|
|
data = data[n * 4:]
|
|
|
|
g.frames = []
|
|
|
|
for k in range(g.numframes):
|
|
|
|
f = frame()
|
|
|
|
x = unpack("<3B B 3B B 16s", data[:24])
|
|
|
|
data = data[24:]
|
|
|
|
f.mins = x[0:3]
|
|
|
|
f.maxs = x[4:7]
|
|
|
|
f.name = x[8]
|
|
|
|
f.verts = []
|
|
|
|
for j in range(mdl.numverts):
|
|
|
|
x = unpack("<3B B", data[:4])
|
|
|
|
data = data[4:]
|
|
|
|
v = vert()
|
|
|
|
v.r = x[:3]
|
|
|
|
v.ni = x[3]
|
|
|
|
f.verts.append(v)
|
|
|
|
g.frames.append(f)
|
|
|
|
f = g
|
|
|
|
mdl.frames.append(f)
|
|
|
|
return mdl
|
|
|
|
|
2011-09-15 04:21:08 +00:00
|
|
|
def make_verts(mdl, framenum, subframenum=0):
|
2011-09-14 10:33:51 +00:00
|
|
|
frame = mdl.frames[framenum]
|
|
|
|
if frame.type:
|
|
|
|
frame = frame.frames[subframenum]
|
|
|
|
verts = []
|
|
|
|
s = mdl.scale
|
|
|
|
o = mdl.scale_origin
|
|
|
|
m = Matrix(((s.x, 0, 0, 0),
|
|
|
|
( 0,s.y, 0, 0),
|
|
|
|
( 0, 0,s.z, 0),
|
|
|
|
(o.x,o.y,o.z, 1)))
|
|
|
|
for v in frame.verts:
|
|
|
|
verts.append(Vector(v.r) * m)
|
2011-09-15 04:21:08 +00:00
|
|
|
return verts
|
|
|
|
|
|
|
|
def make_faces(mdl):
|
|
|
|
faces = []
|
|
|
|
uvs = []
|
|
|
|
for tri in mdl.tris:
|
|
|
|
faces.append (tri.verts)
|
|
|
|
sts = []
|
|
|
|
for v in tri.verts:
|
|
|
|
stv = mdl.stverts[v]
|
|
|
|
s = stv.s
|
|
|
|
t = stv.t
|
|
|
|
if stv.onseam and not tri.facesfront:
|
|
|
|
s += mdl.skinwidth / 2
|
|
|
|
# quake textures are top to bottom, but blender images
|
|
|
|
# are bottom to top
|
|
|
|
sts.append((s * 1.0 / mdl.skinwidth, 1 - t * 1.0 / mdl.skinheight))
|
|
|
|
uvs.append(sts)
|
|
|
|
return faces, uvs
|
2011-09-14 10:33:51 +00:00
|
|
|
|
2011-09-15 02:29:54 +00:00
|
|
|
def load_skins(mdl):
|
|
|
|
for i in range(mdl.numskins):
|
|
|
|
if mdl.skins[i].type:
|
|
|
|
continue #skin groups not yet supported
|
|
|
|
img = bpy.data.images.new("%s_%d" % (mdl.name, i),
|
|
|
|
mdl.skinwidth, mdl.skinheight)
|
|
|
|
p = [0.0] * mdl.skinwidth * mdl.skinheight * 4
|
|
|
|
d = mdl.skins[i].pixels
|
|
|
|
for j in range(mdl.skinheight):
|
|
|
|
for k in range(mdl.skinwidth):
|
|
|
|
c = quakepal.palette[d[j * mdl.skinwidth + k]]
|
|
|
|
# quake textures are top to bottom, but blender images
|
|
|
|
# are bottom to top
|
|
|
|
l = ((mdl.skinheight - 1 - j) * mdl.skinwidth + k) * 4
|
|
|
|
p[l + 0] = c[0] / 255.0
|
|
|
|
p[l + 1] = c[1] / 255.0
|
|
|
|
p[l + 2] = c[2] / 255.0
|
|
|
|
p[l + 3] = 1.0
|
|
|
|
img.pixels[:] = p[:]
|
|
|
|
|
2011-09-14 10:33:51 +00:00
|
|
|
def import_mdl(operator, context, filepath):
|
|
|
|
mdl = load_mdl(filepath)
|
2011-09-15 04:21:08 +00:00
|
|
|
|
2011-09-14 10:33:51 +00:00
|
|
|
bpy.context.user_preferences.edit.use_global_undo = False
|
2011-09-15 04:21:08 +00:00
|
|
|
|
|
|
|
faces, uvs = make_faces (mdl)
|
|
|
|
verts = make_verts (mdl, 0)
|
2011-09-15 02:29:54 +00:00
|
|
|
load_skins (mdl)
|
|
|
|
mesh = bpy.data.meshes.new(mdl.name)
|
2011-09-14 10:33:51 +00:00
|
|
|
mesh.from_pydata(verts, [], faces)
|
2011-09-15 04:21:08 +00:00
|
|
|
uvlay = mesh.uv_textures.new()
|
|
|
|
for i, f in enumerate(uvlay.data):
|
|
|
|
mdl_uv = uvs[i]
|
|
|
|
for j, uv in enumerate(f.uv):
|
|
|
|
uv[0], uv[1] = mdl_uv[j]
|
2011-09-14 10:33:51 +00:00
|
|
|
mesh.update()
|
|
|
|
object_data_add(context, mesh, operator=None)
|
2011-09-15 04:21:08 +00:00
|
|
|
|
2011-09-14 10:33:51 +00:00
|
|
|
bpy.context.user_preferences.edit.use_global_undo = True
|
|
|
|
return 'FINISHED'
|
|
|
|
|