john
john

Reputation: 418

How do I find the time of a particular keyframe using cmds.selectKey?

I'm trying to find the time of a particularly selected key from the keys' animCurve node ("lastKey" in the code below). I can select my key using:

cmds.selectKey(lastKey, index=(1, 1))

However cmds.selectKey only returns the amount of keys selected. And there doesn't look to be a way in the command to query the key index time.

I've tried using cmds.keyframe, but my key is not on a particular attribute, it's a Set Driven Key.

print cmds.keyframe(lastKey, index=(1,1), query=True)

"None" gets returned.

Upvotes: 1

Views: 4182

Answers (1)

Morten
Morten

Reputation: 497

If you know which object and/or attribute you are working on you can get the time like so:

import maya.cmds as cmds

# Get index value of last key
keyIndex = (cmds.keyframe("myObject.attribute", indexValue=True, q=True))[-1]

# Get time of key index
keyFrame = cmds.keyframe("myObject.attribute", q=True, index=(1, keyIndex))

Does this work? cmds.keyframe() returns a list of time values of the queried keyframes.

A simpler way may be to simple query the keyframes of lastKey and then take the last element in the list, like so

import maya.cmds as cmds
keyFrame = (cmds.keyFrame("myAnimCurve", q=True))[-1]

This is assuming that lastKey only has one animCurve attached to it. Otherwise the list will be a bit more messy.


EDIT FOR DRIVEN KEYS

I just noticed the bit about the Driven Key. If the below is not what you're looking for, could you elaborate your question a bit?

If you have the animCurve, you can get a list of the Driver values like so

driverKeys = cmds.keyframe("myAnimCurve", q=True, floatChange=True)

And similarly, you can then get a list of the Driven values like this

drivenKeys = cmds.keyframe("myAnimCurve", q=True, valueChange=True)

Finally, you can simply take the last element in the list using [-1]

print(driverKeys[-1])  # return time of last driven keyframe
print(drivenKeys[-1])  # return value at last driven keyframe

Note: The order of the list is based on the driver's values.

Upvotes: 2

Related Questions