示例#1
0
    def position_bone_in_animation_frame(
            self,
            armature_obj: Object,
            animation_frame_armature_bone_model: 'BlenderPoseModeAnimationFrameModelNode'):
        pose = armature_obj.pose  # type: Pose
        complementary_pose_bone = pose.bones.get(animation_frame_armature_bone_model.bone_name)  # type: PoseBone
        complementary_pose_bone.rotation_mode = 'QUATERNION'

        if animation_frame_armature_bone_model.bone_name != "ROOT_CHANNEL":
            loc = Matrix.Translation(Vector((
                animation_frame_armature_bone_model.position.x,
                animation_frame_armature_bone_model.position.y,
                animation_frame_armature_bone_model.position.z)))
            rot = Quaternion(Vector((
                animation_frame_armature_bone_model.rotation.w,
                animation_frame_armature_bone_model.rotation.x,
                animation_frame_armature_bone_model.rotation.y,
                animation_frame_armature_bone_model.rotation.z)),
                ).to_matrix().to_4x4()
            scale = Matrix()
            scale[0][0] = animation_frame_armature_bone_model.scale.x
            scale[1][1] = animation_frame_armature_bone_model.scale.y
            scale[2][2] = animation_frame_armature_bone_model.scale.z
            world_mat = loc @ rot @ scale
            complementary_pose_bone.matrix = armature_obj.convert_space(
                pose_bone=complementary_pose_bone,
                matrix=world_mat,
                from_space='WORLD',
                to_space='POSE')
示例#2
0
    def add_bone(self,
                 head_position: Tuple[float, float, float],
                 tail_position: Tuple[float, float, float],
                 position: Vector3d,
                 rotation: Quaternion,
                 scale: Vector3d,
                 armature_obj: Object,
                 armature: Armature,
                 name: str):
        edit_bone = armature.edit_bones.new(name=name)  # type: EditBone
        edit_bone.head[0] = head_position[0]
        edit_bone.head[1] = head_position[1]
        edit_bone.head[2] = head_position[2]

        edit_bone.tail[0] = tail_position[0]
        edit_bone.tail[1] = tail_position[1]
        edit_bone.tail[2] = tail_position[2]

        if name != "ROOT_CHANNEL":
            loc = mathutils.Matrix.Translation(mathutils.Vector((
                position.x,
                position.y,
                position.z)))
            rot = mathutils.Quaternion(mathutils.Vector((
                - rotation.w,
                rotation.x,
                rotation.y,
                rotation.z)),
            ).to_matrix().to_4x4()
            scale_mat = mathutils.Matrix()
            scale_mat[0][0] = scale.x
            scale_mat[1][1] = scale.y
            scale_mat[2][2] = scale.z
            world_mat = loc @ rot @ scale_mat
            edit_bone.matrix = armature_obj.convert_space(
                #pose_bone=complementary_pose_bone,
                matrix=world_mat,
                from_space='WORLD',
                to_space='LOCAL')
示例#3
0
    def transform_bone_in_animation_frame(
        cls,
        blender_armature_obj: Object,
        bone_transform_node: BoneTransformNode
    ):
        pose = blender_armature_obj.pose  # type: Pose
        complementary_pose_bone = pose.bones.get(bone_transform_node.bone_name)  # type: PoseBone
        complementary_pose_bone.rotation_mode = 'QUATERNION'

        loc = mathutils.Matrix.Translation(mathutils.Vector((
            bone_transform_node.bone_transform.position.x,
            bone_transform_node.bone_transform.position.y,
            bone_transform_node.bone_transform.position.z,
        )))

        rot = mathutils.Quaternion(mathutils.Vector((
            bone_transform_node.bone_transform.rotation.w,
            bone_transform_node.bone_transform.rotation.x,
            bone_transform_node.bone_transform.rotation.y,
            bone_transform_node.bone_transform.rotation.z
        ))).to_matrix().to_4x4()

        scale = mathutils.Matrix()
        # scale.zero()

        scale[0][0] = bone_transform_node.bone_transform.scale.x
        scale[1][1] = bone_transform_node.bone_transform.scale.y
        scale[2][2] = bone_transform_node.bone_transform.scale.z

        world_mat = loc @ rot @ scale

        # complementary_pose_bone.matrix = world_mat

        complementary_pose_bone.matrix = blender_armature_obj.convert_space(
            pose_bone=complementary_pose_bone,
            matrix=world_mat,
            from_space='LOCAL',
            to_space='LOCAL'
        )