abey79 / abey79.github.io

Source of my personal website
https://bylr.info
1 stars 0 forks source link

articles/2022/11/10/batch-processing-doit-vpype/ #16

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Batch processing SVGs with DoIt and vpype | bylr.info

doit (a.k.a. PyDoIt) is a fantastic Python-based tool to automate repetitive workflows. It works particularly well alongside vpype to address mundane plotting-related tasks. This article explains in details how to automate an SVG optimisation and conversion workflow.

https://bylr.info/articles/2022/11/10/batch-processing-doit-vpype/

Wyth commented 1 year ago

I feel like this definitely opens up a whole world for things I consistently do every day. The biggest being my main workflow for splitting an svg (which always ends in _opt.svg) into plobs and renaming them. I run a command like this for each layer in the SVG:

axicli FileName_opt.svg  --mode layers --layer 1 --preview --config wyth_axi_config.py -o FileName_Lay1_COL_#hr##min_#####m_####pl_plob.svg

Estimated print time: 2:18:33 (Hours, minutes, seconds)
Length of path to draw: 129.03 m
Pen-up travel distance: 16.80 m
Total movement distance: 145.83 m
This estimate took 21 Seconds

Number of pen lifts: 7605

After the plob is created, I then rename it based on the estimated print time (rounded up to the next minute), total movement distance, and number of pen lifts. FileName_Lay1_COL_2hr19min_14583m_7605pl_plob.svg

As you can tell it's a bit tedious, but I'm neurotic about wanting all the information in the file name. (In the prefix part of the filename it usually also contains page size, pen width, etc as well)

abey79 commented 1 year ago

@Wyth Well that sound doable, though clearly a tad more elaborate that what my article does. Here are some thoughts.

You may implement this as a single task whose action is a Python function. This function would basically do the entire processing for a single source SVG: call vpype (with os.system()) to split into layers, iterate on those files, call axicli (this time with os.popen() to capture the output), parse its output, and rename the file accordingly.

Alternatively, you could split the processing in multiple tasks, but it requires a more advanced use of doit.

The first task would split the source SVG into layers. That's easy enough, except you can't really specify the list of "targets", since they are known only at task execution time. It shouldn't be an issue though.

The second task would then rename each layer based on axicli's output. Again, the action would be a Python function that executes axicli and parse its output. The difficulty here is that you don't know how many sub-tasks to create since that depends on the actual number of layers, which is known only after the first task is executed. Luckily, doit offers a delayed task creation mechanism that would be useful here. The rename task creation should be delayed until after the split task execution, at which time it would know how many layers must be renamed.

An interesting example indeed. Unless you want to include other moving parts, I don't see a real reason to go for the dual-task approach, but I'd be glad to assist with it, if only for academic fame. 😃

Wyth commented 1 year ago

@abey79 Wow, not only do you offer one, but two solutions! I might make an attempt at this in a few days after I recover a bit more from covid brain, but if you'd like the academic fame, then it will definitely be appreciated. 😅

schettino72 commented 1 year ago

That's easy enough, except you can't really specify the list of "targets", since they are known only at task execution time.

https://stackoverflow.com/questions/57045829/dynamic-target-names-in-python-doit

abey79 commented 1 year ago

@schettino72 TIL, thanks 👍