pythongimp

Remembering user inputted settings between GIMP sessions


I'm very new to python and even newer to creating plug-ins for gimp so I've been having issues with trying to save specific values between sessions. More specifically I want to save two folder directories so that the user doesn't have to select them every time they open up gimp.

I've tried a few things like using 'pdb.gimp_set_data()' or using 'json.dump(config, f)' to make a config file. All of these methods have lead me nowhere, though I imagine that's because I'm using them wrong. So I've decided to start fresh and ask for some help.

register(
    'ofn-layer-to-tiles-rc',
    splitDescRC,splitDescRC+whoiam,author,author,year,splitDescRC+'...',
    '*',
    [
        (PF_IMAGE,      'image',        'Input image',  None),
        (PF_DRAWABLE,   'layer',        'Layer',        None),
        (PF_STRING,     'pokeName',     'Pokemon',         ""),
        (PF_OPTION,     'direction',    'Facing direction', Direction.SOUTH,Direction.labels),
        (PF_OPTION,     'shadowsize',    'Size of shadow', ShadowSize.MEDIUMSHADOW,ShadowSize.labels),
        (PF_BOOL,       'loop',         'Loop gif',     True),
        (PF_DIRNAME,       'pngFolder',     'Spritesheet folder',     None),
        (PF_DIRNAME,       'gifFolder',     'Finished gif folder',     None),

    ],
    [],
    layerToTilesRC,
    menu=menu
)

The code above is the register code that lets the user decide many things. I specifically want the 'pngFolder' and 'gifFolder' values to be remembered across sessions however I don't get very far when it comes to changing the values in the code.

If anyone has any suggestions or even a solution that would be greatly appreciated, thank you!


Solution

  • If you define a plugin like this:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import datetime
    from gimpfu import *
    
    ### Plugin body
    def plugin(*args):
        gimp.message('Called with %r' % (args,))
    
    ### Registration
    register(
            "testString","Test plugin parms","Test plugin parms",
            "","","",
            "Test plugin string arg",
            "", # Accepted image type
            [
            (PF_STRING, "string", "String arg", datetime.datetime.now().isoformat()),
            ],
            [],
        plugin,
        menu="<Image>/Test",
    )
    
    main()
    

    You find that the default value for the argument is the time when you first call the plugin after starting Gimp (further calls will re-use the value that was used in the previous plugin execution). This means that the value is set by the code when it runs when the plugin is called for execution, and is not some frozen value from registration time.

    So you can indeed save your values somewhere, and each the script is run, the code loads them from file, so your script would look like:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import datetime
    from gimpfu import *
    
    # load args from file
    previousPngFolder,previousGifFolder=loadSavedValues()
    
    ### Plugin body
    def plugin(...,pngFolder,gifFolder,...):
        saveValues(pngFolder,gifFolder)
        moreCode
    
    ### Registration
    register(
            [
            .... 
            (PF_DIRNAME,'pngFolder','Spritesheet folder',previousPngFolder),
            (PF_DIRNAME,'gifFolder','Finished gif folder',previousGifFolder),
            ....
            ],
            [],
        plugin,
        menu="<Image>/Test",
    )
    
    main()
    

    There are plenty of way and places to store values. For your use the best place would be the presets directory in the user Gimp profile (obtained with os.path.join(gimp.directory,'tool-presets')). Given the simple structure reading/writing the file can be done with plain Python I/O, but using JSON or ".ini" format (using the ConfigParser module) are also possible (some examples in other scripts where you got you initial code).

    PS: the first argument in the registration, aka the "atom", is meant to be unique, which is why authors use their own prefix (ofn- here) to avoid clashes, so the first you should do when you borrow code is to replace the atom by your own...