3

I have a script which makes a simple shape* for me:

var docRef = documents.add();
var piRef = activeDocument.pathItems;

{
    var pathRef = piRef.add();
    pathRef.setEntirePath( new Array(
        new Array(475,543.30078125),
        new Array(450,500),
        new Array(475,456.69873046875),
        new Array(525,456.69873046875),
        new Array(550,500),
        new Array(525,543.30078125),
        new Array(475,543.30078125) ) ) ;
}

However, the shape(s) which I create are limited to straight paths, how could I change this to be creating paths with curved edges?

I'm pretty new and inexperienced to Illustrator scripting, I'm sure there's a better way for me to be making this path in the first place.


I use this script from Wolff in order to extract my path coordinates (after creating the path in Illustrator), I assume I'd have to modify that as well in order to have it export round paths.

*The shape being a hexagon is irrelevant, the idea is to be able to create arbitrary shapes.

  • 1
    Welcome to the dark side. – joojaa Oct 23 '18 at 3:59
2

You can't do it with setEntirePath, you have to make a new pathItem (Document.pathItems.add()) and then you have to add your path points individually.

You would do this via:

var myPath = app.activeDocument.pathItems.add();

var newPoint = myPath.pathPoints.add();
newPoint.leftDirection = [10, -5];
newPoint.anchor = [0, 0];
newPoint.rightDirection = [-10, 5];

Now when you add more, this will form your Bezier curve!


var doc = app.activeDocument;
var myPath = doc.selection[0];
var pathPointArray = [];
var thisPoint;
for(var i = 0; i < myPath.pathPoints.length; i++){
    thisPoint = myPath.pathPoints[i];
    pathPointArray.push([
        [
            thisPoint.leftDirection[0],
            thisPoint.leftDirection[1]
        ],
        [
            thisPoint.anchor[0],
            thisPoint.anchor[1]
        ],
        [
            thisPoint.rightDirection[0],
            thisPoint.rightDirection[1]
        ]
    ]);
}
alert(pathPointArray.join("\n"));

Here's the result:

The REAL result!

And here's one where the entire thing is put together whereby it gathers the coordinates from your selection and creates a duplicate item of the same thing (without using copy/pasting or duplicate commands) exactly 100 points above the initial selection.

    var doc = app.activeDocument;
    var myPath = doc.selection[0];
    var pathPointArray = [];
    var thisPoint;
    for(var i = 0; i < myPath.pathPoints.length; i++){
        thisPoint = myPath.pathPoints[i];
        pathPointArray.push([
            [
                thisPoint.leftDirection[0],
                thisPoint.leftDirection[1]
            ],
            [
                thisPoint.anchor[0],
                thisPoint.anchor[1]
            ],
            [
                thisPoint.rightDirection[0],
                thisPoint.rightDirection[1]
            ]
        ]);
    }

    var myNewPath = doc.pathItems.add();
    var thisArrayPointObject;
    var newPoint;
    for(var i = 0; i < pathPointArray.length; i++){
        thisArrayPointObject = pathPointArray[i];
        newPoint = myNewPath.pathPoints.add();
        newPoint.leftDirection = [
            thisArrayPointObject[0][0],
            thisArrayPointObject[0][1] + 100
        ];
        newPoint.anchor = [
            thisArrayPointObject[1][0],
            thisArrayPointObject[1][1] + 100
        ];
        newPoint.rightDirection = [
            thisArrayPointObject[2][0],
            thisArrayPointObject[2][1] + 100
        ];
    };

And here is the result from that:

enter image description here

  • Is there a way I can extract this data from a shape I've made? – WELZ Oct 23 '18 at 1:49
  • I tried adding the code and got the error myPath is undefined, I'm definitely doing something wrong – WELZ Oct 23 '18 at 1:58
  • Well you got to define it first, where did you think it was gonna come from? Try doing this: var myPath = app.activeDocument.pathItems.add(); That of course has to come before any of the stuff which acts on the variable called "myPath". But don't worry, you'll get it! – Silly-V Oct 23 '18 at 2:01
  • Use this exercise to collect pathPoints from one selected pathItem: var doc = app.activeDocument; var myPath = doc.selection[0]; var pathPointArray = []; var thisPoint; for(var i = 0; i < myPath.pathPoints.length; i++){ thisPoint = myPath.pathPoints[i]; pathPointArray.push([thisPoint.leftControl, thisPoint.anchor, thisPoint.rightControl]); } alert(pathPointArray.join("\n")); – Silly-V Oct 23 '18 at 2:02
  • The coordinates that it exports can't be used for a bezier curve, can they? – WELZ Oct 23 '18 at 2:11

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.