Copy link to clipboard
Copied
Hi all
I'm trying to nudge the anchor points on an object randomly (to get an informal look). (I know some Effects do this already.)
Maintaining corner points is no problem - you just set the direction handles to the same coordinates as their anchor.
But I can't nudge a smooth point in the way I want to, which is to move the anchor but maintain the direction handles in exactly the same relation to their anchor as they had before. That is to say, they should remain smooth points.
My script calculates the difference between 'before' and 'after' positions of the anchor, and applies that difference to the direction handles… but in practice the smooth points are converted to corners, because the direction handles don't maintain their relation with the anchor.
Would be grateful if someone can point out the flaw(s!) in my logic.
var docRef = activeDocument;
var objects = activeDocument.selection.length;
var cShift = prompt("Point shift (pt)",5);
// loop through all objects
for(var count=0;count<objects;count++)
{
currentObj = activeDocument.selection[count];
nudge(currentObj);
}
// function nudges each object
function nudge(obj)
{
var objPoints = obj.selectedPathPoints;
var objPointCount = objPoints.length;
for (var i = 0;i<objPointCount;i++){
var thisPoint = objPoints;
var va = thisPoint.anchor[0];
var vb = thisPoint.anchor[1];
var la = thisPoint.leftDirection[0];
var lb = thisPoint.leftDirection[1];
var ra = thisPoint.rightDirection[0];
var rb = thisPoint.rightDirection[1];
if(va==la && va==ra && vb==lb && vb==rb)
{ // if corner point, randomize anchor position
va = va +((Math.random()*cShift));
vb = vb +((Math.random()*cShift));
thisPoint.anchor = Array(va,vb);
// set direction handles at same position, maintaining a corner point
thisPoint.leftDirection = Array(va,vb);
thisPoint.rightDirection = Array(va,vb);
}
else
{ // not a corner point: keep handles in same relative position to their shifted anchor
var va2 = va +((Math.random()*cShift));
var vb2 = vb +((Math.random()*cShift));
thisPoint.anchor = Array(va2,vb2);
// this bit calculates the random shift applied to the anchor
var anchorShifta = va2 - va;
var anchorShiftb = vb2 - vb;
// these lines apply the same shift to the direction handles
thisPoint.leftDirection[0] = la + anchorShifta;
thisPoint.leftDirection[1] = lb + anchorShiftb;
thisPoint.rightDirection[0] = ra + anchorShifta;
thisPoint.rightDirection[1] = rb + anchorShiftb;
}}}
The else part modified:
else
{ // not a corner point: keep handles in same relative position to their shifted anchor
var aShift = Math.random()*cShift;
var bShift = Math.random()*cShift;
thisPoint.anchor = [va + aShift, vb + bShift];
thisPoint.leftDirection = [la + aShift, lb + bShift];
thisPoint.rightDirection = [ra + aShift, rb + bShift];
}
Copy link to clipboard
Copied
The else part modified:
else
{ // not a corner point: keep handles in same relative position to their shifted anchor
var aShift = Math.random()*cShift;
var bShift = Math.random()*cShift;
thisPoint.anchor = [va + aShift, vb + bShift];
thisPoint.leftDirection = [la + aShift, lb + bShift];
thisPoint.rightDirection = [ra + aShift, rb + bShift];
}
Copy link to clipboard
Copied
Thank you - really helpful. I'd realised afterwards that anchor and direction handles like to have their positions set as arrays, but your solution is more elegant than I would have been able to achieve!