Moved things around for packaging on Blender extensions
This commit is contained in:
0
shared/__init__.py
Normal file
0
shared/__init__.py
Normal file
95
shared/data.py
Normal file
95
shared/data.py
Normal file
@@ -0,0 +1,95 @@
|
||||
from ctypes import *
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Color(Structure):
|
||||
_fields_ = [
|
||||
('r', c_ubyte),
|
||||
('g', c_ubyte),
|
||||
('b', c_ubyte),
|
||||
('a', c_ubyte),
|
||||
]
|
||||
|
||||
def __iter__(self):
|
||||
yield self.r
|
||||
yield self.g
|
||||
yield self.b
|
||||
yield self.a
|
||||
|
||||
def __eq__(self, other):
|
||||
return all(map(lambda x: x[0] == x[1], zip(self, other)))
|
||||
|
||||
def __repr__(self):
|
||||
return repr(tuple(self))
|
||||
|
||||
def normalized(self) -> Tuple:
|
||||
return tuple(map(lambda x: x / 255.0, iter(self)))
|
||||
|
||||
|
||||
class Vector2(Structure):
|
||||
_fields_ = [
|
||||
('x', c_float),
|
||||
('y', c_float),
|
||||
]
|
||||
|
||||
def __iter__(self):
|
||||
yield self.x
|
||||
yield self.y
|
||||
|
||||
def __repr__(self):
|
||||
return repr(tuple(self))
|
||||
|
||||
|
||||
class Vector3(Structure):
|
||||
_fields_ = [
|
||||
('x', c_float),
|
||||
('y', c_float),
|
||||
('z', c_float),
|
||||
]
|
||||
|
||||
def __iter__(self):
|
||||
yield self.x
|
||||
yield self.y
|
||||
yield self.z
|
||||
|
||||
def __repr__(self):
|
||||
return repr(tuple(self))
|
||||
|
||||
@classmethod
|
||||
def zero(cls):
|
||||
return Vector3(0, 0, 0)
|
||||
|
||||
|
||||
class Quaternion(Structure):
|
||||
_fields_ = [
|
||||
('x', c_float),
|
||||
('y', c_float),
|
||||
('z', c_float),
|
||||
('w', c_float),
|
||||
]
|
||||
|
||||
def __iter__(self):
|
||||
yield self.w
|
||||
yield self.x
|
||||
yield self.y
|
||||
yield self.z
|
||||
|
||||
def __repr__(self):
|
||||
return repr(tuple(self))
|
||||
|
||||
@classmethod
|
||||
def identity(cls):
|
||||
return Quaternion(0, 0, 0, 1)
|
||||
|
||||
|
||||
class Section(Structure):
|
||||
_fields_ = [
|
||||
('name', c_char * 20),
|
||||
('type_flags', c_int32),
|
||||
('data_size', c_int32),
|
||||
('data_count', c_int32)
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kw):
|
||||
super().__init__(*args, **kw)
|
||||
self.type_flags = 1999801
|
||||
168
shared/helpers.py
Normal file
168
shared/helpers.py
Normal file
@@ -0,0 +1,168 @@
|
||||
import re
|
||||
import typing
|
||||
from typing import List, Iterable
|
||||
|
||||
import addon_utils
|
||||
import bpy.types
|
||||
from bpy.types import NlaStrip, Object, AnimData
|
||||
|
||||
|
||||
def rgb_to_srgb(c: float):
|
||||
if c > 0.0031308:
|
||||
return 1.055 * (pow(c, (1.0 / 2.4))) - 0.055
|
||||
else:
|
||||
return 12.92 * c
|
||||
|
||||
|
||||
def get_nla_strips_in_frame_range(animation_data: AnimData, frame_min: float, frame_max: float) -> List[NlaStrip]:
|
||||
if animation_data is None:
|
||||
return []
|
||||
strips = []
|
||||
for nla_track in animation_data.nla_tracks:
|
||||
if nla_track.mute:
|
||||
continue
|
||||
for strip in nla_track.strips:
|
||||
if (strip.frame_start < frame_min and strip.frame_end > frame_max) or \
|
||||
(frame_min <= strip.frame_start < frame_max) or \
|
||||
(frame_min < strip.frame_end <= frame_max):
|
||||
strips.append(strip)
|
||||
return strips
|
||||
|
||||
|
||||
def populate_bone_collection_list(armature_object: Object, bone_collection_list: bpy.props.CollectionProperty) -> None:
|
||||
'''
|
||||
Updates the bone collections collection.
|
||||
|
||||
Bone collection selections are preserved between updates unless none of the groups were previously selected;
|
||||
otherwise, all collections are selected by default.
|
||||
'''
|
||||
has_selected_collections = any([g.is_selected for g in bone_collection_list])
|
||||
unassigned_collection_is_selected, selected_assigned_collection_names = True, []
|
||||
|
||||
if armature_object is None:
|
||||
return
|
||||
|
||||
if has_selected_collections:
|
||||
# Preserve group selections before clearing the list.
|
||||
# We handle selections for the unassigned group separately to cover the edge case
|
||||
# where there might be an actual group with 'Unassigned' as its name.
|
||||
unassigned_collection_idx, unassigned_collection_is_selected = next(iter([
|
||||
(i, g.is_selected) for i, g in enumerate(bone_collection_list) if g.index == -1]), (-1, False))
|
||||
|
||||
selected_assigned_collection_names = [
|
||||
g.name for i, g in enumerate(bone_collection_list) if i != unassigned_collection_idx and g.is_selected]
|
||||
|
||||
bone_collection_list.clear()
|
||||
|
||||
armature = armature_object.data
|
||||
|
||||
if armature is None:
|
||||
return
|
||||
|
||||
item = bone_collection_list.add()
|
||||
item.name = 'Unassigned'
|
||||
item.index = -1
|
||||
# Count the number of bones without an assigned bone collection
|
||||
item.count = sum(map(lambda bone: 1 if len(bone.collections) == 0 else 0, armature.bones))
|
||||
item.is_selected = unassigned_collection_is_selected
|
||||
|
||||
for bone_collection_index, bone_collection in enumerate(armature.collections):
|
||||
item = bone_collection_list.add()
|
||||
item.name = bone_collection.name
|
||||
item.index = bone_collection_index
|
||||
item.count = len(bone_collection.bones)
|
||||
item.is_selected = bone_collection.name in selected_assigned_collection_names if has_selected_collections else True
|
||||
|
||||
|
||||
def check_bone_names(bone_names: Iterable[str]):
|
||||
pattern = re.compile(r'^[a-zA-Z\d_\- ]+$')
|
||||
invalid_bone_names = [x for x in bone_names if pattern.match(x) is None]
|
||||
if len(invalid_bone_names) > 0:
|
||||
raise RuntimeError(f'The following bone names are invalid: {invalid_bone_names}.\n'
|
||||
f'Bone names must only contain letters, numbers, spaces, hyphens and underscores.\n'
|
||||
f'You can bypass this by disabling "Enforce Bone Name Restrictions" in the export settings.')
|
||||
|
||||
|
||||
def get_export_bone_names(armature_object: Object, bone_filter_mode: str, bone_collection_indices: List[int]) -> List[str]:
|
||||
'''
|
||||
Returns a sorted list of bone indices that should be exported for the given bone filter mode and bone collections.
|
||||
|
||||
Note that the ancestors of bones within the bone collections will also be present in the returned list.
|
||||
|
||||
:param armature_object: Blender object with type 'ARMATURE'
|
||||
:param bone_filter_mode: One of ['ALL', 'BONE_COLLECTIONS']
|
||||
:param bone_collection_indices: List of bone collection indices to be exported.
|
||||
:return: A sorted list of bone indices that should be exported.
|
||||
'''
|
||||
if armature_object is None or armature_object.type != 'ARMATURE':
|
||||
raise ValueError('An armature object must be supplied')
|
||||
|
||||
armature_data = typing.cast(bpy.types.Armature, armature_object.data)
|
||||
bones = armature_data.bones
|
||||
bone_names = [x.name for x in bones]
|
||||
|
||||
# Get a list of the bone indices that we are explicitly including.
|
||||
bone_index_stack = []
|
||||
is_exporting_unassigned_bone_collections = -1 in bone_collection_indices
|
||||
bone_collections = list(armature_data.collections)
|
||||
|
||||
for bone_index, bone in enumerate(bones):
|
||||
# Check if this bone is in any of the collections in the bone collection indices list.
|
||||
this_bone_collection_indices = set(bone_collections.index(x) for x in bone.collections)
|
||||
is_in_exported_bone_collections = len(set(bone_collection_indices).intersection(this_bone_collection_indices)) > 0
|
||||
|
||||
if bone_filter_mode == 'ALL' or \
|
||||
(len(bone.collections) == 0 and is_exporting_unassigned_bone_collections) or \
|
||||
is_in_exported_bone_collections:
|
||||
bone_index_stack.append((bone_index, None))
|
||||
|
||||
# For each bone that is explicitly being added, recursively walk up the hierarchy and ensure that all of
|
||||
# those ancestor bone indices are also in the list.
|
||||
bone_indices = dict()
|
||||
while len(bone_index_stack) > 0:
|
||||
bone_index, instigator_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_index))
|
||||
bone_indices[bone_index] = instigator_bone_index
|
||||
|
||||
# Sort the bone index list in-place.
|
||||
bone_indices = [(x[0], x[1]) for x in bone_indices.items()]
|
||||
bone_indices.sort(key=lambda x: x[0])
|
||||
|
||||
# Split out the bone indices and the instigator bone names into separate lists.
|
||||
# We use the bone names for the return values because the bone name is a more universal way of referencing them.
|
||||
# For example, users of this function may modify bone lists, which would invalidate the indices and require an
|
||||
# index mapping scheme to resolve it. Using strings is more comfy and results in less code downstream.
|
||||
instigator_bone_names = [bones[x[1]].name if x[1] is not None else None for x in bone_indices]
|
||||
bone_names = [bones[x[0]].name for x in bone_indices]
|
||||
|
||||
# Ensure that the hierarchy we are sending back has a single root bone.
|
||||
bone_indices = [x[0] for x in bone_indices]
|
||||
root_bones = [bones[bone_index] for bone_index in bone_indices if bones[bone_index].parent is None]
|
||||
if len(root_bones) > 1:
|
||||
# There is more than one root bone.
|
||||
# Print out why each root bone was included by linking it to one of the explicitly included bones.
|
||||
root_bone_names = [bone.name for bone in root_bones]
|
||||
for root_bone_name in root_bone_names:
|
||||
bone_name = root_bone_name
|
||||
while True:
|
||||
# Traverse the instigator chain until the end to find the true instigator bone.
|
||||
# TODO: in future, it would be preferential to have a readout of *all* instigator bones.
|
||||
instigator_bone_name = instigator_bone_names[bone_names.index(bone_name)]
|
||||
if instigator_bone_name is None:
|
||||
print(f'Root bone "{root_bone_name}" was included because {bone_name} was marked for export')
|
||||
break
|
||||
bone_name = instigator_bone_name
|
||||
|
||||
raise RuntimeError('Exported bone hierarchy must have a single root bone.\n'
|
||||
f'The bone hierarchy marked for export has {len(root_bones)} root bones: {root_bone_names}.\n'
|
||||
f'Additional debugging information has been written to the console.')
|
||||
|
||||
return bone_names
|
||||
|
||||
|
||||
def is_bdk_addon_loaded():
|
||||
return addon_utils.check('bdk_addon')[1]
|
||||
55
shared/types.py
Normal file
55
shared/types.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from bpy.props import StringProperty, IntProperty, BoolProperty, FloatProperty
|
||||
from bpy.types import PropertyGroup, UIList, UILayout, Context, AnyType, Panel
|
||||
|
||||
|
||||
class PSX_UL_bone_collection_list(UIList):
|
||||
|
||||
def draw_item(self, context: Context, layout: UILayout, data: AnyType, item: AnyType, icon: int,
|
||||
active_data: AnyType, active_property: str, index: int = 0, flt_flag: int = 0):
|
||||
row = layout.row()
|
||||
row.prop(item, 'is_selected', text=getattr(item, 'name'))
|
||||
row.label(text=str(getattr(item, 'count')), icon='BONE_DATA')
|
||||
|
||||
|
||||
class PSX_PG_bone_collection_list_item(PropertyGroup):
|
||||
name: StringProperty()
|
||||
index: IntProperty()
|
||||
count: IntProperty()
|
||||
is_selected: BoolProperty(default=False)
|
||||
|
||||
|
||||
class PSX_PG_action_export(PropertyGroup):
|
||||
compression_ratio: FloatProperty(name='Compression Ratio', default=1.0, min=0.0, max=1.0, subtype='FACTOR', description='The key sampling ratio of the exported sequence.\n\nA compression ratio of 1.0 will export all frames, while a compression ratio of 0.5 will export half of the frames')
|
||||
key_quota: IntProperty(name='Key Quota', default=0, min=1, description='The minimum number of frames to be exported')
|
||||
fps: FloatProperty(name='FPS', default=30.0, min=0.0, description='The frame rate of the exported sequence')
|
||||
|
||||
|
||||
class PSX_PT_action(Panel):
|
||||
bl_idname = 'PSX_PT_action'
|
||||
bl_label = 'PSA Export'
|
||||
bl_space_type = 'DOPESHEET_EDITOR'
|
||||
bl_region_type = 'UI'
|
||||
bl_context = 'action'
|
||||
bl_category = 'Action'
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context: 'Context'):
|
||||
return context.active_object and context.active_object.type == 'ARMATURE' and context.active_action is not None
|
||||
|
||||
def draw(self, context: 'Context'):
|
||||
action = context.active_action
|
||||
layout = self.layout
|
||||
flow = layout.grid_flow(columns=1)
|
||||
flow.use_property_split = True
|
||||
flow.use_property_decorate = False
|
||||
flow.prop(action.psa_export, 'compression_ratio')
|
||||
flow.prop(action.psa_export, 'key_quota')
|
||||
flow.prop(action.psa_export, 'fps')
|
||||
|
||||
|
||||
classes = (
|
||||
PSX_PG_action_export,
|
||||
PSX_PG_bone_collection_list_item,
|
||||
PSX_UL_bone_collection_list,
|
||||
PSX_PT_action,
|
||||
)
|
||||
Reference in New Issue
Block a user