#!/usr/bin/env python

#   frame2anim - convert named frames into layered XCF
#   Copyright (C) 2006 Gopal V <gopalv82 -at- yahoo.com>
#
#    This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import math
from gimpfu import *
import sys, os

def frame_to_anim(basedir, format):
	# sane limit for layers seems to be 4096
	newimg = None
	for i in range(0, 4096):
		filename = (basedir + "/" + format) % i
		print filename
		if not os.path.isfile(filename):
			break
		print "processing %s ..." % filename
		frame_img = pdb.gimp_file_load(filename, filename) 
		if not newimg:
			newimg = pdb.gimp_image_new(pdb.gimp_image_width(frame_img), pdb.gimp_image_height(frame_img), 0)
		drawable = pdb.gimp_image_get_active_drawable(frame_img)
		layer = pdb.gimp_layer_new_from_drawable(drawable, newimg)
		pdb.gimp_image_add_layer(newimg, layer, -1)
	disp = gimp.Display(newimg)

register(
    "python_fu_frame2anim",
    "Script to combine multiple frames into layered XCF",
    "Script to combine multiple frames into layered XCF",
    "Gopal V",
    "Gopal V",
    "2006",
    "<Toolbox>/Xtns/Python-Fu/Frame2Anim",
    "RGB*, GRAY*, INDEXED*",
    [
	(PF_FILE, "basedir", "The base directory of the files", ""),
	(PF_STRING, "format", "format string for frame", "frame_.jpg")
    ],
    [],
    frame_to_anim)

main()
