Author Topic: render frame variable for document path? or Inkscape.svg render frame solutions?  (Read 1516 times)

Lantzi

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
  • Scribus User
Hi,
In Render Frames, is there a variable that gives the document file path?
something like $scribus_docpath$ ?
$scribus_dir$ gives the Temporary directory

I use render frames to render Inkscape svg files with inkscape, so i don't have to worry  for the letterboxes positions or usage of inkscape filters.

I made it so that I can write the svg path in the quellcode input box of the render frame and a python script to open the temporary xml file an executing inkscape with it.
But its an absolute path not relative so if i change folder structure or copy to another PC i have to open the scribus .sla file with notepad++ an do a text replacement with the new file paths.

So wondering if there is a more portable solution or maybe an better solution for it than my one.


I didn't use the inbuild scribus python simply because i didn't know how to.

I am using Windows 10, Scribus 1.5.3. , Inkscape 0.91, Python 2.7.11

the xml file for render frames :
Code: [Select]
<editorsettings description="Inkscape" icon="gnuplot.png">
<executable command='py "C:\PATHtoSCRIPT\scribus.py" %file' />
<imagefile extension=".ps" />
<empty-frame-text>C:\PATHtoSVGFILE\FILENAME.svg</empty-frame-text>
<preamble>inkscape  -f "</preamble>
<postamble>" -P "$scribus_file$.ps" -w $scribus_realwidth_px$ -h $scribus_realheight_px$</postamble>
</editorsettings>


the python file:
Code: [Select]
import os
import sys
import subprocess

print(sys.argv)

# Open a file
fo = open(sys.argv[1], "r+")
filepath = fo.read();
print("SVG file path: ", filepath)
# Close opend file
fo.close()

exitcode = subprocess.call(str(filepath))
print("Inkscape exit code (0 OK): ", exitcode)



Thanks in advance for all suggestions!  :)

Lantzi

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
  • Scribus User
Scribus Script to render Render Frames with svg files with Inkscape
« Reply #1 on: October 21, 2016, 01:52:42 pm »
I looked into Scribus python and made a script to solve this:
Script looks for all Render Frames with .svg Files in the sub directory /svg and renders them with Inkscape to PNG Files with 300dpi and replaces the .svg Render Frames with the png File.
If you change the .svg File content run the script in Scribus again, it will rerender the svg File and upate the png File.

This is now working for my purpose of arranging the sites of picture books.




Testet on Windows 10, Inkscape 0.91
Its important to change the Inkscape path to the correct one
inkscapepath = 'C:\Program Files\Inkscape\inkscape' # Path to Inkscape

The Script:

Code: [Select]
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

try:
    # Please do not use 'from scribus import *' . If you must use a 'from import',
    # Do so _after_ the 'import scribus' and only import the names you need, such
    # as commonly used constants.
    import scribus
except ImportError,err:
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    sys.exit(1)

#########################
# YOUR IMPORTS GO HERE  #
#########################
import os
import subprocess


def main(argv):
    """A scribus Script to render image frame svg files with Inkscsape. Script Location in Scribus programm folder \share\scripts  f.e. : C:\Program Files\Scribus 1.5.3.svn\share\scripts. Testet on Windows 10, Inkscape 0.91, Scribus 1.5.3"""
    ############!!! Change here to desired target dpi and image file
    inkscapepath = 'C:\Program Files\Inkscape\inkscape' # Path to Inkscape
    inkscapecommands = ' -T -C ' #-T Text to path; -C export page

    dpi = '75'
   
    #inkscapetargetcommand = ' -l ' #plainsvg file
    #fe = '.svg2' #fileextension
    #imageformatshortdesc = 'plainsvg'

#inkscapetargetcommand = ' -E ' # eps file
#fe = '.ps' #fileextension
#imageformatshortdesc = 'eps'

    inkscapetargetcommand = ' -e ' # png file 300dpi normally enough for printing and 75dpi for desktop viewing
    fe = '.png' #fileextension
    imageformatshortdesc = 'png'




#############

    sp = ' ' # space between commands
    sl = '/' # directoryslash
    dpi = str(dpi)
    targetdirname = str(imageformatshortdesc + dpi + 'dpi' )

    svgdir = os.path.dirname(scribus.getDocName()) + "/svg/"
    if not os.path.exists(svgdir) :
        scribus.messageBox("!!!", ' Unterordner "svg" fehlt! die SVG Dateien müssen in diesem Ordner gespeichert werden; Subdirectory "svg" missing! Please create a subdirectory "svg" for the svg files')
    targetdirpath = os.path.dirname(scribus.getDocName()) + sl + targetdirname + sl
    if not os.path.exists(targetdirpath) :
        os.makedirs(targetdirpath)
    page = 1
    pagenum = scribus.pageCount()
    Messages = ""
    while (page <= pagenum):
        scribus.gotoPage(page)
        d = scribus.getPageItems()
        page += 1
        for item in d:
if item[1] == 2:
imagename, image_extension = os.path.splitext(os.path.basename(scribus.getImageFile(item[0])))
#Messages += (str(item) + imagename + image_extension) + os.linesep
svgfilepath = os.path.abspath(os.path.normpath(svgdir + imagename + ".svg"))
targetfilepath = os.path.abspath(os.path.normpath(targetdirpath + imagename + fe))
#commandlinecommand
befehl = inkscapepath + sp + '-z -f "' + svgfilepath + '" '
befehl += inkscapetargetcommand + ' "' + targetfilepath + '" '
befehl += inkscapecommands + ' -d ' + dpi
if os.path.isfile(svgdir + imagename + ".svg") == True :
if image_extension == fe or image_extension == ".png" or image_extension == ".ps" or image_extension == ".svg2":
if os.path.isfile(targetdirpath + imagename + fe) == False or os.path.getmtime(
targetdirpath + imagename + fe) < os.path.getmtime(svgdir + imagename + ".svg"):
Messages += os.linesep + "Rendered Changed SVG: " +  item[0] + ' | ' + imagename
Messages += ' | 0=OK: ' + str(subprocess.call(str(befehl)))
scribus.loadImage(targetdirpath + imagename + fe, item[0])
else:
Messages += os.linesep + 'Unchanged : ' + item[0] + ' | ' + imagename
scribus.loadImage(targetdirpath + imagename + fe, item[0]) # needed because issues wenn same image several times used
if image_extension == ".svg":
Messages += os.linesep + "Rendered New SVG : " + item[0] + ' | ' + imagename
Messages += ' | 0=OK: ' + str(subprocess.call(str(befehl)))
scribus.loadImage(targetdirpath + imagename + fe, item[0])
    return Messages
   # pass    # <--- Delete this line

def main_wrapper(argv):
    """The main_wrapper() function disables redrawing, sets a sensible generic
    status bar message, and optionally sets up the progress bar. It then runs
    the main() function. Once everything finishes it cleans up after the main()
    function, making sure everything is sane before the script terminates."""
    try:
        scribus.statusMessage("Running script...")
        scribus.progressReset()
        Messages = main(argv)
       
    finally:
        # Exit neatly even if the script terminated with an exception,
        # so we leave the progress bar and status bar blank and make sure
        # drawing is enabled.
        if scribus.haveDoc():
            scribus.setRedraw(True)
        scribus.statusMessage("")
        scribus.messageBox("Just for Info", Messages)
        scribus.progressReset()
       

# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
    main_wrapper(sys.argv)

 

palmate