Tech Tip Tuesday: Keying Keyed Channels

When you’re animating it’s easy to end up with way more keys than you actually need, and that can get messy (and wasteful!). One way to deal with this is to only set keys on channels that already have animation on them. This can be especially useful during blocking, when you often want to deal with as few keys as possible.

MEL:

{
string $sel[] = `ls -sl`;
if(size($sel)){
    string $curves[] = `listConnections -s true -d false -type "animCurve" $sel`;
    if(size($curves)){
        setKeyframe $curves;
    }
}
}

Python:

import maya.cmds as mc
sel = mc.ls(sl=True)
if sel:
    curves = mc.listConnections(sel, s=True, d=False, type='animCurve')
    if curves:
        mc.setKeyframe(curves)

UPDATE:

Someone pointed out that this doesn’t work properly when you middle-mouse drag in the timeline to copy keys. I looked into it, and as it turns out that only works when you setKeyframe on the objects and attributes that are keyed, and not on the animCurve nodes themselves. So for that to work, you’d have to do something like this:

MEL:

{
string $sel[] = `ls -sl`;
if(size($sel)){
    string $connections[] = `listConnections -s true -d false -type "animCurve" -connections true $sel`;
    string $plugs[];
    for($i=0;$i < size($connections);$i+=2){
        $plugs[$i/2] = $connections[$i];
    }
    setKeyframe $plugs;
}
}
Share Button
This entry was posted in Maya, MEL, Python, Tech Tip Tuesday. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *