def construct_from_json_dict(self, bone_bind_pose_json_dict) -> TransformNode: vector3d_factory = Vector3dFactory() quaternion_factory = QuaternionFactory() result = TransformNode() result.position = vector3d_factory.construct_from_json_dict(bone_bind_pose_json_dict["position"]) result.rotation = quaternion_factory.construct_from_json_dict(bone_bind_pose_json_dict["rotation"]) result.scale = vector3d_factory.construct_from_json_dict(bone_bind_pose_json_dict["scale"]) return result
def execute(self): BlenderSceneManipulator().clear_scene() home_transform = TransformNode() first_keyframe = 0 second_keyframe = 5 test_armature_obj, test_armature_tree_hierarchy = \ BlenderArmatureBuilder() \ .with_armature_name(armature_name="TEST_ARMATURE") \ .with_bone(name="BONE_A", transform=home_transform) \ .with_bone(name="BONE_B", transform=home_transform) \ .build() animated_armature_tree_hierarchies = BlenderArmatureAnimator \ .for_armature(test_armature_obj, test_armature_tree_hierarchy) \ .animate_bone(name="BONE_A", local_transform= TransformNode.construct_with( position=Vector3d(0.3, 0.5, 0.7), rotation=Quaternion(), scale=Vector3d(0.2, 0.4, 0.6) ), keyframe_number=first_keyframe) \ .animate_bone(name="BONE_A", local_transform= TransformNode.construct_with( position=Vector3d(0.9, 0.1, 0.3), rotation=Quaternion(), scale=Vector3d(0.6, 1.6, 1.3) ), keyframe_number=second_keyframe) \ .commit() calculated_bone_animation_world_matrix_a = BoneMatrixHelper.get_world_matrix_for_bone( "BONE_A", animated_armature_tree_hierarchies[0]) # type: Matrix4x4 calculated_bone_animation_world_matrix_b = BoneMatrixHelper.get_world_matrix_for_bone( "BONE_A", animated_armature_tree_hierarchies[1]) # type: Matrix4x4 BlenderArmatureAnimator \ .for_armature(test_armature_obj, test_armature_tree_hierarchy) \ .animate_bone( name="BONE_B", local_transform=TransformNode.from_matrix4x4(calculated_bone_animation_world_matrix_a), keyframe_number=first_keyframe) \ .animate_bone( name="BONE_B", local_transform=TransformNode.from_matrix4x4(calculated_bone_animation_world_matrix_b), keyframe_number=second_keyframe) \ .commit()
def construct_from_json_dict(self, channel_transform_json_dict) -> TransformNode: result = TransformNode() result.position = Vector3dFactory().construct_from_json_dict( channel_transform_json_dict["position"]) # altered_position = Vector3d(-result.position.x, -result.position.z, result.position.y) # result.position = altered_position result.rotation = QuaternionFactory().construct_from_json_dict( channel_transform_json_dict["rotation"]) result.scale = Vector3dFactory().construct_from_json_dict( channel_transform_json_dict["scale"]) return result
def get_from_bind_bone_pose( bone_name: str, bind_bone_pose: TransformNode) -> BoneTransformNode: result = BoneTransformNode() result.bone_name = bone_name result.bone_transform = bind_bone_pose.copy() return result
def get_displaced_transforms_for(cls, bones_count: int, seed: int) -> List[TransformNode]: random.seed(seed) displaced_coordinates = [ random.uniform(-1.0, 1.0) for _ in range(bones_count) ] displaced_scale_coordinates = [ random.uniform(0.5, 1.2) for _ in range(bones_count) ] result = [] # type: List[TransformNode] home_rotation = Quaternion() for index in range(bones_count): result.append( TransformNode.construct_with( position=Vector3d(displaced_coordinates[index], displaced_coordinates[index], displaced_coordinates[index]), rotation=home_rotation, scale=Vector3d(displaced_scale_coordinates[index], displaced_scale_coordinates[index], displaced_scale_coordinates[index]))) return result
def from_matrix4x4(bone_name: str, matrix: Matrix4x4, is_keyframe: bool = False) -> 'BoneTransformNode': result = BoneTransformNode() result.bone_name = bone_name result.bone_transform = TransformNode.from_matrix4x4(matrix) result.is_keyframe = is_keyframe return result
def from_transform_node(bone_name: str, transform_node: TransformNode, is_keyframe: bool = False) -> 'BoneTransformNode': result = BoneTransformNode() result.bone_name = bone_name result.bone_transform = transform_node.copy() result.is_keyframe = is_keyframe return result
def zero_out_not_used_subobjects_bones_transformations_adding_them_to_pose_hierarchy( cls, pose_hierarchy: TreeHierarchy, all_actual_armature_bones_names: List[str]): for armature_bone_name in all_actual_armature_bones_names: if not pose_hierarchy.contains_node_key(armature_bone_name): effectively_zeroed_scale_out_transform_node = TransformNode() effectively_zeroed_scale_out_transform_node.scale = Vector3d( 0.0, 0.0, 0.0) pose_hierarchy.add_node( parent_key=UnifiedArmatureWithDeformSetsBonesNamingHelper. get_bone_name_for_root_channel(), node_key=armature_bone_name, node=BoneTransformNode.from_transform_node( bone_name=armature_bone_name, transform_node= effectively_zeroed_scale_out_transform_node, is_keyframe=True))
def get_home_transformed_bone_node(self, bone_name: str) -> EditModeBoneNode: bone_node = EditModeBoneNode() bone_node.bone_name = bone_name head_position, tail_position = EditModeBoneNodeDataFactory.get_head_and_tail_position_from( TransformNode()) bone_node.head_position = head_position bone_node.tail_position = tail_position return bone_node
def get_armature_chained_bones_transforms_for( cls, bones_count: int) -> List[TransformNode]: result = [] # type: List[TransformNode] current_position = Vector3d() home_rotation = Quaternion() home_scale = Vector3d(1.0, 1.0, 1.0) for _ in range(bones_count): current_position += Vector3d(x=0.0, y=1.0, z=0.0) result.append(TransformNode.construct_with( position=current_position, rotation=home_rotation, scale=home_scale)) return result
def interpolate_linear_for_frame( cls, frame_number: int, frames_count: int, channel_timeline_keyframes: Dict[int, TransformNode]) -> TransformNode: preceeding_keyframe_key_appropriate_for_frame_number = \ cls._get_preeceding_keyframe_key_appropriate_for_frame_number( frame_number=frame_number, frames_count=frames_count, channel_timeline_keyframes=channel_timeline_keyframes) # type: int frames_since_keyframe = frame_number - preceeding_keyframe_key_appropriate_for_frame_number # type: int if cls._is_end_keyframe( preceeding_keyframe_key_appropriate_for_frame_number, channel_timeline_keyframes): next_keyframe_key = cls._get_first_keyframe_key( channel_timeline_keyframes) # type: int frames_difference = frames_count - 1 + next_keyframe_key - preceeding_keyframe_key_appropriate_for_frame_number # type: int if frames_difference == 0: interpolation = 0.0 # type: float else: interpolation = frames_since_keyframe / float( frames_difference) # type: float else: next_keyframe_key = cls._get_next_keyframe_key_after( preceeding_keyframe_key_appropriate_for_frame_number, channel_timeline_keyframes) # type: int frames_difference = next_keyframe_key - preceeding_keyframe_key_appropriate_for_frame_number # type: int interpolation = frames_since_keyframe / float( frames_difference) # type: float keyframe_channel_transform_a = channel_timeline_keyframes[ preceeding_keyframe_key_appropriate_for_frame_number] # type: TransformNode keyframe_channel_transform_b = channel_timeline_keyframes[ next_keyframe_key] # type: TransformNode return TransformNode.lerp(keyframe_channel_transform_a, keyframe_channel_transform_b, interpolation)
def construct_bones_keyframes_transforms_tree_hierarchy_for( cls, bones_keyframes: Dict[str, TransformNode], armature_hierarchy: TreeHierarchy ) -> TreeHierarchy: result_tree_hierarchy = TreeHierarchy() home_transform = TransformNode() for hierarchy_iter in armature_hierarchy.iterate_nodes(): bone_name = hierarchy_iter.key # type: str result_tree_hierarchy.add_node( parent_key=hierarchy_iter.parent_key, node_key=bone_name, node=BoneTransformNode.from_transform_node( bone_name=bone_name, transform_node=bones_keyframes[bone_name], is_keyframe=True) if bone_name in bones_keyframes else BoneTransformNode.from_transform_node( bone_name=bone_name, transform_node=home_transform, is_keyframe=False)) return result_tree_hierarchy
class BoneTransformNode: def __init__(self): self.bone_name = None # type: str self.bone_transform = TransformNode() self.is_keyframe = False def copy(self) -> 'BoneTransformNode': result = BoneTransformNode() result.bone_name = self.bone_name result.bone_transform = self.bone_transform.copy() return result @staticmethod def from_transform_node(bone_name: str, transform_node: TransformNode, is_keyframe: bool = False) -> 'BoneTransformNode': result = BoneTransformNode() result.bone_name = bone_name result.bone_transform = transform_node.copy() result.is_keyframe = is_keyframe return result @staticmethod def from_matrix4x4(bone_name: str, matrix: Matrix4x4, is_keyframe: bool = False) -> 'BoneTransformNode': result = BoneTransformNode() result.bone_name = bone_name result.bone_transform = TransformNode.from_matrix4x4(matrix) result.is_keyframe = is_keyframe return result @classmethod def get_zero_transform_with_bone_name( cls, bone_name: str) -> 'BoneTransformNode': return cls.from_transform_node(bone_name=bone_name, transform_node=TransformNode())
def __init__(self): self.bone_name = None # type: str self.bone_transform = TransformNode() self.is_keyframe = False
def with_bone(self, name: str, transform: TransformNode) -> 'BlenderArmatureBuilder': self.bones_edit_mode_transforms.append((name, transform.copy())) return self
def get_zero_transform_with_bone_name( cls, bone_name: str) -> 'BoneTransformNode': return cls.from_transform_node(bone_name=bone_name, transform_node=TransformNode())
def animate_bone(self, name: str, local_transform: TransformNode, keyframe_number: int) -> 'BlenderArmatureAnimator': self._add_keyframe_if_does_not_exist(keyframe_number) self.bones_keyframes[keyframe_number][name] = local_transform.copy() return self
def _synthetic_transformation_verification_example(self): BlenderSceneManipulator().clear_scene() home_transform = TransformNode() first_keyframe = 0 second_keyframe = 10 chained_armature_chained_bones_transforms = \ ChainedBonesTransformsFactory.get_armature_chained_bones_transforms_for( bones_count=7 ) # type: List[TransformNode] blender_flat_armature_obj, blender_flat_armature_tree_hierarchy = \ BlenderArmatureBuilder() \ .with_armature_name("SYNTHETIC_EXAMPLE_FLAT_ARMATURE") \ .with_bone(name="E1_BONE_TOP", transform=home_transform) \ .with_bone(name="E1_BONE_BOTTOM", transform=chained_armature_chained_bones_transforms[6]) \ .parent_bones(child="E1_BONE_BOTTOM", parent="E1_BONE_TOP") \ .build() blender_parented_armature_obj, blender_parented_armature_tree_hierarchy = \ BlenderArmatureBuilder() \ .with_armature_name("SYNTHETIC_EXAMPLE_PARENTED_ARMATURE") \ .with_bone(name="E2_BONE_TOP", transform=home_transform) \ .with_bone(name="E2_BONE_CHAIN_1", transform=chained_armature_chained_bones_transforms[1]) \ .with_bone(name="E2_BONE_CHAIN_2", transform=chained_armature_chained_bones_transforms[2]) \ .with_bone(name="E2_BONE_CHAIN_3", transform=chained_armature_chained_bones_transforms[3]) \ .with_bone(name="E2_BONE_CHAIN_4", transform=chained_armature_chained_bones_transforms[4]) \ .with_bone(name="E2_BONE_CHAIN_5", transform=chained_armature_chained_bones_transforms[5]) \ .with_bone(name="E2_BONE_BOTTOM", transform=chained_armature_chained_bones_transforms[6]) \ .parent_bones(child="E2_BONE_BOTTOM", parent="E2_BONE_CHAIN_5") \ .parent_bones(child="E2_BONE_CHAIN_5", parent="E2_BONE_CHAIN_4") \ .parent_bones(child="E2_BONE_CHAIN_4", parent="E2_BONE_CHAIN_3") \ .parent_bones(child="E2_BONE_CHAIN_3", parent="E2_BONE_CHAIN_2") \ .parent_bones(child="E2_BONE_CHAIN_2", parent="E2_BONE_CHAIN_1") \ .parent_bones(child="E2_BONE_CHAIN_1", parent="E2_BONE_TOP") \ .build() displaced_chained_bones_transforms_first_keyframe = DisplacedChainedBonesTransformsFactory.get_displaced_transforms_for( bones_count=7, seed=1 ) displaced_chained_bones_transforms_second_keyframe = DisplacedChainedBonesTransformsFactory.get_displaced_transforms_for( bones_count=7, seed=2 ) animated_armature_tree_hierarchies = BlenderArmatureAnimator \ .for_armature(blender_parented_armature_obj, blender_parented_armature_tree_hierarchy) \ .animate_bone(name="E2_BONE_TOP", local_transform=displaced_chained_bones_transforms_first_keyframe[0], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_1", local_transform=displaced_chained_bones_transforms_first_keyframe[1], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_2", local_transform=displaced_chained_bones_transforms_first_keyframe[2], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_3", local_transform=displaced_chained_bones_transforms_first_keyframe[3], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_4", local_transform=displaced_chained_bones_transforms_first_keyframe[4], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_5", local_transform=displaced_chained_bones_transforms_first_keyframe[5], keyframe_number=first_keyframe) \ .animate_bone(name="E2_BONE_BOTTOM", local_transform=displaced_chained_bones_transforms_first_keyframe[6], keyframe_number=first_keyframe) \ \ \ .animate_bone(name="E2_BONE_TOP", local_transform=displaced_chained_bones_transforms_second_keyframe[0], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_1", local_transform=displaced_chained_bones_transforms_second_keyframe[1], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_2", local_transform=displaced_chained_bones_transforms_second_keyframe[2], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_3", local_transform=displaced_chained_bones_transforms_second_keyframe[3], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_4", local_transform=displaced_chained_bones_transforms_second_keyframe[4], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_CHAIN_5", local_transform=displaced_chained_bones_transforms_second_keyframe[5], keyframe_number=second_keyframe) \ .animate_bone(name="E2_BONE_BOTTOM", local_transform=displaced_chained_bones_transforms_second_keyframe[6], keyframe_number=second_keyframe) \ .commit() #bone_animation_local_matrix_a = BlenderArmatureBoneCurrentAnimationDataHelper \ # .get_bone_local_matrix( # armature=blender_parented_armature_obj, bone_name="E2_BONE_BOTTOM", frame_number=first_keyframe) #bone_animation_local_matrix_b = BlenderArmatureBoneCurrentAnimationDataHelper \ # .get_bone_local_matrix( # armature=blender_parented_armature_obj, bone_name="E2_BONE_BOTTOM", frame_number=second_keyframe) calculated_bone_animation_world_matrix_a = BoneMatrixHelper.get_world_matrix_for_bone( "E2_BONE_BOTTOM", animated_armature_tree_hierarchies[0]) # type: Matrix4x4 calculated_bone_animation_world_matrix_b = BoneMatrixHelper.get_world_matrix_for_bone( "E2_BONE_BOTTOM", animated_armature_tree_hierarchies[1]) # type: Matrix4x4 BlenderArmatureAnimator \ .for_armature(blender_flat_armature_obj, blender_flat_armature_tree_hierarchy) \ .animate_bone( name="E1_BONE_BOTTOM", local_transform=TransformNode.from_matrix4x4(calculated_bone_animation_world_matrix_a), keyframe_number=first_keyframe) \ .animate_bone( name="E1_BONE_BOTTOM", local_transform=TransformNode.from_matrix4x4(calculated_bone_animation_world_matrix_b), keyframe_number=second_keyframe) \ .commit()