Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with guide position
#4
(11-05-2022, 08:49 AM)teapot Wrote:
(11-05-2022, 12:19 AM)alvk Wrote: Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")

Hello alvk,

There's quite a lot wrong so here's a new version which I think is doing what you wanted:

Code:
(define (script-fu-custom-guide image drawable orientation position in-toggle)

  (let ((image-width (car (gimp-image-width image)))
        (image-height (car (gimp-image-height image)))
        (layer-width (car (gimp-drawable-width drawable)))
        (layer-height (car (gimp-drawable-height drawable)))
        (offx (car (gimp-drawable-offsets drawable)))
        (offy (cadr (gimp-drawable-offsets drawable)))
        (guide-position 0))

    (if (= in-toggle TRUE)
      (set! position (- position)))

    (set! guide-position
      (case orientation
        ((0) (- offx position))                ; guide on left
        ((1) (+ offx layer-width position))    ; guide on right
        ((2) (- offy position))                ; guide on top
        ((3) (+ offy layer-height position)))) ; guide on bottom

    (case orientation
      ((0 1) (if (<= 0 guide-position image-width)
                 (gimp-image-add-vguide image guide-position)))
      ((2 3) (if (<= 0 guide-position image-height)
                 (gimp-image-add-hguide image guide-position))))

    (gimp-displays-flush)))

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"     0
 SF-DRAWABLE    "Input drawable"  0
 SF-OPTION     _"_Orientation"   '("_Left" "_Right" "_Top" "_Bottom")
 SF-ADJUSTMENT _"_Position"       (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?" FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                         "<Image>/Figures")

Thank you! Everything works just fine!

(11-05-2022, 09:20 AM)Ofnuts Wrote: “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?”  ― Brian Kernighan

Not a scheme expert. Code typically too clever to be debuggable (I don't even know how to print a value in Scheme...).  You obviously try to play a clever trick to get the guide position but it it obviously wrong: if you are layer or image-centric, this doesn't just change the sign of the position.

IMHO, the simplest way:
  • If you work on an image, then offsets are 0,0, and W&H are the image dimension
  • If you work on layer, then offset are the layer's offset and W&H are the layers dimension
Then your guides are at
  • offsetX+position (left),
  • offsetY+position (top),
  • offsetX+width-position (right),
  • offsetY+height-position (bottom).
Also, despite not being a Scheme person:
  • I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
  •  (if (= in_toggle TRUE) ...) is better written (if in_toggle ...)
  •  (* -1 position) is better written (- position)
If you have Python support in your Gimp and always put the guides at the same place relative to image or layers, you can try my ofn-guide-presets script (use it as is or steal the code).

Thank you for your reply. I'm using the development version of Gimp, installed from Debian repository. Unfortunately, I have no python support in my version of Gimp. As far as I know, there is no support for python2 in Debian. Strangely, I didn't find python3 support in the development version of Gimp from the repository. Probably, they compiled it without python support?

(11-05-2022, 09:20 AM)Ofnuts Wrote: I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
In Gimp3 on my computer, gimp-drawable-height and others doesn't work without get. Probably they have changed something?
Reply


Messages In This Thread
Troubles with guide position - by alvk - 11-05-2022, 12:19 AM
RE: Troubles with guide position - by teapot - 11-05-2022, 08:49 AM
RE: Troubles with guide position - by alvk - 11-05-2022, 10:00 AM
RE: Troubles with guide position - by Ofnuts - 11-05-2022, 10:41 AM
RE: Troubles with guide position - by alvk - 11-05-2022, 10:57 AM
RE: Troubles with guide position - by Ofnuts - 11-05-2022, 09:20 AM

Forum Jump: