Adam Benson
Adam Benson

Reputation: 41

Get Animation start and end keyframes in Maya Standalone (No User Interface)

I'm trying to get the first and last keyframes of an animation using Maya Standalone. If you're unsure, Maya Standalone means no User Interface. This is all purely command line and Maya is not running; which means that certain things you can normally do in Maya require special work-a-rounds. What I was doing was to use maya.mel.eval('setPlaybackRangeToMinMax'), but surprise, surprise, that doesn't work in Standalone Maya. So, I'm looking for a work around to set the minimum and maximum playback range, based on an existing animation, using pymel.core. So far, I'm not finding any success, and I'm wondering if anyone has any examples of this. Thanks much in advance.

Upvotes: 2

Views: 10013

Answers (2)

Adam Benson
Adam Benson

Reputation: 41

Ok, after a little further testing I was able to get the following to work.

# Assuming that I've already imported or opened an animation file and selected the object
import pymel.core as pmc

keyframes = pmc.keyframe(q=True, time=(-9999999, 9999999))
min_max = sorted(keyframes)
min = min_max[0]
max = min_max[-1]

This has successfully returned the first and last keyframes that I need to do the rest of the work. I'm dealing with vendor MoCap data, so I have to test for extreme time ranges in order to find any and all keyframes, but it works.

Upvotes: 1

Green Cell
Green Cell

Reputation: 4777

You can get the current scene ranges with the following for both pymel or cmds:

import pymel.core as pymel

pymel.playbackOptions(q=True, min=True)
pymel.playbackOptions(q=True, max=True)

import maya.cmds as cmds

cmds.playbackOptions(q=True, min=True)
cmds.playbackOptions(q=True, max=True)

Running it in batch or interface shouldn't matter. Check out the docs for more info.

Edit:

Are you trying to get the range of a given object's first and last keyframe? If that's the case then you need to use cmds.keyframe. You can pass in an object and it will return a list of frames from all keys. From there you can sort it then get the first and last keys, which will be the range you're looking for:

import maya.cmds

obj = "pSphere1"  # Object to check animation range with.
all_keys = sorted(cmds.keyframe(obj, q=True) or [])  # Get all the keys and sort them by order. We use `or []` in-case it has no keys, which will use an empty list instead so it doesn't crash `sort`.
if all_keys:  # Check to see if it at least has one key.
    print all_keys[0], all_keys[-1]  # Print the start and end frames

If you don't want to check all attributes then you can include an attribute to check keys on. So for example, let's say you animated a sphere's translation you can get all of its keys from translateX like this: cmds.keyframe("pSphere1.tx", q=True).

Upvotes: 1

Related Questions