0

I found this page explaining that some of the gimp functions won't return values consistently, so I implemented a do while loop to make sure the functions are returning pairs before using car. Still, I get the error Error: ( : 1) car: argument 1 must be: pair, but I'm not sure how that's possible as it should keep running the function until it returns a pair.

(define (script-fu-scratchpad drawable)
  (let* ((imgHeight 0)
         (imgWidth)
         (bpp)
         (pixel))
    (set! imgHeight (gimp-drawable-height drawable))
    (do ()
        [(pair? imgHeight)]
      (set! imgHeight (gimp-drawable-height drawable)))
    (set! imgHeight (car imgHeight))

    (set! imgWidth (gimp-drawable-width drawable))
    (do ()
        [(pair? imgWidth)]
      (set! imgWidth (gimp-drawable-width drawable)))
    (set! imgWidth (car imgWidth))

    (set! bpp (gimp-drawable-bpp drawable))
    (do ()
        [(pair? bpp)]
      (set! bpp (gimp-drawable-bpp drawable)))
    (set! bpp (car bpp))

    (display bpp) (newline)
    (set! pixel (cons-array bpp 'byte))
    (aset pixel 0 150)
    (aset pixel 1 150)
    (aset pixel 2 150)
    (aset pixel 3 0)

    (gimp-drawable-set-pixel drawable (/ imgHeight 2) (/ imgWidth 2) bpp pixel)
    (gimp-context-set-background '(100 100 100))
    (define county 0)
    (define countx 0)
    (do ()
        [(= countx imgWidth)]
      (do ()
          [(= county imgHeight)]
        (gimp-drawable-set-pixel drawable county countx bpp pixel)
        (set! county (+ county 1)))
      (set! countx (+ countx 1)))))

In response to GoZoner, I edited it and received the following error: Error: (:1) car: argument 1 must be: pair

(define
    (script-fu-scratchpad drawable)
    (let*
        ( 
            (imgHeight 0)
            (imgWidth 0)
            (bpp 0)
            (pixel 0)
        )

        (set! imgHeight (gimp-drawable-height drawable))    
        (set! imgWidth (gimp-drawable-width drawable))

        (set! bpp (gimp-drawable-bpp drawable))
        (do ()
            [(pair? bpp)]
            (set! bpp (gimp-drawable-bpp drawable))
        )
        (set! bpp (car bpp))

        (display bpp) (newline)
        (set! pixel (cons-array bpp 'byte))
        (aset pixel 0 150)
        (aset pixel 1 150)
        (aset pixel 2 150)
        (aset pixel 3 0)

        (gimp-drawable-set-pixel drawable (/ imgHeight 2) (/ imgWidth 2) bpp pixel)
        (gimp-context-set-background '(100 100 100))
        (define county 0)
        (define countx 0)
        (do ()
            [(= countx imgWidth)]
            (do ()
                [(= county imgHeight)]
                (gimp-drawable-set-pixel drawable county countx bpp pixel)
                (set! county (+ county 1))
            )
            (set! countx (+ countx 1))
        )
    )
)
JVE999
  • 3,327
  • 10
  • 54
  • 89
  • While googling I read that `while` was the only looping construct in Script-fu. So what is `do`? – uselpa Apr 21 '14 at 08:00
  • @uselpa The information you found seems to be obsolete. SIOD did not have `do`, but gimp is now using TinyScheme, which is a more complete Scheme implementation. – Terje D. Apr 21 '14 at 08:14
  • this is something I do not like others to do, not even me, but here it goes: if you are trying to learn scheme, or lisp, all right, go ahead. But if you are trying to get things done in GIMP scripts or Plug-ins, maybe you should try to use Python instead. As a higher level and imperative language you will find it much easier to "give orders" to GIMP, without having to worry with such minor language details as how to check a list lenght or how to retrieve a specifc item from a list. – jsbueno Apr 21 '14 at 13:57
  • @jsbueno Having learned my first programming language in the 90's it's easy to agree with you but thats only because Python is a familiar Algol dialect. If you don't know a dialect yet LISP is just as easy to learn as ALGOL. In Python this SO entry would have been `TypeError: 'int' object has no attribute '__getitem__'` or perhaps `IndexError: list index out of range` With a "why" appended to the end. – Sylwester Apr 21 '14 at 18:44
  • I did an intro to Gimp's Python some time ago, but it turned out to be more involved than Script-fu with TinyScheme. Also, for some reason I could not find that Scheme had a `while`, actually, only a `do`. I wasn't aware that TinyScheme has a `do` and Scheme has a `while`. – JVE999 Apr 24 '14 at 07:10

1 Answers1

0

A couple of things.

  1. In your highest level let* you should be initializing each of the variables rather than just imgHeight or none of them. Actual Scheme requires all to be initialized.

  2. Based on name along, I wouldn't expect (gimp-drawable-height drawable) to return a list/cons; it should return a height as a number. Therefore:

    • I can't imagine (pair? imgHeight) would ever be true

    • I would expect (car imgHeight) to fail - and it apparently has based on the error you've reported.

  3. The function aset is presumably acting on a multidimensional ((>= rank 2)) array. Therefore its 'index' argument ought to have more then just a single integer. But, perhaps aset is just simply vector-ref in GIMP's scripting variant.

[EDIT: to be more specific] I've annotated your code

(set! bpp (gimp-drawable-bpp drawable))         ; bpp is NOT a pair
(do ()
    [(pair? bpp)]                               ; bpp is NOT a pair
 (set! bpp (gimp-drawable-bpp drawable)))
(set! bpp (car bpp))                            ; bpp is NOT a pair => ERROR
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • I had tried step 2 already and got an error. I went ahead and tried it out. I'm not sure how'd I'd implement the advice from list number 3. The new error is `Error: (:1) car: argument 1 must be: pair` – JVE999 Apr 24 '14 at 07:08
  • Perhaps `(set! bpp (car bpp))` is failing because `bpp` is a number, not a pair? – GoZoner Apr 24 '14 at 18:47
  • Presumably. I thought the code would make sure it returns a pair, however. – JVE999 Apr 24 '14 at 23:34
  • Your `bpp` is never a pair. Thus `(car bpp)` fails. Look at the documentation: `gint gimp_drawable_bpp (gint32 drawable_ID);` An integer is returned, not a pair, the `car` fails. Please up vote and check-mark this answer. – GoZoner Apr 25 '14 at 14:31