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

'random' rotation at specified angles?

Explorer ,
Mar 10, 2018 Mar 10, 2018

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
6.2K
Translate
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

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();

}

Translate
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

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

Translate
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

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.

Translate
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

@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.

Translate
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

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

Translate
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

this works like a charm!

thank you so much!!

Translate
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

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!!

Translate
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

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.

Translate
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

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

Translate
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

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

Translate
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

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

Translate
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

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();
}
Translate
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

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?

Translate
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

ooh!

I changed line 10 so that:

i < 2

and it only repeats the cycle once.

that's a start!

Translate
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

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

Translate
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

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();

}

Translate
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

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

Translate
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

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

Translate
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

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.

Translate
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

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.

Translate
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

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

Translate
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 Beginner ,
May 28, 2020 May 28, 2020

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

Translate
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

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();

}
Translate
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

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é.

Translate
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