Post Reply 

Script-fu "unbound variable" error (FIXED)

Jul 31, 2011, 06:45 (This post was last modified: Aug 1, 2011 01:55 by Seikatsukan.)
Post: #1
Script-fu "unbound variable" error (FIXED)
EDIT: The problem was fixed by paynekj over at the Gimptalk froums; look at the end of the post for the solution.

I'm adapting some pictures I have to use as a rotating wallpaper, which almost always involves resizing to 1920x1080, copying the background layer onto a new, black-filled one, flattening and saving. Since I have hundreds of images, I figured it was time for me to learn how to write scripts for GIMP. I have a little previous programming knowledge (not in Scheme, though) and with the help of the User Manual, Procedure Browser and scipt-fu console, I came up with this:

Code:
; Create a 1920x1080, black-background image with the original unchanged and centered.

(define
    (script-fu-seikatsukan-wallpaperfy)

    (let*
        (
          (wallImage (aref (cadr (gimp-image-list)) 0))
          (imageName (car (gimp-image-get-filename wallImage)))
          (offsetX (- (/ 1920 2) (quotient (car (gimp-image-width wallImage)) 2)))
          (offsetY (- (/ 1080 2) (quotient (car (gimp-image-height wallImage)) 2)))
          (baseLayer (gimp-image-get-active-drawable wallImage))
          (wallLayer (car (gimp-layer-new wallImage 1920 1080 RGBA-IMAGE "newLayer" 100 NORMAL)))
        )
    )

    (gimp-image-resize
        wallImage
        1920
        1080
        offsetX
        offsetY
    )

    (gimp-image-add-layer
        wallImage
        wallLayer
        -1
    )

    (gimp-context-set-foreground
        '(0 0 0)
    )

    (gimp-edit-bucket-fill-full
        wallLayer
        FG-BUCKET-FILL
        NORMAL-MODE
        100
        0
        FALSE
        TRUE
        SELECT-CRITERION-COMPOSITE
        0
        0
    )

    (gimp-layer-copy
        baseLayer
        TRUE
    )

    (gimp-edit-paste
        wallLayer
        FALSE
    )

    (set! baseLayer
        (gimp-image-flatten
            wallImage
        )
    )

    (gimp-file-save
        RUN-NONINTERACTIVE
        wallImage
        baseLayer
        imageName
        imageName
    )

)

(script-fu-register
    "script-fu-seikatsukan-wallpaperfy"
    "<Image>/Seikatsukan/Wallpaperfy"
    "Create a 1920x1080, black-background image with the original unchanged and centered."
    "Seikatsukan"
    "Seikatsukan"
    "July 2011"
    ""
)

The problem is, I can't get it to actually run from the menu bar nor from the right-click menu; it always gives the same error message: "Execution error for 'Wallpaperfy': Error: eval: unbound variable: wallImage"
I'm running GIMP 2.6.11 on Arch Linux, and tried running that code in the script-fu console with the exception of the variable declarations, using the appropriate values in each case: the script executes perfectly. The problem seems to be that the variables somehow don't get "set".

I've also tried declaring all variables as zero and then using (set! ... ), which nets me an identical error message. If I replace every instance of the "wallImage" variable with the corresponding command for getting the current image ID, the error message states that the unbound variable is now "offsetX".

It's been three hours and I can't solve this puzzle on my own; Looking through the sample scripts in /usr/share/gimp/2.0/scripts didn't help: they seem to declare variables in a similar fashion; Google failed me as well. Any help you can give on this matter will be greatly appreciated. By the way, feel free to use the script for whatever if you can get it to run.

EDIT: As pointed out by paynekj at the Gimptalk forums, the error was that the closing bracket for the (let*should have been at the end of the whole code, just prior to the closing of the (define, and not right after the variable declarations as I had done:

Code:
(define
        ; The script takes no input from the user, receives no variables.
        (script-fu-seikatsukan-wallpaperfy)

        (let*
                (
<snip>


                  ; Create new layer, store its ID in variable.
                  (wallLayer (car (gimp-layer-new wallImage wallWidth wallHeight RGBA-IMAGE "newLayer" 100 NORMAL)))
                )  ;  <=== This closes variable declarations but not the (let* ...)

        ; Resize canvas to wallpaper size.
        (gimp-image-resize wallImage wallWidth wallHeight offsetX offsetY)

<snip>
        ; Remove the "dirty" flag.
        (gimp-image-clean-all wallImage)

        ) ; <=== This is where the parenthesis closing the (let* should be.
)

; Register. The script is automatic, it takes no input from the user.
(script-fu-register
<snip>

The problem was evidently one of not knowing enough about the Scheme programming language. Attached is the complete and working script, which asks interactively for screen width, height and background color and processes the open image accordingly. To install, change its extension to ".scm" and move to your GIMP's script folder. Feel free to use, modify and redistribute the script.


Attached File(s)
.txt  wallpaperize.txt (Size: 2.85 KB / Downloads: 172)

Sic transit gloria mundi
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Want to create the logos using script-fu and PHP dabhadepravin 1 55 Apr 14, 2014 08:29
Last Post: ofnuts
  GIMP batch process script run-time error Paddy Landau 4 115 Apr 8, 2014 22:42
Last Post: ofnuts
  [SOLVED] How to apply filters and input levels with script fu? tahi.laci 2 147 Apr 4, 2014 15:14
Last Post: tahi.laci
  resize and add layer script bulevardi 8 218 Apr 2, 2014 06:01
Last Post: paynekj
  how to use the smudge tool in command line or script ? mygost 26 492 Mar 6, 2014 16:45
Last Post: mygost

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS