Skip to content

Instantly share code, notes, and snippets.

@slapin
Created November 9, 2020 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slapin/5fb4cea9e17d6014b2c953029cd75015 to your computer and use it in GitHub Desktop.
Save slapin/5fb4cea9e17d6014b2c953029cd75015 to your computer and use it in GitHub Desktop.
import bpy
scene = bpy.context.scene
# make a list of strips used in the NLA
# strips are found in -
# object.animation_data.nla_tracks[TrackName].strips[stripName]
nla_strips = []
orig_frame_start = scene.frame_start
orig_frame_end = scene.frame_end
hips_name = "Hips"
root_name = "root"
fixup_bones = ["elbow_ik_L", "elbow_ik_R", "wrist_ik_L", "wrist_ik_R", "knee_ik_L", "knee_ik_R", "foot_ik_L", "foot_ik_R"]
for obj in scene.objects:
if obj.type == 'ARMATURE':
if obj.animation_data and obj.animation_data.nla_tracks:
obj.animation_data.action = None
for track in obj.animation_data.nla_tracks:
if track.mute and track.lock:
# track.mute = False
track.lock = False
for strip in track.strips:
print(strip, track.mute, strip.mute, strip.frame_start, strip.frame_end)
obj.animation_data.action = strip.action
scene.frame_start = strip.frame_start
scene.frame_end = strip.frame_end
offsets_hips = []
offsets_root = []
offsets_fixup = []
delta = 1.0/60.0
print("Action %s %d %d" % (obj.animation_data.action.name, scene.frame_start, scene.frame_end))
for f in range(scene.frame_start, scene.frame_end + 1):
scene.frame_set(f)
pbone_hips = obj.pose.bones[hips_name]
pbone_root = obj.pose.bones[root_name]
m_hips = obj.convert_space(pose_bone=pbone_hips,
matrix=pbone_hips.matrix,
from_space='POSE',
to_space='WORLD')
m_root = obj.convert_space(pose_bone=pbone_root,
matrix=pbone_root.matrix,
from_space='POSE',
to_space='WORLD')
offsets_hips.append(m_hips)
offsets_root.append(m_root)
fixups = {}
for e in fixup_bones:
fb = obj.pose.bones[e]
m_fix = obj.convert_space(pose_bone=fb,
matrix=fb.matrix,
from_space='POSE',
to_space='WORLD')
fixups[e] = m_fix
offsets_fixup.append(fixups)
# for f in range(scene.frame_end + 1, scene.frame_start - 1, -1):
for f in range(scene.frame_start, scene.frame_end + 1):
scene.frame_set(f)
fid = f - scene.frame_start
pbone_hips = obj.pose.bones[hips_name]
pbone_root = obj.pose.bones[root_name]
root_m = offsets_root[fid]
hips_m = offsets_hips[fid]
root_m.translation[0] = hips_m.translation[0]
root_m.translation[1] = hips_m.translation[1]
pbone_root.matrix = obj.convert_space(pose_bone=pbone_root,
matrix=root_m,
from_space='WORLD',
to_space='POSE')
pbone_hips.matrix = obj.convert_space(pose_bone=pbone_hips,
matrix=hips_m,
from_space='WORLD',
to_space='POSE')
for e in fixup_bones:
fb = obj.pose.bones[e]
fb.matrix = obj.convert_space(pose_bone=fb,
matrix=offsets_fixup[fid][e],
from_space='WORLD',
to_space='POSE')
fb.keyframe_insert(data_path = 'location', index = -1)
pbone_root.keyframe_insert(data_path = 'location', index = -1)
pbone_hips.keyframe_insert(data_path = 'location', index = -1)
scene.frame_set(1)
# track.mute = True
track.lock = True
obj.animation_data.action = None
scene.frame_start = orig_frame_start
scene.frame_end = orig_frame_end
scene.frame_set(1)
bpy.ops.wm.save_as_mainfile(filepath="tmp.blend")
# bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
# nla_strips.append((strip, strip.mute))
# strip.mute = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment