Question

I'm working on my first script-fu and scheme is still not very clear for me.

My script works fine but I want to add an other parameter (onlyvisible) and I have a line causing illegal function error in a certain place but not in an other place.

Thank you for your help :-)

Here is my line:

(display " onlyvisible: ")(display onlyvisible)(newline)

Here is my code:

(define (pitibalrog-test img filename onlyvisible)
(let*
    (
        (imgcopy (car ( gimp-image-duplicate img))) ; Copy to avoid changes on the original image
    )
    (display " onlyvisible: ")(display onlyvisible)(newline)
    (pitibalrog-export-layers imgcopy (gimp-image-get-layers imgcopy) filename onlyvisible) 
)
)


(define (pitibalrog-export-layers img listlayers filename onlyvisible)
(let*
    (
        (nblayers (car listlayers))
        (layers (cadr listlayers))
        (display "EXPORT LAYERS: LAYERS = ")(display layers)(newline)

        (display " onlyvisible: ")(display onlyvisible)(newline) ; <--- HERE IT WORKS
        (index 0)

        (basename (unbreakupstr (butlast (strbreakup filename ".")) "."))
        (extension (car (last (strbreakup filename "."))))
        (layer)
    )

    (display " onlyvisible: ")(display onlyvisible)(newline) ; <--- HERE IS THE PROBLEM


    (while (< index nblayers)
        (set! layer (aref layers index))
        (gimp-item-set-visible layer FALSE)
        (set! index (+ index 1))
    )
    (set! index 0)
    (while (< index nblayers)
        (set! layer (aref layers index))
        (set! filename (string-append basename (car(gimp-drawable-get-name layer)) "." extension))

        (pitibalrog-export-layer img layer filename onlyvisible)

        (set! index (+ index 1))
    )
)
)

(define (pitibalrog-export-layer img layer filename onlyvisible)
    (display " - export layer: ")(display layer)(newline)

    (gimp-item-set-visible layer TRUE)

    ; LAYER GROUP
    (when (= (car(gimp-item-is-group layer)) 1)
        (display "Layer ")(display layer)(display " is a group")(newline)
        (pitibalrog-export-layers img (gimp-item-get-children layer) filename onlyvisible)
    )
    ; REAL LAYER
    (when (= (car(gimp-item-is-group layer)) 0)
        (display "Layer ")(display layer)(display " is not a group")(newline)
        ; (gimp-file-save RUN-NONINTERACTIVE img layer filename filename) ; NO MASK HANDLING!!!
        (gimp-file-save RUN-WITH-LAST-VALS img layer filename filename)
    )

    (gimp-item-set-visible layer FALSE)
)

(script-fu-register "pitibalrog-test"
"<Image>/Script-Fu/Utils/pitibalrog-test..."
"Export all layers of the image in separete files" ;comment
"pitiBalrog" ;author
"pitiBalrog" ;copyright
"November 2012" ;date
"*A"
SF-IMAGE "img" 0
SF-FILENAME "destination" ""
SF-TOGGLE   "Export only visible layers" TRUE
)
Was it helpful?

Solution

Disclaimer: I have never done any work with script-fu, so I have no idea what those script-fu specific procedures do. Scheme, however, I can do.

Please look closely at the syntax required for the let special form:

(let <List of forms that assign values>
   <body>)

I think your main problem comes from the fact that in scheme you are allowed to change the value of almost anything -- there are very few reserved words like other languages. So, when you say (let ((display 3)) <body>), display no longer points to the procedure that displays things to the REPL. Then, in the body of your let* when you say (display " onlyvisible") you're trying to call something as a function that is not a function -- in this case whatever the value of layers is.

In general, all code that needs to do something like display should be in body of the function. For example:

(let ((foo 3)                 ; associate symbol foo with the value 3
      (bar "I'm a string!")   ; associate symbol bar with a string
      (* '(a b c)))           ; associate symbol * with a list '(a b c)
  (display foo)               ;\
  (newline)                   ; \    
  (display bar))              ;  }-- expressions that make up the body
  (newline)                   ;  /
  (display *)                 ; /
  (* 3 4))                    ;/ --- this is the same type of error you made

;;Output
3
I'm a string!
(a b c)
ERROR -- invalid function

Finally, please do not format scheme code as you would C or Java, etc. Here is the schemer-friendly version of your first procedure:

(define (pitibalrog-test img filename onlyvisible)
  (let ((img copy (car (gimp-image-duplicate img))))
    (display " onlyvisible: ")
    (display onlyvisible)
    (newline)
    (pitibalrog-export-layers imgcopy (gimp-image-get-layers imgcopy) filename onlyvisible)))

Well formatted code makes schemers happy and you are more likely to receive speedy help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top