Skip to content

Commit

Permalink
vrm bones info
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonnji committed Sep 3, 2021
1 parent 4b71f28 commit 7dad53c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.blend1
*.pyc
*.vrm
*.zip
__pycache__
16 changes: 16 additions & 0 deletions addon_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

import os
import zipfile
from kitsunetsuki import bl_info


version = '{}.{}.{}'.format(*bl_info['version'])


with open(f'KAT_blender_addon_{version}.zip', 'wb') as f:
with zipfile.ZipFile(f, 'w') as z:
for root, dirs, files in os.walk('kitsunetsuki'):
for i in files:
if i != '__pycache__' and not i.endswith('.pyc'):
z.write(os.path.join(root, i))
File renamed without changes.
2 changes: 1 addition & 1 deletion kitsunetsuki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
bl_info = {
'name': 'KITSUNETSUKI Asset Tools',
'author': 'kitsune.ONE team',
'version': (0, 6, 0),
'version': (0, 6, 3),
'blender': (2, 92, 0),
'location': 'File > Import-Export',
'description': 'Exports: glTF, VRM',
Expand Down
2 changes: 2 additions & 0 deletions kitsunetsuki/base/matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def matrix_to_list(matrix):
def get_bone_matrix_local(bone):
if isinstance(bone, bpy.types.PoseBone):
return bone.matrix
if isinstance(bone, bpy.types.EditBone):
return bone.matrix
elif isinstance(bone, bpy.types.Bone):
return bone.matrix_local

Expand Down
37 changes: 28 additions & 9 deletions kitsunetsuki/exporter/gltf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,19 @@ def make_armature(self, parent_node, armature):
}
self._root['skins'].append(gltf_skin)

bone_tails_local = {}
for bone_name, bone in armature.data.bones.items():
bone_tails_local[bone_name] = bone.tail

bone_tails_off = {}
with Mode('EDIT'):
for bone_name, bone in armature.data.edit_bones.items():
bone_tails_off[bone_name] = bone.tail - bone.head

if self._pose_freeze:
set_active_object(armature)

# disconnect bones
# disconnect all bones
with Mode('EDIT'):
for bone_name, bone in armature.data.edit_bones.items():
bone.use_connect = False
Expand All @@ -276,30 +285,40 @@ def make_armature(self, parent_node, armature):
for bone_name, bone in armature.data.edit_bones.items():
bone.roll = 0
bone.length = 10

# pos_matrix = mathutils.Matrix.Translation(bone.matrix.to_translation())
# rot_matrix = bone.matrix.to_quaternion().to_matrix()
# bone.transform(pos_matrix.inverted()) # reset origin
# bone.transform(rot_matrix.inverted()) # reset rotation
# bone.transform(pos_matrix) # restore origin

bone.tail = bone.head + mathutils.Vector((0, bone.length, 0))
bone.roll = 0

# create joint nodes
gltf_joints = {}
for bone_name, bone in armature.data.bones.items():
bone_matrix = self._transform(get_bone_matrix(bone, armature))
bone_tail_matrix = self._transform(
mathutils.Matrix.Translation(bone_tails_local[bone_name]))

if self._pose_freeze:
bone_matrix = self._freeze(bone_matrix)
bone_tail_matrix = self._transform(
mathutils.Matrix.Translation(bone_tails_off[bone_name]))

# print('\u2605' * 80)
# print(bone_name, bone_tails[bone_name], bone.matrix_basis)
# print(mathutils.Matrix.Translation(bone_tails[bone_name]).to_translation())
# print((mathutils.Matrix.Translation(bone_tails[bone_name]) @
# bone.matrix.to_4x4() @
# bone.matrix.to_4x4().inverted()).to_translation())


gltf_joint = {
'name': bone_name,
'children': [],
'rotation': quat_to_list(bone_matrix.to_quaternion()),
'scale': list(bone_matrix.to_scale()),
'translation': list(bone_matrix.to_translation()),
# 'matrix': matrix_to_list(bone_matrix),
'extras': {
'tail': {
'translation': list(bone_tail_matrix.to_translation()),
}
}
}
gltf_joints[bone_name] = gltf_joint

Expand Down
2 changes: 1 addition & 1 deletion kitsunetsuki/exporter/gltf/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def make_action(self, node, armature, action):
for gltf_joint_id in gltf_skin['joints']:
gltf_joint = self._root['nodes'][gltf_joint_id]
bone = armature.pose.bones[gltf_joint['name']]
bone_matrix = get_bone_matrix(bone, armature)
bone_matrix = self._transform(get_bone_matrix(bone, armature))

rotation = quat_to_list(bone_matrix.to_quaternion())
scale = list(bone_matrix.to_scale())
Expand Down
12 changes: 11 additions & 1 deletion kitsunetsuki/gltf_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,20 @@ def print_node(gltf_data, node_id, joints=None, indent=1, parent_node=None, extr
extra += ' {' + ', '.join(['{}: {}'.format(*i) for i in refs]) + '}'

if 'VRM' in gltf_data['extensions']:
vrm_extra = []

vrm_bones = gltf_data['extensions']['VRM']['humanoid']['humanBones']
for vrm_bone in vrm_bones:
if node_id == vrm_bone['node']:
extra += ' {VRM: %s}' % vrm_bone['bone']
vrm_extra.append('VRM bone: {}'.format(vrm_bone['bone']))

for group in gltf_data['extensions']['VRM']['secondaryAnimation']['boneGroups']:
if node_id in group['bones']:
vrm_extra.append('bonegroup')
break

if vrm_extra:
extra += ' {%s}' % ', '.join(vrm_extra)

for child_node_id in gltf_node.get('children', []):
child_gltf_node = gltf_data['nodes'][child_node_id]
Expand Down

0 comments on commit 7dad53c

Please sign in to comment.