Hi all, I am trying to export out the animation keyframes of some of the 
channel attributes of objects.
While my code does works, however it is not exporting out the whole range 
(I am exporting them based on the time slider values).

import maya.cmds as cmds
import json
# Specify your directory..
file_export_dir = 'C:\Users\abc\Desktop\export_test.txt'

all_data = {}
attributes = ['translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY'
, 'rotateZ']

def get_frame_range():
    # get the frame range in the time slider
    start_frame = int(cmds.playbackOptions(query=True, min=True))
    end_frame = int(cmds.playbackOptions(query=True, max=True))
    return(start_frame, end_frame)

def export_keyframe(sel):
    attr_data = {}
    (start_frame, end_frame) = get_frame_range()

    for time in range(start_frame , (end_frame + 1)):
        #>>> print "Frames iteration : ", time
        cmds.currentTime(time)
        # Get the value in each attribute
        attr_data['frame'] = str(time)
        for attribute in attributes:
            attr_value = cmds.getAttr( sel + '.' + attribute)
            # Assign the value into the dict
            attr_data[attribute] = str(attr_value)
        # Store the data into the main
        all_data[sel] = attr_data
    # Write the overall data into file
    with open(file_export_dir, 'w') as outfile:
        outfile.write(json.dumps(all_data, indent=4, sort_keys=True))
        

def main():
    sels = cmds.ls(sl=True, l=True)
    if not len(sels):
        cmds.warning('Nothing is selected!')
        
        # Create the key and assign an empty value
        all_data[str(sel)] = None
        export_keyframe(sel)

I am using nested dictionaries to store my data, where dict `attr_data` 
will stores the frame number and the values of the channel attributes, then 
this dict will then later be 'added' as a value to the dict `all_data`
In the text file, te results will be displayed as:
{
    "|locator2": {
        "frame": "5", 
        "rotateX": "2.0", 
        "rotateY": "2.0", 
        "rotateZ": "2.0", 
        "translateX": "2.5", 
        "translateY": "2.5", 
        "translateZ": "2.5"
    }
}

In this text file of mine, as I mentioned earlier that it works, it is only 
exporting out the values of the last frame as determined in the time 
slider. If I replaced `attr_data` from dict to list, I will then be able to 
get all the attribute, names and frames exported correctly however, it will 
be in the form of '<attribute name> <attribute value> <frame number> \n'

I used dictionary thinking that it will be easier for me to get the values 
easily and if there is a need for me to debug, it will be easier to see too 
(the list method I had, simply prints all out with no indentation)

How can I improve this code of mine?

To test the code, simply create a locator, set the time slider to '1' to 
'5', add in some keyframes in the attributes - namely the translate and 
rotate attributes..

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/4d49de7f-054c-497a-91e7-0179402f5045%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to