Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GIMP script to add margin adn spacing to a tilsheet
(define (script-fu-respace-tiles-addMarginSpacing inImage inLayer tileSize)
(let* (
(margin 1)
(spacing 2)
(duplicatePadding TRUE)
(theWidth (car (gimp-drawable-width inLayer)))
(theHeight (car (gimp-drawable-height inLayer)))
(theMarginX (* 2 margin))
(theMarginY (* 2 margin))
(theSpaceX (* (- (/ theWidth tileSize) 1) spacing))
(theSpaceY (* (- (/ theHeight tileSize) 1) spacing))
(newWidth (+ theWidth theMarginX theSpaceX))
(newHeight (+ theHeight theMarginY theSpaceY))
(theImage (car(gimp-image-new newWidth newHeight RGB)))
(theLayer)
)
(gimp-context-push)
(gimp-image-undo-disable inImage)
(set! theLayer (car (gimp-layer-new theImage newWidth newHeight RGBA-IMAGE "Tiles" 100 NORMAL-MODE)))
(gimp-image-add-layer theImage theLayer 0)
(gimp-image-undo-enable theImage)
(gimp-edit-clear theLayer)
(gimp-display-new theImage)
(letrec (
(loopTiles
(lambda(sx sy tx ty)
(cond
((> (+ tileSize sy) theHeight) #t)
((> (+ tileSize sx) theWidth) (loopTiles 0 (round (+ sy tileSize)) margin (round (+ ty tileSize spacing)) ))
((begin
(gimp-selection-clear inImage)
(gimp-rect-select inImage (round sx) (round sy) tileSize tileSize 0 FALSE 0)
(gimp-edit-copy-visible inImage)
(let (
(newTile (car (gimp-edit-paste theLayer FALSE)))
)
(gimp-layer-set-offsets newTile (round tx) (round ty))
)
(loopTiles (round (+ sx tileSize)) sy (round (+ tx tileSize spacing)) ty)
))
))
)
)
(loopTiles 0 0 margin margin)
)
(gimp-selection-clear inImage)
(gimp-floating-sel-anchor (car (gimp-image-get-floating-sel theImage)))
(gimp-image-undo-enable theImage)
(gimp-displays-flush)
(gimp-image-undo-enable inImage)
(gimp-context-pop)
(if (= duplicatePadding TRUE)
(script-fu-respace-tiles-fillSpaces theImage theLayer tileSize margin spacing 1 TRUE TRUE TRUE TRUE)
)
)
)
(script-fu-register "script-fu-respace-tiles-addMarginSpacing"
_"Add margin and spacing"
_"Add margin and spacing to existing tileset which has no margin and spacing. "
"Premik"
"LGPL"
"2010"
""
SF-IMAGE "Image" 0
SF-DRAWABLE "Drawable" 0
SF-ADJUSTMENT _"TileSize" '(32 2 1600 1 8 0 0)
SF-TOGGLE "Duplicate edge pixels into margin/spacing (Helps alleviate texture filter artifacts)" TRUE
)
(script-fu-menu-register "script-fu-respace-tiles-addMarginSpacing" "<Image>/Filters/TileSet")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (script-fu-respace-tiles-fillSpaces inImage inLayer tileSize margin spacing deep top right bottom left)
(let* (
(theWidth (car (gimp-drawable-width inLayer)))
(theHeight (car (gimp-drawable-height inLayer)))
)
(gimp-context-push)
(gimp-image-undo-group-start inImage)
(letrec (
(copyColumn (lambda(fromX toX)
(gimp-selection-clear inImage)
(gimp-rect-select inImage (round fromX) 0 1 theHeight 0 FALSE 0)
(gimp-edit-copy-visible inImage)
(let (
(newStrip (car (gimp-edit-paste inLayer FALSE)))
)
(gimp-layer-set-offsets newStrip (round toX) (- margin 1))
(gimp-floating-sel-anchor (car (gimp-image-get-floating-sel inImage)))
)
))
(copyRow (lambda(fromY toY)
(gimp-selection-clear inImage)
(gimp-rect-select inImage 0 (round fromY) theWidth 1 0 FALSE 0)
(gimp-edit-copy-visible inImage)
(let (
(newStrip (car (gimp-edit-paste inLayer FALSE)))
)
(gimp-layer-set-offsets newStrip (- margin 1) (round toY))
(gimp-floating-sel-anchor (car (gimp-image-get-floating-sel inImage)))
)
))
(loopCols (lambda(x)
(cond
((> (+ tileSize x) theWidth) #t)
((begin
(if (= left TRUE) (copyColumn (+ x deep -1) (- x 1)))
(if (= right TRUE) (copyColumn (+ x tileSize (- deep)) (+ x tileSize)))
(loopCols (round (+ x tileSize spacing)))
))
))
)
(loopRows (lambda(y)
(cond
((> (+ tileSize y) theHeight) #t)
((begin
(if (= top TRUE) (copyRow (+ y deep -1) (- y 1)))
(if (= bottom TRUE) (copyRow (+ y tileSize (- deep)) (+ y tileSize)))
(loopRows (round (+ y tileSize spacing)))
))
))
)
)
(if (or (= left TRUE) (= right TRUE)) (loopCols margin))
(gimp-displays-flush)
(gimp-selection-clear inImage)
(if (or (= top TRUE) (= bottom TRUE)) (loopRows margin))
(gimp-displays-flush)
(gimp-selection-clear inImage)
)
(gimp-image-undo-group-end inImage)
(gimp-context-pop)
)
)
@PoppyWorks
Copy link

PoppyWorks commented Jul 18, 2018

Excellent! Thanks a lot!

@mhmas
Copy link

mhmas commented Mar 21, 2021

اشكرك

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment