Script rotation does not behave like Object>Transform>Rotate...
- July 11, 2024
- 4 replies
- 718 views
Hi there!
I've spent so many hours on this problem, and tried so many permutations of 'Actions' and scripting, but I've run out of things to try. I'm on the verge of doing everything manually (which will take many hours), so I'm posting here in the hope that someone cleverer than me have figure out how to accomplish what I'm trying to do.
I have a composition where I'd like to grab a few points and run 'Transform>Rotate' by 1 degree, export the image, and repeat this action 360 times, incrementing the rotation by 1 degree each time.
What complicates this a little is that some of the points are connected to an object, but I'm only selecting a few of them. Rotating just some of the points creates an interesting 'chaos composition' (see attachment) – this is the effect I'm going for.
I can't do this rotation with 'Actions' because actions doesn't let me export the image with incrementing file names (I have to manually type it out each time).
However, I can do all this with a script, HOWEVER, the rotate function in my script produces different results than the 'in-app' 'transform>rotate' function.
Here's the script I use to rotate the composition with:
// Get the center point of the selection
var centerX = 0;
var centerY = 0;
for (var i = 0; i < selection.length; i++) {
centerX += selection[i].position[0] + selection[i].width / 2;
centerY += selection[i].position[1] - selection[i].height / 2;
}
centerX /= selection.length;
centerY /= selection.length;
// Perform rotation and export 360 times
for (var i = 0; i < 10; i++) {
// Rotate the selected objects around their collective center by -1 degree (counter-clockwise)
for (var j = 0; j < selection.length; j++) {
var obj = selection[j];
var angle = -1; // rotation angle in degrees
var rad = angle * Math.PI / 180; // convert angle to radians
// Get the object's current position
var objCenterX = obj.position[0] + obj.width / 2;
var objCenterY = obj.position[1] - obj.height / 2;
// Calculate the new position
var newX = Math.cos(rad) * (objCenterX - centerX) - Math.sin(rad) * (objCenterY - centerY) + centerX - obj.width / 2;
var newY = Math.sin(rad) * (objCenterX - centerX) + Math.cos(rad) * (objCenterY - centerY) + centerY + obj.height / 2;
// Set the new position
obj.position = [newX, newY];
// Rotate the object
obj.rotate(angle);
}
// Define the file name with the increment
var fileName = "RotatedImage_" + (increment + i) + ".png";
var file = new File(folderPath + fileName);
// Export the file
doc.exportFile(file, fileType, exportOptions);
}
I've also tried creating a script that runs an action that rotates the shape, but this doesn't yield the same result as 'native rotation' either.
Does anyone have experience with rotation and scripting and can potentially tell me what to change here?
