add comments

This commit is contained in:
2020-06-24 12:25:53 +02:00
parent f064f010dc
commit 8a4f690bb2

View File

@@ -1,19 +1,51 @@
import bpy import bpy
from random import random from random import random
from mathutils import Matrix, Quaternion from mathutils import Matrix, Quaternion
from typing import Tuple
class ArmGraspController: class ArmGraspController:
def __init__(self, armature_name): """Class to help with controlling a single arm in blender.
self.arm = bpy.data.objects[armature_name] """
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.open = False
self.finger_grab_bones = ["index_3", "middle_3", "ring_3", "pinky_3"] 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): def deselect_all_bones(self):
"""Deselects all bones in the armature (the arm).
"""
for bone in self.arm.pose.bones: # Deselect all selected bones for bone in self.arm.pose.bones: # Deselect all selected bones
bone.bone.select = False 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): def open_fingers(self):
self.grab_movement(0.8) self.grab_movement(0.8)
@@ -57,33 +89,32 @@ class ArmGraspController:
# Now rotate the thumb too # Now rotate the thumb too
self.deselect_all_bones() 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 if bone: bone.select = True
bpy.ops.transform.rotate(value=angle, orient_axis='Z', orient_type='LOCAL') bpy.ops.transform.rotate(value=angle, orient_axis='Z', orient_type='LOCAL')
bpy.ops.anim.keyframe_insert(type='Rotation') bpy.ops.anim.keyframe_insert(type='Rotation')
ob = bpy.data.objects['Sphere']
# Script start
frame_number = 0 frame_number = 0
max_dist = 0.5 max_dist = 0.5
#start_location = ob.location.copy() #start_location = ob.location.copy()
start_location = (1.6766993999481201, 0.3146645724773407, -1.3483989238739014) start_location = (1.6766993999481201, 0.3146645724773407, -1.3483989238739014)
controller = ArmGraspController("Armature", "Sphere")
# Script start
controller = ArmGraspController("Armature")
for i in range(12): for i in range(12):
bpy.context.scene.frame_set(frame_number)
x = random()*max_dist x = random()*max_dist
y = random()*max_dist y = random()*max_dist
z = random()*max_dist z = random()*max_dist
controller.move((
bpy.context.scene.frame_set(frame_number)
ob.location = (
start_location[0] + x, start_location[0] + x,
start_location[1] + y, start_location[1] + y,
start_location[2] + z start_location[2] + z
) ))
ob.keyframe_insert(data_path='location', index=-1)
controller.move_fingers() controller.move_fingers()
frame_number += 20 frame_number += 20