Bone group filtering is now working for both PSK and PSA exports.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
from .data import *
|
||||
from ..helpers import *
|
||||
|
||||
|
||||
class PsaBuilderOptions(object):
|
||||
def __init__(self):
|
||||
self.actions = []
|
||||
self.bone_filter_mode = 'NONE'
|
||||
self.bone_filter_mode = 'ALL'
|
||||
self.bone_group_indices = []
|
||||
|
||||
|
||||
@@ -34,39 +35,31 @@ class PsaBuilder(object):
|
||||
# armature bones.
|
||||
bone_names = [x.name for x in bones]
|
||||
pose_bones = [(bone_names.index(bone.name), bone) for bone in armature.pose.bones]
|
||||
del bone_names
|
||||
pose_bones.sort(key=lambda x: x[0])
|
||||
pose_bones = [x[1] for x in pose_bones]
|
||||
|
||||
bone_indices = list(range(len(bones)))
|
||||
|
||||
# If bone groups are specified, get only the bones that are in that specified bone groups and their ancestors.
|
||||
if options.bone_filter_mode == 'BONE_GROUPS':
|
||||
# Get a list of the bone indices that are explicitly part of the bone groups we are including.
|
||||
bone_index_stack = []
|
||||
for bone_index, pose_bone in enumerate(pose_bones):
|
||||
if pose_bone.bone_group_index in options.bone_group_indices:
|
||||
bone_index_stack.append(bone_index)
|
||||
bone_indices = get_export_bone_indices_for_bone_groups(armature, options.bone_group_indices)
|
||||
|
||||
# For each bone that is explicitly being added, recursively walk up the hierarchy and ensure that all of
|
||||
# those bone indices are also in the list.
|
||||
bone_indices = set()
|
||||
while len(bone_index_stack) > 0:
|
||||
bone_index = bone_index_stack.pop()
|
||||
bone = bones[bone_index]
|
||||
if bone.parent is not None:
|
||||
parent_bone_index = bone_names.index(bone.parent.name)
|
||||
if parent_bone_index not in bone_indices:
|
||||
bone_index_stack.append(parent_bone_index)
|
||||
bone_indices.add(bone_index)
|
||||
|
||||
del bone_names
|
||||
|
||||
# Sort out list of bone indices to be exported.
|
||||
bone_indices = sorted(list(bone_indices))
|
||||
|
||||
# The bone lists now contains only the bones that are going to be exported.
|
||||
# Make the bone lists contain only the bones that are going to be exported.
|
||||
bones = [bones[bone_index] for bone_index in bone_indices]
|
||||
pose_bones = [pose_bones[bone_index] for bone_index in bone_indices]
|
||||
|
||||
if len(bones) == 0:
|
||||
# No bones are going to be exported.
|
||||
raise RuntimeError('No bones available for export')
|
||||
|
||||
# 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 pose_bone in bones:
|
||||
psa_bone = Psa.Bone()
|
||||
psa_bone.name = bytes(pose_bone.name, encoding='utf-8')
|
||||
|
||||
@@ -5,6 +5,8 @@ from bpy_extras.io_utils import ExportHelper
|
||||
from typing import Type
|
||||
from .builder import PsaBuilder, PsaBuilderOptions
|
||||
from .data import *
|
||||
from ..types import BoneGroupListItem
|
||||
from ..helpers import *
|
||||
import re
|
||||
|
||||
|
||||
@@ -43,30 +45,29 @@ class PsaExportActionListItem(PropertyGroup):
|
||||
return self.action.name
|
||||
|
||||
|
||||
class PsaExportBoneGroupListItem(PropertyGroup):
|
||||
name: StringProperty()
|
||||
index: IntProperty()
|
||||
is_selected: BoolProperty(default=False)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.bone_group.name
|
||||
|
||||
|
||||
class PsaExportPropertyGroup(PropertyGroup):
|
||||
action_list: CollectionProperty(type=PsaExportActionListItem)
|
||||
action_list_index: IntProperty(default=0)
|
||||
bone_filter_mode: EnumProperty(
|
||||
name='Bone Filter',
|
||||
items={
|
||||
('NONE', 'None', 'All bones will be exported.'),
|
||||
('BONE_GROUPS', 'Bone Groups', 'Only bones belonging to the selected bone groups will be exported.'),
|
||||
}
|
||||
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=PsaExportBoneGroupListItem)
|
||||
bone_group_list: CollectionProperty(type=BoneGroupListItem)
|
||||
bone_group_list_index: IntProperty(default=0)
|
||||
|
||||
|
||||
def is_bone_filter_mode_item_available(context, identifier):
|
||||
if identifier == "BONE_GROUPS":
|
||||
obj = context.active_object
|
||||
if not obj.pose or not obj.pose.bone_groups:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class PsaExportOperator(Operator, ExportHelper):
|
||||
bl_idname = 'export.psa'
|
||||
bl_label = 'Export'
|
||||
@@ -84,29 +85,34 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
scene = context.scene
|
||||
property_group = context.scene.psa_export
|
||||
|
||||
# ACTIONS
|
||||
box = layout.box()
|
||||
box.label(text='Actions', icon='ACTION')
|
||||
|
||||
row = box.row()
|
||||
row.template_list('PSA_UL_ExportActionList', 'asd', scene.psa_export, 'action_list', scene.psa_export, 'action_list_index', rows=10)
|
||||
|
||||
row = box.row()
|
||||
row.operator('psa_export.actions_select_all', text='Select All')
|
||||
row.operator('psa_export.actions_deselect_all', text='Deselect All')
|
||||
row.template_list('PSA_UL_ExportActionList', 'asd', property_group, 'action_list', property_group, 'action_list_index', rows=10)
|
||||
row = box.row(align=True)
|
||||
row.label(text='Select')
|
||||
row.operator('psa_export.actions_select_all', text='All')
|
||||
row.operator('psa_export.actions_deselect_all', text='None')
|
||||
|
||||
# BONES
|
||||
box = layout.box()
|
||||
box.label(text='Bone Filter', icon='FILTER')
|
||||
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)
|
||||
|
||||
row = box.row()
|
||||
row.alignment = 'EXPAND'
|
||||
row.prop(scene.psa_export, 'bone_filter_mode', expand=True, text='Bone Filter')
|
||||
|
||||
if scene.psa_export.bone_filter_mode == 'BONE_GROUPS':
|
||||
if property_group.bone_filter_mode == 'BONE_GROUPS':
|
||||
box = layout.box()
|
||||
row = box.row()
|
||||
rows = max(3, min(len(scene.psa_export.bone_group_list), 10))
|
||||
row.template_list('PSA_UL_ExportBoneGroupList', 'asd', scene.psa_export, 'bone_group_list', scene.psa_export, 'bone_group_list_index', rows=rows)
|
||||
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 is_action_for_armature(self, action):
|
||||
if len(action.fcurves) == 0:
|
||||
@@ -122,6 +128,8 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
return False
|
||||
|
||||
def invoke(self, context, event):
|
||||
property_group = context.scene.psa_export
|
||||
|
||||
if context.view_layer.objects.active is None:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'An armature must be selected')
|
||||
return {'CANCELLED'}
|
||||
@@ -133,33 +141,29 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
self.armature = context.view_layer.objects.active
|
||||
|
||||
# Populate actions list.
|
||||
context.scene.psa_export.action_list.clear()
|
||||
property_group.action_list.clear()
|
||||
for action in bpy.data.actions:
|
||||
item = context.scene.psa_export.action_list.add()
|
||||
item = property_group.action_list.add()
|
||||
item.action = action
|
||||
item.action_name = action.name
|
||||
if self.is_action_for_armature(action):
|
||||
item.is_selected = True
|
||||
|
||||
if len(context.scene.psa_export.action_list) == 0:
|
||||
if len(property_group.action_list) == 0:
|
||||
# If there are no actions at all, we have nothing to export, so just cancel the operation.
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'There are no actions to export.')
|
||||
return {'CANCELLED'}
|
||||
|
||||
# Populate bone groups list.
|
||||
context.scene.psa_export.bone_group_list.clear()
|
||||
for bone_group_index, bone_group in enumerate(self.armature.pose.bone_groups):
|
||||
item = context.scene.psa_export.bone_group_list.add()
|
||||
item.name = bone_group.name
|
||||
item.index = bone_group_index
|
||||
item.is_selected = False
|
||||
populate_bone_groups_list(self.armature, property_group.bone_group_list)
|
||||
|
||||
context.window_manager.fileselect_add(self)
|
||||
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def execute(self, context):
|
||||
actions = [x.action for x in context.scene.psa_export.action_list if x.is_selected]
|
||||
property_group = context.scene.psa_export
|
||||
actions = [x.action for x in property_group.action_list if x.is_selected]
|
||||
|
||||
if len(actions) == 0:
|
||||
self.report({'ERROR_INVALID_CONTEXT'}, 'No actions were selected for export.')
|
||||
@@ -167,8 +171,8 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
|
||||
options = PsaBuilderOptions()
|
||||
options.actions = actions
|
||||
options.bone_filter_mode = context.scene.psa_export.bone_filter_mode
|
||||
options.bone_group_indices = [x.index for x in context.scene.psa_export.bone_group_list if x.is_selected]
|
||||
options.bone_filter_mode = property_group.bone_filter_mode
|
||||
options.bone_group_indices = [x.index for x in property_group.bone_group_list if x.is_selected]
|
||||
builder = PsaBuilder()
|
||||
psa = builder.build(context, options)
|
||||
exporter = PsaExporter(psa)
|
||||
@@ -176,13 +180,6 @@ class PsaExportOperator(Operator, ExportHelper):
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class PSA_UL_ExportBoneGroupList(UIList):
|
||||
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
|
||||
layout.alignment = 'LEFT'
|
||||
layout.prop(item, 'is_selected', icon_only=True)
|
||||
layout.label(text=item.name, icon='GROUP_BONE')
|
||||
|
||||
|
||||
class PSA_UL_ExportActionList(UIList):
|
||||
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
|
||||
layout.alignment = 'LEFT'
|
||||
@@ -210,12 +207,14 @@ class PsaExportSelectAll(bpy.types.Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
action_list = context.scene.psa_export.action_list
|
||||
property_group = context.scene.psa_export
|
||||
action_list = property_group.action_list
|
||||
has_unselected_actions = any(map(lambda action: not action.is_selected, action_list))
|
||||
return len(action_list) > 0 and has_unselected_actions
|
||||
|
||||
def execute(self, context):
|
||||
for action in context.scene.psa_export.action_list:
|
||||
property_group = context.scene.psa_export
|
||||
for action in property_group.action_list:
|
||||
action.is_selected = True
|
||||
return {'FINISHED'}
|
||||
|
||||
@@ -226,11 +225,13 @@ class PsaExportDeselectAll(bpy.types.Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
action_list = context.scene.psa_export.action_list
|
||||
property_group = context.scene.psa_export
|
||||
action_list = property_group.action_list
|
||||
has_selected_actions = any(map(lambda action: action.is_selected, action_list))
|
||||
return len(action_list) > 0 and has_selected_actions
|
||||
|
||||
def execute(self, context):
|
||||
for action in context.scene.psa_export.action_list:
|
||||
property_group = context.scene.psa_export
|
||||
for action in property_group.action_list:
|
||||
action.is_selected = False
|
||||
return {'FINISHED'}
|
||||
|
||||
@@ -17,9 +17,9 @@ class PsaImporter(object):
|
||||
pass
|
||||
|
||||
def import_psa(self, psa_reader: PsaReader, sequence_names: List[AnyStr], context):
|
||||
properties = context.scene.psa_import
|
||||
property_group = context.scene.psa_import
|
||||
sequences = map(lambda x: psa_reader.sequences[x], sequence_names)
|
||||
armature_object = properties.armature_object
|
||||
armature_object = property_group.armature_object
|
||||
armature_data = armature_object.data
|
||||
|
||||
class ImportBone(object):
|
||||
@@ -190,6 +190,14 @@ class PsaImporter(object):
|
||||
print(f'total_time: {total_time}')
|
||||
|
||||
|
||||
class PsaImportPsaBoneItem(PropertyGroup):
|
||||
bone_name: StringProperty()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.bone_name
|
||||
|
||||
|
||||
class PsaImportActionListItem(PropertyGroup):
|
||||
action_name: StringProperty()
|
||||
frame_count: IntProperty()
|
||||
@@ -201,16 +209,22 @@ class PsaImportActionListItem(PropertyGroup):
|
||||
|
||||
|
||||
def on_psa_file_path_updated(property, context):
|
||||
context.scene.psa_import.action_list.clear()
|
||||
property_group = context.scene.psa_import
|
||||
property_group.action_list.clear()
|
||||
property_group.psa_bones.clear()
|
||||
try:
|
||||
# Read the file and populate the action list.
|
||||
p = os.path.abspath(context.scene.psa_import.psa_file_path)
|
||||
p = os.path.abspath(property_group.psa_file_path)
|
||||
psa_reader = PsaReader(p)
|
||||
for sequence in psa_reader.sequences.values():
|
||||
item = context.scene.psa_import.action_list.add()
|
||||
item = property_group.action_list.add()
|
||||
item.action_name = sequence.name.decode('windows-1252')
|
||||
item.frame_count = sequence.frame_count
|
||||
item.is_selected = True
|
||||
|
||||
for psa_bone in psa_reader.bones:
|
||||
item = property_group.psa_bones.add()
|
||||
item.bone_name = psa_bone.name
|
||||
except IOError as e:
|
||||
print('ERROR READING FILE')
|
||||
print(e)
|
||||
@@ -218,9 +232,19 @@ def on_psa_file_path_updated(property, context):
|
||||
pass
|
||||
|
||||
|
||||
def on_armature_object_updated(property, context):
|
||||
# TODO: ensure that there are matching bones between the two rigs.
|
||||
property_group = context.scene.psa_import
|
||||
armature_object = property_group.armature_object
|
||||
if armature_object is not None:
|
||||
armature_bone_names = set(map(lambda bone: bone.name, armature_object.data.bones))
|
||||
psa_bone_names = set(map(lambda psa_bone: psa_bone.name, property_group.psa_bones))
|
||||
|
||||
|
||||
class PsaImportPropertyGroup(bpy.types.PropertyGroup):
|
||||
psa_file_path: StringProperty(default='', subtype='FILE_PATH', update=on_psa_file_path_updated)
|
||||
armature_object: PointerProperty(name='Armature', type=bpy.types.Object)
|
||||
psa_bones: CollectionProperty(type=PsaImportPsaBoneItem)
|
||||
armature_object: PointerProperty(name='Object', type=bpy.types.Object, update=on_armature_object_updated)
|
||||
action_list: CollectionProperty(type=PsaImportActionListItem)
|
||||
action_list_index: IntProperty(name='', default=0)
|
||||
action_filter_name: StringProperty(default='')
|
||||
@@ -265,12 +289,14 @@ class PsaImportSelectAll(bpy.types.Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
action_list = context.scene.psa_import.action_list
|
||||
property_group = context.scene.psa_import
|
||||
action_list = property_group.action_list
|
||||
has_unselected_actions = any(map(lambda action: not action.is_selected, action_list))
|
||||
return len(action_list) > 0 and has_unselected_actions
|
||||
|
||||
def execute(self, context):
|
||||
for action in context.scene.psa_import.action_list:
|
||||
property_group = context.scene.psa_import
|
||||
for action in property_group.action_list:
|
||||
action.is_selected = True
|
||||
return {'FINISHED'}
|
||||
|
||||
@@ -281,35 +307,41 @@ class PsaImportDeselectAll(bpy.types.Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
action_list = context.scene.psa_import.action_list
|
||||
property_group = context.scene.psa_import
|
||||
action_list = property_group.action_list
|
||||
has_selected_actions = any(map(lambda action: action.is_selected, action_list))
|
||||
return len(action_list) > 0 and has_selected_actions
|
||||
|
||||
def execute(self, context):
|
||||
for action in context.scene.psa_import.action_list:
|
||||
property_group = context.scene.psa_import
|
||||
for action in property_group.action_list:
|
||||
action.is_selected = False
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class PSA_PT_ImportPanel(Panel):
|
||||
bl_space_type = 'VIEW_3D'
|
||||
bl_space_type = 'NLA_EDITOR'
|
||||
bl_region_type = 'UI'
|
||||
bl_label = 'PSA Import'
|
||||
bl_context = 'objectmode'
|
||||
bl_context = 'object'
|
||||
bl_category = 'PSA Import'
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return context.view_layer.objects.active is not None
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
scene = context.scene
|
||||
property_group = context.scene.psa_import
|
||||
row = layout.row()
|
||||
row.prop(scene.psa_import, 'psa_file_path', text='PSA File')
|
||||
row.prop(property_group, 'psa_file_path', text='PSA File')
|
||||
row = layout.row()
|
||||
row.prop_search(scene.psa_import, 'armature_object', bpy.data, 'objects')
|
||||
row.prop_search(property_group, 'armature_object', bpy.data, 'objects')
|
||||
box = layout.box()
|
||||
box.label(text=f'Actions ({len(scene.psa_import.action_list)})', icon='ACTION')
|
||||
box.label(text=f'Actions ({len(property_group.action_list)})', icon='ACTION')
|
||||
row = box.row()
|
||||
rows = max(3, min(len(scene.psa_import.action_list), 10))
|
||||
row.template_list('PSA_UL_ImportActionList', 'asd', scene.psa_import, 'action_list', scene.psa_import, 'action_list_index', rows=rows)
|
||||
rows = max(3, min(len(property_group.action_list), 10))
|
||||
row.template_list('PSA_UL_ImportActionList', '', property_group, 'action_list', property_group, 'action_list_index', rows=rows)
|
||||
row = box.row(align=True)
|
||||
row.label(text='Select')
|
||||
row.operator('psa_import.actions_select_all', text='All')
|
||||
@@ -323,14 +355,16 @@ class PsaImportOperator(Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
action_list = context.scene.psa_import.action_list
|
||||
property_group = context.scene.psa_import
|
||||
action_list = property_group.action_list
|
||||
has_selected_actions = any(map(lambda action: action.is_selected, action_list))
|
||||
armature_object = context.scene.psa_import.armature_object
|
||||
armature_object = property_group.armature_object
|
||||
return has_selected_actions and armature_object is not None
|
||||
|
||||
def execute(self, context):
|
||||
psa_reader = PsaReader(context.scene.psa_import.psa_file_path)
|
||||
sequence_names = [x.action_name for x in context.scene.psa_import.action_list if x.is_selected]
|
||||
property_group = context.scene.psa_import
|
||||
psa_reader = PsaReader(property_group.psa_file_path)
|
||||
sequence_names = [x.action_name for x in property_group.action_list if x.is_selected]
|
||||
PsaImporter().import_psa(psa_reader, sequence_names, context)
|
||||
return {'FINISHED'}
|
||||
|
||||
@@ -351,6 +385,7 @@ class PsaImportFileSelectOperator(Operator, ImportHelper):
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def execute(self, context):
|
||||
context.scene.psa_import.psa_file_path = self.filepath
|
||||
property_group = context.scene.psa_import
|
||||
property_group.psa_file_path = self.filepath
|
||||
# Load the sequence names from the selected file
|
||||
return {'FINISHED'}
|
||||
|
||||
Reference in New Issue
Block a user