• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Can a script divide a curved path into segments?

New Here ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

There are three scenarios — are any or all possible?

 

Scenario 1. Divide a path at its pre-existing anchor points.

Scenario 2. Divide a path into a specified number of equal segments, eg two halves, three thirds, four quarters, ... 

Scenario 3. Divide a path into a specified length of equal segments, eg 10mm.

TOPICS
Scripting

Views

1.1K

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
community guidelines
Adobe
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Hi @cleoWestCoast, all those are possible via scripting. - Mark

Votes

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
community guidelines
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

1

Select menu: Object > Direction Handles. Cut, Delete, Paste.

2

https://shanfan.github.io/Illustrator-Scripts-Archive/ Divide (length).jsx Divides each selected segment into specified number. based on the length. Length of each segment in each divided segments is equal.

Votes

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
community guidelines
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

Cleo,

 

Though possible when Mark says so, I believe the usability/convenience, or basis, of scripting depends on a few things.

 

Scenario 1) is quite different to scenarios 2) and 3).

 

The non scripting solution to 1) by Ton is exquisite in its simplicity as it is, but it can undoubtedly also form the basis of a script or an action.

 

The right solutions for 2) and 3), whether by scripting or not, depend on whether each path in question consists of only one segment, which is the easy case, or multiple segments (usually of different lengths), which is the difficult case.

 

As Ton mentions for 2), the Divide script by Satō Hiroyuki makes equal divisions of each exisisting segment if more than one, rather than of the whole path.

 

A similar approach for 3) would give a similar challenge with multiple segments paths.

 

Obviously, a crucial part of the solution is to keep the original shape and at the same time get rid of the original Anchor Points between the original segments and thereby only have the desired segments, all of which can only be done by creating a new path.

 

For such cases of multiple segment paths, the following quite similar ways, which may be usable as the bases for scripts, can be used, in both cases with the use of Brushes (always keep a locked/hidden backup of the original path):

 

For 2):

2A) Create a horizontal line (of arbitrary length) with only one segment;

2B) Apply the Divide script by Satō Hiroyuki to get the desired number of segments, and make a New Brush of it as Art Brush with default properties;

2C) Apply the Art Brush from B) to the path;

2D) Object>Expand Appearance, which will give you a Group with the original path and a Group consisting of the new path, then in the Expanded Layer in the Layers panel delete the original path and Ungroup twice to just have the new path;

2E) Enjoy the new path with the original shape and the desired number of segments of equal lengths.

 

For 3):

3A) Create a horizontal line of the desired (approximate, see 3E) below) segment length with only one segment;

3B) Make a New Brush of it as Pattern Brush with default properties;

3C) Apply the Pattern Brush from 3B) to the path;

3D) Object>Expand Appearance, which will give you a Group with the original path and a Group consisting of the new multiple paths, then in the Expanded Layer in the Layers panel delete the original path and Ungroup twice to just have the new paths, then select all of them and join them; you can use Ctrl/Cmd+J (hold Ctrl/Cmd and press J);

3E) Enjoy the new path with the original shape and the desired lengths of segments of equal length, as close as possible to the original segment length from 3A); you can only get equal lengths by adjusting the segment lengths, corresponding to adjusting dashes when applying Dashed Line (Stroke panel) so the original choice of segment length can only be approximate.

 

For a scenario 4), if you wish to have separate paths as in 1) but in a specified number of equal lengths, you can start with 2) and then use the solution to 1) by Ton as follows:

 

For 4):

4A) Go through the steps 2A) - 2D);

4B) Apply the solution to 1) by Ton to the new path which has the desired number of segments;

4C) Enjoy the new set of one segment paths with the original shape and the desired number of separate paths of equal length.

 

Votes

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
community guidelines
Guide ,
Mar 12, 2023 Mar 12, 2023

Copy link to clipboard

Copied

Scenario 1 is easy (see blow).  Scenario 2 needs a bit more thought, but I agree that it's doable.  Scenario 3 is just a variation of scenario 2 (divide the length of the path by the length of the required segments to get the number of the required segments).

// instruction: select a path
var doc = app.activeDocument;
// step 1:
// get an an array of the path's segments, 
// a segment being a 4-point object {P0, P1, P2, P3}
var segments = getSegments(doc.selection[0]);
function getSegments(path) {
    var points = path.pathPoints;
    var segments = [];
    for (var i = 0; i < points.length - 1; i++) {
        var segment = {
            P0: {
                X: points[i].anchor[0], 
                Y: points[i].anchor[1]
            },
            P1: {
                X: points[i].rightDirection[0], 
                Y: points[i].rightDirection[1]
            },
            P2: {
                X: points[i + 1].leftDirection[0], 
                Y: points[i + 1].leftDirection[1]
            },
            P3: {
                X: points[i + 1].anchor[0], 
                Y: points[i + 1].anchor[1]
            }
        };
        segments.push(segment);
    }
    return segments;
}
// step 2:
// iterate the path's segments,
// redrawing them one at a time
for (var i = 0; i < segments.length; i++) {
    var segment1 = segments[i];
    var segment2 = doc.pathItems.add();
    var p1 = segment2.pathPoints.add();
    var p2 = segment2.pathPoints.add();
    p1.anchor = [segment1.P0.X, segment1.P0.Y];
    p1.rightDirection = [segment1.P1.X, segment1.P1.Y];
    p1.leftDirection = [segment1.P0.X, segment1.P0.Y];
    p2.anchor = [segment1.P3.X, segment1.P3.Y];
    p2.rightDirection = [segment1.P3.X, segment1.P3.Y];
    p2.leftDirection = [segment1.P2.X, segment1.P2.Y];
}
// step 3:
// remove the first path
doc.selection[0].remove();

 

 

Votes

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
community guidelines
Explorer ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

You can break apart objects at the existing anchor points using an action or a script (Scenario 1).

Here are the steps:


With your object(s) selected (they can be open or closed paths):

1. Select > Object > Direction Handles

2. Edit > Cut   [Ctrl+X]

3. Edit > Clear     [Delete]

4. Edit > Paste In Place    [Shift+Ctrl+V] (or paste in front/back of you prefer those work too)

 

I have done this as part of a script before, but I don't have my scripts or scripting tools on this computer and I don't know the commands off the top of my head.  It's easy to record this as an action and assign a keyboard shortcut to it if it's something you need to do often.

Votes

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
community guidelines
New Here ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

@m1b @Ton Frederiks @Jacob Bugge @femkeblanco @Aunt Amy Many thanks y'all.  I'll report back after looking at the answers in detail.  

Votes

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
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Cleo,

 

I believe at least five of us hope you will report your findings.

 

Votes

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
community guidelines
Advocate ,
Apr 23, 2023 Apr 23, 2023

Copy link to clipboard

Copied

LATEST

Bonjour à tous!

J'ai réalisé un script qui répond aux trois scénarios cités plus haut.

Si intéressé, me contacter par mail...

René

PS Possibilité d'adapter ce script à votre convenance.

exemples scénario 2

decoupe1.png

découpe2.png

decoupe3.png

Votes

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
community guidelines