Copy link to clipboard
Copied
I have a whole lot of parallel paths that have different directions (some up, some down). "Reverse path direction" is not helpful unless I select manually, which is super time consuming.
Wondering if there is a way to quickly align all path directions?
try this script, select your items before running
// equalizePathDirection,jsx
// CarlosCanto // 06/15/2020
// https://community.adobe.com/t5/illustrator/how-to-make-all-paths-the-same-direction/td-p/11207971?page=1
function main() {
var idoc = app.activeDocument;
var sel = idoc.selection;
var pathitems = [];
if (sel.length>1) {
for (var a = 0; a<sel.length; a++) {
var pgItem = sel[a];
if (pgItem.typename == 'PathItem' && !pgItem.closed
...
Copy link to clipboard
Copied
Hi, Did you try the solution from the following thread
https://community.adobe.com/t5/illustrator/reverse-path-direction/td-p/3298182?page=1
This script also works when you select the path Items, Here is the updated version of the same script that works for all pathItems with no selection.
// A Javascript for Adobe Illustrator
// by Wolfgang Reszel (ai-js@rumborak.de)
// -------------------------------------------------------------------
var language="en"; // "de" fuer Deutsch
// -------------------------------------------------------------------
var WR="WR-reversePathDirection v0.2\n\n";
if (language == "de") {
var MSG_noobjects = WR+"Bitte w\xE4hle vorher einige Objekte aus.";
var MSG_nodocs = WR+"Kein Dokument ge\xF6ffnet.";
} else {
var MSG_noobjects = WR+"Please select some objects.";
var MSG_nodocs = WR+"You have no open document."
}
var error=0;
if (documents.length<1) {
error++;
alert(MSG_nodocs);
}
if (error == 0) {
var theObjects= app.activeDocument.pathItems; //selection;
if (theObjects.length<1 && error == 0) {
error++;
alert(MSG_noobjects);
}
}
if (error < 1) {
reversePaths(theObjects);
}
function reversePaths(theItems) {
if (theItems.typename == "TextPath") {
pathLen = theItems.pathPoints.length;
for ( k = 0; k < pathLen/2; k++ ) {
h = pathLen-k-1;
HintenAnchor = theItems.pathPoints[h].anchor;
HintenLeft = theItems.pathPoints[h].leftDirection;
HintenType = theItems.pathPoints[h].pointType;
HintenRight = theItems.pathPoints[h].rightDirection;
theItems.pathPoints[h].anchor = theItems.pathPoints[k].anchor;
theItems.pathPoints[h].leftDirection = theItems.pathPoints[k].rightDirection;
theItems.pathPoints[h].pointType = theItems.pathPoints[k].pointType;
theItems.pathPoints[h].rightDirection = theItems.pathPoints[k].leftDirection;
theItems.pathPoints[k].anchor = HintenAnchor;
theItems.pathPoints[k].leftDirection = HintenRight;
theItems.pathPoints[k].pointType = HintenType;
theItems.pathPoints[k].rightDirection = HintenLeft;
}
}
for (var i = 0 ; i < theItems.length; i++)
{
if (theItems[i].typename == "GroupItem" || theItems[i].typename == "CompoundPathItem" ) {
reversePaths(theItems[i].pathItems);
try {reversePaths(theItems[i].compoundPathItems)} catch (e) {};
}
if (theItems[i].typename == "TextFrame" ) {reversePaths(theItems[i].textPath);}
if (theItems[i].typename == "TextArtItem" ) {reversePaths(theItems[i].textPaths);}
if ( theItems[i].typename == "PathItem" && !theItems[i].locked && !theItems[i].parent.locked && !theItems[i].layer.locked ) {
pathLen = theItems[i].pathPoints.length;
for ( k = 0; k < pathLen/2; k++ ) {
h = pathLen-k-1;
HintenAnchor = theItems[i].pathPoints[h].anchor;
HintenLeft = theItems[i].pathPoints[h].leftDirection;
HintenType = theItems[i].pathPoints[h].pointType;
HintenRight = theItems[i].pathPoints[h].rightDirection;
theItems[i].pathPoints[h].anchor = theItems[i].pathPoints[k].anchor;
theItems[i].pathPoints[h].leftDirection = theItems[i].pathPoints[k].rightDirection;
theItems[i].pathPoints[h].pointType = theItems[i].pathPoints[k].pointType;
theItems[i].pathPoints[h].rightDirection = theItems[i].pathPoints[k].leftDirection;
theItems[i].pathPoints[k].anchor = HintenAnchor;
theItems[i].pathPoints[k].leftDirection = HintenRight;
theItems[i].pathPoints[k].pointType = HintenType;
theItems[i].pathPoints[k].rightDirection = HintenLeft;
}
}
if ( theItems[i].typename == "TextPath" && !theItems[i].locked && !theItems[i].parent.locked && !theItems[i].parent.layer.locked ) {
pathLen = theItems[i].textPathObject.pathPoints.length;
for ( k = 0; k < pathLen/2; k++ ) {
h = pathLen-k-1;
HintenAnchor = theItems[i].textPathObject.pathPoints[h].anchor;
HintenLeft = theItems[i].textPathObject.pathPoints[h].leftDirection;
HintenType = theItems[i].textPathObject.pathPoints[h].pointType;
HintenRight = theItems[i].textPathObject.pathPoints[h].rightDirection;
theItems[i].textPathObject.pathPoints[h].anchor = theItems[i].textPathObject.pathPoints[k].anchor;
theItems[i].textPathObject.pathPoints[h].leftDirection = theItems[i].textPathObject.pathPoints[k].rightDirection;
theItems[i].textPathObject.pathPoints[h].pointType = theItems[i].textPathObject.pathPoints[k].pointType;
theItems[i].textPathObject.pathPoints[h].rightDirection = theItems[i].textPathObject.pathPoints[k].leftDirection;
theItems[i].textPathObject.pathPoints[k].anchor = HintenAnchor;
theItems[i].textPathObject.pathPoints[k].leftDirection = HintenRight;
theItems[i].textPathObject.pathPoints[k].pointType = HintenType;
theItems[i].textPathObject.pathPoints[k].rightDirection = HintenLeft;
}
}
}
}
Let us know if this works for you.
Copy link to clipboard
Copied
Yeah thanks I had. It just reverses all the paths. I need to set all paths to the same direction.
Tried hacking away at the script to set all pathPoints to right direction, but I don't know what I'm doing.
Copy link to clipboard
Copied
Hi,
Could you post Before and After screenshots or share ai file. I am not sure how direction will have effect on your paths. So, first I would like to see what you are trying to achieve here.
Copy link to clipboard
Copied
Hi Charu,
Long story if you're interested: What I'm doing is making a set of paths for a very specific write-on effect in After Effects (using 'trim paths'). I want to fill in a complex shape with continuously drawing lines. It's easy to do if you don't mind waiting for lines to draw outside the shape (ie the open part of a C or O), but I need the path to jump straight from the end of one part to the start of another, hence the paths cut to the specific shape. This is probably something that could be done with pure code, but I'm trying to keep it all editable in AE/AI as much as possible for design reasons.
Here's a file that shows the paths with random directions (the arrow heads are just to show direction):
https://www.dropbox.com/s/8gftbff2pu9q8h5/TEST%20K_arrows.ai?dl=0
And without arrowheads: https://www.dropbox.com/s/52637cxs4aprsn4/TEST%20K.ai?dl=0
First I'm trying to get them all aligned the same way. IF that's possible then I'd ask how to use a Modulus function or something to make every second one reverse, so the lines can draw up then down then up etc. But one step at a time. Just aligning them all the same way is enough to get the job done.
Copy link to clipboard
Copied
try this script, select your items before running
// equalizePathDirection,jsx
// CarlosCanto // 06/15/2020
// https://community.adobe.com/t5/illustrator/how-to-make-all-paths-the-same-direction/td-p/11207971?page=1
function main() {
var idoc = app.activeDocument;
var sel = idoc.selection;
var pathitems = [];
if (sel.length>1) {
for (var a = 0; a<sel.length; a++) {
var pgItem = sel[a];
if (pgItem.typename == 'PathItem' && !pgItem.closed) {
pathitems.push(pgItem);
}
}
selection = null;
equalizeDirection(pathitems);
}
else {
alert('select two or more path items and try again');
}
}
main ();
function equalizeDirection(pa /*pathItems array*/) {
var p0, p1, pp;
for (var p in pa) {
pp = pa[p].pathPoints;
p0 = pp[0].anchor;
p1 = pp[pp.length-1].anchor;
if (p0[1]<p1[1]) {
pa[p].selected = true;
}
}
app.executeMenuCommand('Reverse Path Direction');
}
Copy link to clipboard
Copied
That's great thanks. Nailed it.
Copy link to clipboard
Copied
you're welcome!
Copy link to clipboard
Copied
Good evening, Carlos.
I'd like to inform you that your script doesn't seem to work as expected in my environment.
Actually, it doesn't do anything. It only deselects the current selection.
Tested with Illustrator 24.1.3 on Macintosh OS 10.14.6
Just thought that might be worth of a note for you.
Copy link to clipboard
Copied
Hello Kurt, thanks for letting me know. Are you trying to reverse Closed Shapes? the script only works with Open Paths, I excluded closed shapes on purpose.
Would you mind doint a test for me?
can you run this one liner script, this is the command responsible for reversing. Select open or closed paths before running.
app.executeMenuCommand('Reverse Path Direction');
thanks
Carlos
Copy link to clipboard
Copied
Thanks, Carlos.
No, my test was based on open paths. I did not use any closed path.
Will do some more experiments tomorrow. Now it's time to say ...
Good night (well, at least over here).
Copy link to clipboard
Copied
ok, see you tomorrow. Have a good night
Copy link to clipboard
Copied
Yes, Carlos, the one liner script does work (of course).
I did some more testing and it turned out that the issue with your script is a special (and somehow beautiful) one.
My tests yesterday were based on horizontal straight lines (with different path directions). You may try the following test: With the line tool draw a dozen of horizontal lines and give them different path directions. Now execute your script. Does it actually work? No, at least not for me.
Do the same with a couple of vertical lines. Does it work? Yes, at least for me.
The same goes for straight lines drawn at other angles (other than horizontal). The script will work.
There seem to be some issues with curvy paths drawn with the Pen tool for example, but I would have to do some further tests in order to describe them in detail.
Copy link to clipboard
Copied
Hi Kurt, thanks for the detailed report.
I see what happened, and I get the same results you did.
the truth is, the script is not very robust. The OP requested to reverse vertical paths, so that's all I did. I did't have time to make a "production" ready script covering all possible scenarios.
as usual thanks for checking so diligently!!
Carlos
Copy link to clipboard
Copied
A quick fix for the horizontal lines issue would be to clandestinely rotate the current selection 0,0001° as an evil mischief inside your script code.
Probably no one will ever notice that trick.
8)
Copy link to clipboard
Copied
hahaha it is not as simple Kurt, as of now, all paths are evaluated as if they're vertical. Even almost horizontal lines would get their Y position tested.
A more complete solution would be to treat lines less than 45 degrees as horizontal and over 45 as vertical. Then we could process both vertical and horizontal lines more precisely.
Copy link to clipboard
Copied
I think an action that would first do the (dirty) tiny rotation and then executes the script may be used as a bumpy workaround to solve the "horizontal issue". Just an idea. No proper one, I know.
Have a good day (and evening).
Copy link to clipboard
Copied
noted, thanks have a good one as well.