This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/animate_order/animate_order.py

88 lines
5.0 KiB
Python
Raw Normal View History

2021-04-21 18:02:20 +02:00
#!/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)
- 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")
2021-04-21 19:01:27 +02:00
pars.add_argument("--time", type=float, default = 5.0, help="Duration (frames)")
2021-04-21 18:02:20 +02:00
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( '<html>' , file=text_file)
print( ' <head>' , file=text_file)
print( ' <meta charset="UTF-8">' , file=text_file)
print( ' <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">' , file=text_file)
print( ' <meta name="viewport" content="width=device-width, initial-scale=1.0">' , file=text_file)
print(f' <title>{title}</title>' , file=text_file)
print( ' <meta name="description" content="SVG Drawing Animation">' , file=text_file)
print( ' </head>' , file=text_file)
print( ' <body>' , file=text_file)
print( ' <button onclick="vi.reset().play();">replay</button>' , file=text_file)
print( ' <br/>' , file=text_file)
print(f' <object id="animate_order" type="image/svg+xml" data="{inFile}"></object>' , file=text_file)
print(f' <script src="{vivus_include}"></script>' , file=text_file)
print( ' <script>' , file=text_file)
print( " var vi = new Vivus('animate_order', {type: '" + f'{type}' + "', \
duration:" + f'{duration}' + ", reverseStack:" + f'{reverse}' + "});" , file=text_file)
print( ' </script>' , file=text_file)
print( ' </body>' , file=text_file)
print( '</html>' , 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()