Skip to content

Commit

Permalink
Merge pull request #36 from Brachi/feature/ISSUE-26-bone-groups-and-l…
Browse files Browse the repository at this point in the history
…ayers

Feature/issue 26 bone groups and layers
  • Loading branch information
Brachi committed May 29, 2017
2 parents 7eeadf4 + df9a4d1 commit 2128ab7
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 4 deletions.
2 changes: 1 addition & 1 deletion albam/engines/mtframework/blender_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def export_mod156(parent_blender_object):
bones_array=saved_mod.bones_array,
bones_unk_matrix_array=saved_mod.bones_unk_matrix_array,
bones_world_transform_matrix_array=saved_mod.bones_world_transform_matrix_array,
unk_13=saved_mod.unk_13,
bones_animation_mapping=saved_mod.bones_animation_mapping,
bone_palette_array=bone_palette_array,
textures_array=exported_materials.textures_array,
materials_data_array=exported_materials.materials_data_array,
Expand Down
88 changes: 87 additions & 1 deletion albam/engines/mtframework/blender_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
texture_code_to_blender_texture,

)
from albam.engines.mtframework.mappers import BONE_INDEX_TO_GROUP
from albam.lib.misc import chunks
from albam.lib.half_float import unpack_half_float
from albam.lib.geometry import y_up_to_z_up
Expand Down Expand Up @@ -317,9 +318,94 @@ def _create_blender_armature_from_mod(blender_object, mod, armature_name):

bpy.ops.object.mode_set(mode='OBJECT')
assert len(armature.bones) == len(mod.bones_array)

_create_bone_groups(armature_ob, mod)
return armature_ob


def _create_bone_groups(armature_ob, mod):
bone_groups_cache = {
'BONE_GROUP_MAIN': {
'color_set': 'THEME03',
'name': 'Main',
'layer': 1,
},
'BONE_GROUP_ARMS': {
'color_set': 'THEME02',
'name': 'Arms',
'layer': 2,
},
'BONE_GROUP_LEGS': {
'color_set': 'THEME05',
'name': 'Legs',
'layer': 3,
},
'BONE_GROUP_HANDS': {
'color_set': 'THEME06',
'name': 'Hands',
'layer': 4,
},
'BONE_GROUP_HAIR': {
'color_set': 'THEME07',
'name': 'Hair',
'layer': 5,
},
'BONE_GROUP_FACIAL_BASIC': {
'color_set': 'THEME02',
'name': 'Facial Basic',
'layer': 6,
},
'BONE_GROUP_FACIAL': {
'color_set': 'THEME01',
'name': 'Facial',
'layer': 7,
},
'BONE_GROUP_ACCESORIES': {
'color_set': 'THEME14',
'name': 'Accessories',
'layer': 8,
},
'OTHER': {
'color_set': 'THEME20',
'name': 'Other',
'layer': 9,
},
}

bpy.ops.object.mode_set(mode='POSE')
for i, bone in enumerate(armature_ob.pose.bones):
source_bone = mod.bones_array[i]
anim_index = source_bone.anim_map_index
bone.bone_group = _get_or_create_bone_group(anim_index, armature_ob, i, bone_groups_cache)
bpy.ops.object.mode_set(mode='OBJECT')


def _get_or_create_bone_group(bone_anim_index, armature_ob, bone_index, bone_groups_cache):
bone_group_name = BONE_INDEX_TO_GROUP.get(bone_anim_index, 'OTHER')

bone_group_cache = bone_groups_cache.get(bone_group_name) or bone_groups_cache['OTHER']
layer_index = bone_group_cache['layer']
_move_bone_to_layers(armature_ob, bone_index, 0, layer_index)

if bone_group_cache.get('bl_group'):
return bone_group_cache['bl_group']

bl_bone_group = armature_ob.pose.bone_groups.new(bone_group_cache['name'])
bl_bone_group.color_set = bone_group_cache['color_set']
bone_group_cache['bl_group'] = bl_bone_group

return bl_bone_group


def _move_bone_to_layers(armature_ob, bone_index, *layer_indices):
layers = [False] * 32
for layer_index in layer_indices:
layers[layer_index] = True
armature_ob.data.bones[bone_index].select = True
bpy.ops.pose.bone_layers(layers=layers)
armature_ob.data.bones[bone_index].select = False


def _get_weights_per_bone(mod, mesh, vertices_array):
weights_per_bone = {}
if not mod.bone_count or not hasattr(vertices_array[0], 'bone_indices'):
Expand All @@ -328,7 +414,7 @@ def _get_weights_per_bone(mod, mesh, vertices_array):
for vertex_index, vertex in enumerate(vertices_array):
for bi, bone_index in enumerate(vertex.bone_indices):
if bone_index >= bone_palette.unk_01:
real_bone_index = mod.unk_13[bone_index]
real_bone_index = mod.bones_animation_mapping[bone_index]
else:
real_bone_index = bone_palette.values[bone_index]
if bone_index + vertex.weight_values[bi] == 0:
Expand Down

0 comments on commit 2128ab7

Please sign in to comment.