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

'random' rotation at specified angles?

Explorer ,
Mar 10, 2018 Mar 10, 2018

Copy link to clipboard

Copied

To elaborate...

I already have a script for randomly rotating objects, but for this particular project I don't want them to rotate randomly from range of degrees; I'm looking for a way to randomly rotate a group of objects at either 90, 180, 270, or 360 degrees only.

I approached an engineer friend about this, and her suggestion was this:

var randNum = random.(0,3); // pick random # in range

var degrees = [90, 180, 270, 360];

var myRandDegree = degrees[randNum];

At this point, I generally understand the general principle of mapping 1-4 to the degrees I want to rotate, but—as I am not a programmer—I'm looking for help in getting the syntax of the script straight. Any help will be greatly appreciated. 

Thanks in advance!

*Also, would there be a way of applying this concept to randomly coloring a group of objects with a specific color palette? That would also be very useful.

TOPICS
Scripting

Views

4.2K

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
Advocate ,
Mar 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

Bonjour,

Je peux réaliser des scripts sur demande avec explications détaillées par mail.

Je te donne une solution pour la rotation...

// JavaScript Document pour Illustrator

// elleere  Sun, 11 March 2018 09:40:58 GMT

// sélectionnez un objet

  var plageAg = [90,180,270,360];

//------------------------------

  var d = app.activeDocument;

  if (selection.length) {

    var objet = selection[0];

    var liste = "";

      for (var i = 0; i < 60; i++) {

        rotationObjet(objet,plageAg);

        // boucle pour ralentir l'affichage

        for (var k = 0; k < 125625; k++) {

          var t = Math.sqrt(2.0565*k/45);

        }

      }

  createListeText = d.textFrames.pointText([50,-30]);

  createListeText.contents = liste;

  }

function rotationObjet(obj,tab)

{ // faire tourner obj angle plage tab de façon aléatoire

    var rang = Math.round(Math.random()*(tab.length-1));

    var angle = tab[rang];

      liste += angle+"\r";

      obj.rotate(angle,true,undefined,false,false,Transformation.CENTER);

      redraw();

}

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

hmm...all i'm getting from running this script is a text list of the operation 'performed'... but no transformation of the objects.

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

The snippet written by at once. It works as expected.

Aktualisiert

Your screenshot comes in time.

You have multiple groups in one layer. What result do you wish (for every single group - or for all grouped elements in the layer???) Please explain in detail, perhaps with screenshots before and after.

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

@pixxxel schubser

Okay so, each group is the shape you see with no-fill and no-stroke square around it.

By grouping these two elements, I am able to define and maintain a center point around which the group can rotate.

I use an invisible square because I will be arranging the objects on a grid with zero padding between them, so the square is the the most helpful bounding shape; the structure of the grid.

As I already have scripts to distribute stacked objects to a grid, my next desire is to be able to randomly rotate each object in the grid by either 90, 180, 270, or 360 (or zero; which i understand that this doesn't appear to affect the object, but i'm okay with some selected objects not rotating so long as 75% of the others do) degrees.

In this way, the result is a new variation of pattern; the starting shapes no longer all face the same direction and angle.

Essentially, this is a geometric pattern generator for shapes on a grid.

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

One shot into dark. How about:

var aDoc = app.activeDocument;

var theGrp = aDoc.groupItems;

for (i = 0; i<theGrp.length; i++) {

rotateSel (theGrp);

}

function rotateSel (obj) {

    var grd = [90, 180, 270, 360];

    obj.rotate(grd[Math.ceil (Math.random()*4) -1], true, true, true, true, Transformation.CENTER);

}

Have fun

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

this works like a charm!

thank you so much!!

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

BUT!

...this doesn't work for objects that AREN'T grouped haha.

SO, how to modify the last script to work for ungrouped items?

thanks again! i super appreciate your help with this!!

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

briank53220431  schrieb

… Okay so, each group is the shape you see with no-fill and no-stroke square around it …

This was the rule for the script snippet. Changing the rule requires another script. Without to know exactly which structure your document has and what you really want - it's hard or impossible to write a script for all possible cases.

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

I definitely appreciate the difference in scripting—there are just situations where the same operation applied to ungrouped objects would be ideal.

Apologies for being a pest, but I'm learning from these examples!

Much appreciation for your patience with me

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

Perhaps you try to use pageItems in the snippet instead of groupItems or try to separate simple pathItems at one layer and groupItems at another layer or or or … there are hundreds of possibilities to reach the goal. Maybe a sample file could be helpful.

If you have additional questions in this topic - we are here.

If you have questions about other "problems" - please open a new thread.

Have fun

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

haha thanks for the nudge(s) in the right direction(s...s...s)!

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 ,
Mar 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

Bonjour,

// JavaScript Document pour Illustrator
// elleere  Mon, 12 March 2018 14:12:22 GMT
// sélectionnez un objet
  var plageAg = [90,180,270,360];
//------------------------------
  var d = app.activeDocument;
  if (selection.length) {
    var objet;
    var liste = "";
      for (var i = 0; i < 60; i++) {
        for (var j = 0; j < selection.length; j++) {
          objet = selection;
          rotationObjet(objet,plageAg);
        }
        // boucle pour ralentir l'affichage
        for (var k = 0; k < 125625; k++) {
          var t = Math.sqrt(2.0565*k/45);
        }
      }
  createListeText = d.textFrames.pointText([50,-30]);
  createListeText.contents = liste;
  }
function rotationObjet(obj,tab)
{ // faire tourner obj angle plage tab de façon aléatoire
    var rang = Math.round(Math.random()*(tab.length-1));
    var angle = tab[rang];
      liste += angle+"\r";
      obj.rotate(angle,true,undefined,false,false,Transformation.CENTER);
      redraw();
}

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 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

so fun to watch!

interesting how this script repeats multiple times—but DOES eventually do exactly what I want!

is there any way to only rotate each object once, and then stop?

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 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

ooh!

I changed line 10 so that:

i < 2

and it only repeats the cycle once.

that's a start!

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 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

AH!

I deleted line 10 and 14, and it rotates everything once and stops!

I think that's it!

Thank you so much renél80416020

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 ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

Bonjour Briank,

Très bien !

https://share.orange.fr/#nww74WiboG1e5790991b

Pdf Hybride (ouvrir dans Illustrator CS6 et pus)

Je peux résoudre le problème de couleurs aléatoires (contacte moi par mail)

// JavaScript Document pour Illustrator

// elleere  Tue, 13 March 2018 09:43:12 GMT

// sélectionnez un ou plusieurs objets

  var plageAg = [90,180,270,360];

//------------------------------

  var d = activeDocument;

  if (selection.length) {

    var mySel = selection;

        d.selection = null;

    var objet;

         for (var j = 0; j < mySel.length; j++) {

          objet = mySel;

          rotationObjet(objet,plageAg);

        }

  }

function rotationObjet(obj,tab)

{ // faire tourner obj angle plage tab de façon aléatoire

    var rang = Math.round(Math.random()*(tab.length-1));

    var angle = tab[rang];

      obj.rotate(angle,true,undefined,false,false,Transformation.CENTER);

      //redraw();

}

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 ,
May 29, 2020 May 29, 2020

Copy link to clipboard

Copied

Hi Rene,

 

Wow! I've never known that simply 

selection

works the same as

app.activeDocument.selection

 

Is there documentation for that somewhere? Are there other such shortcuts?

 

Regards,

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

briank53220431​

Or do you mean something simple like that:

required: open document with a selected path item

rotateSel();

function rotateSel () {

    var grd = [90, 180, 270, 360];

    activeDocument.selection[0].rotate(grd[Math.ceil (Math.random()*4) -1], true, true, true, true, Transformation.CENTER);

}

If so, have fun

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

cool—thanks for the reply!

seems to be a step in the right direction, but only one object selected in a group rotates when i run the script.

then, if i run the script again, i get nothing and have to re-select the group...only to have one object affected by the script.

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, 2018 Mar 11, 2018

Copy link to clipboard

Copied

This snippet also should works with a selected group. (But be sure, a rotation of 360 degrees is the same as no rotation )

Please show a screenshot of your artwork with visible layers panel and the selected group.

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 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

so i'm noticing that only the top object of the stack is affected; which in this case, is the triangle.Screen Shot 2018-03-11 at 11.35.35 AM.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
New Here ,
May 28, 2020 May 28, 2020

Copy link to clipboard

Copied

Was excited to find this thread but in every case I've tried here I get this error.

Maybe something has changed in the newest releases of CS.

 

Screen Shot 2020-05-28 at 11.46.55 AM.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
Community Expert ,
May 28, 2020 May 28, 2020

Copy link to clipboard

Copied

Hi julian, the new forum format messed up existing scripts, try this version

 

// JavaScript Document pour Illustrator

// elleere  Tue, 13 March 2018 09:43:12 GMT

// sélectionnez un ou plusieurs objets

  var plageAg = [90,180,270,360];

//------------------------------

  var d = activeDocument;

  if (selection.length) {

    var mySel = selection;

        d.selection = null;

    var objet;

         for (var j = 0; j < mySel.length; j++) {

          objet = mySel[j];

          rotationObjet(objet,plageAg);

        }

  }

function rotationObjet(obj,tab)

{ // faire tourner obj angle plage tab de façon aléatoire

    var rang = Math.round(Math.random()*(tab.length-1));

    var angle = tab[rang];

      obj.rotate(angle,true,undefined,false,false,Transformation.CENTER);

      //redraw();

}

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 ,
May 29, 2020 May 29, 2020

Copy link to clipboard

Copied

Just for interest, because I had to solve this recently, here's my version of the calculation:

 

 

var angleStep = 90;
var randomAngle = angleStep * Math.floor(Math.random() * (360 / angleStep));

 

 

Change angleStep to 120 or 72 and you'll have 3 or 5 directions instead of 4. 

 

Or maybe specify the number of directions you want:

 

var directions = 5;
var angleStep = 360 / directions;
var randomAngle = angleStep * Math.floor(Math.random() * (360 / angleStep));

 

 

Edit: I've fixed a stupid error in my code thanks to René.

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