Bone group filtering is now working for both PSK and PSA exports.

This commit is contained in:
Colin Basnett
2022-01-22 18:11:41 -08:00
parent 41e14d5e19
commit abef2a7f45
8 changed files with 353 additions and 124 deletions

View File

@@ -2,8 +2,8 @@ import bpy
import bmesh
from collections import OrderedDict
from .data import *
from ..helpers import *
# https://github.com/bwrsandman/blender-addons/blob/master/io_export_unreal_psk_psa.py
class PskInputObjects(object):
def __init__(self):
@@ -11,6 +11,12 @@ class PskInputObjects(object):
self.armature_object = None
class PskBuilderOptions(object):
def __init__(self):
self.bone_filter_mode = 'ALL'
self.bone_group_indices = []
class PskBuilder(object):
def __init__(self):
pass
@@ -51,13 +57,16 @@ class PskBuilder(object):
return input_objects
def build(self, context) -> Psk:
def build(self, context, options: PskBuilderOptions) -> Psk:
input_objects = PskBuilder.get_input_objects(context)
armature_object = input_objects.armature_object
psk = Psk()
bones = []
materials = OrderedDict()
if input_objects.armature_object is None:
if armature_object is None:
# Static mesh (no armature)
psk_bone = Psk.Bone()
psk_bone.name = bytes('static', encoding='utf-8')
@@ -68,15 +77,30 @@ class PskBuilder(object):
psk_bone.rotation = Quaternion(0, 0, 0, 1)
psk.bones.append(psk_bone)
else:
bones = list(input_objects.armature_object.data.bones)
bones = list(armature_object.data.bones)
# If bone groups are specified, get only the bones that are in the specified bone groups and their ancestors.
if len(options.bone_group_indices) > 0:
bone_indices = get_export_bone_indices_for_bone_groups(armature_object, options.bone_group_indices)
bones = [bones[bone_index] for bone_index in bone_indices]
# Ensure that the exported hierarchy has a single root bone.
root_bones = [x for x in bones if x.parent is None]
if len(root_bones) > 1:
root_bone_names = [x.name for x in bones]
raise RuntimeError('Exported bone hierarchy must have a single root bone.'
f'The bone hierarchy marked for export has {len(root_bones)} root bones: {root_bone_names}')
for bone in bones:
psk_bone = Psk.Bone()
psk_bone.name = bytes(bone.name, encoding='utf-8')
psk_bone.flags = 0
psk_bone.children_count = len(bone.children)
psk_bone.children_count = 0
try:
psk_bone.parent_index = bones.index(bone.parent)
parent_index = bones.index(bone.parent)
psk_bone.parent_index = parent_index
psk.bones[parent_index].children_count += 1
except ValueError:
psk_bone.parent_index = 0
@@ -90,8 +114,8 @@ class PskBuilder(object):
parent_tail = quat_parent @ bone.parent.tail
location = (parent_tail - parent_head) + bone.head
else:
location = input_objects.armature_object.matrix_local @ bone.head
rot_matrix = bone.matrix @ input_objects.armature_object.matrix_local.to_3x3()
location = armature_object.matrix_local @ bone.head
rot_matrix = bone.matrix @ armature_object.matrix_local.to_3x3()
rotation = rot_matrix.to_quaternion()
psk_bone.location.x = location.x
@@ -177,14 +201,34 @@ class PskBuilder(object):
psk.faces.append(face)
# WEIGHTS
# TODO: bone ~> vg might not be 1:1, provide a nice error message if this is the case
if input_objects.armature_object is not None:
armature = input_objects.armature_object.data
bone_names = [x.name for x in armature.bones]
if armature_object is not None:
# Because the vertex groups may contain entries for which there is no matching bone in the armature,
# we must filter them out and not export any weights for these vertex groups.
bone_names = [x.name for x in bones]
vertex_group_names = [x.name for x in object.vertex_groups]
bone_indices = [bone_names.index(name) for name in vertex_group_names]
vertex_group_bone_indices = dict()
for vertex_group_index, vertex_group_name in enumerate(vertex_group_names):
try:
vertex_group_bone_indices[vertex_group_index] = bone_names.index(vertex_group_name)
except ValueError:
# The vertex group does not have a matching bone in the list of bones to be exported.
# Check to see if there is an associated bone for this vertex group that exists in the armature.
# If there is, we can traverse the ancestors of that bone to find an alternate bone to use for
# weighting the vertices belonging to this vertex group.
if vertex_group_name in armature_object.data.bones:
bone = armature_object.data.bones[vertex_group_name]
while bone is not None:
try:
bone_index = bone_names.index(bone.name)
vertex_group_bone_indices[vertex_group_index] = bone_index
break
except ValueError:
bone = bone.parent
for vertex_group_index, vertex_group in enumerate(object.vertex_groups):
bone_index = bone_indices[vertex_group_index]
if vertex_group_index not in vertex_group_bone_indices:
continue
bone_index = vertex_group_bone_indices[vertex_group_index]
# TODO: exclude vertex group if it doesn't match to a bone we are exporting
for vertex_index in range(len(object.data.vertices)):
try:
weight = vertex_group.weight(vertex_index)

View File

@@ -1,9 +1,11 @@
from .data import *
from .builder import PskBuilder
from ..types import BoneGroupListItem
from ..helpers import populate_bone_group_list
from .builder import PskBuilder, PskBuilderOptions
from typing import Type
from bpy.types import Operator
from bpy.types import Operator, PropertyGroup
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
from bpy.props import StringProperty, CollectionProperty, IntProperty, BoolProperty, EnumProperty
MAX_WEDGE_COUNT = 65536
MAX_POINT_COUNT = 4294967296
@@ -58,6 +60,16 @@ class PskExporter(object):
self.write_section(fp, b'RAWWEIGHTS', Psk.Weight, self.psk.weights)
def is_bone_filter_mode_item_available(context, identifier):
input_objects = PskBuilder.get_input_objects(context)
armature_object = input_objects.armature_object
if identifier == 'BONE_GROUPS':
if not armature_object.pose or not armature_object.pose.bone_groups:
return False
# else if... you can set up other conditions if you add more options
return True
class PskExportOperator(Operator, ExportHelper):
bl_idname = 'export.psk'
bl_label = 'Export'
@@ -73,17 +85,66 @@ class PskExportOperator(Operator, ExportHelper):
def invoke(self, context, event):
try:
PskBuilder.get_input_objects(context)
input_objects = PskBuilder.get_input_objects(context)
except RuntimeError as e:
self.report({'ERROR_INVALID_CONTEXT'}, str(e))
return {'CANCELLED'}
property_group = context.scene.psk_export
# Populate bone groups list.
populate_bone_group_list(input_objects.armature_object, property_group.bone_group_list)
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
scene = context.scene
property_group = scene.psk_export
# BONES
box = layout.box()
box.label(text='Bones', icon='BONE_DATA')
bone_filter_mode_items = property_group.bl_rna.properties['bone_filter_mode'].enum_items_static
row = box.row(align=True)
for item in bone_filter_mode_items:
identifier = item.identifier
item_layout = row.row(align=True)
item_layout.prop_enum(property_group, 'bone_filter_mode', item.identifier)
item_layout.enabled = is_bone_filter_mode_item_available(context, identifier)
if property_group.bone_filter_mode == 'BONE_GROUPS':
row = box.row()
rows = max(3, min(len(property_group.bone_group_list), 10))
row.template_list('PSX_UL_BoneGroupList', '', property_group, 'bone_group_list', property_group, 'bone_group_list_index', rows=rows)
def execute(self, context):
property_group = context.scene.psk_export
builder = PskBuilder()
psk = builder.build(context)
options = PskBuilderOptions()
options.bone_group_indices = [x.index for x in property_group.bone_group_list if x.is_selected]
psk = builder.build(context, options)
exporter = PskExporter(psk)
exporter.export(self.filepath)
return {'FINISHED'}
class PskExportPropertyGroup(PropertyGroup):
bone_filter_mode: EnumProperty(
name='Bone Filter',
description='',
items=(
('ALL', 'All', 'All bones will be exported.'),
('BONE_GROUPS', 'Bone Groups', 'Only bones belonging to the selected bone groups and their ancestors will be exported.')
)
)
bone_group_list: CollectionProperty(type=BoneGroupListItem)
bone_group_list_index: IntProperty(default=0)
__classes__ = [
PskExportOperator,
PskExportPropertyGroup
]