5 Replies Latest reply on Apr 3, 2015 8:43 AM by Mr. Gecko

    Randomly choose color for shape

    Mr. Gecko

      I am working on a project for fun in which I need to have 144 squares choose a random color out of a pool (array) of predetermined colors and I need these shapes to do so every 1 second in the project. I was thinking the only way would be to write an after effects script, but I also thought it'll be cool if I could per object place a script which I do not know if it's possible.

       

      The only thing I am familiar with in after effects is creating time lapse. I have not played with it much more than that, however I am familiar with coding and have written some advanced scripts for Photoshop.

       

      Any help in pointing me into the right direction will be appreciated.

       

      Thanks

        • 1. Re: Randomly choose color for shape
          Mylenium Most Valuable Participant

          Yes, you can use an expression, but since your description is rather vague, nobody can really tell you much more than that and things being what they are, this could become complicated. If you're really talking a regular array of identical shapes, using effects like Card Dance or Trapcode Form might offer an alternative to create variations based on an animated pre-comp.

           

          Mylenium

          • 2. Re: Randomly choose color for shape
            Mr. Gecko Level 1

            I am not sure how to explain this better, but I'll try.

             

            Take 1 square shape.

            Have the square take on a fill color at random where the color can be ether: red, green, or blue.

            Have the square change it's fill color immediately (no transition) every 1 second.

             

            Is that more clear? My project is going to be different shades of yellow, but I don't think that is necessary to be known as I would define the colors it can take on.

            I will play around with scripting when I'm done with homework. The thing that I know will be a mystery to me is how do I make it change the color once a second. I think I can have it change color randomly easily.

            • 3. Re: Randomly choose color for shape
              Mylenium Most Valuable Participant

              Well, if you use a texture approach as I suggested and no expressions at all, simply keyframing e.g. a Fractal Noise's Evolution parameter with Hold keyframes might do just that. Otherwise in an expression a simple seedRandom(Math.floor(time),true) preceding any random operations will take care of that.

               

              Mylenium

              • 4. Re: Randomly choose color for shape
                Mr. Gecko Level 1

                I will play with that, thank you.

                • 5. Re: Randomly choose color for shape
                  Mr. Gecko Level 1

                  So, played around with what you mentioned, and nether of them will do what I needed. I ended up playing with scripting and finally finished getting what I wanted.

                   

                  #target aftereffects
                  
                  var colors = [[1,0.95294117647059,0.4,1],[1,1,0.6,1],[1,0.91764705882353,0.21960784313725,1],[1,1,0.054901960784314,1],[1,0.96470588235294,0.36470588235294,1],[1,0.8156862745098,0.26274509803922,1],[1,0.92156862745098,0.26274509803922,1],[1,0.88235294117647,0.26274509803922,1],[1,0.90980392156863,0.43529411764706,1],[1,0.97254901960784,0.49803921568627,1]];
                  
                  app.beginUndoGroup("Color Change");
                  
                  var comp1 = app.project.item(1); //Comp 1
                  var duration = comp1.workAreaDuration;
                  var squares = comp1.layer(2).property("ADBE Root Vectors Group"); //Squares
                  for (var i=1; i<=squares.numProperties; i++) {
                      var square = squares.property(i);
                      var fillColor = square.property("ADBE Vectors Group").property("ADBE Vector Graphic - Fill").property("ADBE Vector Fill Color");
                      while (fillColor.numKeys!=0) {
                          fillColor.removeKey(1);
                      }
                      var previousColor = 0;
                      for (var t=0; t<duration; t+=0.5) {
                          var choosenColor  = 0;
                          while (true) {
                              var choosenColor = Math.floor(Math.random()*colors.length);
                              if (t==0 || choosenColor!=previousColor) {
                                  break;
                              }
                          }
                          previousColor = choosenColor;
                          var color = colors[choosenColor];
                          fillColor.setValueAtTime(t, color);
                      }
                      for (var k=1; k<=fillColor.numKeys; k++) {
                          fillColor.setInterpolationTypeAtKey(k, KeyframeInterpolationType.HOLD, KeyframeInterpolationType.HOLD);
                      }
                  }
                  
                  app.endUndoGroup();