Skip to content

Instantly share code, notes, and snippets.

@cyaoeu
Created September 13, 2017 17:57
Show Gist options
  • Save cyaoeu/752ea5dbe029b938ba16b26842c4ecc1 to your computer and use it in GitHub Desktop.
Save cyaoeu/752ea5dbe029b938ba16b26842c4ecc1 to your computer and use it in GitHub Desktop.
import bpy
from math import pi, floor
rig = bpy.context.selected_objects[0]
def is_keyframe(ob, frame, data_path, array_index=-1):
if ob is not None and ob.animation_data is not None and ob.animation_data.action is not None:
for fcu in ob.animation_data.action.fcurves:
if fcu.data_path == data_path:
if array_index == -1 or fcu.array_index == array_index:
return frame in (p.co.x for p in fcu.keyframe_points)
return False
def CreatePose0Frame(rig):
bpy.ops.object.select_all(action="DESELECT")
targetrig = rig
bpy.ops.object.select_pattern(pattern=targetrig.name)
bpy.context.scene.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.mode_set(mode='POSE', toggle=False)
bpy.ops.pose.select_all(action="SELECT")
bone = bpy.context.selected_pose_bones[0]
bpy.types.Object.is_keyframe = is_keyframe
if targetrig.is_keyframe(0,bone.path_from_id("location"), array_index=2) == True:
print("keyframe on frame 0 exists - stop")
else:
bpy.data.scenes['Scene'].frame_current = 0
bpy.ops.object.mode_set(mode='POSE', toggle=False)
#bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.transforms_clear()
def SnapCursorToCenterAndRotateRig(rig):
targetrig = rig
bone = bpy.context.selected_pose_bones[0]
bpy.types.Object.is_keyframe = is_keyframe
if targetrig.is_keyframe(0,bone.path_from_id("location"), array_index=2) == True:
print("keyframe on frame 0 exists - stop")
else:
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
ctx = bpy.context.copy()
ctx['area'] = area
ctx['region'] = area.regions[-1]
bpy.ops.view3d.snap_cursor_to_center(ctx)
area.spaces[0].pivot_point = "CURSOR"
bpy.ops.transform.rotate(ctx, value=(pi*90/180), axis=(1,0,0))
bpy.ops.anim.keyframe_insert(type="LocRotScale")
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.data.scenes[0].frame_end = floor(bpy.context.selected_objects[0].animation_data.action.frame_range[1])
def ConstrainBones(rig):
targetrig = rig
bpy.ops.object.select_all(action="DESELECT")
bpy.ops.object.select_pattern(pattern="armature")
bpy.context.scene.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.mode_set(mode='POSE', toggle=False)
bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.transforms_clear()
selbones = bpy.context.selected_pose_bones
ignore = ("CS_thigh","CS_calf", "CS_upperarm","CS_lowerarm","CS_LegPoletarget","CS_ElboRoll","CS_foot_l","CS_foot_r","CS_FootRoolMain","CS_hand_l","CS_hand_r","CS_thumb_all","CS_middle_All","CS_index_all","CS_pinky_All","CS_ring_All")
for bone in selbones:
abort = 0
for thing in ignore:
if bone.name.startswith(thing) == True:
abort = 1
splitname = bone.name.split("_")
if len(splitname) == 4:
if splitname[1] == "ik":
fixedname = splitname[2] + "_" + splitname[3]
abort = 2
else:
fixedname = splitname[1] + "_" + splitname[2] + "_" + splitname[3]
if len(splitname) == 3:
if splitname[1] == "FootMainIK":
fixedname = "foot" + "_" + splitname[2].lower()
abort = 2
else:
fixedname = splitname[1] + "_" + splitname[2]
if len(splitname) == 2:
if splitname[1] == "neck":
fixedname = "neck_01"
else:
fixedname = splitname[1]
if len(splitname) == 1:
fixedname = splitname[0]
if fixedname == "root":
print("root")
else:
if abort == 1:
continue
if abort == 2:
nc = bone.constraints.new(type='CHILD_OF')
nc.influence = 1
nc.target = targetrig
nc.subtarget = fixedname
nc.owner_space = "WORLD"
nc.target_space = "WORLD"
context_copy = bpy.context.copy()
context_copy["constraint"] = bone.constraints["Child Of"]
bpy.context.active_object.data.bones.active = bone.bone
bpy.ops.constraint.childof_set_inverse(context_copy, constraint="Child Of", owner='BONE') # set inverse - resets the root bone to original location
else:
nc = bone.constraints.new(type='COPY_TRANSFORMS')
nc.influence = 1
nc.target = targetrig
nc.subtarget = fixedname
nc.owner_space = "WORLD"
nc.target_space = "WORLD"
def BakeAction():
removeanimbones = []
bpy.ops.nla.bake(frame_start=1,frame_end=bpy.data.scenes[0].frame_end, only_selected=True,visual_keying=True)
for bone in bpy.context.selected_pose_bones:
if bone.name.startswith("CS_LegPoletarget"):
removeanimbones.append(bone)
print("added " + bone.name)
if bone.name.startswith("CS_ElboRoll"):
removeanimbones.append(bone)
print("added " + bone.name)
bpy.ops.pose.select_all(action="DESELECT")
for removebone in removeanimbones:
bpy.data.objects['armature'].data.bones[removebone.name].select = True
bpy.ops.anim.keyframe_clear_v3d()
def RemoveConstraints():
bpy.ops.pose.select_all(action="SELECT")
selbones = bpy.context.selected_pose_bones
for bone in selbones:
for constraint in bone.constraints:
if constraint.type == "CHILD_OF" or constraint.type == "COPY_TRANSFORMS":
bone.constraints.remove(constraint)
CreatePose0Frame(rig)
SnapCursorToCenterAndRotateRig(rig)
ConstrainBones(rig)
BakeAction()
RemoveConstraints()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment