splinecraft
5/30/2017 - 2:23 AM

Nudge keyframes by randomized amounts

Nudge keyframes by randomized amounts

import pymel.core as pm
import random

def nudge_keys(nudge_amt=0.1, default_val_range=1.0, force_default_val_range=False):

    curves = pm.keyframe(q=True, sl=True, name=True)

    for curve in curves:
        key_vals = pm.keyframe(curve, q=True, sl=True, vc=True)
        key_times = pm.keyframe(curve, q=True, sl=True, tc=True)
        
        if not force_default_val_range:
            val_range = abs(max(key_vals)-min(key_vals))
            
            if val_range < 1:
                val_range = default_val_range
        else:
            val_range = default_val_range
        
        for t, v in zip(key_times, key_vals): 
            val = random.uniform(val_range*.01, val_range*nudge_amt)
            pm.keyframe(curve, e=True, vc=val*random.choice([-1, 1]), r=True, time=(t,))