Code

fix bug [ 1685070 ] PathAlongPath - Error message when no paths selected
[inkscape.git] / share / extensions / gimp_xcf.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
19 import inkex
20 import sys, os, tempfile
22 class MyEffect(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25     def output(self):
26         pass
27     def effect(self):
28         svg_file = self.args[-1]
29         node = inkex.xml.xpath.Evaluate('/svg',self.document)[0]
30         docname = node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname').value[:-4]
32         #create os temp dir
33         tmp_dir = tempfile.mkdtemp()
35         area = '--export-area-canvas'
36         pngs = []
37         names = []
38         path = "/svg/*[name()='g' or @style][@id]"
39         for node in inkex.xml.xpath.Evaluate(path,self.document):
40             id = node.attributes.getNamedItem('id').value
41             name = "%s.png" % id
42             filename = os.path.join(tmp_dir, name)
43             command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
44             f = os.popen(command,'r')
45             f.read()
46             f.close()
47             pngs.append(filename)
48             names.append(id)
50         filelist = '"%s"' % '" "'.join(pngs)
51         namelist = '"%s"' % '" "'.join(names)
52         xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
53         script_fu = """
54 (define
55   (png-to-layer img png_filename layer_name)
56   (let*
57     (
58       (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
59       (png_layer (car (gimp-image-get-active-layer png)))
60       (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
61     )
62     (gimp-image-add-layer img xcf_layer -1)
63     (gimp-drawable-set-name xcf_layer layer_name)
64   )
65 )
66 (let*
67   (
68     (img (car (gimp-image-new 200 200 RGB)))
69   )
70   (gimp-image-undo-disable img)
71   (let* ((filelist '(%s))(namelist '(%s)))
72     (while filelist
73       (let* ((filename (car filelist))(layername (car namelist)))
74         (png-to-layer img filename layername)
75       )
76       (set! filelist (cdr filelist))
77       (set! namelist (cdr namelist))
78     )
79   )
80   (gimp-image-resize-to-layers img)
81   (gimp-image-undo-enable img)
82   (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
83 (gimp-quit 0)
84         """ % (filelist, namelist, xcf, xcf)
86         junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
87         f = os.popen('gimp -i -b - > %s' % junk,'w')
88         f.write(script_fu)
89         f.close()
90         
91         x = open(xcf, 'r')
92         sys.stdout.write(x.read())
93         x.close()
95 e = MyEffect()
96 e.affect()