abey79 / vpype

The Swiss-Army-knife command-line tool for plotter vector graphics.
https://vpype.readthedocs.io/
MIT License
685 stars 61 forks source link

Feature: inkscape extension encapsulating vpype #67

Open abey79 opened 3 years ago

haschdl commented 3 years ago

hello @abey79 I found this issue while trying to find a way to run basic vpype commands from Inkspace. It shouldn't be too hard, but if possible, I wish I wouldn't start from scratch. Have you given the idea a try?

abey79 commented 3 years ago

@haschdl no I haven't.

I did recently add an API function wrapper to vpype's core that would come in handy: https://vpype.readthedocs.io/en/stable/api/vpype_cli.html#vpype_cli.execute

The way I think things should be going is as follows:

import vpype as vp
from vpype_cli import execute

doc = vp.Document()
doc.add(....)  # populate document with Inkscape vectors

doc = vp.execute("linesort linemerge linesimplify", doc)  # pipeline should come from UI

for layer_id in doc:
    for line in doc[layer_id]:
        ... # put line back into inkscape

The main complexity is that vpype accepts only polylines, so all curved elements will have to be sampled (ideally with a selectable accuracy). In vpype, this is actually done in svgelements, a project to which I contributed parallelised discretisation functions (these are in the various shapes' npoint() methods, eg QuadraticBezier.npoint().

If you want to give this a shot, I'll happily make myself available if you have questions or need help. If you haven't already, I suggest joining the drawingbots.net's discord server.

haschdl commented 3 years ago

It might be relevant to mention that I only use a limited set of vpype’s features, namely the optimization and layouting features. I do not use the drawing features. Most of the time, I generate my SVG in Processing, and sometimes do clean-ups in Illustrator, in Inkscape or using vpype. Axidraw plot optimization has let me down many times, so I want to incorporate vpype to my workflow.

With that said, l just put together some spaghetti code in which I experimented with a Inkscape extension calling vpype client - I wasn’t aware of the API! it’s basically and xml describing the extension, and an accompanying bash script to call vpype.

Calling the cli worked after a few attempts. It was a simple use case calling linesort, and it worked! The extension framework for Inkscape works so that it passes a path to a temp file to the extension.

With this in mind, do you still think the API via Python should the way to go? It sounds more robust and testable! Could the API simply handle the input as a file, in similar way as the client? I didn’t get why I would need to create a vp.Document

Inkscape runs your script (optionally with an interpreter, more info here: Extension Interpreters), passing it any number of parameters in long GNU style. The final argument is the name of the temporary SVG file your script should read. After processing, the script should return the modified SVG file to Inkscape on STDOUT.
abey79 commented 3 years ago

It might be relevant to mention that I only use a limited set of vpype’s features, namely the optimization and layouting features. I do not use the drawing features

Do you plan to have a detailed GUI or just a text field where the user would type commands? A mix of both would make it easy to run the usual optimisation commands (your use case sounds rather typical) and a text field would enable more specific scenarios.

The extension framework for Inkscape works so that it passes a path to a temp file to the extension. With this in mind, do you still think the API via Python should the way to go? It sounds more robust and testable! Could the API simply handle the input as a file, in similar way as the client? I didn’t get why I would need to create a vp.Document.

I didn't know that Inkscape API worked that way. It sounds rather inefficient to me be it certainly makes our life easier. For the execute() API, you have two choices that are nearly 100% equivalent:

# option A
doc = vp.read_multilayer_svg(temp_file_path, quantization=0.1)
doc = vp.execute("linemerge ...", doc)

# option B
doc = vp.execute(f"read {temp_file_path} linemerge ...")

Full docs for both approaches: https://vpype.readthedocs.io/en/stable/api/vpype.html#vpype.read_multilayer_svg https://vpype.readthedocs.io/en/stable/reference.html#read

haschdl commented 3 years ago

Thanks again. It seems I pasted either outdated or incomplete information regarding Inkscape. There is an API based approach using the inkex module, so I should be good to go for an MVP. The documentation for Inkscape extension is not in good shape but there are plenty of working examples, so I should have enough for an MVP.

I will give another with a basic UI to pick a few vpype commands, create a pipeline and execute. I will share something here once I’m have a working example!

abey79 commented 3 years ago

I will share something here once I’m have a working example!

Nice. I can't recommend enough joining the discord server I mentioned. I know there are plenty of people would will be interested in this.

vmario89 commented 3 years ago

Hey there, i did my best coding last days to implement vpype into InkScape. It's not complete but a lot of things are already working. Happy to get your feedback and maybe some more code :-)

code: https://gitea.fablabchemnitz.de/MarioVoigt/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/vpypetools docs: https://stadtfabrikanten.org/display/IFM/vpypetools and sub-pages

regards, Mario