* Tested in GIMP 2.99.17 *

There comes a time when you’ll want to use a bit of code in multiple plug-ins. You can copy and paste that code, and end up maintaining many copies, or you can create a library of useful functions and maintain one copy.

How do you create a Script-Fu library and include a procedure by reference? We can use the load function, to pop in chunks code like lego blocks. Here’s an example from my local directory of plug-ins.

#!/usr/bin/env gimp-script-fu-interpreter-3.0
(define (PROC-PATH)(if #t "/home/mark/github/script-fu.github.io/procedures/"))
(define (include filename)(string-append (PROC-PATH) filename ".scm"))
(load (include "util"))
(load (include "mask-box-crop"))

(define (script-fu-crop-layer-to-mask img drwbls expand)
  (gimp-image-undo-group-start img)
  (mask-box-crop img drwbls expand)
  (gimp-image-undo-group-end img)
)

(define debug #t)

(script-fu-register-filter "script-fu-crop-layer-to-mask"
  "Crop Layer to Mask"
  "Crops layers to the mask area, with a border" 
  "Mark Sweeney"
  "Under GNU GENERAL PUBLIC LICENSE Version 3"
  "2023"
  "*"
  SF-ONE-OR-MORE-DRAWABLE
  SF-ADJUSTMENT  "safe border (in pixels)" (list 64 0 5000 1 10 0 SF-SPINNER)
)
(script-fu-menu-register "script-fu-crop-layer-to-mask" "<Image>/Layer")

This local plug-in uses a wrapper function named include that uses a bit of Script-Fu to assemble a path in the form of a string that is then passed to the load function.

(load (include “util”)) becomes (load “/home/mark/github/script-fu.github.io/procedures/util.scm”)

The ‘release’ version of this plug-in, that will stand-alone as a single file looks different. It has the loaded functions added as code via some further text processing, they both do the same thing. One is easier to test and maintain, the other is good for sharing as a plug-in.

Here’s my library of functions that can be used this way.

#!/usr/bin/env gimp-script-fu-interpreter-3.0
(define (PROC-PATH)(if #t "/path/to/all/the/procedures/"))
(define (include filename)(string-append (PROC-PATH) filename ".scm"))
(load (include "procedure name"))

<
Previous Post
Themes
>
Next Post
Set the Foreground to White