# 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 ##### # from struct import unpack from pprint import pprint import bpy from bpy_extras.object_utils import object_data_add from mathutils import Vector,Matrix from . import quakepal class MDL: pass class skin: pass 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", data[:76]) data = data[76:] mdl = MDL() mdl.name = filepath.split('/')[-1] mdl.name = mdl.name.split('.')[0] 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 = 0 mdl.size = 1.0 # random number ;) if mdl.version == 6: m = (" 1: c += 1 if f > 2: c += 1 if f < m: c += 1 if f < m - 1: c += 1 for k in fck: k[1].keyframe_points.add(c) kp = k[1].keyframe_points i = 0 if f > 2: kp[i].co = 1.0,0.0 i += 1 x = f * 1.0 if f > 1: kp[i].co = x - 1.0,0.0 i += 1 if k[0] == current: y = 1.0 else: y = 0.0 kp[i].co = x,y i += 1 if f < m: kp[i].co = x + 1.0,0.0 i += 1 if f < m - 1: kp[i].co = m * 1.0,0.0 def build_actions(mdl): sk = mdl.mesh.shape_keys for i in range(mdl.numframes): frame = mdl.frames[i] sk.animation_data_create() sk.animation_data.action = bpy.data.actions.new(frame.name) act=sk.animation_data.action fck = [] for k in mdl.keys: dp = """key_blocks["%s"].value""" % k.name fck.append((k, act.fcurves.new(data_path=dp))) if frame.type: for j in range(frame.numframes): set_keys (fck, frame.frames[j].key, j + 1, frame.numframes) else: set_keys (fck, frame.key, 1, 1) def get_base(name): i = 0 while name[i] not in "0123456789": i += 1 return name[:i] def merge_frames(mdl): i = 0 while i < mdl.numframes: if mdl.frames[i].type: i += 1 continue base = get_base(mdl.frames[i].name) j = i + 1 while j < mdl.numframes: if mdl.frames[j].type: break if get_base(mdl.frames[j].name) != base: break j += 1 f = frame() f.name = base f.numframes = j - i f.type = 1 f.frames = mdl.frames[i:j] mdl.frames[i:j] = [f] mdl.numframes -= f.numframes - 1 i += 1 def import_mdl(operator, context, filepath): bpy.context.user_preferences.edit.use_global_undo = False for obj in bpy.context.scene.objects: obj.select = False mdl = load_mdl(filepath) faces, uvs = make_faces (mdl) verts = make_verts (mdl, 0) mdl.mesh = bpy.data.meshes.new(mdl.name) mdl.mesh.from_pydata(verts, [], faces) mdl.obj = bpy.data.objects.new(mdl.name, mdl.mesh) bpy.context.scene.objects.link(mdl.obj) bpy.context.scene.objects.active = mdl.obj mdl.obj.select = True setup_skins (mdl, uvs) merge_frames(mdl) if mdl.numframes > 1 or mdl.frames[0].type: build_shape_keys(mdl) build_actions(mdl) mdl.mesh.update() bpy.context.user_preferences.edit.use_global_undo = True return 'FINISHED'