After a long time playing with ideas for an easy Processing to Illustrator tool that would allow fancy coding to be piped directly into Illustrator for further layout (and was vector-based the whole way through – no terrible rasterization allowed), I now have a really easy solution that doesn’t involve any fancy code, external libraries, or command-line nonsense. The process:
1. Write your code as usual, but add PDF export
2. Run once and use File > Place… to insert the PDF into your AI document (do not simply open the document, as changes will not be reflected) — NOTE: be sure “Link” is clicked when placing
3. The resulting PDF, placed into the document as a single unit
4. Make changes in Processing and re-save the pdf – here we reduce the number of lines from 1,000 to 10
5. Back in AI, you will be prompted to update the pdf – easy! No need to ask it, simply moving between programs updates your work
6. Awesome, done.
A dead end for this process was SVG export from Processing, which is not super easy (surprisingly). Here’s my hack that uses Inkscape’s command line tools:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import processing.pdf.*; String filename = "test.pdf"; boolean deletePDF = false; void setup() { size(11*72, 17*72); // pretend we're creating a normal pdf beginRecord(PDF, filename); // do some stuff here! for (int i=0; i<1000; i++) { line(random(0,width), random(0,height), random(0,width), random(0,height)); } endRecord(); // convert to svg // via: http://www.inkscapeforum.com/viewtopic.php?f=5&t=5391 String[] outputFile = split(filename, '.'); runUnixCommand("/Applications/Inkscape.app/Contents/Resources/script --without-gui " + sketchPath + "/" + filename + " --export-plain-svg=" + sketchPath + "/" + outputFile[0] + ".svg", sketchPath); // if specified, delete the original pdf if (deletePDF) { runUnixCommand("rm " + filename + " -f", sketchPath); } // X11 will still be running, so you'll have to quit it by hand... } void runUnixCommand(String commandToRun, String dir) { File workingDir = new File(dir); // where to do it - should be full path String returnedValues; // value to return any results // run the command! try { Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir); int i = p.waitFor(); if (i == 0) { BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); while ( (returnedValues = stdInput.readLine ()) != null) { println(returnedValues); } } else { BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ( (returnedValues = stdErr.readLine ()) != null) { println(returnedValues); } } } catch (Exception e) { println("Error running command!"); println(e); } } |
Hello,
thanks for that. Thats was very useful and you explain it good.
Only the svg export in step 6 doesnt work for me. Should I install some libraries or software. I installed Inkscape, but it doenst work either.
Regards
Patrick
Hmm, it should work if Inkscape was installed correctly. What error do you get?
Thanks for the fast answer, but today I get another interesting thing. Inkscape isn’t necessary for exporting to vector. The exported pdf file is already a vector graphic. You just have to embed the linked file in Illustrator, then you have a common vector graphic.
Yep, that’s what the first part of the post is about! But sometimes you need SVG, and it’s very odd that Processing will load but not export them, hence the hack.