Proper way to delete animation keys with a populated array of unwanted keys.

Proper way to delete animation keys with a populated array of unwanted keys.

Anonymous
Not applicable
5,624 Views
5 Replies
Message 1 of 6

Proper way to delete animation keys with a populated array of unwanted keys.

Anonymous
Not applicable

So naturally (And this confused me for 10 min why it was not working for me) I would just drop all my keys in a loop and delete:

for key in AKeys:
    cmds.cutKey(index=(key, key))

But when deleting a key it automatically indexes the next key into the deleted index, so I was preforming this:

 

DELETE [0]

[key 1 is now in index 0]

DELETE [2]

[key 3 is now in index 2]

etc...

 

So I was skipping every second index in my array. What my question is: How can I delete all these keys in my array that contains an assortment of random key indexes [10][11][12][15][16][22]...? 

 

Can I do something like cmds.cutKey(index=(deleteKeys, len(deleteKeys)) or simmilar?

 

Is the only way to subtract 1 from my loop every time I remove an index?

 

0 Likes
Accepted solutions (2)
5,625 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Accepted solution

Hi,

 

 cutKeys will usually cut out the keys and put them to the clipboard. When you don't want to do this, then it is better to specify the clear parameter.

 

As far as I know in Python it is only possible to delete spans of keys, e.g.

// Deletes key 0 until 2
cmds.cutKey(index=(0,2), option="keys", clear=1)

In mel script it would be possible

// Deletes key 0 and key 2
cutKey -index 0 -index 2 -clear;

When I understood your problem correctly then you only have to delete from the back to the front (this works only when your list of deleting indices are sorted!).

Lets say you have 11 keys and you want to delete 3, 6, 9. Then you simply first delete 9, with it 3 and 6 are not changed, but index 10 will be index 9. After this you delete index 6 and finally 3.

 

 

0 Likes
Message 3 of 6

Anonymous
Not applicable
Accepted solution

Reverse the array and delete it... Gah, I'm an idiot...

I shouldn't even consider myself a programmer for that mistake.

Thanks so much man!!! 

0 Likes
Message 4 of 6

Anonymous
Not applicable

haha, something like this happen to everybody. Please set it to "solved".

0 Likes
Message 5 of 6

RFlannery1
Collaborator
Collaborator

You can actually delete all the keyframes on your list in a single call to "cutKey".  If you are dealing with lots and lots of keyframes, this will be faster.  Here's the trick: the "index" parameter does not accept a list of keys, but it *does* accept a list of key ranges.  So you can simply send it a list of single key ranges.  For example, if your list of keys is [10, 11, 15], you would pass to the "cutKey" command [(10,), (11,), (15,)].

 

This is what is looks like in code:

indexRanges = [(index,) for index in AKeys]
cmds.cutKey(index=indexRanges, option='keys', clear=True)

 

0 Likes
Message 6 of 6

Anonymous
Not applicable

Ahh nice, seems to be the best solution. We have a new king 🙂

 

 

0 Likes