#!/usr/bin/env python # Author: helour # Copyright: 2013 helour # Based on the cr33dog's script Export Layers as PNG (http://registry.gimp.org/node/18440) # License: GPL v3+ # Version: 0.2 # GIMP plugin to export layers as a animated SWF file # # # For Windows users: # # You need add SWFTools directory (which consist 'png2swf.exe' executable file) # to the GIMP environment PATH variable into the file: # C:\Program Files\GIMP 2\lib\gimp\2.0\environ\default.env # # like in example here: # PATH=${gimp_installation_dir}\bin;C:\Program Files\SWFTools # import os import gtk from subprocess import check_call from tempfile import mkstemp from gimpfu import * def mktmpfile(suffix): fd, filename = mkstemp(suffix=suffix) fptr = os.fdopen(fd) return filename def get_layers_to_export(layers, only_visible, gimp_version): result = [] for layer in layers: if gimp_version >= 2.8 and pdb.gimp_item_is_group(layer): result += get_layers_to_export(layer.children, only_visible, gimp_version) else: if only_visible: if layer.visible: result.append(layer) else: result.append(layer) return result def combine_images_into_swf(img_files, swf_file, framerate): try: check_call(['png2swf'] + ['-o'] + [swf_file] + ['-r'] + [framerate] + img_files) except Exception as e: pdb.gimp_message("Error while executing 'png2swf' command:\n" + str(e) + "\n\nHave you installed the SWFTools package\nand/or\nset the GIMP environment PATH variable?") def export_layers(image, drawable, only_visible, reversed_order, framerate): chooser = gtk.FileChooserDialog(title = None, action = gtk.FILE_CHOOSER_ACTION_SAVE, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) chooser.set_current_folder(os.path.dirname(image.filename)) chooser.set_current_name(os.path.splitext(image.filename)[0] + ".swf") if chooser.run() != gtk.RESPONSE_OK: return filename = chooser.get_filename() chooser.destroy() version = gimp.version[0:2] gimp_version = float(version[0]) + float(version[1]) / 10 duplicated_image = image.duplicate() if reversed_order: pdb.script_fu_reverse_layers(duplicated_image, drawable) layers_to_export = get_layers_to_export(duplicated_image.layers, only_visible, gimp_version) img_files = [] try: for layer in layers_to_export: fullpath = mktmpfile('.png') img_files.append(fullpath) png_filename = os.path.basename(fullpath) pdb.file_png_save(duplicated_image, layer, fullpath, png_filename, 0, 9, 1, 1, 1, 1, 1) combine_images_into_swf(img_files, filename, str(framerate)) finally: for img in img_files: try: os.remove(img) except: pass register( "export-layers-to-swf", #name "Export layers to a SWF file", #description "Export all layers as animation to SWF file", #help "helour", #author "helour", #copyright "2013", #year "Export layers to SWF", #menu label "*", # image format [ #input args. Format (type, name, description, default [, extra]) (PF_IMAGE, "image", "Image", None), (PF_DRAWABLE, "drawable", "Drawable", None), (PF_BOOL, "only_visible", "Only Visible Layers?", False), (PF_BOOL, "reversed_order", "Layers in Reversed Order?", True), (PF_SPINNER, "framerate", "Movie framerate (fps)", 24, (1,100,1)), ], [], #results. Format (type, name, description) export_layers, #callback menu=("/File/Export/"), ) main()