#!/usr/bin/env python3 import inkex import subprocess import shutil import os import sys """ Extension for InkScape 1.X Features - Create SVG preview file and show it in browser. Helps to quickly evaluate line order for cutting processes Author: Mario Voigt / FabLab Chemnitz Mail: mario.voigt@stadtfabrikanten.org Date: 21.04.2021 Last patch: 21.04.2021 License: GNU GPL v3 Used version of Vivus JS library: https://github.com/maxwellito/vivus/releases/tag/v0.4.6 - MIT License Browser config: Firefox via about:config -> privacy.file_unique_origin = false ToDo: - adjust width and height (give options) - embed config buttons inside html to adjust time/type/... (more flexible than clicking from Inkscape) - we should do it the way like vivus instant -> http://maxwellito.github.io/vivus / https://maxwellito.github.io/vivus-instant - the generated SVGs can be downloaded again and include all animations! - calculate the total length of all paths and auto-adjust the speed to have good visibility """ class AnimateOrder (inkex.EffectExtension): def add_arguments(self, pars): pars.add_argument("--tab") pars.add_argument("--time", type=float, default = 5.0, help="Duration (frames)") pars.add_argument("--fps", type=int, default = 60.0, help="Frames per second (fps)") pars.add_argument("--sequence_type", help="Sequence type") pars.add_argument("--reverse", type = inkex.Boolean, default = False, help="Reverse order") pars.add_argument("--browser", help="Select your desired browser (must be installed and must exist in %PATH% variable).") def effect(self): #write current SVG to extensions' directory. Target name must be "drawing.svg" because it is embedded in animate_order.html statically inFile = "drawing.svg" extension_dir = os.path.dirname(os.path.realpath(__file__)) shutil.copy2(self.options.input_file, os.path.join(extension_dir, inFile)) target_html = os.path.join(extension_dir, "animate_order.html") title = "Animate Order - " + self.document.getroot().get("{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}docname") if title is None: title = "Animate Order - Vivus JS" vivus_include = "./vivus-0.4.6/dist/vivus.js" duration = self.options.time * self.options.fps # we guess we have 20 ... 60 fps. depends on performance of the machine frames_per_second = self.options.fps type = self.options.sequence_type reverse = str(self.options.reverse).lower() with open(target_html, "w") as text_file: print( '' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print(f' {title}' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( '
' , file=text_file) print(f' ' , file=text_file) print(f' ' , file=text_file) print( ' ' , file=text_file) print( ' ' , file=text_file) print( '' , file=text_file) #now open firefox args = [self.options.browser, target_html] proc = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if proc.returncode != 0: inkex.utils.debug("%d %s %s" % (proc.returncode, stdout, stderr)) if __name__ == '__main__': AnimateOrder().run()