Example #1
0
 def pose_armature_bone(bone):
     pose_bone = armature_rig.pose.bones[bone.name]
     pose_bone.rotation_quaternion = Quaternion(quat_wxyz(bone.orientation))
     pose_bone.location = bone.position
     if not bone.is_animated() or  (bone.is_root() and not bone.is_export_root()):
         pose_bone.bone.hide = True
         print('Hiding pose bone %s' % bone.name)
Example #2
0
 def pose_armature_bone(bone):
     pose_bone = armature_rig.pose.bones[bone.name]
     pose_bone.rotation_quaternion = Quaternion(quat_wxyz(bone.orientation))
     pose_bone.location = bone.position
     if bone.is_root():
         pose_bone.custom_shape = root_shape
     elif bone.is_slot():
         pose_bone.bone.hide = True
     else:
         pose_bone.custom_shape = bone_shape
Example #3
0
def load_bone_delta(bond, skeleton):
    assert isinstance(bond, BoneDelta)

    for pose_bone in skeleton.pose.bones:
        hash = FNV32.hash(pose_bone.name)
        if hash in bond.deltas:
            delta = bond.deltas[hash]
            assert isinstance(delta, BoneDelta.Delta)
            pose_bone.scale += Vector(swizzle_v3(delta.scale))
            pose_bone.location += Vector(swizzle_v3(delta.position))
            pose_bone.rotation_quaternion += Quaternion(quat_wxyz(delta.orientation))
Example #4
0
def load_bone_delta(bond, skeleton):
    assert isinstance(bond, BoneDelta)

    for pose_bone in skeleton.pose.bones:
        hash = FNV32.hash(pose_bone.name)
        if hash in bond.deltas:
            delta = bond.deltas[hash]
            assert isinstance(delta, BoneDelta.Delta)
            pose_bone.scale += Vector(swizzle_v3(delta.scale))
            pose_bone.location += Vector(swizzle_v3(delta.position))
            pose_bone.rotation_quaternion += Quaternion(
                quat_wxyz(delta.orientation))
Example #5
0
def load_clip(clip_resource, skeleton, scale=1.0):
    print('Loading clip...')

    #clear existing animations
    skeleton.animation_data_clear()

    clip = clip_resource.clip
    skeleton.hide = False
    bpy.context.scene.objects.active = skeleton
    skeleton.animation_data_clear()
    skeleton.animation_data_create()
    action = skeleton.animation_data.action = bpy.data.actions.new(clip.name)

    #set custom props
    action.s3py.actor_name = clip_resource.actor_name
    action.s3py.name = clip.name
    action.s3py.source_name = clip.source_file_name

    bone_transforms = {}
    pose_bone_map = {}
    for pose_bone in skeleton.pose.bones:
        hash = FNV32.hash(pose_bone.name)
        pose_bone_map[hash] = pose_bone
        bone_transforms[hash] = pose_bone.bone.matrix_local
    bpy.ops.object.mode_set(mode='POSE')

    #reset to bind pose
    bpy.ops.pose.user_transforms_clear()

    frame_map = {}
    for track in clip.tracks:
        if not track.track_key in frame_map:
            frame_map[track.track_key] = {}
        if track.orientations:
            for orientation_key in track.orientations.frames:
                if not orientation_key.frame_index in frame_map[track.track_key].keys():
                    frame_map[track.track_key][orientation_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                frame_map[track.track_key][orientation_key.frame_index]['orientation'] = Quaternion(
                    quat_wxyz(orientation_key.data))
        if track.positions:
            for position_key in track.positions.frames:
                if not position_key.frame_index in frame_map[track.track_key].keys():
                    frame_map[track.track_key][position_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                position = Vector(position_key.data)
#                if track.track_key == FNV32.hash(ROOT_BIND):
#                    position[1] /= scale
                frame_map[track.track_key][position_key.frame_index]['position'] = position

        if track.morphs:
            for morph_key in track.morphs.frames:
                if not morph_key.frame_index in frame_map[track.track_key].keys():
                    frame_map[track.track_key][morph_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                frame_map[track.track_key][morph_key.frame_index]['morph'] = morph_key.data

    def animate_driver_bones(driver_root):
        for shape_key in driver_root.children:
            track_key = FNV64.hash(shape_key.name) & 0xFFFFFFFF
            print('shape_key %s' % shape_key)
            print('track_key: %08X' % track_key)
            if track_key in frame_map:
                group = action.groups.new(name=shape_key.name)
                data_path = 'pose.bones["%s"].morph_value' % shape_key.name
                fcurve = action.fcurves.new(data_path=data_path, index=0)
                fcurve.group = group
                frame_indices = sorted(frame_map[track_key].keys())

                for frame_index in frame_indices:
                    print('[%i]: %s' % ( frame_index, frame_map[track_key][frame_index]['morph'][0]))
                    val = frame_map[track_key][frame_index]['morph'][0]
                    fcurve.keyframe_points.add(1)
                    fcurve.keyframe_points[-1].co = [frame_index, val]

    def animate_bone(root_bone):
        track_key = FNV32.hash(root_bone.name)
        if track_key in frame_map:
            group = action.groups.new(name=root_bone.name)
            data_path = 'pose.bones["%s"].' % root_bone.name
            location_path = '%slocation' % data_path
            rotation_path = '%srotation_quaternion' % data_path
            fcurves_translate = []
            fcurves_rotate = []
            for i in range(3):
                fcurve = action.fcurves.new(data_path=location_path, index=i)
                fcurve.group = group
                fcurves_translate.append(fcurve)
            for i in range(4):
                fcurve = action.fcurves.new(data_path=rotation_path, index=i)
                fcurve.group = group
                fcurves_rotate.append(fcurve)
            frame_indices = sorted(frame_map[track_key].keys())
            current_matrix = root_bone.bone.matrix_local
            rotation_matrix = current_matrix.to_3x3().to_4x4()
            translation_matrix = Matrix.Translation(current_matrix.to_translation())
            for frame_index in frame_indices:
                frame_data = frame_map[track_key][frame_index]
                if frame_data['orientation']:
                    rotation_matrix = (
                        Quaternion() if not root_bone.parent else frame_data['orientation']).to_matrix().to_4x4()
                if frame_data['position']:
                    translation_matrix = Matrix.Translation(frame_data['position'])
                transform_matrix = translation_matrix * rotation_matrix
                if root_bone.parent:
                    transform_matrix = root_bone.parent.matrix * transform_matrix
                root_bone.matrix = transform_matrix
                if frame_data['position']:
                    for i in range(3):
                        fcurves_translate[i].keyframe_points.add(1)
                        fcurves_translate[i].keyframe_points[-1].co = [frame_index, root_bone.location[i]]
                if frame_data['orientation']:
                    for i in range(4):
                        fcurves_rotate[i].keyframe_points.add(1)
                        fcurves_rotate[i].keyframe_points[-1].co = [frame_index, root_bone.rotation_quaternion[i]]
            root_bone.matrix = root_bone.bone.matrix_local
        for child in root_bone.children:
            animate_bone(child)


    animate_bone(skeleton.pose.bones[ROOT_BONE])
    if ROOT_MORPH in skeleton.pose.bones:
        animate_driver_bones(skeleton.pose.bones[ROOT_MORPH])

    bpy.context.scene.frame_set(0)
    print('Done.')
    return action
Example #6
0
def load_clip(clip_resource, skeleton, scale=1.0):
    print('Loading clip...')

    #clear existing animations
    skeleton.animation_data_clear()

    clip = clip_resource.clip
    skeleton.hide = False
    bpy.context.scene.objects.active = skeleton
    skeleton.animation_data_clear()
    skeleton.animation_data_create()
    action = skeleton.animation_data.action = bpy.data.actions.new(clip.name)

    #set custom props
    action.s3py.actor_name = clip_resource.actor_name
    action.s3py.name = clip.name
    action.s3py.source_name = clip.source_file_name

    bone_transforms = {}
    pose_bone_map = {}
    for pose_bone in skeleton.pose.bones:
        hash = FNV32.hash(pose_bone.name)
        pose_bone_map[hash] = pose_bone
        bone_transforms[hash] = pose_bone.bone.matrix_local
    bpy.ops.object.mode_set(mode='POSE')

    #reset to bind pose
    bpy.ops.pose.user_transforms_clear()

    frame_map = {}
    for track in clip.tracks:
        if not track.track_key in frame_map:
            frame_map[track.track_key] = {}
        if track.orientations:
            for orientation_key in track.orientations.frames:
                if not orientation_key.frame_index in frame_map[
                        track.track_key].keys():
                    frame_map[track.track_key][orientation_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                frame_map[track.track_key][
                    orientation_key.frame_index]['orientation'] = Quaternion(
                        quat_wxyz(orientation_key.data))
        if track.positions:
            for position_key in track.positions.frames:
                if not position_key.frame_index in frame_map[
                        track.track_key].keys():
                    frame_map[track.track_key][position_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                position = Vector(position_key.data)
                #                if track.track_key == FNV32.hash(ROOT_BIND):
                #                    position[1] /= scale
                frame_map[track.track_key][
                    position_key.frame_index]['position'] = position

        if track.morphs:
            for morph_key in track.morphs.frames:
                if not morph_key.frame_index in frame_map[
                        track.track_key].keys():
                    frame_map[track.track_key][morph_key.frame_index] = {
                        'orientation': None,
                        'position': None,
                        'morph': None
                    }
                frame_map[track.track_key][
                    morph_key.frame_index]['morph'] = morph_key.data

    def animate_driver_bones(driver_root):
        for shape_key in driver_root.children:
            track_key = FNV64.hash(shape_key.name) & 0xFFFFFFFF
            print('shape_key %s' % shape_key)
            print('track_key: %08X' % track_key)
            if track_key in frame_map:
                group = action.groups.new(name=shape_key.name)
                data_path = 'pose.bones["%s"].morph_value' % shape_key.name
                fcurve = action.fcurves.new(data_path=data_path, index=0)
                fcurve.group = group
                frame_indices = sorted(frame_map[track_key].keys())

                for frame_index in frame_indices:
                    print('[%i]: %s' %
                          (frame_index,
                           frame_map[track_key][frame_index]['morph'][0]))
                    val = frame_map[track_key][frame_index]['morph'][0]
                    fcurve.keyframe_points.add(1)
                    fcurve.keyframe_points[-1].co = [frame_index, val]

    def animate_bone(root_bone):
        track_key = FNV32.hash(root_bone.name)
        if track_key in frame_map:
            group = action.groups.new(name=root_bone.name)
            data_path = 'pose.bones["%s"].' % root_bone.name
            location_path = '%slocation' % data_path
            rotation_path = '%srotation_quaternion' % data_path
            fcurves_translate = []
            fcurves_rotate = []
            for i in range(3):
                fcurve = action.fcurves.new(data_path=location_path, index=i)
                fcurve.group = group
                fcurves_translate.append(fcurve)
            for i in range(4):
                fcurve = action.fcurves.new(data_path=rotation_path, index=i)
                fcurve.group = group
                fcurves_rotate.append(fcurve)
            frame_indices = sorted(frame_map[track_key].keys())
            current_matrix = root_bone.bone.matrix_local
            rotation_matrix = current_matrix.to_3x3().to_4x4()
            translation_matrix = Matrix.Translation(
                current_matrix.to_translation())
            for frame_index in frame_indices:
                frame_data = frame_map[track_key][frame_index]
                if frame_data['orientation']:
                    rotation_matrix = (
                        Quaternion() if not root_bone.parent else
                        frame_data['orientation']).to_matrix().to_4x4()
                if frame_data['position']:
                    translation_matrix = Matrix.Translation(
                        frame_data['position'])
                transform_matrix = translation_matrix * rotation_matrix
                if root_bone.parent:
                    transform_matrix = root_bone.parent.matrix * transform_matrix
                root_bone.matrix = transform_matrix
                if frame_data['position']:
                    for i in range(3):
                        fcurves_translate[i].keyframe_points.add(1)
                        fcurves_translate[i].keyframe_points[-1].co = [
                            frame_index, root_bone.location[i]
                        ]
                if frame_data['orientation']:
                    for i in range(4):
                        fcurves_rotate[i].keyframe_points.add(1)
                        fcurves_rotate[i].keyframe_points[-1].co = [
                            frame_index, root_bone.rotation_quaternion[i]
                        ]
            root_bone.matrix = root_bone.bone.matrix_local
        for child in root_bone.children:
            animate_bone(child)

    animate_bone(skeleton.pose.bones[ROOT_BONE])
    if ROOT_MORPH in skeleton.pose.bones:
        animate_driver_bones(skeleton.pose.bones[ROOT_MORPH])

    bpy.context.scene.frame_set(0)
    print('Done.')
    return action