Code

made it callable by other extensions. the workaround is sorta ugly, but it should...
[inkscape.git] / share / extensions / g2pngs.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 cedric GEMY, cedric@le-radar.com
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
19 TODO :
20 specify save format, scale
21 save selected
23 RELEASE NOTE
24 - can export layers only as separate pngs 
25 - Default directory = $Home
26 - create the dest dir if doesn't exists
28 '''
29 import inkex
30 import sys, os, tempfile
32 class MyEffect(inkex.Effect):
33     def __init__(self):
34         inkex.Effect.__init__(self)
35         self.OptionParser.add_option("-d", "--directory",
36                         action="store", type="string", 
37                         dest="directory", default=os.path.expanduser("~"),
38                         help="Existing destination directory")
39         self.OptionParser.add_option("-l", "--layers",
40                         action="store", type="inkbool", 
41                         dest="layers", default=False,
42                         help="Save layers with their groups")
43         '''self.OptionParser.add_option("-s", "--scale",
44                         action="store", type="float", 
45                         dest="scale", default=100,
46                         help="Scales the group at the specified value")
47         self.OptionParser.add_option("-f", "--format",
48                         action="store", type="string", 
49                         dest="format", default="png",
50                         help="Save at the specified format [only PNG implemented yet]") 
51         '''
52     def output(self):
53         pass
54     
55     def effect(self):
56         svg_file = self.args[-1]
57         node = inkex.xml.xpath.Evaluate('/svg',self.document)[0]
58         '''docname = node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname').value[:-4]'''
60         #create os temp dir
61         '''tmp_dir = tempfile.mkdtemp()'''
62         directory = self.options.directory
63         """area = '--export-area-canvas'"""
64         pngs = []
65         if self.options.layers:
66             path = "/svg/*[name()='g' or @style][@id]"
67         else:
68             path = "/svg/g/*[name()='g' or @style][@id]"
69         
70         for node in inkex.xml.xpath.Evaluate(path,self.document):
71             id = node.attributes.getNamedItem('id').value
72             name = "%s.png" % id
73             filename = os.path.join(directory, name)
74             command = "inkscape -i \"%s\" -e \"%s\" \"%s\" " % (id, filename, svg_file)
75             f = os.popen(command,'r')
76             f.read()
77             f.close()
78             pngs.append(filename)
80 e = MyEffect()
81 e.affect()