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


All times are UTC - 5 hours [ DST ]


Switch to mobile style

Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: filter a list of layers for later processing (solved)
PostPosted: Mon Jul 31, 2017 2:50 am  (#1) 
Offline
GimpChat Member

Joined: Jul 31, 2017
Posts: 8
Ok well after spending a whole day on this one simple problem I am now looking for help.... Yes this is a simple problem and although I found many samples, examples, tutorials, this continues to elude me. What exactly....

I have a bunch of layers that were chosen and populated to a list previously I need to do a filtering based on each layer name and remove the ones that do not match.

This is the code that populates the list, that seems to work. The simplest cond for that follows:
  (set! layerList (get-all-real-layers image)))


So now I have layerList and filter-regex, which is a string containing a simple regex. The default regex is ".". I want to remove all layers where the layer name does not match with filter-regex, the regex. Currently my code reads:
  (if (<> (strcmp filter-regex "") 0)
    (gimp-message (string-append "filter-regex = " filter-regex))
    (map (lambda (x) (
      (let* (
          (layerName (car (gimp-layer-get-name x)))
          )
        (gimp-message (string-append "layerName = " layerName))
        (if (re-match filter-regex layerName)
          (set! layerList (delq x layerList))
          ) ;enif re-match
        ) ;;enlet
      )) layerList)
     
    ) ;enif <>


It seems to loop through as an empty list, no error reported. It does print the filter as "." (the default), but no layer names.

Thanks in advance. Yeah I have done a few dozen pyfu scripts, and many years ago I did a few scriptfu ones, but it is like learning all over again.


Last edited by script-dig on Mon Jul 31, 2017 10:10 pm, edited 3 times in total.

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: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 2:54 am  (#2) 
Offline
GimpChat Member

Joined: Jul 31, 2017
Posts: 8
I should add, to answer the obvious questions...
this is a scriptfu.
because this is working on existing scriptfu, if I was writing from scratch I would certainly use pythonfu.


Top
 Post subject: Re: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 3:18 am  (#3) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
The line
(layerName (gimp-layer-get-name x))

gimp-layer-get-name returns a single element list and you need to extract the first element to be able to do the regex:
> (gimp-layer-get-name 2)
("Background")
> (car (gimp-layer-get-name 2))
"Background"
(re-match "." (gimp-layer-get-name 2))
#f
> (re-match "." (car (gimp-layer-get-name 2)))
#t


Kevin


Top
 Post subject: Re: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 3:52 am  (#4) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
I think you haven't got the structure of the "if" block correct:
    (if (<> (strcmp filter-regex "") 0)
       (do this if the result of the test was true)
       (do this if the result of the test was false)
    ) ;enif <>

Because filter-regex isn't null, it just does the first block, which only prints the value of filter-regex.

It would only execute the loop if filter-regex is null.

You could try it this way round:
  (if (<> (strcmp filter-regex "") 0)
    (map (lambda (x) (
      (let* (
          (layerName (car (gimp-layer-get-name x)))
          )
        (gimp-message (string-append "layerName = " layerName))
        (if (re-match filter-regex layerName)
          (set! layerList (delq x layerList))
          ) ;enif re-match
        ) ;;enlet
      )) layerList)
    (gimp-message (string-append "filter-regex  is null"))
   
    ) ;enif <>


Kevin


Top
 Post subject: Re: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 4:27 am  (#5) 
Offline
GimpChat Member

Joined: Jul 31, 2017
Posts: 8
Kevin,

Thanks... um... strcmp will be zero if they equal (filter and "") which logic wants to mean no need to do a a filtering, but if not zero than the filtering should happen.

yeah I tired this, with filter-regex as "." (a dot, match any single character anywhere in the string):
;;filter out non-matching layer names 
(if (<> (strcmp filter-regex "") 0)
  (map (lambda (x) (
    (let* (
        (layerName (car (gimp-layer-get-name x)))
        )
      (gimp-message (string-append "layerName = " layerName))
      (if (re-match filter-regex layerName)
        (gimp-message (string-append "matched " layerName))
        )
      ) ;;enlet
    )) layerList)
  ) ;enif <>
(gimp-message "done with filter")


which results in this, from the console:
Multiple Layer Actions-Warning: layerName = Layer copy

Multiple Layer Actions-Warning: matched Layer copy

Multiple Layer Actions-Warning: Error while executing script-fu-multiple-layer-actions:

Error: ( : 1) illegal function


GIMP-Warning: Plug-In 'Muliple Layer Actions' left image undo in inconsistent state, closing open undo groups.


Notice is never says it finished the code.


Top
 Post subject: Re: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 7:19 am  (#6) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
Here's some code that runs. I think you have an extra pair of parentheses that you didn't need.

Although I'm not sure why you are creating a list of layers that don't match the filter:
(define (script-fu-kp24_zz_test12 image drawable)
  (let* (
          (filter-regex ".png")
          (layerList (vector->list (cadr (gimp-image-get-layers image))))
        )
  (if (<> (strcmp filter-regex "") 0)
    (map
      (lambda (x)
        (let* (
                (layerName (car (gimp-layer-get-name x)))
              )
              (gimp-message (string-append "layerName = " layerName))
              (if (re-match filter-regex layerName)
                (begin
                  (gimp-message (string-append "matched " layerName))
                  (set! layerList (delq x layerList))
                )
              ) ;enif re-match
        ) ;enlet
      ) layerList)
    (gimp-message (string-append "filter-regex is null"))
  ) ;enif <>
  ) 
)


Top
 Post subject: Re: filter a list of layers for later processing (beginner question)
PostPosted: Mon Jul 31, 2017 8:24 pm  (#7) 
Offline
GimpChat Member

Joined: Jul 31, 2017
Posts: 8
paynekj,

thank you so much for your efforts.

yeah, that is true (regarding the removing matches), that is reversed logic. I wasn't there yet so didn't notice. :)

Cheers


Top
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Apply filter to all layers

6

No new posts Attachment(s) How To Run A GMIC Filter On Hundreds Or Thousands Of Layers

61

No new posts Attachment(s) Trying to open as layers 3 til files Red, Green, Blue, Filter images

2

No new posts How to find a list of enums?

5

No new posts Attachment(s) cli345's cartoon filter as a native GEGL Filter.

10



* Login  



Powered by phpBB3 © phpBB Group