spakin / SimpInkScr

Simple Inkscape Scripting
https://inkscape.org/~pakin/%E2%98%85simple-inkscape-scripting
GNU General Public License v3.0
321 stars 32 forks source link

Clear canvas or create new document (feature request?) #9

Closed jumalakarhu closed 2 years ago

jumalakarhu commented 2 years ago

Is it possible within the script to create a new document or at least to clear canvas? What I would like to achieve is to create similar Seasons greeting cards with unique name of recipient.

spakin commented 2 years ago

I can suggest two answers to this request:

  1. The most recent version of Simple Inkscape Scripting exposes the root of the SVG XML tree in a variable called svg_root. Hence, you can can use inkex and lxml methods to remove all shape objects from the current document:
for c in svg_root.iter():
    if isinstance(c, inkex.ShapeElement) and not isinstance(c, inkex.Layer):
        c.delete()
  1. You can automate greeting-card creation entirely by running the extension right from the command line instead of launching it from the Inkscape GUI. Use os.environ in your script to read the recipient's name from an environment variable:
    cx, cy = width/2, height/2
    text('Merry Christmas,', (cx, cy/2), font_size='24pt',
     font_family="'Zapf Chancery', cursive", text_anchor='middle',
     fill='red')
    try:
    name = os.environ['RECIPIENT']
    except KeyError:
    name = 'Dude'
    more_text(name, (cx, cy/2 + (24 + 2)*0.75*2))
    style(stroke='none')
    regular_polygon(3, (cx, cy + 75), 100, fill='#008000')
    regular_polygon(3, (cx, cy), 100, fill='#008000')
    rect((cx - 25, cy + 125), (cx + 25, cy + 200), fill='#784421')

    Then run Simple Inkscape Scripting using a command like the following:

    env RECIPIENT=jumalakarhu PYTHONPATH=/usr/share/inkscape/extensions:$PYTHONPATH ~/.config/inkscape/extensions/simple_inkscape_scripting/simple_inkscape_scripting.py --py-source=greeting-card.py --output=greeting-card-jumalakarhu.svg greeting-card-template.svg

    in which your script is called greeting-card.py, the output file is called greeting-card-jumalakarhu.svg (which you should change per recipient), and greeting-card-template.svg is an SVG image that contains everything that should stay the same from recipient to recipient (page dimensions, artwork, etc.).

jumalakarhu commented 2 years ago

Fantastic! Thanks