Gimp-Forum.net
Scheme help (unbound error) - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Scheme help (unbound error) (/Thread-Scheme-help-unbound-error)



Scheme help (unbound error) - ssallen - 12-21-2017

See below.
I get a g1Layer is unbound error, despite the fact that (non-empty) text is being passed in to the g1Text parameter.
The intent is to create a text layer and then position a comic book style talk bubble underneath it.

Can you place conditionals inside the local variable declaration area in scheme? How do I prevent bubbles/text layer being created if the user passes in a blank string?


Code:
  (script-fu-register
    "script-fu-talk-bubble"                        ;func name
    "Diag Bubble"                                  ;menu label
    "Creates a dialog bubble"              ;description
    "-0-"                             ;author
    "copyright 2017, -0-"        ;copyright notice
    "November 10, 2017"                          ;date created
    ""                     ;image type that the script works on
    SF-IMAGE       "Input image" 0
    SF-DRAWABLE    "Input drawable" 0
    SF-STRING      "g1Text"          "**"   ;a string variable

    SF-FONT        "Font"          "Sans"    ;a font variable
    SF-ADJUSTMENT  "Font size"     '(32 1 1000 1 10 0 1) ;a spin-button
    SF-COLOR       "gText Color"    '(250 104 255)     ;color variable
    SF-COLOR       "mText Color"    '(0 0 0)     ;color variable
    SF-COLOR       "dText Color"    '(208 59 59)     ;color variable

    SF-COLOR      "Bubble color"   '(255 255 255)
    SF-TOGGLE     _"createBubbles"          TRUE
  )
  (script-fu-menu-register "script-fu-talk-bubble" "<Image>/Image/CreateLayers")
  
 (define (script-fu-talk-bubble aimg adraw g1Text inFont inFontSize textColorG textColorM textColorD bubbleColor createBubbles)
    
    (gimp-context-set-foreground textColorG)
    (gimp-context-set-background bubbleColor)
    
    (let*
        (
            (theImageWidth  (car (gimp-drawable-width  adraw) ) )
            (theImage aimg)
            (theImageHeight (car (gimp-drawable-height adraw) ) )
            (theImageX)
            (theImageY)
            (theText)     
            (theTextBubble)        
            (theBuffer) 
            (relLocX 12)
            (space 32)
            (g1Len (string-length g1Text))        

            (set! theImageWidth   (car (gimp-drawable-width  adraw) ) )
            (set! theImageHeight  (car (gimp-drawable-height adraw) ) )    
            
            (if (> g1Len 0)
                (g1Layer
                    (car
                         (gimp-text-layer-new
                          aimg
                          g1Text
                          inFont
                          inFontSize
                          PIXELS
                         )
                     )
                )
            )
                                              
        ) 
    

        (if (= createBubbles TRUE)
        (         
            (gimp-image-add-layer theImage g1Layer 0)
            
            (gimp-text-layer-set-color m1Layer textColorM)
            
            (gimp-layer-set-offsets g1Layer 12 relLocX)
            (set! relLocX (+ (+ 12 inFontSize) space))

            
            (set! theImageWidth   (car (gimp-drawable-width  g1Layer) ) )
            (set! theImageHeight  (car (gimp-drawable-height g1Layer) ) )         
            (set! theImageX   (car (gimp-drawable-offsets  g1Layer) ) )
            (set! theImageY  (cadr (gimp-drawable-offsets g1Layer) ) )         
         

            (set! theTextBubble
                    (car
                          (gimp-image-select-round-rectangle
                          theImage 0 theImageX theImageY theImageWidth theImageHeight 12 12)
                    )            
            )    
        
            (gimp-selection-grow theImage 4)
            (gimp-context-set-foreground bubbleColor)

            (gimp-edit-bucket-fill bubbleLayer 0 0 100 0 0 0 0 )

            (gimp-layer-set-opacity bubbleLayer 90)         
         
        )) 
    )
  )         



RE: Scheme help (unbound error) - Ofnuts - 12-21-2017

Far from a Scheme expert(*), but I would expect to do something like:
Code:
(if (> ((string-length g1Text)  0)
that would entirely side-step the issue.

Also it is usually considered good style to use "guard clauses" to avoid putting the bulk of the code in a nested condition. In other words, instead of doing:
Code:
if (some-condition)
    do a lot of things
else
    complain about some-condition not met
endif
return
Good style would be:
Code:
if (not some-condition)
  complain and return
# from that point everything is fine...

do a lot of things
return

This becomes even more necessary when you have several conditions.

(*) We are in the third millennium, why don't you use Python instead of a 1950-era language?


RE: Scheme help (unbound error) - ssallen - 12-21-2017

(12-21-2017, 09:25 PM)Ofnuts Wrote: Far from a Scheme expert(*), but I would expect to do something like:
Code:
(if (> ((string-length g1Text)  0)
that would entirely side-step the issue.

++ No dice, it seems that Scheme really doesn't want if clauses in the variable declaration section of its Let statement.

(12-21-2017, 09:25 PM)Ofnuts Wrote: Also it is usually considered good style to use "guard clauses" to avoid putting the bulk of the code in a nested condition. In other words, instead of doing:

++ Yeah, I know. I tried but I couldn't find the scheme version of return or exit so I just trudged ahead with that mess.

(12-21-2017, 09:25 PM)Ofnuts Wrote: (*) We are in the third millennium, why don't you use Python instead of a 1950-era language?

++ Haha, believe me I wish I had. When I first started looking at writing a Gimp plugin it appeared Scheme had more tutorials and was better supported so I just went with it. Believe me, I regret the decision.


RE: Scheme help (unbound error) - Ofnuts - 12-21-2017

If you need Python examples: http://sourceforge.net/projects/gimp-tools/files/scripts/