# An independent IQM reader for tools/fixtures/iqm.py (plain Python, from the IQM SDK's format description and its # demo's skinning: joint matrices = pose world * inverse bind world): header counts and the bounding box of the # skinned vertices for every animation frame, computed from the joints, poses and frames (not from the file's bounds # block, which it returns too for comparison). import math, struct HEADER = ("version filesize flags num_text ofs_text num_meshes ofs_meshes num_vertexarrays num_vertexes ofs_vertexarrays " "num_triangles ofs_triangles ofs_adjacency num_joints ofs_joints num_poses ofs_poses num_anims ofs_anims num_frames " "num_framechannels ofs_frames ofs_bounds num_comment ofs_comment num_extensions ofs_extensions").split() FMT = {0: ("b", 1), 1: ("B", 1), 2: ("h", 2), 3: ("H", 2), 4: ("i", 4), 5: ("I", 4), 7: ("f", 4), 8: ("d", 8)} def mat(t, q, s): x, y, z, w = q n = math.sqrt(x * x + y * y + z * z + w * w) or 1 x, y, z, w = x / n, y / n, z / n, w / n r = [[1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)], [2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)], [2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)]] return [[r[i][0] * s[0], r[i][1] * s[1], r[i][2] * s[2], t[i]] for i in range(3)] + [[0, 0, 0, 1]] def mul(a, b): return [[sum(a[i][k] * b[k][j] for k in range(4)) for j in range(4)] for i in range(4)] def inv(m): # general 4x4 inverse (Gauss-Jordan) a = [row[:] + [1 if i == j else 0 for j in range(4)] for i, row in enumerate(m)] for c in range(4): p = max(range(c, 4), key=lambda r: abs(a[r][c])) a[c], a[p] = a[p], a[c] d = a[c][c] or 1e-30 a[c] = [v / d for v in a[c]] for r in range(4): if r != c: f = a[r][c] a[r] = [v - f * w for v, w in zip(a[r], a[c])] return [row[4:] for row in a] def read(data): h = dict(zip(HEADER, struct.unpack_from("<27I", data, 16))) text = data[h["ofs_text"]:h["ofs_text"] + h["num_text"]] def s(o): return text[o:text.index(b"\0", o)].decode("latin1") arrays = {} for i in range(h["num_vertexarrays"]): typ, flags, fmt, size, ofs = struct.unpack_from("<5I", data, h["ofs_vertexarrays"] + i * 20) c, n = FMT[fmt] vals = struct.unpack_from("<%d%s" % (h["num_vertexes"] * size, c), data, ofs) arrays[typ] = (fmt, size, vals) joints = [] for j in range(h["num_joints"]): v = struct.unpack_from("= 0 else m) invbind = [inv(m) for m in bind] poses = [] for p in range(h["num_poses"]): v = struct.unpack_from("= 0 else m) skin = [mul(world[j], invbind[j]) for j in range(len(world))] lo, hi = [1e30] * 3, [-1e30] * 3 for v in range(h["num_vertexes"]): x, y, z = pos[v * 3:v * 3 + 3] if bi and bw: sx = sy = sz = 0.0 tot = 0.0 for c in range(4): w = bw[2][v * 4 + c] / 255.0 if bw[0] == 1 else bw[2][v * 4 + c] if w > 0: m = skin[bi[2][v * 4 + c]] sx += w * (m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3]) sy += w * (m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3]) sz += w * (m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3]) tot += w if tot > 0: x, y, z = sx, sy, sz lo = [min(a, b) for a, b in zip(lo, (x, y, z))] hi = [max(a, b) for a, b in zip(hi, (x, y, z))] boxes.append({"min": lo, "max": hi}) stored = [] if h["ofs_bounds"]: for f in range(h["num_frames"]): v = struct.unpack_from("<8f", data, h["ofs_bounds"] + f * 32) stored.append({"min": list(v[0:3]), "max": list(v[3:6])}) return {"meshes": h["num_meshes"], "vertexes": h["num_vertexes"], "triangles": h["num_triangles"], "joints": h["num_joints"], "jointNames": [j[0] for j in joints], "anims": anims, "frames": h["num_frames"], "skinned": boxes, "stored": stored}