k4/k3ex.py

248 lines
9.4 KiB
Python
Raw Normal View History

2025-01-19 17:29:52 +02:00
import bpy, bmesh, struct, collections, bpy_extras, re
from collections import namedtuple
AxisChange = bpy_extras.io_utils.axis_conversion(to_forward = "-Z", to_up = "Y").to_4x4()
def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
def normalize_weights(items):
items = list(items)
items.sort(key = lambda x: x[1], reverse = True)
items = items[:4]
while len(items) < 4: items.append((0, 0))
normFac = sum([w[1] for w in items])
return tuple([(w[0], w[1] / normFac) for w in items])
V = namedtuple("V", ["pos", "nrm", "uv", "weights", "col"])
def vert2tup(bm, loop):
return V(
pos = (loop.vert.co.x, loop.vert.co.y, loop.vert.co.z),
nrm = (loop.vert.normal.x, loop.vert.normal.y, loop.vert.normal.z),
uv = (loop[bm.loops.layers.uv.active].uv[0], 1 - loop[bm.loops.layers.uv.active].uv[1]) if bm.loops.layers.uv.active is not None else (0, 0),
weights = normalize_weights(loop.vert[bm.verts.layers.deform.active].items()) if bm.verts.layers.deform.active is not None else ((0, 0), (0, 0), (0, 0), (0, 0)),
col = tuple(list(loop[bm.loops.layers.color.active]) + [1.0]) if bm.loops.layers.color.active is not None else None
)
patrn = re.compile(r"^\s*(\d+)\s*\(\s*(\d+)\s*\@\s*(\d+)\s*\)\s*$")
def write_some_data(context, filepath, doExportEverything):
if doExportEverything:
bpy.ops.object.select_all(action = "DESELECT")
objs = [o for o in context.scene.objects if o.layers[0] and o.type == "MESH"]
for o in objs:
o.select = True
bpy.ops.object.duplicate()
objs = [o for o in context.scene.objects if o.select]
bpy.ops.object.select_all(action = "DESELECT")
for o in objs:
o.select = True
context.scene.objects.active = o
for m in o.modifiers:
bpy.ops.object.modifier_apply(modifier=m.name)
o.select = False
for o in objs:
o.select = True
bpy.ops.object.join()
context.scene.objects.active = [o for o in context.scene.objects if o.select][0]
bpy.ops.object.transform_apply(location = True, scale = True, rotation = True)
arm = None
obj = None
if context.active_object.type == "ARMATURE":
arm = context.active_object
obj = arm.children[0]
elif context.active_object.type == "MESH":
obj = context.active_object
arm = obj.parent
if not doExportEverything:
bpy.ops.object.select_all(action = "DESELECT")
obj.select = True
bpy.ops.object.duplicate()
obj = context.scene.objects.active = [o for o in context.scene.objects if o.select][0]
for m in obj.modifiers:
if m.type != "ARMATURE":
bpy.ops.object.modifier_apply(modifier=m.name)
bm = bmesh.new()
bm.from_mesh(obj.data)
bmesh.ops.triangulate(bm, faces = bm.faces)
vertdict = collections.OrderedDict()
for face in bm.faces:
for loop in face.loops:
vertdict.setdefault(vert2tup(bm, loop), None)
vertkeys = list(vertdict.keys())
tris = [[] for i in range(len(obj.data.materials))]
for face in bm.faces:
for loop in face.loops:
tris[face.material_index].append(vertkeys.index(vert2tup(bm, loop)))
bm.verts.ensure_lookup_table()
bm.faces.ensure_lookup_table()
print("Beginning to write..")
with open(filepath, "wb") as out:
out.write(b"K3M ")
out.write(struct.pack("I", len(vertdict)))
out.write(struct.pack("I", 3 * len(bm.faces)))
out.write(struct.pack("B", len(arm.data.bones) if arm else 0))
print("A")
colorsEnabled = any([v.col is not None for v in vertdict.keys()])
print("B", colorsEnabled)
out.write(struct.pack("B", 1 if colorsEnabled else 0))
out.write(struct.pack("H", len([a for a in bpy.data.actions if patrn.match(a.name)]) if arm else 0))
print("Wrote header.")
if arm:
dabones = list(arm.data.bones)
for b in dabones:
m = (AxisChange * b.matrix_local).inverted()
for c in range(4):
out.write(struct.pack("4f", m.col[c][0], m.col[c][1], m.col[c][2], m.col[c][3]))
for b in dabones:
out.write(struct.pack("B", dabones.index(b.parent) if b.parent else 255))
for v in vertdict.keys():
out.write(struct.pack("3f", v[0][0], v[0][2], -v[0][1]))
for v in vertdict.keys():
invFactor = max(abs(v[1][0]), max(abs(v[1][1]), abs(v[1][2])))
out.write(struct.pack("3b", round(v[1][0] * 127 / invFactor), round(v[1][2] * 127 / invFactor), round(-v[1][1] * 127 / invFactor)))
for v in vertdict.keys():
out.write(struct.pack("2f", v[2][0], v[2][1]))
for v in vertdict.keys():
out.write(struct.pack("4B", v[3][0][0], v[3][1][0], v[3][2][0], v[3][3][0]))
for v in vertdict.keys():
out.write(struct.pack("4H", round(65535 * v[3][0][1]), round(65535 * v[3][1][1]), round(65535 * v[3][2][1]), round(65535 * v[3][3][1])))
if colorsEnabled:
for v in vertdict.keys():
out.write(struct.pack("4B", int(255 * v.col[0]), int(255 * v.col[1]), int(255 * v.col[2]), int(255 * v.col[3])))
for mat in tris:
for idx in mat:
out.write(struct.pack("H", idx))
out.write(struct.pack("H", len(tris)))
offset = 0
for i, mat in enumerate(tris):
out.write(struct.pack("2H", offset, len(mat)))
out.write(obj.material_slots[i].name.encode("UTF-8"))
out.write(b"\0")
offset += len(mat)
if arm:
actions = [a for a in bpy.data.actions if patrn.match(a.name)]
actions.sort(key = lambda a: int(patrn.match(a.name).group(1)))
for action in actions:
matched = patrn.match(action.name)
arm.animation_data.action = action
id, framecount, fps = int(matched.group(1)), int(matched.group(2)), int(matched.group(3))
out.write(struct.pack("4H", id, framecount, fps, 0))
for f in range(framecount):
bpy.context.scene.frame_set(1 + int(f * context.scene.render.fps / fps))
bpy.context.scene.update()
for bone in arm.pose.bones:
matrix = bone.matrix
if bone.parent:
matrix = bone.parent.matrix.inverted() * matrix
else:
matrix = AxisChange * matrix
translation, rotation, scale = matrix.decompose()
if rotation.w <= 0: rotation = -rotation
out.write(struct.pack("4f", translation.x, translation.y, translation.z, 1))
out.write(struct.pack("4f", rotation.x, rotation.y, rotation.z, rotation.w))
bpy.ops.object.select_all(action = "DESELECT")
obj.select = True
context.scene.objects.active = obj
bpy.ops.object.delete()
return {'FINISHED'}
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class ExportSomeData(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "export_k3m.some_data" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Export"
# ExportHelper mixin class uses this
filename_ext = ".k3m"
filter_glob = StringProperty(
default="*.k3m",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
use_setting = BoolProperty(
name="Export Everything in Layer",
description="",
default=False,
)
type = EnumProperty(
name="Example Enum",
description="Choose between two items",
items=(('OPT_A', "First Option", "Description one"),
('OPT_B', "Second Option", "Description two")),
default='OPT_A',
)
def execute(self, context):
return write_some_data(context, self.filepath, self.use_setting)
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(ExportSomeData.bl_idname, text="k3 Model")
def register():
bpy.utils.register_class(ExportSomeData)
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportSomeData)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
# test call
#bpy.ops.export_test.some_data('INVOKE_DEFAULT')