Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python scripts: handling PF_OPTION and PF_RADIO choices by name
#4
(08-15-2018, 10:19 PM)lordcayleth Wrote: I'm rather new to python.

Can you please give me an example how to use this convention with PF_RADIO?

I suspect there is a way to use the existing data structure to give PF_RADIO the pairs it needs the way it wants them, but I don't have a clue how to do it.

Thanks.

For radio button the function to create the symbols and labels is a bit different:

For instance, taking $random_script:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *


from collections import namedtuple

def createButtons(name,pairs):
    optsclass=namedtuple(name+'Type',[symbol for symbol,label in pairs]+['labels'])
    opts=optsclass(*(range(len(pairs))+[[(label,i) for i,(symbol,label) in enumerate(pairs)]]))
    return opts

Directions=createButtons('Directions',[('UP','Up'),('DOWN','Down'),('LEFT','Left'),('RIGHT','Right')])

def celenicorn_auto_mirror(image, drawable, direction = Directions.UP) :
    image.undo_group_start()
    width = image.width
    height = image.height
    newLayer = drawable.copy()
    image.add_layer(newLayer, 0)

    dirParms={
        Directions.UP   : (1, width, height * 2, 0, height, 0, 0),
        Directions.DOWN : (1, width, height * 2, 0, 0, 0, height),
        Directions.LEFT : (0, width * 2, height, width, 0, 0, 0),
        Directions.RIGHT: (0, width * 2, height, 0, 0, width, 0)
        }

    vhflip, sizex, sizey, ofsx, ofsy, movx, movy=dirParms[direction]
    pdb.gimp_image_resize(image, sizex, sizey, ofsx, ofsy)
    pdb.gimp_drawable_transform_flip_simple(newLayer, vhflip, True, 0, False)
    pdb.gimp_layer_set_offsets(newLayer, movx, movy)
    image.flatten()

    image.undo_group_end()
    
register(
        "celenicorn_auto_mirror",
        "Mirror the image in a specific direction.",
        "Mirror the image in a specific direction.",
        "Arlo Horner",
        "Arlo Horner",
        "2018",
        "Cosmic Auto Mirror",
        "*",
        [
            (PF_IMAGE,   'image', 'Input image', None),
            (PF_DRAWABLE,'layer', 'Input layer', None),
            (PF_RADIO, "direction", "Mirror towards which direction: ", 0,Directions.labels)
        ],
        [],
        celenicorn_auto_mirror,
        menu="<Image>/Celenicorn")
main()

Independently of the use of the Directions object, you will note that the registration is a bit different. There are now explicit Image/Drawable parameters. When the filter is called as usual, Gimp automatically fills them with the current image and drawable and the dialog only shows widgets for the 3rd parameter and next. But if you use Filters>Re-run|Re-show (that now work...) these parameters have matching widgets in the dialog.

EDIT: updated initial post with function that creates an object usable with both PF_OPTIONS and PF_RADIO.

Incidentally, PF_RADIO is seldom used because it takes a lot of vertical space.
Reply


Messages In This Thread
RE: Python scripts: handling PF_OPTION choices by name - by Ofnuts - 08-15-2018, 11:17 PM

Forum Jump: