3

I have 100+ SVG files, each containing a few closed paths (with Bezier curves), like this:

example1

I'd like to do the following:

  1. select the first (bottom) path
  2. duplicate it
  3. move the duplicate by an (x, y) amount

example2

  1. subtract the duplicate from the original path.

example3

And save the SVG.

Is there any way I can do this automatically?

Technologies I looked at:

  • Inkscape - as far as I can see, you can only write scripts in Python that manipulate the SVG node hierarchy directly, can't call the built-in path subtract function.
  • Illustrator - I tried to record the steps in an Action, but can't seem to be able to select the bottom path automatically, you can only select objects in an Action by name.
  • Affinity Designer - not scriptable.
  • CorelDRAW - messed up the width / height of the SVG straight away, didn't look further.
  • Scribus - superb scriptability with Python, but can't seem to find the Python function for the subtract path operation (all the other steps work).
  • Clipper library (C++, C# and Delphi) - doesn't support curves, only straight lines + you would need to write the SVG parsing yourself.

I'm looking for a solution using any scriptable vector editing tool, plugin, or programming language + library combo.

  • While Scribus might be very close to be able to do what you want (adding the path operations to the scripter would be rather easy), it really feels like the wrong tool to use... I really hope that Inkscape will get there soon! – a.l.e May 18 '17 at 8:15
  • 1
    @a.l.e - Why do you think so? I only discovered Scribus a few days ago, but as a programmer working with Python every day, I was very impressed with its scripting feature. It seems super powerful. I used Inkscape for all my vector work so far, but it feels great being able to control everything in a Python script. When you say adding the path operations to the scripter would be rather easy - do you mean for the developers of Scribus? Or for a user extending the Scribus source code? Or doing it with the current version in Python somehow? – Attila Tanyi May 18 '17 at 8:52
  • 1
    Scribus is a WYSISWYG tool for creating a layout and produce a PDF to be printed. Of course you can use it because of the neat Python embedding, but your goal is to manipulate SVGs. Concerning extending the scripter: you need (very) basic c++ skills and a bit of code reading... – a.l.e May 18 '17 at 10:02
  • 1
    Sorry, not an answer but a question: You got most steps in Scribus. How do you select the lowest of your three objects please? I experimented and it seems to be the first mentioned in the .sla file, but what handle would you use from the API please? And when there is no hammer around, we can use even a torque-wrench to drive those rusty bolts out... And a.l.e is right too - in theory. Just Scribus these days can do many more things than just prepare print products - we are cooking WhatsApp content in Scribus, it is a very dynamic and well supported tool. – Martin Zaske May 18 '17 at 11:23
  • 1
    @Martin Zaske - I'm not selecting the lowest, but the bottom object (on the Z axis), which is the first path in an SVG (I'm not familiar with the .SLA format you mentioned). (Actually, Scribus automatically groups all the objects in the SVG, so you need to ungroup first. But after that, it's getAllObjects()[0].) – Attila Tanyi May 18 '17 at 13:07
1

You should be able to do this using the Illustrator actions with an odd trick.

This would work assuming the shapes in your files are of a different color as shown in your provided image.

  1. Start recording your action.
  2. Add a rectangle and give it a stroke (You will delete this later)
  3. With your new rectangle still selected click Select -> Next Object Below
  4. Click Select -> Next Object Below several more times
  5. Click Object -> Transform -> Move and set your desired offset
  6. Click the copy button in the move dialog
  7. Click Select -> Same -> Fill & Stroke
  8. Click Subtract in the Pathfinder toolbox
  9. Click Select -> Next Object Above several times
  10. Hit your delete key
  11. Save the document
  12. Stop recording the action

You can then use the batch scripting to run it on all of your files.

  • Or you can just select the object with a script and then call the action. – joojaa May 18 '17 at 7:34
  • This was a way to do it without scripting. I was trying to show how to do it with just actions since getting into scripting can be difficult if you are not familiar with it. – zachzurn May 18 '17 at 16:35
  • I'm shocked to find that this solution... actually works. Quick and dirty. Thank you! – Attila Tanyi May 19 '17 at 19:37
  • Happy to help! Scripting is really the way to go, but for something that you will only use once, might as well do a quick action. – zachzurn May 20 '17 at 2:08
1

In Inkscape, you can also call verbs from commandline.

The only problem is that they don't take parameters (yet) so you can do for instance stuff like

inkscape --select=rect3680 --verb=EditDuplicate --verb=SelectionInset --select=rect3680 --verb=SelectionDiff --verb FileSave --verb FileQuit drawing-2.svg

but the "3. move the duplicate by an (x, y) amount" is actually the hardest part

  • I forgot, the "select the bottommost object" can be something like inkscape --verb=EditSelectNext if you don't know its id – Mc- May 18 '17 at 7:41
-2

Maybe whit this solve the problem: https://two.js.org/

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • don't support diff operation – Anibal Anto May 25 '17 at 3:04
  • svgjs.com is other option (no diff) – Anibal Anto May 25 '17 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.