3 Replies Latest reply: Jun 23, 2009 11:29 AM by elaine montoya RSS

    Problems with an expression...

    sbaden04 Community Member

      I hope I'm not double posting but was told that this would be a better place to post this question:

       

      Here's the scenario:
      I have two comps: CompA and CompB

      Source Text example:  "DEN" (text layer in CompA)
      Source Name example: "DEN" (mov in CompB)

      I have written an expression and put it into the Source Text of a text layer in CompA. The code gets the source text and then loops through the layers in CompB comparing the Source Name to the Source Text in CompA. If the layer's source name matches the source text of CompA it enables the layer otherwise it disables it.


      The code doesn't get any errors and appears to run but does not work. Any help would be greatly appreciated.



      Here is the code:

      var triCode = text.sourceText;   //Gets source text from this comp (comp A)
      var numberOfLayers = comp("teamLogo").numLayers;   //Gets number of layers from comp B

      /*  Loops through comp B layers comparing layer name to source text in comp A  */
      for ( i=1; i <= numberOfLayers; i++ )
      {
      var currentLayer = comp("teamLogo").layer(i)

      if (currentLayer.source.name == triCode)
      {
      currentLayer.enabled = true;   //If comparison is true turns layer on
      }
      else {
      currentLayer.enabled = false;   //If comparison is false turns layer off
      }
      }

       

      I noticed that the script is actually changing the text layer text in Comp A to "false". Not sure why... When I change the reference to false in the code to something else I get an error.

       

      Mylenium recomended that I change to setting the opacity value instead of enabling the layer because expressions can't control switches like enable. I changed the code to:

       

      var triCode = thisLayer.text.sourceText;
      var numberOfLayers = comp("teamLogo").numLayers;

       

      for ( i=1; i <= numberOfLayers; i++ )
          {
              var teamLayer = comp("teamLogo").layer(i)

       

                  if (teamLayer.source.name == triCode)
                      {
                          teamLayer.opacity = 100;
                      }
                  else
                      {
                          teamLayer.opacity = 0;
                      }
          }

       

       

      I'm getting the same results as with the previous code.

        • 1. Re: Problems with an expression...
          elaine montoya CommunityMVP

          Just looking at your code, without actually trying it, my guess would be that it is because you didn't put the layer name triCode in quotes.

           

          change:

          if (currentLayer.source.name == triCode)

           

          to:

          if (currentLayer.source.name == "triCode")

           

           

          your way would make all of the results false, because their is no variable named triCode .... and that's what it would be referencing without quotes.

           

          Give that a try and let me know if it works.

           

          good luck!

          elaine

          • 2. Re: Problems with an expression...
            sbaden04 Community Member

            Thanks for the help Elaine,

             

            I actually just figured it out. I was going at it all backwards. I was trying to control from Comp A the properties on multiple layers in Comp B. I instead I put this code in the opacity of all the layers in Comp B:

             

            if (comp("changeName").layer("triCode").text.sourceText == thisLayer.source.name)
                {
                    transform.opacity = 100;
                }
            else
                {
                    transform.opacity = 0;
                }

             

             

            Now if the sourceText  in Comp A changes the layers in Comp B compare their values to it's value and change accordingly.

             

             

            Thanks again,

            Shawn

            • 3. Re: Problems with an expression...
              elaine montoya CommunityMVP

              Much better approach! Glad you were able to get it resolved.

               

              take care,

              elaine