Highlighted

'random' rotation at specified angles?

Explorer ,
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

1.5K

Likes

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

'random' rotation at specified angles?

Explorer ,
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

1.6K

Likes

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

}

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Most Valuable Participant ,
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

Likes

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
Reply
Loading...
Explorer ,
Mar 11, 2018

Copy link to clipboard

Copied

this works like a charm!

thank you so much!!

Likes

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
Reply
Loading...
Explorer ,
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!!

Likes

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
Reply
Loading...
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Explorer ,
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

Likes

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
Reply
Loading...
Most Valuable Participant ,
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

Likes

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
Reply
Loading...
Explorer ,
Mar 11, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Enthusiast ,
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();
}

Likes

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
Reply
Loading...
Explorer ,
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?

Likes

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
Reply
Loading...
Explorer ,
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!

Likes

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
Reply
Loading...
Explorer ,
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

Likes

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
Reply
Loading...
Enthusiast ,
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();

}

Likes

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
Reply
Loading...
Contributor ,
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

Likes

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
Reply
Loading...
Most Valuable Participant ,
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

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Explorer ,
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

Likes

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
Reply
Loading...
New Here ,
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

Likes

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
Reply
Loading...
Adobe Community Professional ,
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();

}

Likes

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
Reply
Loading...
Contributor ,
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é.

Likes

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
Reply
Loading...
Enthusiast ,
May 30, 2020

Copy link to clipboard

Copied

Bonjour m1b,

Nombre entier aléatoire  multiple de 72 situé entre [ 0 et 360]   (72*5)
L'analyse est juste, sauf que la fonction parseInt(string[, base]) ne te permettra pas d'obtenir 360,

de plus tu as de la chance que Javascript soit un langage très conciliant:
string
Valeur qu'on souhaite analyser et convertir. Si l'argument string n'est pas une chaîne de caractères, elle sera convertie en une chaîne (grâce à l'opération abstraite ToString) . Les blancs contenus au début de l'argument sont ignorés.

 

 

 

for (var j = 0; j < 10; j++) {
var randomAngle =  parseInt(Math.random() *5); // nombre entre 0 et 5
    randomAngle *= 72;  // nombres  multiple de 72 situé entre 0 et 72*5=360
$.write(randomAngle+"\r");  // alert(randomAngle+"\r");
}

 

 

Juste pour info

var randomAngle = Math.round(Math.random() *5); // nombre entre 0 et 5

 

 

 

Likes

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
Reply
Loading...
m1b LATEST
Contributor ,
May 30, 2020

Copy link to clipboard

Copied

Thank you Rene, I see your point. I don't know why I used parseInt! That was silly of me. 🙂

 

I don't really want 360 degrees because 360° is the same as 0° rotation.

Likes

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
Reply
Loading...