2019-12-01 18:18:05 -08:00
|
|
|
import bpy
|
|
|
|
import bmesh
|
2019-12-02 01:13:41 -08:00
|
|
|
import mathutils
|
2019-12-01 18:18:05 -08:00
|
|
|
from .psk import Psk, Vector3
|
|
|
|
|
|
|
|
|
|
|
|
class PskBuilder(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def build(self, context) -> Psk:
|
|
|
|
object = context.view_layer.objects.active
|
2019-12-02 01:53:32 -08:00
|
|
|
|
2019-12-01 18:18:05 -08:00
|
|
|
if object.type != 'MESH':
|
2019-12-02 01:53:32 -08:00
|
|
|
raise RuntimeError('selected object must be a mesh')
|
|
|
|
|
|
|
|
if len(object.data.materials) == 0:
|
|
|
|
raise RuntimeError('the mesh must have at least one material')
|
2019-12-01 18:18:05 -08:00
|
|
|
|
|
|
|
# ensure that there is exactly one armature modifier
|
|
|
|
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
|
2019-12-03 01:27:50 -08:00
|
|
|
|
2019-12-01 18:18:05 -08:00
|
|
|
if len(modifiers) != 1:
|
|
|
|
raise RuntimeError('the mesh must have one armature modifier')
|
2019-12-03 01:27:50 -08:00
|
|
|
|
2019-12-01 18:18:05 -08:00
|
|
|
armature_modifier = modifiers[0]
|
|
|
|
armature_object = armature_modifier.object
|
|
|
|
|
|
|
|
if armature_object is None:
|
|
|
|
raise RuntimeError('the armature modifier has no linked object')
|
|
|
|
|
|
|
|
# TODO: probably requires at least one bone? not sure
|
|
|
|
mesh_data = object.data
|
|
|
|
|
|
|
|
# TODO: if there is an edge-split modifier, we need to apply it.
|
|
|
|
|
|
|
|
# TODO: duplicate all the data
|
|
|
|
mesh = bpy.data.meshes.new('export')
|
|
|
|
|
|
|
|
# copy the contents of the mesh
|
|
|
|
bm = bmesh.new()
|
|
|
|
bm.from_mesh(mesh_data)
|
|
|
|
# triangulate everything
|
|
|
|
bmesh.ops.triangulate(bm, faces=bm.faces)
|
|
|
|
bm.to_mesh(mesh)
|
|
|
|
bm.free()
|
|
|
|
del bm
|
|
|
|
|
|
|
|
psk = Psk()
|
|
|
|
|
2019-12-02 01:13:41 -08:00
|
|
|
# VERTICES
|
|
|
|
for vertex in object.data.vertices:
|
2019-12-03 01:27:50 -08:00
|
|
|
point = Vector3()
|
|
|
|
point.x = vertex.co.x
|
|
|
|
point.y = vertex.co.y
|
|
|
|
point.z = vertex.co.z
|
|
|
|
psk.points.append(point)
|
2019-12-01 18:18:05 -08:00
|
|
|
|
2019-12-02 01:13:41 -08:00
|
|
|
# WEDGES
|
|
|
|
uv_layer = object.data.uv_layers.active.data
|
2019-12-03 01:27:50 -08:00
|
|
|
if len(object.data.loops) <= 65536:
|
|
|
|
wedge_type = Psk.Wedge16
|
|
|
|
else:
|
|
|
|
wedge_type = Psk.Wedge32
|
|
|
|
psk.wedges = [wedge_type() for _ in range(len(object.data.loops))]
|
2019-12-01 18:18:05 -08:00
|
|
|
|
2019-12-02 01:13:41 -08:00
|
|
|
for loop_index, loop in enumerate(object.data.loops):
|
|
|
|
wedge = psk.wedges[loop_index]
|
2019-12-02 01:53:32 -08:00
|
|
|
wedge.material_index = 0
|
2019-12-02 01:13:41 -08:00
|
|
|
wedge.point_index = loop.vertex_index
|
|
|
|
wedge.u, wedge.v = uv_layer[loop_index].uv
|
|
|
|
psk.wedges.append(wedge)
|
|
|
|
|
|
|
|
# MATERIALS
|
2019-12-01 18:18:05 -08:00
|
|
|
for i, m in enumerate(object.data.materials):
|
|
|
|
material = Psk.Material()
|
2019-12-03 01:27:50 -08:00
|
|
|
material.name = bytes(m.name, encoding='utf-8')
|
2019-12-01 18:18:05 -08:00
|
|
|
material.texture_index = i
|
|
|
|
psk.materials.append(material)
|
|
|
|
|
2019-12-02 01:13:41 -08:00
|
|
|
# FACES
|
2019-12-03 01:27:50 -08:00
|
|
|
# TODO: this is making the assumption that the mesh is triangulated
|
2019-12-02 01:13:41 -08:00
|
|
|
object.data.calc_loop_triangles()
|
|
|
|
poly_groups, groups = object.data.calc_smooth_groups(use_bitflags=True)
|
|
|
|
for f in object.data.loop_triangles:
|
|
|
|
face = Psk.Face()
|
|
|
|
face.material_index = f.material_index
|
|
|
|
face.wedge_index_1 = f.loops[2]
|
|
|
|
face.wedge_index_2 = f.loops[1]
|
|
|
|
face.wedge_index_3 = f.loops[0]
|
|
|
|
face.smoothing_groups = poly_groups[f.polygon_index]
|
|
|
|
psk.faces.append(face)
|
2019-12-02 01:53:32 -08:00
|
|
|
# update the material index of the wedges
|
|
|
|
for i in range(3):
|
|
|
|
psk.wedges[f.loops[i]].material_index = f.material_index
|
2019-12-02 01:13:41 -08:00
|
|
|
|
|
|
|
# BONES
|
|
|
|
bone_list = list(armature_object.data.bones)
|
|
|
|
for b in armature_object.data.bones:
|
|
|
|
bone = psk.Bone()
|
2019-12-03 01:27:50 -08:00
|
|
|
bone.name = bytes(b.name, encoding='utf-8')
|
2019-12-02 01:13:41 -08:00
|
|
|
bone.children_count = len(b.children)
|
|
|
|
bone.flags = 0 # look up what this is
|
|
|
|
bone.length = 10.0 # TODO: not sure what this is
|
|
|
|
try:
|
|
|
|
bone.parent_index = bone_list.index(b.parent)
|
|
|
|
except ValueError:
|
|
|
|
# this should be -1?
|
|
|
|
bone.parent_index = 0
|
2019-12-03 01:27:50 -08:00
|
|
|
bone.position.x = b.head_local.x
|
|
|
|
bone.position.y = b.head_local.y
|
|
|
|
bone.position.z = b.head_local.z
|
|
|
|
print(bone.name)
|
|
|
|
print(bone.position.x)
|
|
|
|
print(bone.position.y)
|
|
|
|
print(bone.position.z)
|
|
|
|
print('----')
|
|
|
|
rotation = b.matrix_local.to_quaternion()
|
2019-12-02 01:13:41 -08:00
|
|
|
bone.rotation.x = rotation.x
|
|
|
|
bone.rotation.y = rotation.y
|
|
|
|
bone.rotation.z = rotation.z
|
|
|
|
bone.rotation.w = rotation.w
|
2019-12-03 01:27:50 -08:00
|
|
|
# TODO: not sure what "size" is supposed to be exactly
|
2019-12-02 01:13:41 -08:00
|
|
|
bone.size.x = 1
|
|
|
|
bone.size.y = 1
|
|
|
|
bone.size.z = 1
|
|
|
|
psk.bones.append(bone)
|
|
|
|
|
|
|
|
# WEIGHTS
|
|
|
|
# TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case
|
|
|
|
armature = armature_object.data
|
|
|
|
bone_names = [x.name for x in armature.bones]
|
|
|
|
vertex_group_names = [x.name for x in object.vertex_groups]
|
|
|
|
bone_indices = [bone_names.index(name) for name in vertex_group_names]
|
|
|
|
for vertex_group_index, vertex_group in enumerate(object.vertex_groups):
|
|
|
|
bone_index = bone_indices[vertex_group_index]
|
|
|
|
for vertex_index in range(len(object.data.vertices)):
|
|
|
|
weight = vertex_group.weight(vertex_index)
|
|
|
|
if weight == 0.0:
|
|
|
|
continue
|
|
|
|
w = Psk.Weight()
|
|
|
|
w.bone_index = bone_index
|
|
|
|
w.point_index = vertex_index
|
|
|
|
w.weight = weight
|
|
|
|
psk.weights.append(w)
|
2019-12-01 18:18:05 -08:00
|
|
|
|
|
|
|
return psk
|