It is currently Mon Apr 15, 2024 8:15 am


All times are UTC - 5 hours [ DST ]


Switch to mobile style

Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Variable PF_SPINNER
PostPosted: Fri Jul 06, 2018 2:33 pm  (#1) 
Offline
New Member

Joined: Jul 06, 2018
Posts: 4
How in this line
(PF_SPINNER, "size", "Number", 5, (1, 55, 1)),

instead of "55" insert the variable "end"?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Hello World in GIMP Python

from gimpfu import *

def hello_world33(size) :
    end=55
    print()

register(
    "python_fu_hello_world33",
    "1",
    "2",
    "3",
    "4",
    "5",
    "start",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "size", "Number", 5, (1, 55, 1)),
    ],
    [],
    hello_world33, menu="<Image>/File/Create")

main()



Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Fri Jul 06, 2018 3:46 pm  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
You can't because at the call you're only registering hello_world33.
You don't have access to end variable until hello_world33 is executed.

_________________
TinT


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sat Jul 07, 2018 3:52 am  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
If the question is "Can I use variables instead of hard-coded values in the definition of parameters?" the answer is yes. The question is how useful this is., because these are the values at registration time, which occurs quire rarely, when Gimp notices the new/updated script file and reruns its registration. This is not run when the automatic dialog is generated, so you cannot use things like the size of the current image.

Several scripts of mine use a .ini file so that user can define default values(*), and I even have a script where the actual python functions and their parameters are dynamically generated...

(*) with the catch that changing the .ini doesn't change the script file itself, so Gimp doesn't rerun the registration, and you have to take additional steps to insure this.

_________________
Image


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sat Jul 07, 2018 5:02 am  (#4) 
Offline
New Member

Joined: Jul 06, 2018
Posts: 4
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Hello World in GIMP Python

from gimpfu import *

def hello_world33(num) :
    f = open("/home/master/.gimp-2.8/plug-ins/1.txt", 'r')
    img = gimp.Image(1, 1, RGB)
    pdb.gimp_context_push()
    layer =   pdb.gimp_layer_new(img, 2480, 3508, 0, "new", 100, 0)
    img.resize(layer.width, layer.height, 0, 0)
    end=len(open("/home/master/.gimp-2.8/plug-ins/1.txt").readlines())
    background = gimp.Layer(img, "Background", layer.width, layer.height, RGB_IMAGE, 100, NORMAL_MODE)
    background.fill(BACKGROUND_FILL)
    img.add_layer(background, 1)
    gimp.Display(img)
    gimp.displays_flush()
    pdb.gimp_context_pop()                           
    strings = f.readlines()
       
    layer3= pdb.gimp_text_fontname(img, None, 1, 1, strings[int(num)], 0, True, 60, PIXELS, "URW Palladio L Bold")
   
    pdb.gimp_text_layer_resize(layer3, 1140, 600)
    drawable = img.active_drawable
    print()
   

register(
    "python_fu_hello_world33",
    "1",
    "2",
    "3",
    "4",
    "5",
    "start",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "num", "Number strong", 5, (1,55,1)),
    ],
    [],
    hello_world33, menu="<Image>/File/Create")

main()



I have a file "/home/master/.gimp-2.8/plug-ins/1.txt" of 55 lines, it is necessary that the user could not enter a value greater than the number of lines in the file.
The text file can be changed. How to read the number of lines automatically "end", without editing the script? (PF_SPINNER, "num", "Number strong", 5, (1,55,1))


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sat Jul 07, 2018 8:37 am  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
You can just move end outside of the function, and put it in place of 55 in your register call.
Then as long as the 1.txt is updated/saved, when you call the script from GIMP it'll grab the updated end value to use as maxium. I just tested it as well so see that it does indeed grab the updated end value each time script is ran.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Hello World in GIMP Python

from gimpfu import *

#moved outside of function so register call has access to this
end=len(open("/home/master/.gimp-2.8/plug-ins/1.txt").readlines())

def hello_world33(num) :
    f = open("/home/master/.gimp-2.8/plug-ins/1.txt", 'r')
    img = gimp.Image(1, 1, RGB)
    pdb.gimp_context_push()
    layer =   pdb.gimp_layer_new(img, 2480, 3508, 0, "new", 100, 0)
    img.resize(layer.width, layer.height, 0, 0)

    background = gimp.Layer(img, "Background", layer.width, layer.height, RGB_IMAGE, 100, NORMAL_MODE)
    background.fill(BACKGROUND_FILL)
    img.add_layer(background, 1)
    gimp.Display(img)
    gimp.displays_flush()
    pdb.gimp_context_pop()                           
    strings = f.readlines()
       
    layer3= pdb.gimp_text_fontname(img, None, 1, 1, strings[int(num)], 0, True, 60, PIXELS, "URW Palladio L Bold")
   
    pdb.gimp_text_layer_resize(layer3, 1140, 600)
    drawable = img.active_drawable
    print()
   

register(
    "python_fu_hello_world33",
    "1",
    "2",
    "3",
    "4",
    "5",
    "start",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "num", "Number strong", 5, (1,end,1)),
    ],
    [],
    hello_world33, menu="<Image>/File/Create")

main()

_________________
TinT


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sat Jul 07, 2018 9:29 am  (#6) 
Offline
New Member

Joined: Jul 06, 2018
Posts: 4
Thank you very much!


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sat Jul 07, 2018 4:57 pm  (#7) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
Learn something everyday, I thought these value never changed (you still cannot change their type, though), until the script was re-registered.

Given this, you can do a lot better, instead of asking for an index, you can display the lines in a PF_OPTION:

with open("/tmp/file.dat") as f:
    choices=[line.strip() for line in f]

register(
    "python_fu_hello_world33",
    "1",
    "2",
    "3",
    "4",
    "5",
    "start",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_OPTION, "text", "Text", 0,choices),
    ],
    [],
    hello_world33, menu="<Image>/Debug/Value")

main()

The PF_OPTION parameter is an integer index into the "choices" list/array (so you don't need to read the file again).

PS: the "/home/master/.gimp-2.8/" in your code is in the variable "gimp.directory" that you can use to avoid making code that you cannot move to another id.

_________________
Image


Top
 Post subject: Re: Variable PF_SPINNER
PostPosted: Sun Jul 08, 2018 10:50 am  (#8) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
hehe, i didn't actually pay attention to the what the code was doing.
But yeah now that i look at it... ofnuts version would work much nicer.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Hello World in GIMP Python

from gimpfu import *
with open("/home/master/.gimp-2.8/plug-ins/1.txt") as f:
    choices=[line.strip() for line in f]

def hello_world33(option) :
    #don't need to read this file anymore
    #f = open("/home/master/.gimp-2.8/plug-ins/1.txt", 'r')
    img = gimp.Image(1, 1, RGB)
    pdb.gimp_context_push()
    layer =   pdb.gimp_layer_new(img, 2480, 3508, 0, "new", 100, 0)
    img.resize(layer.width, layer.height, 0, 0)

    background = gimp.Layer(img, "Background", layer.width, layer.height, RGB_IMAGE, 100, NORMAL_MODE)
    background.fill(BACKGROUND_FILL)
    img.add_layer(background, 1)
    gimp.Display(img)
    gimp.displays_flush()
    pdb.gimp_context_pop()
    #don't need to read this file anymore                         
    #strings = f.readlines()
       
    #old code
    # layer3 = pdb.gimp_text_fontname(img, None, 1, 1, strings[int(num)], 0, True, 60, PIXELS, "URW Palladio L Bold")
    layer3 = pdb.gimp_text_fontname(img, None, 1, 1, choices[option], 0, True, 60, PIXELS, "URW Palladio L Bold")
    pdb.gimp_text_layer_resize(layer3, 1140, 600)
    drawable = img.active_drawable
    print()
   

register(
    "python_fu_hello_world33",
    "1",
    "2",
    "3",
    "4",
    "5",
    "start",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_OPTION, "text", "Text", 0,choices),
   #(PF_SPINNER, "num", "Number strong", 5, (1,end,1)),
    ],
    [],
    hello_world33, menu="<Image>/File/Create")

main()


It's also nicer in this version because you get to see what line of text you're choosing instead of just selecting the index (where you're kind'a blind unless you have the text file opened).

_________________
TinT


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Unbound variable floating-sel Help!

3

No new posts Error: eval: unbound variable: WHITE-IMAGE-FILL

4



* Login  



Powered by phpBB3 © phpBB Group