Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justinfx/697f5c7a3453848b44f1ed0f6de3d9bd to your computer and use it in GitHub Desktop.
Save justinfx/697f5c7a3453848b44f1ed0f6de3d9bd to your computer and use it in GitHub Desktop.
back and front key in the middle
import math
import pymel.core as pm
def insertKeyframeBetweenKeys():
try:
objects = pm.ls(sl=1)
attrs = pm.selectionConnection('graphEditor1FromOutliner', q=1, object=1)
for attr in attrs:
buffer = attr.split(".")
buffer = buffer[1]
# Get the current time
currentTime = pm.currentTime(query=True)
# Get the keyframe times of the selected attribute
keyTimes = pm.keyframe(objects, attribute=buffer, query=True, timeChange=True)
# Sort the keyframe times
keyTimes.sort()
# Find the keyframes before and after the current time
prevKeyTime = None
nextKeyTime = None
for keyTime in keyTimes:
if keyTime < currentTime:
prevKeyTime = keyTime
elif keyTime > currentTime:
nextKeyTime = keyTime
break
# Insert a keyframe between the nearest keyframes, if they exist
if prevKeyTime is not None and nextKeyTime is not None:
# Calculate the distance to the nearest keyframes
prevDistance = currentTime - prevKeyTime
nextDistance = nextKeyTime - currentTime
# Insert a keyframe at the midpoint between the nearest keyframes
newKeyTime = (prevKeyTime + nextKeyTime) / 2
pm.setKeyframe(time=newKeyTime, attribute=buffer, insert=True)
# Check if inserting a keyframe before the current time is necessary
if prevKeyTime != currentTime:
newKeyTime = math.ceil(prevKeyTime + prevDistance / 2)
pm.setKeyframe(time=newKeyTime, attribute=buffer, insert=True)
# Check if inserting a keyframe after the current time is necessary
if nextKeyTime != currentTime:
newKeyTime = math.floor(nextKeyTime - nextDistance / 2
pm.setKeyframe(time=newKeyTime, attribute=buffer, insert=True)
elif prevKeyTime is not None and nextKeyTime is None:
# If there is only one keyframe after the current time, insert a keyframe after it
timeDifference = currentTime - prevKeyTime
newKeyTime = prevKeyTime + (timeDifference * 2)
pm.setKeyframe(time=newKeyTime, attribute=buffer)
elif prevKeyTime is None and nextKeyTime is not None:
# If there is only one keyframe before the current time, insert a keyframe before it
timeDifference = nextKeyTime - currentTime
newKeyTime = nextKeyTime - (timeDifference * 2)
pm.setKeyframe(time=newKeyTime, attribute=buffer)
# After inserting all keyframes, snap them
for obj in objects:
for attr in attrs:
pm.snapKey(obj, attribute=attr)
# Adjust keyframes to integers
for attr in attrs:
keyTimes = pm.keyframe(objects, attribute=attr, query=True, timeChange=True)
for keyTime in keyTimes:
pm.cutKey(objects, time=(keyTime,), attribute=attr)
pm.setKeyframe(time=round(keyTime), attribute=attr)
# Snap keyframes for all selected objects
for obj in objects:
pm.snapKey(obj, timeMultiple=1.0)
except Exception as e:
print("Error:", e)
insertKeyframeBetweenKeys()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment