Highlighted

How to make all paths the same direction

Explorer ,
Jun 14, 2020

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? 

 

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

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');
}

TOPICS
How to, Scripting

Views

344

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to make all paths the same direction

Explorer ,
Jun 14, 2020

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? 

 

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

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');
}

TOPICS
How to, Scripting

Views

345

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 14, 2020 1
Adobe Community Professional ,
Jun 14, 2020

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.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 14, 2020 0
Explorer ,
Jun 14, 2020

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.

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

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.

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 14, 2020 0
Explorer ,
Jun 15, 2020

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.

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0
Adobe Community Professional ,
Jun 15, 2020

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');
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 1
Explorer ,
Jun 15, 2020

Copy link to clipboard

Copied

@CarlosCanto

That's great thanks. Nailed it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

you're welcome!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0
Adobe Community Professional ,
Jun 15, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0
Adobe Community Professional ,
Jun 15, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0
Adobe Community Professional ,
Jun 15, 2020

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).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2020 0