Maya Animation Events in a JSON file

I’m looking right know to create a tool to export a json file with with animations events like in unity.
Do you know a base python tool to start or example to start?

I found this tool that it could be as start, but just wondering if you know more about this topic
Thanks in advice!

What do you mean by animation events? The actual exporting to JSON will be ultra easy since python has a built in JSON module and can easily dump and load JSON. A python dict is directly translatable into a JSON object with that module, and most basic data types map 1:1 to JSON data types.

you can populate a dict with data and dump to json like this.


import json


with open('MyFilePath', 'w') as file:
    json.dump(myData, file, indent=4, sort_keys=True)

Thanks passerby for your reply!

I want to set in the timeslider a tag / flag in some of the keyframes and then export the frame number + the string tag / flag in a json file
Do you know what I meant?

Ya I can see what I can make up as a exemple tonight

I usually create an animcurveTU for a given event type and then mark an event for each key in the timeline on that curve.

Thanks passerby I really appreciate it!

I got the thing working, but now I have a little issue.
I’m using the json.dumps to write the file, but It writes a string


json_data = json.dumps( { 'frames' : self.frames, 'comments' : self.comments } )
            print json_data
            json.dump( json_data, file, indent=4, sort_keys=True, separators=(',', ': ') )

and I get this:


"{\"frames\": [63, 5, 98], \"comments\": [\"damage\", \"start\", \"end\"]}"

Do you know why is writting in a wrong format?
As far as I know should be like this:


{
    "frames": [
        63,
        5,
        98
    ],
    "comments": [
        "damage",
        "start",
        "end"
    ]
}

Thanks in advice guys! :slight_smile:

try this,


import json


frames = [
    63,
    5,
    98
]

comments = [
    "damage",
    "start",
    "end"
]

with open('c:/file.json', 'w') as file:
    json.dump({'frames': frames, 'comments': comments}, file, sort_keys=True, indent=4)

the separators you added was messing it up, also json.dumps creates json and returns it to a string. Which you would have to manually write to a file. json.dump creates json from the given object like dumps, but also takes a filestream to write the data to instead of returning as a string. Your not meant to use both of them together, but to choose based on if you want to dump to a file stream like i did, or dump to a string.

it can be read back into python like this.


with open('c:/file.json', 'r') as file:
    jsondata = json.load(file)

print jsondata

Thanks man!
Yes, I realized the problem with the json.dumps. Now it´s working.
I just changed the json.dump({‘frames’: frames, ‘comments’: comments}, file, sort_keys=True, indent=4) and it works like a charm!

Thanks!!