Forum: Inkscape Tips: How to Invoke One Inkscape Extension from Another + Other stuff I learned when writing the ext

F91fc0a43611147f2a559db40bda9083?d=identicon&s=25 Shlomi Fish (Guest)
on 2013-06-06 13:42
(Received via mailing list)
Hi all,

here are some tips you can use from my experience on working on an
inkscape
extension:

1. Calling one extension from another:
--------------------------------------

This is the recipe I used in Python to call one extension from another:

import subprocess

def id_arg(my_id):
    return '--id=' + my_id

with_perspective_applied_text = subprocess.check_output(
        ['python',
            '/usr/share/inkscape/extensions/perspective.py',
            id_arg(main_path_id()),
            id_arg(pers_e.get_path_id()),
            pers_e.calc_out_fn()
        ]
)

main_path_id() and pers_e.get_path_id() are the IDs passed to the
extension as
selections and pers_e.calc_out_fn() contains the path to where the
intermediate SVG was written.

This can be done securely by doing:

temp_dir = tempfile.mkdtemp()

def temp_svg_fn(basename):
    global temp_dir
    return join(temp_dir, basename + '.svg')

2. Output from inkex.Effect to a file.
------------------------------------------

The following class that inherits from inkex.Effect writes to a
temporary file
instead of to STDOUT.

[QUOTE]

class GenericAddPathEffect(inkex.Effect):

    def __init__(self, basename):
        self.basename = basename
        inkex.Effect.__init__(self)

    def effect(self):
        draw_generic_style_path(self.get_path_name(),
self.get_path_id(),
self.current_layer, self.calc_d_s()
        )

    def calc_out_fn(self):
        return temp_svg_fn(self.basename)

    def write_to_temp(self, input_fn):
        self.affect(args=[input_fn], output=False)
        with open(self.calc_out_fn(), 'w') as fh:
            self.document.write(fh)

        return

[/QUOTE]

Customise it to your heart's content. (Note that the passing of
parameters is a
little counter-intuitive now - I'm planning to fix it).

3. Adding a gradient:
---------------------

You can add a gradient by manipulating the <defs section in the file.
See:

[QUOTE]

class StyleEffect(GenericAddPathEffect):
    def effect(self):
        self.xpathSingle(u'svg:defs').insert(0,
                inkex.etree.XML('''<svg:linearGradient
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
id="bk2hp_grad"> <stop style="stop-color:#0000ff;stop-opacity:1"
offset="0"
                    id="b2h_0" />
                <stop style="stop-color:#00deff;stop-opacity:1"
                    offset="0.4"
                    id="b2h_1" />
                <stop style="stop-color:#00deff;stop-opacity:1"
                    offset="0.6"
                    id="b2h_2" />
                <stop style="stop-color:#0000ff;stop-opacity:1"
                    offset="1"
                    id="b2h_3" />
                </svg:linearGradient>''')
                )
        self.xpathSingle(u'svg:defs').insert(1,
                inkex.etree.XML('''<svg:linearGradient
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" id="bk2hp_grad_rot"
gradientTransform="rotate(90)" xlink:href="#bk2hp_grad" />''') )
self.xpathSingle(u'//*[@id="' + main_path_id() + '"]').set( 'style',
simplestyle.formatStyle({ 'stroke': '#888888', 'stroke-width': '1pt',
                    'fill': 'url(#bk2hp_grad_rot)',
                    }
                    ))

[/QUOTE]

==========================

Regards,

  Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Text Editors and IDEs - http://shlom.in/IDEs

Flock aims to be the browser for the social web, but I found it to be
the
completely anti-social browser.

Please reply to list if it's a mailing list post - http://shlom.in/reply
.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.