GIMP Layers to SL Animated Texture

From Second Life Wiki
Revision as of 16:40, 5 December 2010 by Pedro Oval (talk | contribs) (→‎Usage: Add note about dealing with frames that are not the same size as the image)
Jump to navigation Jump to search

This is a GIMP script (written in Script-Fu, the Scheme-based scripting language that GIMP uses) that takes the layers of an image representing an animation, and converts them into a texture suitable for use with llSetTextureAnim.

Installation

Copy and paste the script below into a simple text editor of your choice (Notepad is probably the best choice for Windows systems). Save it into your scripts folder with the extension .scm - the suggested name is "layers-to-sl-animation.scm". The scripts folder can be found by going to Preferences > Folders > Scripts. After it's in the Scripts folder, do a refresh by selecting Filters > Script-Fu > Refresh Scripts.

Usage

First, create or load an animation with one layer per frame (the first frame is the bottom layer). An existing GIF animation will work for this purpose, or you can create your own, or import one if you have enough memory. Note that only GIF animations that use the replace mode will work properly. GIF animations which use the combine mode won't work in this version. You can convert animations that use combine layers to the normal replace mode by invoking Filters > Animation > Unoptimize. That filter is useful also in case the frames aren't originally the same size as the image, because the current version of this script doesn't deal with that.

Next, ensure that it has the correct size and number of frames desired. It's preferable to have a plan at this point of what will the final resolution be, and how many horizontal and vertical frames the final image will have. If you are not going to use all the frames in your final image (for example, you have 5 frames and decide to do a 3x2 image), you still need to fill the holes. Create empty layers or duplicate existing ones until completing the final frame count. If you need to rescale the image at this point and the image is indexed, it's better if you convert it to RGB mode first.

Now invoke the script. It is invoked using Script-fu > SecondLife > Frames to texture...

A window will appear, asking the number of horizontal and vertical frames of the final image. As noted above, that number must match the number of frames in your layers, or the script will show an error instead. Click OK and you will get a new image window consisting of the original frames arranged according to the specified values, suitable for use in SL. Now you can save the file and upload it to SL.

The script

<scheme>

Layers to SL Texture Animation, version 1.0.
Written by Pedro Oval, 2010-12-04.
Donated to the public domain.
Thanks to Digital Dharma for the suggestions.

(define (script-fu-layers-to-sl-anim curimg curdrawable xframes yframes)

 (define layerset (gimp-image-get-layers curimg))
 (define numframes (car layerset))
 (define layerset (cadr layerset))
 (define framexsize (car (gimp-image-width curimg)))
 (define frameysize (car (gimp-image-height curimg)))
 (define imgtype (car (gimp-image-base-type curimg)))
 (if (= numframes (* xframes yframes))
   (begin
     (define img (car (gimp-image-new (* framexsize xframes) (* frameysize yframes) imgtype)))
     (gimp-image-undo-group-start img)
     (if (= imgtype INDEXED)
       (begin
         (define palette (gimp-image-get-colormap curimg))
         (gimp-image-set-colormap img (car palette) (cadr palette))))
     (define frame 0)
     (while (< frame numframes)
       (begin
         (define newlayer (car (gimp-layer-new-from-drawable (vector-ref layerset (- numframes frame 1)) img)))
         (gimp-image-add-layer img newlayer frame)
         (define framey (truncate (/ frame xframes)))
         (define framex (- frame (* framey xframes)))
         (gimp-layer-set-offsets newlayer (* framex framexsize) (* framey frameysize))
         (set! frame (+ frame 1))))
     (gimp-image-merge-visible-layers img CLIP-TO-IMAGE)
     (if (not (= imgtype RGB))
       (gimp-image-convert-rgb img))
     (gimp-image-undo-group-end img)
     (gimp-display-new img))
   (gimp-message "Error: Number of layers doesn't match horizontal x vertical frames")))

(script-fu-register "script-fu-layers-to-sl-anim"

                   _"<Image>/Script-Fu/SecondLife/Frames to texture..."
                   _"Convert a set of layers (frames) to a texture suitable for llSetTextureAnim."
                   "Pedro Oval"
                   _"Public Domain"
                   "2010-12-04"
                   "RGB*, GRAY*, INDEXED*"
                   SF-IMAGE        "Image"        0
                   SF-DRAWABLE     "Drawable"     0
                   SF-ADJUSTMENT   _"# of horizontal frames" '(2 1 1024 1 10 0 1)
                   SF-ADJUSTMENT   _"# of vertical frames" '(2 1 1024 1 10 0 1))

</scheme>

Change Log

  • Version 1.0: First public release.

See also User:Pedro Oval/GIMP Layers to SL Animated Texture for the (possibly unstable) current development version and future plans.

Credits/License

Written by Pedro Oval. Donated to the public domain.