It is currently Thu Jul 04, 2024 4:36 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 16 posts ] 
Author Message
 Post subject: Help from python experts
PostPosted: Sat Mar 19, 2016 9:47 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
GIMP Version: 2.8.16
Operating System: Windows
GIMP Experience: Basic Level

List any ERROR messages you received:
TypeError: object of type 'NoneType' has no len()



I'm trying to create the elementary base for a main python filter which -at a certain point- needs to get an array created by an ancillary python function.
I coded the ancillary function (as an example) as such:
#!/usr/bin/env python
from gimpfu import *

def get_array (nrc):
     
    array = []
       
    for c in range (0, int(nrc)):
        array.append(10*c) 
       
    gimp.message ("executed")
    gimp.message ("first value = "+str(array[0]))
    gimp.message ("last value = "+str(array[c]))
    gimp.message ("number of values = "+str(len(array)))
   
    return(array)

register(
    "get_array",
    "to test creating and returning an array",   
    "This script tests how creating and returning an array",
    "Diego",
    "Diego Nassetti ",
    "March 2016",
    "get_array",
    "",
    [
      (PF_ADJUSTMENT, "howmany", "how many values", 12, (4, 64, 1)),
    ],
    [],
    get_array,
    menu="<Image>/DiegoTest"
    )
main()

If called directly it display all the expected messages:
  getarray Warning
executed

  getarray Warning
first value = 0

  getarray Warning
last value = 110

  getarray Warning
number of values = 12



Then I created (again as an example) a caller as such:
#!/usr/bin/env python
from gimpfu import *
def Test_Call2 (inNr) :
   
    values = [] # values is an empty array (a list) initially
    values = (pdb.python_fu_get_array (inNr))
    c = len(values)
    gimp.message ("last value = "+str(values[c]))
   
    return()

register(
    "Test_Call2",
    "to test calling the Get_Array",   
    "This script tests how calling the Get_Array",
    "Diego",
    "Diego Nassetti ",
    "March 2016",
    "Test Call Get Array",
    "",
    [
      (PF_ADJUSTMENT, "howmany", "how many values", 12, (5, 24, 1)),

    ],
    [],
    Test_Call2,
    menu="<Image>/DiegoTest"
    )
main()


If I launch the caller I get:
- first all the messages listed above issued by the called ancillary function, then the error indicated in the "List any ERROR...", i.e. TypeError: object of type 'NoneType' has no len()

The error in the code of my caller is likely very stupid, but because I'm novice on python I cannot identify it.

Can some expert in Python tell me what is wrong in the caller (or in the return statement of the called)? Thanks a lot !

_________________
"Where am I ?"


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: Help from python experts
PostPosted: Sat Mar 19, 2016 11:37 am  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
hey,
i don't know much about the pdb.python_fu_get_array (inNr) call
but i would try to print that out first and see what it is before calling len() on it.

_________________
TinT


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 11:51 am  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
trandoductin wrote:
hey,
i don't know much about the pdb.python_fu_get_array (inNr) call
but i would try to print that out first and see what it is before calling len() on it.

Tin, I posted the source of python_fu_get_array, and also said that standalone it works perfectly.
The error should be either in the form of the called return (?) or in the form of getting the array from the caller.

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 11:57 am  (#4) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
dinasset wrote:
trandoductin wrote:
hey,
i don't know much about the pdb.python_fu_get_array (inNr) call
but i would try to print that out first and see what it is before calling len() on it.

Tin, I posted the source of python_fu_get_array, and also said that standalone it works perfectly.
The error should be either in the form of the called return (?) or in the form of getting the array from the caller.

my bad, i see the first portion of code now... it looks good to me. maybe gimp does something to the function it's returning type None and it's not iterate-able ... i am trying to debug this right now and see what it is maybe i'll learn something too ;)

_________________
TinT


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 12:24 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks for your tests.
Note that if the code is put alltogether in a same source (having the ancillary function called via only its name without the prefix "pdb.python_fu_), the code executes correctly, that is: the caller receives the array from the called ancillary def

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 12:48 pm  (#6) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
solved...

for your first portion of the code instead of
register(
    "get_array",
    "to test creating and returning an array",   
    "This script tests how creating and returning an array",
    "Diego",
    "Diego Nassetti ",
    "March 2016",
    "get_array",
    "",
    [
      (PF_ADJUSTMENT, "howmany", "how many values", 12, (4, 64, 1)),
    ],
    [],
    get_array,
    menu="<Image>/DiegoTest"
    )


use something like this to specify there's one return value of list or array.
register(
    "get_array",
    "to test creating and returning an array",   
    "This script tests how creating and returning an array",
    "Diego",
    "Diego Nassetti ",
    "March 2016",
    "get_array",
    "",
    [
      (PF_ADJUSTMENT, "howmany", "how many values", 12, (4, 64, 1)),
    ],
    [ (PF_VALUE, "return_value", "returns a list/array")
    ],
    get_array,
    menu="<Image>/DiegoTest"
    )

The below is information for people searching for how to
How to return value(s)/list with Python Fu Gimp register.
the 10th parameter of register call specifies results, if you leave this as empty list, it doesn't matter what your function returns, the return values will be ignored.
You have to specify a tuple (type,name,description) to indicate that you're returning something to the register call.

_________________
TinT


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 1:51 pm  (#7) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Many thanks Tin, I didn't know at all the "definition" of the retuned value.
That problem is solved (no more "None type"), but now the problem is that the returned value is a string of characters: first value is "[", last balue is "]", number of values (len()) is 49.
Could you solve for me also this problem? what should I put instead of "value"? (I just copied your indications)

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 2:00 pm  (#8) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
i.e. how to specify in the called function that it's returning an array of integers?
or how to specify in the caller that it is receiving that string as an array of integers?

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 2:10 pm  (#9) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
probably I solved it: I have to use the "split" python function (and remove the square brackets)

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 2:17 pm  (#10) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
yes, in the caller I had to put:
string = (pdb.python_fu_get_array (inNr))
ls = len(string)
cleanstring = string[1:ls-1]
values = cleanstring.split(",")

very likely there is a much more elegant way to solve the problem, but the importante is to be able of getting my array of values.
Thanks again Tin !

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 2:52 pm  (#11) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
I didn't find out how to pass it as an array...

so in the caller i used this eval to turn it back to a list

values = eval(pdb.python_fu_get_array (inNr))

_________________
TinT


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 3:12 pm  (#12) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thank again, Tin, much more elegant than my verbose technique!

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 4:48 pm  (#13) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
http://wiki.elvanor.net/index.php/GIMP_Scripting
According to above link.
all the PF_ array types are obsolete. And they suggested using a workaround in the caller code anyways.

Obsolete:

#PF_INT8ARRAY = PDB_INT8ARRAY
#PF_INT16ARRAY = PDB_INT16ARRAY
#PF_INT32ARRAY = PDB_INT32ARRAY
#PF_INTARRAY = PF_INT32ARRAY
#PF_FLOATARRAY = PDB_FLOATARRAY
#PF_STRINGARRAY = PDB_STRINGARRAY
#PF_SELECTION = PDB_SELECTION
#PF_BOUNDARY = PDB_BOUNDARY
#PF_PATH = PDB_PATH
#PF_STATUS = PDB_STATUS

Note that you cannot currently pass an array to a Python Gimp script, since PF_STRINGARRAY has been removed (see this bug for reference). One workaround is to pass a string with special separators (like slashes), and split it into an array inside the Python code.

_________________
TinT


Top
 Post subject: Re: Help from python experts
PostPosted: Sat Mar 19, 2016 4:50 pm  (#14) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks for the info, in fact I tried ...ARRAY but wasn't accepted

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sun Mar 20, 2016 4:22 am  (#15) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
you made a good discovery, Tin, so far (also googling here and there) I always thought to "eval()" to a way to execute a statement, like eval(expr) where expr=1+2 and eval returns 3.
This possibility is very interesting, given the lack of arrays as parameters in Gimp PDB.
Thanks.

_________________
"Where am I ?"


Top
 Post subject: Re: Help from python experts
PostPosted: Sun Mar 20, 2016 11:06 am  (#16) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4003
Location: Canada
dinasset wrote:
you made a good discovery, Tin, so far (also googling here and there) I always thought to "eval()" to a way to execute a statement, like eval(expr) where expr=1+2 and eval returns 3.
This possibility is very interesting, given the lack of arrays as parameters in Gimp PDB.
Thanks.


eval() just evaluates whatever expression you give it..and our string for example [1,2,3] is how we express a list in code so that's why it works...str(list) in python returns a string that is identical as the code as if we were to code it so it's all good there.

_________________
TinT


Top
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Convert GIMP plugin from Python 2 to Python 3

4

No new posts Difference between "Python-Fu" and "Python" plugin

6

No new posts Python 3 With GIMP 2

0

No new posts Help with pdb.plug_in_displace when using python-fu in OS X

1

No new posts Attachment(s) Help with pdb.plug_in_displace when using python-fu

4



* Login  



Powered by phpBB3 © phpBB Group