Search code examples
menumaya

maya saving menu info (frame range)


enter image description hereI have a menu that has multiple frame ranges for different objects being animated. I need that menu to save that frame range everytime its closed and reopened. Is it possible to save out data to an external file.


Solution

  • There are many ways to save out data externally. Probably one of the easiest ways is using the json module:

    import os
    import json
    
    
    path = "PATH/TO/YOUR/FILE/data.json"
    
    
    def save_data(frame_range):
        with open(path, "w") as f:
            f.write(json.dumps(frame_range))
    
    
    def load_data():
        if os.path.exists(path):
            with open(path, "r") as f:
                return json.loads(f.read())
    
    
    save_data([1, 100])
    
    stored_range = load_data()
    
    print stored_range
    # Output: [1, 100]
    

    In this case it's dumping a list, but it supports much more (dictionaries, nested data structures)

    Another alternative is saving data with the pickle module:

    import pickle
    
    
    path = "PATH/TO/YOUR/FILE/data.p"
    
    
    def save_data(frame_range):
        with open(path, "w") as f:
            f.write(pickle.dumps(frame_range))
    
    
    save_data([1, 100])
    

    You can also use cpickle to export as a binary format.

    In Maya itself, you can save settings directly to the user's preferences:

    cmds.optionVar(iv=("frameStart", 1))
    cmds.optionVar(iv=("frameEnd", 100))
    

    You can also simply store a json string directly in cmds.optionVar for more complex data structures.