import bpy from random import random ob = bpy.data.objects['Sphere'] frame_number = 0 max_dist = 0.5 #start_location = ob.location.copy() start_location = (1.6766993999481201, 0.3146645724773407, -1.3483989238739014) finger_grab_bones = ["index_3", "middle_3", "ring_3", "pinky_3"] thumb_rot_bone = "thumb_3" def grab_movement(open): # Todo: # select the 4 base finger bones # Then: bpy.ops.transform.rotate(value=0.1, orient_axis='X', orient_type='LOCAL') # To rotate around local axis (will cause to grab) # Then do the same with thumb, but only around local Z axis! bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') # Deselect all objects arm = bpy.data.objects['Armature'] bpy.context.view_layer.objects.active = arm # Make the Armature the active object bpy.ops.object.mode_set(mode='POSE') # Rotate all fingers if open: angle = -0.8 else: angle = 0.8 # Select all fingers for bone in arm.pose.bones: # Deselect all selected bones bone.bone.select = False for name in finger_grab_bones: bone = arm.pose.bones.get(name).bone #if bone: bone.select = True # rotate # TODO: change to posebone X axis instead of world X.l axis = bone.x_axis bone.rotation_euler = (obj.rotation_euler.to_matrix() * Matrix.Rotation(angle, 3, 'X')).to_euler() #bpy.ops.transform.rotate(value=angle, orient_axis=axis, orient_type='LOCAL') bpy.ops.anim.keying_set_active_set(type='Rotation') bpy.ops.anim.keyframe_insert(type='Rotation') # Now rotate the thumb too for bone in arm.pose.bones: # Deselect all selected bones bone.bone.select = False bone = arm.pose.bones.get(thumb_rot_bone).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') # Script start open = False for i in range(50): x = random()*max_dist y = random()*max_dist z = random()*max_dist bpy.context.scene.frame_set(frame_number) ob.location = ( start_location[0] + x, start_location[1] + y, start_location[2] + z ) ob.keyframe_insert(data_path='location', index=-1) grab_movement(open) open = not open frame_number += 20