Skip to main content
runninghead_design
Inspiring
November 2, 2020
Answered

Script to rotate selected objects by random increments of your choice

  • November 2, 2020
  • 2 replies
  • 3163 views

Just in case anyone's making patterns or something.

 

I wrote this tiny script that rotates all selected objects by random increments of 60 degrees, or 90, or whatever you want. It makes use of modulo for efficiency.

Free to use and share.

 

Please post to this thread anything you create with it as I believe it could produce some interesting patterns, mazes, etc. If you can find a way to do the same thing using just AI native tools please share your technique here.

Any bugs / feedback please also post here so I can improve the script.

 

First, a section of a seamless pattern I made just now using 3 hexagonal tiles and a few tricks (custom line profiles, Roughen filter, etc).

 

Enjoy!

 

Runninghead_Design 2020

 

// Script to rotate all selected objects by set increments with the intention that it will be useful for the generation of pattern tiles. Copyright runninghead.com 2020. If you use this script please send me examples of your work, I'd love to see what you can do with it! Any suggestions / bug reports welcome.

// NOTE: Does not currently work through Groupings, so Ungroup your objects before running this script!

aDoc = app.activeDocument;
aSel = aDoc.selection;
nSel = aSel.length;// number of objects currently selected in illustrator
increment = Number(prompt("Rotation degree increments (30, 45, 60, 90, etc?)", 60));

for(i=0; i<nSel; i++)
{
	randomAngle = Math.floor(Math.random()*360);
	rotationAngle = randomAngle - (randomAngle % increment);
	aSel[i].rotate(rotationAngle, rotationAngle);
}

// end of script

 

This topic has been closed for replies.
Correct answer m1b

I like the pattern! Clever choice of hexagonal cells I think. Would you mind posting a screen shot of the 3 hexes you used?

 

Also, not a bug, but just for seeing different ways of scripting, here's how I would start your script if I wrote it:

 

 

var items = app.activeDocument.selection;

// then, I'd use items.length here 
for (var i=0; i<items.length; i++) {

 

 

 

Also, the rotate method just needs the angle parameter once, eg. pathItem.rotate(60).

 

Thanks for sharing!

2 replies

m1b
Community Expert
November 2, 2020

And here's another approach to calculating the rotation angle. I've only tested on a calculator, so let me know if it doesn't work right for your use.

 

 

 

// gives a number between 0 and 5 for increment = 60
var rotationIndex = Math.floor(Math.random * (360 / increment - 1));

// so rotation is that many times the increment
var rotationAngle = rotationIndex * increment;

 

 

 

runninghead_design
Inspiring
November 2, 2020

Interesting! I'll look in to this ASAP and amend the script (almost midnight here). Thank you @m1b !

As I hope is visible in the screen grab, I used 0% transparency on some line segments to preserve the overall hexagonal shape for the 3 symbols (restored these to 10% for the grab).

 

Roughness provided by a custom line profile.

 

Many Escher tesselations seem to break down to 6 triangles within each hex, triangles that link back in to each other. This affords a powerful method for interlinking of decorative or illustrative artwork. I'll attempt some more patterns soon and post here.

 

m1b
Community Expert
November 3, 2020

Thank you, that's excellent!

m1b
m1bCorrect answer
Community Expert
November 2, 2020

I like the pattern! Clever choice of hexagonal cells I think. Would you mind posting a screen shot of the 3 hexes you used?

 

Also, not a bug, but just for seeing different ways of scripting, here's how I would start your script if I wrote it:

 

 

var items = app.activeDocument.selection;

// then, I'd use items.length here 
for (var i=0; i<items.length; i++) {

 

 

 

Also, the rotate method just needs the angle parameter once, eg. pathItem.rotate(60).

 

Thanks for sharing!