diff --git a/source/move.py b/source/move.py index 969ada2..79c72bd 100644 --- a/source/move.py +++ b/source/move.py @@ -1,19 +1,51 @@ import bpy from random import random from mathutils import Matrix, Quaternion +from typing import Tuple class ArmGraspController: - def __init__(self, armature_name): - self.arm = bpy.data.objects[armature_name] + """Class to help with controlling a single arm in blender. + """ + def __init__(self, armature_name: str, wrist_object_name: str, + start_pos: Tuple[float] = (1.6766993999481201, 0.3146645724773407, -1.3483989238739014)): + """Constructor for ArmGraspController + + Args: + armature_name (str): The exact name of the blender armature to control. + wrist_object_name (str): The exact name of the blender object which is used to control the arm's IK solutions. + start_pos (Tuple[float]): 3D wrist position to start in. + """ + self.armature_name = armature_name + self.arm = bpy.data.objects[self.armature_name] self.open = False self.finger_grab_bones = ["index_3", "middle_3", "ring_3", "pinky_3"] - self.thumb_rot_bone = "thumb_3" + self.thumb_rot_name = "thumb_3" + self.upper_arm_name = "upper_arm" + self.upper_arm_bone = self.arm.pose.bones[self.upper_arm_name] + self.wrist_object_name = wrist_object_name + self.wrist_object = bpy.data.objects[self.wrist_object_name] + self.start_pos = start_pos def deselect_all_bones(self): + """Deselects all bones in the armature (the arm). + """ for bone in self.arm.pose.bones: # Deselect all selected bones bone.bone.select = False + def move(self, new_pos: Tuple[float]): + """Moves wrist to new position. + + Args: + new_pos (Tuple[float]): New wrist position. Tuple size: 3 floats. + """ + self.wrist_object.location = new_pos + self.wrist_object.keyframe_insert(data_path='location', index=-1) + + def move_shoulder(self, new_pos: Tuple[float]): + self.upper_arm_bone.location = new_pos + self.upper_arm_bone.keyframe_insert(data_path='location') + def open_fingers(self): self.grab_movement(0.8) @@ -57,33 +89,32 @@ class ArmGraspController: # Now rotate the thumb too self.deselect_all_bones() - bone = self.arm.pose.bones.get(self.thumb_rot_bone).bone + bone = self.arm.pose.bones.get(self.thumb_rot_name).bone if bone: bone.select = True bpy.ops.transform.rotate(value=angle, orient_axis='Z', orient_type='LOCAL') bpy.ops.anim.keyframe_insert(type='Rotation') -ob = bpy.data.objects['Sphere'] + +# Script start frame_number = 0 max_dist = 0.5 #start_location = ob.location.copy() start_location = (1.6766993999481201, 0.3146645724773407, -1.3483989238739014) - -# Script start -controller = ArmGraspController("Armature") +controller = ArmGraspController("Armature", "Sphere") for i in range(12): + bpy.context.scene.frame_set(frame_number) + x = random()*max_dist y = random()*max_dist z = random()*max_dist - - bpy.context.scene.frame_set(frame_number) - ob.location = ( + controller.move(( start_location[0] + x, start_location[1] + y, start_location[2] + z - ) - ob.keyframe_insert(data_path='location', index=-1) + )) + controller.move_fingers() frame_number += 20 \ No newline at end of file