14 Replies Latest reply: Apr 27, 2012 5:04 PM by [Jongware] RSS

    height parameter and math.random constraint

    Petros_ Community Member

      I'm a scripting newbie. I'm trying to make a script that will...

      1. Grab all the objects I have selected
      2. Scale them down (in height) randomly to between 30% and 100% of their original height.

       

      Here's what I have so far...

       

      //get all the objects that are currently selected in the document

      var selectedObjects = app.activeDocument.selection;

       

      //decrease their size by a random number between 0% and 70%

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

                // gets the height of the current object

                var myHeights = selectedObjects[i].height;

                //take the height of the current object and scale it by a random percentage

                selectedObjects[i].height= Math.floor((Math.random()*myHeights)+.3);

      }

       

       

      And here's what it's doing, which is great...

      ThisPartIsWorking.gif

      But there are two problems I'm running into...

      [Problem A] The height is getting scaled relative to the document instead of relative to the object. I would like to be able to rotate the objects around an axis manually before running the script and still have them scale each individual height relative to each object. So far, my script is scaling relative to the document x, y (I think).

      ThisPartIsNotWorking.gif

      I have set the pivot point for each of the rectangles to bottom center, but no dice. In the script, how do I tell it to use the anchor point of the object for scaling instead of the document?

       

      [Problem B] I'm having trouble constraining the math.random to my values. For instance, notice here

      ThisPartIsNotReallyWorking.gif

      that the some of the rectangles end up teeny (smaller than 30% of the original height). How do I set constraints on the math.random so that it's easy for me to adjust the parameters? Right now I'm using math.floor but I'm obviously doing something wrong.

       

      Any insight?

        • 1. Re: height parameter and math.random constraint
          CarlosCanto MVP

          I made a quick search and found a function to get a random number.

           

          Use Resize to change the height instead of changing the height property, that'll take care of problem B.

           

          //get all the objects that are currently selected in the document
          
          var selectedObjects = app.activeDocument.selection;
          
          //decrease their size by a random number between 0% and 70%
          
          for (var i=0; i<selectedObjects.length; i++){
          
                    //get the scale (random number between 30-100)
                    var scale = randomXToY (30, 100);
                    // scale Y only, from top
                    selectedObjects[i].resize (100, scale, true, true, true, true, 1, Transformation.TOP)
          
          }
          
          //function to get random number upto m, by Roshan Bhattarai
          function randomXToY(minVal,maxVal,floatVal)
          {
            var randVal = minVal+(Math.random()*(maxVal-minVal));
            return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
          }
          
          //alert(randomXToY (30, 100));
          

           

           

          for problem [A], I would use a different approach. I would start with 1 object (since they're all the same) and duplicate it, scale it, and rotate it as many times as I need it to complete the circle. We can use a fixed point using these methods.

          • 2. Re: height parameter and math.random constraint
            Petros_ Community Member

            Thanks Carlos! And thanks Roshan! As to Problem A, my hope is to come up with a more generic script that I could reuse in different scenarios within Illustrator. For example I might not always want to rotate the object. I prefer to be able to control the shape, size, rotation angle, and rotation point manually in Illustrator and then do this transformation. Is there a way to easily access the anchor point that I've defined for a given object?

            Screen Shot 2012-04-25 at 2.19.16 PM.png

            • 3. Re: height parameter and math.random constraint
              CarlosCanto MVP

              yes, the rotateAbout or scaleAbout or transformAbout all refer to those 9 points (I used TOP in the previous example), plus the document origin, which is the (0,0) you can change that manually or via script

               

              Transformation

              BOTTOM, LEFT

              BOTTOMLEFT, RIGHT

              BOTTOMRIGHT, TOP

              CENTER, TOPLEFT

              DOCUMENTORIGIN, TOPRIGHT

               

              edit:

              I re-read your previous question, the problem with rotating your object before you scale it is that it will get skewed like in you example. Scaling is only vertical and/or horizontal, you need to scale before rotate.

               

              Message was edited by: CarlosCanto

              • 4. Re: height parameter and math.random constraint
                Petros_ Community Member

                I tried inserting BOTTOM instead of TOP, but it's still resizing based off an (imaginary to me) bounding box around the rotated rectangles. Doesn't Illustrator track the rotation angle of an object after it has been rotated? For instance, if I make a rectangle and rotate 30 degrees, doesn't Illustrator know that the object is actually a rectangle rotated 30 degrees?

                 

                On a related note, do you know where I can find information about the "true", "1", etc. values?:

                selectedObjects[i].resize (100, scale, true, true, true, true, 1, Transformation.TOP)

                 

                Thanks again!

                • 5. Re: height parameter and math.random constraint
                  CarlosCanto MVP

                  I tried inserting BOTTOM instead of TOP, but it's still resizing based off an (imaginary to me) bounding box around the rotated rectangles

                  try using BOTTOM on an unrotated item, to see how it works first, as I said, resize first, then rotate, otherwise shapes will not look right.

                   

                   

                  doesn't Illustrator know that the object is actually a rectangle rotated 30 degrees?

                  no, it just looks at the bounding box of a rotated or un-rotated objects.

                   

                  do you know where I can find information about the "true", "1", etc. values?:

                  1. Scripting Reference

                  2. Object Model Viewer

                   

                  they're the parameters to scale patterns, gradients, strokes, etc.

                  • 6. Re: height parameter and math.random constraint
                    Petros_ Community Member

                    What if instead of rectangles I used stroked lines? Could I then control the line length after I've rotated it?

                    • 7. Re: height parameter and math.random constraint
                      CarlosCanto MVP

                      is there a reason you MUST rotate first?

                      • 8. Re: height parameter and math.random constraint
                        Petros_ Community Member

                        The number of items I'm going to duplicate in a circle and at what angle and what reference point varies just about every time I duplicate objects around a point (I'm using alt+click with rotate tool and ctrl+D to create the rotated objects). I usually rely heavily on my eyes to figure out what angle to rotate things. If I encode all that in the script, I'll be forced to modify the script in a trial-and-error process every time I want to use the script on a set of rotated objects.  Hope that makes sense, I really appreciate the help you've offered.

                        • 9. Re: height parameter and math.random constraint
                          CarlosCanto MVP

                          you lost me, I don't understand what you need. If you have to vary the rotation then no script can help you. If you have to rely on your eyes...same thing, that can't be scripted either.

                           

                          post a real example of what you need, with pretty pictures like you did in your fist post, we might come up with alternatives.

                          • 10. Re: height parameter and math.random constraint
                            Petros_ Community Member

                            Here's an example, which isn't that hard to make manually, but it would be fun if I could have the flexibility to make this more quickly with more rectangles (or lines), more rings of circles, yet retain direct control (in the UI) over the exact sizes of the rectangles moving out from the center white circle.

                            YearGraph.gif

                            If I wanted to make more rings, different sizes, with more rectangles (or lines) right now I would have to manually adjust each rectangle length (which I don't want to do, tedious and long, and not very creative). With the script I could create a rectangle (or line), dupe it a bunch of times around a rotation point, and then run the script to vary how far each object scales in line with the center point.

                            • 11. Re: height parameter and math.random constraint
                              Muppet Mark Community Member

                              Petros, Carlos is right in the fact that this is much easier if you…

                               

                              dupe rectangle ( front/back what ever )

                              scale the duplicate while verticle or horizontal

                              rotate about some origin

                               

                              The other opition is to calculate all the points with math and create the paths… That's Jong business…

                              • 12. Re: height parameter and math.random constraint
                                CarlosCanto MVP

                                calculating all the points is just overkill, specially when there's really no need for it.

                                 

                                Petros, here's a sample of the same thing we've been talking about, scale first, rotate next.

                                 

                                randomRotate.PNG

                                 

                                move your key object close to the (0,0), actually right above the (0,0) on the y axis or (0,radius) before running.

                                 

                                //select ONE object before running
                                var idoc = app.activeDocument;
                                var copies = 20;
                                var angle = 360/copies;
                                
                                
                                var igroup = idoc.groupItems.add();
                                var sel = idoc.selection[0];
                                sel.move (igroup, ElementPlacement.PLACEATBEGINNING);
                                
                                
                                var colorRef = new RGBColor;
                                colorRef.red = 255;
                                
                                
                                var ipath = igroup.pathItems[0];
                                for (var i=0; i<copies; i++){
                                                    var idup = ipath.duplicate();
                                          //get the scale (random number between 30-100)
                                          var scale = randomXToY (0, 70);
                                                      $.writeln(scale);
                                          // scale Y only, from top
                                          idup.resize (100, scale, true, true, true, true, 1, Transformation.BOTTOM);
                                                     idup.rotate(angle*i,true,true,true,true,Transformation.DOCUMENTORIGIN);
                                                    idup.fillColor = colorRef;
                                }
                                
                                ipath.hidden = true;
                                //function to get random number upto m, by Roshan Bhattarai
                                function randomXToY(minVal,maxVal,floatVal)
                                {
                                  var randVal = minVal+(Math.random()*(maxVal-minVal));
                                  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
                                }
                                
                                • 13. Re: height parameter and math.random constraint
                                  Muppet Mark Community Member

                                  Yep thats almost the exact same way I tested… as long as the starting shape is vertial or horizontal then it won't become a shear when scaled…

                                  • 14. Re: height parameter and math.random constraint
                                    [Jongware] MVP

                                    (Just dropping in on something only slightly related)

                                     

                                    Your original random scaling factor fails because your random is no good. Instead of this

                                     

                                    h = Math.floor((Math.random()*myHeights)+.3);

                                     

                                    you should have used this

                                     

                                    h = Math.floor(myHeights*(0.3 + 0.7*Math.random()));

                                     

                                    That gives you a nice spread between 30% and 100% of the original heights.