* Multiple meshes can now be exported as a single PSK
* Mesh object transforms are now properly accounted for when exporting a PSK * Fixed a bug where the PSA export dialog wouldn't auto-select valid actions
This commit is contained in:
@@ -58,15 +58,15 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
def is_action_for_armature(self, action):
|
||||
if len(action.fcurves) == 0:
|
||||
return False
|
||||
bone_names = [x.name for x in self.armature.data.bones]
|
||||
bone_names = set([x.name for x in self.armature.data.bones])
|
||||
for fcurve in action.fcurves:
|
||||
match = re.match('pose\.bones\["(.+)"\].\w+', fcurve.data_path)
|
||||
match = re.match(r'pose\.bones\["(.+)"\].\w+', fcurve.data_path)
|
||||
if not match:
|
||||
continue
|
||||
bone_name = match.group(1)
|
||||
if bone_name not in bone_names:
|
||||
return False
|
||||
return True
|
||||
if bone_name in bone_names:
|
||||
return True
|
||||
return False
|
||||
|
||||
def invoke(self, context, event):
|
||||
if context.view_layer.objects.active.type != 'ARMATURE':
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import bpy
|
||||
import bmesh
|
||||
from collections import OrderedDict
|
||||
from .data import *
|
||||
|
||||
# https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py
|
||||
|
||||
class PskBuilder(object):
|
||||
def __init__(self):
|
||||
@@ -9,98 +11,50 @@ class PskBuilder(object):
|
||||
pass
|
||||
|
||||
def build(self, context) -> Psk:
|
||||
object = context.view_layer.objects.active
|
||||
# TODO: it would be nice to be able to do this with MULTIPLE meshes so long as they both have the same armature
|
||||
mesh_objects =[]
|
||||
for object in context.view_layer.objects.selected:
|
||||
if object.type != 'MESH':
|
||||
raise RuntimeError(f'Selected object "{object.name}" is not a mesh')
|
||||
mesh_objects.append(object)
|
||||
|
||||
if object.type != 'MESH':
|
||||
raise RuntimeError('Selected object must be a mesh')
|
||||
if len(mesh_objects) == 0:
|
||||
raise RuntimeError('At least one mesh must be selected')
|
||||
|
||||
if len(object.data.materials) == 0:
|
||||
raise RuntimeError('Mesh must have at least one material')
|
||||
for object in mesh_objects:
|
||||
if len(object.data.materials) == 0:
|
||||
raise RuntimeError(f'Mesh "{object.name}" must have at least one material')
|
||||
|
||||
# ensure that there is exactly one armature modifier
|
||||
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
|
||||
# ensure that there is exactly one armature modifier object shared between all selected meshes
|
||||
armature_modifier_objects = set()
|
||||
|
||||
if len(modifiers) != 1:
|
||||
raise RuntimeError('Mesh must have one armature modifier')
|
||||
for object in mesh_objects:
|
||||
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
|
||||
if len(modifiers) != 1:
|
||||
raise RuntimeError(f'Mesh "{object.name}" must have one armature modifier')
|
||||
armature_modifier_objects.add(modifiers[0].object)
|
||||
|
||||
armature_modifier = modifiers[0]
|
||||
armature_object = armature_modifier.object
|
||||
if len(armature_modifier_objects) > 1:
|
||||
raise RuntimeError('All selected meshes must have the same armature modifier')
|
||||
|
||||
if object.modifiers[-1] != armature_modifier:
|
||||
raise RuntimeError('Armature modifier must be the last modifier in the stack')
|
||||
armature_object = list(armature_modifier_objects)[0]
|
||||
|
||||
if armature_object is None:
|
||||
raise RuntimeError('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 (maybe?)
|
||||
|
||||
# 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)
|
||||
bmesh.ops.triangulate(bm, faces=bm.faces)
|
||||
bm.to_mesh(mesh)
|
||||
bm.free()
|
||||
del bm
|
||||
|
||||
psk = Psk()
|
||||
|
||||
# VERTICES
|
||||
for vertex in object.data.vertices:
|
||||
point = Vector3()
|
||||
point.x = vertex.co.x
|
||||
point.y = vertex.co.y
|
||||
point.z = vertex.co.z
|
||||
psk.points.append(point)
|
||||
|
||||
# WEDGES
|
||||
uv_layer = object.data.uv_layers.active.data
|
||||
if len(object.data.loops) <= 65536:
|
||||
wedge_count = sum([len(m.data.loops) for m in mesh_objects])
|
||||
print(wedge_count)
|
||||
if wedge_count <= 65536:
|
||||
wedge_type = Psk.Wedge16
|
||||
else:
|
||||
wedge_type = Psk.Wedge32
|
||||
psk.wedges = [wedge_type() for _ in range(len(object.data.loops))]
|
||||
|
||||
for loop_index, loop in enumerate(object.data.loops):
|
||||
wedge = psk.wedges[loop_index]
|
||||
wedge.material_index = 0
|
||||
wedge.point_index = loop.vertex_index
|
||||
wedge.u, wedge.v = uv_layer[loop_index].uv
|
||||
wedge.v = 1.0 - wedge.v
|
||||
psk.wedges.append(wedge)
|
||||
psk = Psk()
|
||||
|
||||
# MATERIALS
|
||||
for i, m in enumerate(object.data.materials):
|
||||
if m is None:
|
||||
raise RuntimeError('Material cannot be empty (index ' + str(i) + ')')
|
||||
material = Psk.Material()
|
||||
material.name = bytes(m.name, encoding='utf-8')
|
||||
material.texture_index = i
|
||||
psk.materials.append(material)
|
||||
materials = OrderedDict()
|
||||
|
||||
# FACES
|
||||
# TODO: this is making the assumption that the mesh is triangulated
|
||||
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)
|
||||
# update the material index of the wedges
|
||||
for i in range(3):
|
||||
psk.wedges[f.loops[i]].material_index = f.material_index
|
||||
|
||||
# https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py
|
||||
# TODO: maybe we should use the EDIT bones instead???
|
||||
bones = list(armature_object.data.bones)
|
||||
for bone in bones:
|
||||
psk_bone = Psk.Bone()
|
||||
@@ -138,25 +92,89 @@ class PskBuilder(object):
|
||||
|
||||
psk.bones.append(psk_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)):
|
||||
try:
|
||||
weight = vertex_group.weight(vertex_index)
|
||||
except RuntimeError:
|
||||
continue
|
||||
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)
|
||||
vertex_offset = 0
|
||||
wedge_offset = 0
|
||||
weight_offset = 0
|
||||
|
||||
# TODO: if there is an edge-split modifier, we need to apply it (maybe?)
|
||||
for object in mesh_objects:
|
||||
# VERTICES
|
||||
for vertex in object.data.vertices:
|
||||
point = Vector3()
|
||||
v = object.matrix_world @ vertex.co
|
||||
point.x = v.x
|
||||
point.y = v.y
|
||||
point.z = v.z
|
||||
psk.points.append(point)
|
||||
|
||||
# WEDGES
|
||||
uv_layer = object.data.uv_layers.active.data
|
||||
# needs to be additive!!!
|
||||
psk.wedges.extend([wedge_type() for _ in range(len(object.data.loops))])
|
||||
|
||||
for loop_index, loop in enumerate(object.data.loops):
|
||||
wedge = psk.wedges[wedge_offset + loop_index]
|
||||
wedge.material_index = 0 # NOTE: this material index is set properly while building the faces
|
||||
wedge.point_index = loop.vertex_index + vertex_offset
|
||||
wedge.u, wedge.v = uv_layer[loop_index].uv
|
||||
wedge.v = 1.0 - wedge.v
|
||||
psk.wedges.append(wedge)
|
||||
|
||||
# MATERIALS
|
||||
material_indices = []
|
||||
for i, m in enumerate(object.data.materials):
|
||||
if m is None:
|
||||
raise RuntimeError('Material cannot be empty (index ' + str(i) + ')')
|
||||
if m.name in materials:
|
||||
material_index = list(materials.keys()).index(m.name)
|
||||
else:
|
||||
material = Psk.Material()
|
||||
material.name = bytes(m.name, encoding='utf-8')
|
||||
material.texture_index = len(psk.materials)
|
||||
psk.materials.append(material)
|
||||
materials[m.name] = m
|
||||
material_index = material.texture_index
|
||||
material_indices.append(material_index)
|
||||
|
||||
# FACES
|
||||
# TODO: this is making the assumption that the mesh is triangulated
|
||||
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 = material_indices[f.material_index]
|
||||
face.wedge_index_1 = f.loops[2] + wedge_offset
|
||||
face.wedge_index_2 = f.loops[1] + wedge_offset
|
||||
face.wedge_index_3 = f.loops[0] + wedge_offset
|
||||
face.smoothing_groups = poly_groups[f.polygon_index]
|
||||
psk.faces.append(face)
|
||||
# update the material index of the wedges
|
||||
for i in range(3):
|
||||
psk.wedges[wedge_offset + f.loops[i]].material_index = face.material_index
|
||||
|
||||
# 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)):
|
||||
try:
|
||||
weight = vertex_group.weight(vertex_index)
|
||||
except RuntimeError:
|
||||
continue
|
||||
if weight == 0.0:
|
||||
continue
|
||||
w = Psk.Weight()
|
||||
w.bone_index = bone_index
|
||||
w.point_index = vertex_offset + vertex_index
|
||||
w.weight = weight
|
||||
psk.weights.append(w)
|
||||
|
||||
vertex_offset += len(psk.points)
|
||||
wedge_offset += len(psk.wedges)
|
||||
weight_offset += len(psk.weights)
|
||||
|
||||
return psk
|
||||
|
||||
@@ -19,33 +19,33 @@ class PskExportOperator(Operator, ExportHelper):
|
||||
default='')
|
||||
|
||||
def invoke(self, context, event):
|
||||
object = context.view_layer.objects.active
|
||||
|
||||
if object.type != 'MESH':
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'The selected object must be a mesh.')
|
||||
return {'CANCELLED'}
|
||||
|
||||
if len(object.data.materials) == 0:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have at least one material')
|
||||
return {'CANCELLED'}
|
||||
|
||||
# ensure that there is exactly one armature modifier
|
||||
modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
|
||||
|
||||
if len(modifiers) != 1:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have one armature modifier')
|
||||
return {'CANCELLED'}
|
||||
|
||||
armature_modifier = modifiers[0]
|
||||
armature_object = armature_modifier.object
|
||||
|
||||
if object.modifiers[-1] != armature_modifier:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier must be the last modifier in the stack')
|
||||
return {'CANCELLED'}
|
||||
|
||||
if armature_object is None:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier has no linked object')
|
||||
return {'CANCELLED'}
|
||||
# object = context.view_layer.objects.active
|
||||
#
|
||||
# if object.type != 'MESH':
|
||||
# self.report({'ERROR_INVALID_CONTEXT'}, 'The selected object must be a mesh.')
|
||||
# return {'CANCELLED'}
|
||||
#
|
||||
# if len(object.data.materials) == 0:
|
||||
# self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have at least one material')
|
||||
# return {'CANCELLED'}
|
||||
#
|
||||
# # ensure that there is exactly one armature modifier
|
||||
# modifiers = [x for x in object.modifiers if x.type == 'ARMATURE']
|
||||
#
|
||||
# if len(modifiers) != 1:
|
||||
# self.report({'ERROR_INVALID_CONTEXT'}, 'Mesh must have one armature modifier')
|
||||
# return {'CANCELLED'}
|
||||
#
|
||||
# armature_modifier = modifiers[0]
|
||||
# armature_object = armature_modifier.object
|
||||
#
|
||||
# if object.modifiers[-1] != armature_modifier:
|
||||
# self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier must be the last modifier in the stack')
|
||||
# return {'CANCELLED'}
|
||||
#
|
||||
# if armature_object is None:
|
||||
# self.report({'ERROR_INVALID_CONTEXT'}, 'Armature modifier has no linked object')
|
||||
# return {'CANCELLED'}
|
||||
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
Reference in New Issue
Block a user